mutable scripts.

This commit is contained in:
Christopher Jeffrey 2016-04-17 20:18:03 -07:00
parent a4f0807c50
commit f4c991ab77
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
9 changed files with 43 additions and 27 deletions

View File

@ -45,7 +45,9 @@ function Coin(tx, index) {
this.version = tx.version;
this.height = tx.height;
this.value = tx.outputs[index].value;
this.script = tx.outputs[index].script;
if (tx.mutable)
this.value = this.value.clone();
this.script = bcoin.script(tx.outputs[index].script, false);
this.coinbase = tx.isCoinbase();
this.hash = tx.hash('hex');
this.index = index;
@ -55,7 +57,7 @@ function Coin(tx, index) {
this.version = options.version;
this.height = options.height;
this.value = options.value;
this.script = bcoin.script(options.script);
this.script = bcoin.script(options.script, false);
this.coinbase = options.coinbase;
this.hash = options.hash;
this.index = options.index;

View File

@ -478,6 +478,9 @@ Mempool.prototype.addTX = function addTX(tx, callback, force) {
if (!unlock)
return;
if (tx.mutable)
tx = tx.toTX();
if (this.chain.segwitActive) {
flags |= constants.flags.VERIFY_WITNESS;
flags |= constants.flags.VERIFY_DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM;

View File

@ -441,7 +441,12 @@ MinerBlock.prototype.updateMerkle = function updateMerkle() {
*/
MinerBlock.prototype.addTX = function addTX(tx) {
var size = this.block.getVirtualSize(true) + tx.getVirtualSize();
var size;
if (tx.mutable)
tx = tx.toTX();
size = this.block.getVirtualSize(true) + tx.getVirtualSize();
// Deliver me from the block size debate, please
if (size > constants.block.MAX_SIZE)
@ -574,6 +579,8 @@ MinerBlock.prototype.mine = function mine(callback) {
if (!self.findNonce())
return self.mine(callback);
self.block.txs[0] = self.block.txs[0].toTX();
return callback(null, self.block);
}, 100);
};
@ -585,6 +592,7 @@ MinerBlock.prototype.mine = function mine(callback) {
MinerBlock.prototype.mineSync = function mineSync() {
while (!this.findNonce());
this.block.txs[0] = this.block.txs[0].toTX();
return this.block;
};

View File

@ -75,6 +75,7 @@ function MTX(options) {
this.ps = options.ps != null ? options.ps : utils.now();
this.changeIndex = options.changeIndex != null ? options.changeIndex : -1;
this.height = -1;
this.mutable = true;
this._hash = null;
this._whash = null;
@ -254,12 +255,6 @@ MTX.prototype.addInput = function addInput(options, index) {
input = bcoin.input(options, true);
if (options.script instanceof Script)
input.script = options.script.clone();
if (options.witness instanceof Witness)
input.witness = options.witness.clone();
this.inputs.push(input);
return this;
@ -828,9 +823,7 @@ MTX.prototype.scriptOutput = function scriptOutput(index, options) {
output = this.outputs[index];
assert(output);
if (options.script instanceof Script)
output.script = options.script.clone();
else if (options.script)
if (options.script)
output.script = Script(options.script);
else
output.script = Script.createOutputScript(options);

View File

@ -39,7 +39,7 @@ function Output(options, mutable) {
this.mutable = !!mutable;
this.value = utils.satoshi(value || new bn(0));
this.script = bcoin.script(options.script, this.mutable);
this.script = bcoin.script(options.script, false);
assert(typeof value !== 'number');
assert(!this.mutable || !this.value.isNeg());

View File

@ -312,10 +312,14 @@ Peer.prototype.broadcast = function broadcast(items) {
items = [items];
items.forEach(function(item) {
var key = item.hash('hex');
var old = this._broadcast.map[key];
var type = item.type;
var entry, packetType;
var key, old, type, entry, packetType;
if (item.mutable)
item = item.toTX();
key = item.hash('hex');
old = this._broadcast.map[key];
type = item.type;
if (typeof type === 'string')
type = constants.inv[type.toUpperCase()];

View File

@ -1866,10 +1866,14 @@ Pool.prototype.sendTX = function sendTX(tx, callback) {
Pool.prototype.broadcast = function broadcast(msg, callback) {
var self = this;
var e = new EventEmitter();
var entry;
callback = utils.once(callback);
var entry = {
if (msg.mutable)
msg = msg.toTX();
entry = {
msg: msg,
e: e,
timer: setTimeout(function() {

View File

@ -44,8 +44,8 @@ var STACK_NEGATE = new Buffer([0xff]);
function Witness(items, mutable) {
if (items instanceof Witness) {
items.mutable = !!mutable;
items.redeem = null;
if (mutable || items.mutable)
return items.clone(mutable);
return items;
}
@ -82,8 +82,8 @@ Witness.prototype.inspect = function inspect() {
* @returns {Witness} A clone of the current witness object.
*/
Witness.prototype.clone = function clone() {
return new Witness(this.items.slice());
Witness.prototype.clone = function clone(mutable) {
return new Witness(this.items.slice(), mutable);
};
/**
@ -794,9 +794,8 @@ Stack.isStack = function isStack(obj) {
function Script(code, mutable) {
if (code instanceof Script) {
code.mutable = !!mutable;
code.raw = null;
code.redeem = null;
if (mutable || code.mutable)
return code.clone(mutable);
return code;
}
@ -834,8 +833,8 @@ function Script(code, mutable) {
* @returns {Script} Cloned script.
*/
Script.prototype.clone = function clone() {
return new Script(this.code.slice());
Script.prototype.clone = function clone(mutable) {
return new Script(this.code.slice(), mutable);
};
/**

View File

@ -337,6 +337,9 @@ TXDB.prototype._add = function add(tx, map, callback, force) {
callback = utils.wrap(callback, unlock);
if (tx.mutable)
tx = tx.toTX();
// Attempt to confirm tx before adding it.
this._confirm(tx, map, function(err, existing) {
if (err)