diff --git a/lib/bcoin/coin.js b/lib/bcoin/coin.js index 217c4397..584011f6 100644 --- a/lib/bcoin/coin.js +++ b/lib/bcoin/coin.js @@ -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; diff --git a/lib/bcoin/mempool.js b/lib/bcoin/mempool.js index a8fe922d..917bcc3d 100644 --- a/lib/bcoin/mempool.js +++ b/lib/bcoin/mempool.js @@ -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; diff --git a/lib/bcoin/miner.js b/lib/bcoin/miner.js index 8936a428..10971438 100644 --- a/lib/bcoin/miner.js +++ b/lib/bcoin/miner.js @@ -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; }; diff --git a/lib/bcoin/mtx.js b/lib/bcoin/mtx.js index 30893e70..e3fd9b76 100644 --- a/lib/bcoin/mtx.js +++ b/lib/bcoin/mtx.js @@ -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); diff --git a/lib/bcoin/output.js b/lib/bcoin/output.js index 6029082a..44cf1417 100644 --- a/lib/bcoin/output.js +++ b/lib/bcoin/output.js @@ -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()); diff --git a/lib/bcoin/peer.js b/lib/bcoin/peer.js index f4c885ae..b9577982 100644 --- a/lib/bcoin/peer.js +++ b/lib/bcoin/peer.js @@ -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()]; diff --git a/lib/bcoin/pool.js b/lib/bcoin/pool.js index d2c57c6d..bc30afb2 100644 --- a/lib/bcoin/pool.js +++ b/lib/bcoin/pool.js @@ -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() { diff --git a/lib/bcoin/script.js b/lib/bcoin/script.js index 8b5a6f26..8d0cc901 100644 --- a/lib/bcoin/script.js +++ b/lib/bcoin/script.js @@ -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); }; /** diff --git a/lib/bcoin/txdb.js b/lib/bcoin/txdb.js index 0a621414..fd023a9c 100644 --- a/lib/bcoin/txdb.js +++ b/lib/bcoin/txdb.js @@ -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)