From c9d8ae9f4aa1f6695ae310de064b778036b24ecd Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sun, 6 Mar 2016 12:17:33 -0800 Subject: [PATCH] more serialization. --- lib/bcoin/block.js | 7 ++- lib/bcoin/blockdb.js | 4 +- lib/bcoin/coin.js | 26 ++++++++++- lib/bcoin/mtx.js | 107 ++++--------------------------------------- lib/bcoin/tx.js | 3 ++ lib/bcoin/txdb.js | 8 ++-- 6 files changed, 47 insertions(+), 108 deletions(-) diff --git a/lib/bcoin/block.js b/lib/bcoin/block.js index e285122f..bb39a96f 100644 --- a/lib/bcoin/block.js +++ b/lib/bcoin/block.js @@ -355,9 +355,14 @@ Block.fromRaw = function fromRaw(data, enc, type) { Block.prototype.toSmall = function toSmall() { var block = this.abbr(); - var buf = new Buffer(block.length + 4 + utils.sizeIntv(this.txs.length) + this.txs.length * 32); var height = this.height; var off = 0; + var buf; + + buf = new Buffer( + block.length + 4 + + utils.sizeIntv(this.txs.length) + + this.txs.length * 32); if (height === -1) height = 0x7fffffff; diff --git a/lib/bcoin/blockdb.js b/lib/bcoin/blockdb.js index 82ce3ec6..5d7ab222 100644 --- a/lib/bcoin/blockdb.js +++ b/lib/bcoin/blockdb.js @@ -242,7 +242,7 @@ BlockDB.prototype.connectBlock = function connectBlock(block, callback, batch) { if (address) batch.put('u/a/' + address + '/' + hash + '/' + i, DUMMY); - batch.put('u/t/' + hash + '/' + i, bcoin.coin(tx, i).toRaw()); + batch.put('u/t/' + hash + '/' + i, bcoin.coin(tx, i).toExtended()); }); }); @@ -312,7 +312,7 @@ BlockDB.prototype.disconnectBlock = function disconnectBlock(hash, callback, bat batch.put('u/t/' + input.prevout.hash + '/' + input.prevout.index, - input.output.toRaw()); + input.output.toExtended()); }); tx.outputs.forEach(function(output, i) { diff --git a/lib/bcoin/coin.js b/lib/bcoin/coin.js index e9a90915..5d0968cf 100644 --- a/lib/bcoin/coin.js +++ b/lib/bcoin/coin.js @@ -160,7 +160,7 @@ Coin.fromJSON = function fromJSON(json) { }; Coin.prototype.toRaw = function toRaw(enc) { - var data = bcoin.protocol.framer.coin(this, true); + var data = bcoin.protocol.framer.coin(this, false); if (enc === 'hex') data = utils.toHex(data); @@ -172,7 +172,7 @@ Coin._fromRaw = function _fromRaw(data, enc) { if (enc === 'hex') data = new Buffer(data, 'hex'); - data = bcoin.protocol.parser.parseCoin(data, true); + data = bcoin.protocol.parser.parseCoin(data, false); return data; }; @@ -181,6 +181,28 @@ Coin.fromRaw = function fromRaw(data, enc) { return new Coin(Coin._fromRaw(data, enc)); }; +Coin.prototype.toExtended = function toExtended(enc) { + var data = bcoin.protocol.framer.coin(this, true); + + if (enc === 'hex') + data = utils.toHex(data); + + return data; +}; + +Coin._fromExtended = function _fromExtended(data, enc) { + if (enc === 'hex') + data = new Buffer(data, 'hex'); + + data = bcoin.protocol.parser.parseCoin(data, true); + + return data; +}; + +Coin.fromExtended = function fromExtended(data, enc) { + return new Coin(Coin._fromExtended(data, enc)); +}; + /** * Expose */ diff --git a/lib/bcoin/mtx.js b/lib/bcoin/mtx.js index 860163a3..1bc74386 100644 --- a/lib/bcoin/mtx.js +++ b/lib/bcoin/mtx.js @@ -1076,115 +1076,24 @@ MTX.prototype.increaseFee = function increaseFee(unspent, address, fee) { } }; -MTX.prototype.toCompact = function toCompact(coins) { - return { - type: 'mtx', - block: this.block, - height: this.height, - ts: this.ts, - ps: this.ps, - changeIndex: this.changeIndex, - coins: coins ? this.inputs.map(function(input) { - return input.output ? input.output.toRaw('hex') : null; - }) : null, - tx: utils.toHex(this.render()) - }; -}; - -MTX._fromCompact = function _fromCompact(json) { - var raw, data, tx; - - assert.equal(json.type, 'mtx'); - - raw = new Buffer(json.tx, 'hex'); - data = bcoin.protocol.parser.parseTX(raw); - - data.height = json.height; - data.block = json.block; - data.ts = json.ts; - data.ps = json.ps; - data.changeIndex = json.changeIndex; - - if (json.coins) { - json.coins.forEach(function(output, i) { - if (!output) - return; - - data.inputs[i].output = bcoin.coin._fromRaw(output, 'hex'); - }); - } - - return data; -}; - -MTX.fromCompact = function fromCompact(json) { - return new MTX(MTX._fromCompact(json)); -}; - -MTX.prototype.toJSON = function toJSON() { - return { - type: 'mtx', - hash: utils.revHex(this.hash('hex')), - witnessHash: utils.revHex(this.witnessHash('hex')), - height: this.height, - block: this.block ? utils.revHex(this.block) : null, - ts: this.ts, - ps: this.ps, - changeIndex: this.changeIndex, - version: this.version, - inputs: this.inputs.map(function(input) { - return input.toJSON(); - }), - outputs: this.outputs.map(function(output) { - return output.toJSON(); - }), - locktime: this.locktime - }; -}; - -MTX._fromJSON = function fromJSON(json) { - assert.equal(json.type, 'mtx'); - return { - block: json.block ? utils.revHex(json.block) : null, - height: json.height, - ts: json.ts, - ps: json.ps, - changeIndex: json.changeIndex, - version: json.version, - inputs: json.inputs.map(function(input) { - return bcoin.input._fromJSON(input); - }), - outputs: json.outputs.map(function(output) { - return bcoin.output._fromJSON(output); - }), - locktime: json.locktime - }; -}; +MTX._fromJSON = bcoin.tx._fromJSON; MTX.fromJSON = function fromJSON(json) { return new MTX(MTX._fromJSON(json)); }; -MTX.prototype.toRaw = function toRaw(enc) { - var data = this.render(); - - if (enc === 'hex') - data = utils.toHex(data); - - return data; -}; - -MTX._fromRaw = function _fromRaw(data, enc) { - if (enc === 'hex') - data = new Buffer(data, 'hex'); - - return bcoin.protocol.parser.parseTX(data); -}; +MTX._fromRaw = bcoin.tx._fromRaw; MTX.fromRaw = function fromRaw(data, enc) { return new MTX(MTX._fromRaw(data, enc)); }; +MTX._fromExtended = bcoin.tx._fromExtended; + +MTX.fromExtended = function fromExtended(data, enc) { + return new MTX(MTX._fromExtended(data, enc)); +}; + MTX.fromTX = function fromTX(tx) { return new MTX(tx); }; diff --git a/lib/bcoin/tx.js b/lib/bcoin/tx.js index 35f4cec8..97fdffb4 100644 --- a/lib/bcoin/tx.js +++ b/lib/bcoin/tx.js @@ -1013,6 +1013,7 @@ TX.prototype.toJSON = function toJSON() { ts: this.ts, ps: this.ps, index: this.index, + changeIndex: this.changeIndex || -1, version: this.version, inputs: this.inputs.map(function(input) { return input.toJSON(); @@ -1032,6 +1033,7 @@ TX._fromJSON = function fromJSON(json) { ts: json.ts, ps: json.ps, index: json.index, + changeIndex: json.changeIndex || -1, version: json.version, inputs: json.inputs.map(function(input) { return bcoin.input._fromJSON(input); @@ -1084,6 +1086,7 @@ TX.prototype.toExtended = function toExtended(coins) { off += utils.writeU32(buf, this.index, off); off += utils.writeU32(buf, this.ts, off); off += utils.writeU32(buf, this.ps, off); + // off += utils.writeU32(buf, this.changeIndex || -1, off); if (coins) { off += utils.writeIntv(buf, this.inputs.length, off); diff --git a/lib/bcoin/txdb.js b/lib/bcoin/txdb.js index ec3083c6..a6cbc02c 100644 --- a/lib/bcoin/txdb.js +++ b/lib/bcoin/txdb.js @@ -463,7 +463,7 @@ TXPool.prototype._add = function add(tx, map, callback, force) { }); } - batch.put(prefix + 'u/t/' + hash + '/' + i, coin.toRaw()); + batch.put(prefix + 'u/t/' + hash + '/' + i, coin.toExtended()); updated = true; } @@ -552,7 +552,7 @@ TXPool.prototype._confirm = function _confirm(tx, map, callback) { coin.height = tx.height; - batch.put(prefix + 'u/t/' + hash + '/' + i, coin.toRaw()); + batch.put(prefix + 'u/t/' + hash + '/' + i, coin.toExtended()); next(); }); @@ -679,7 +679,7 @@ TXPool.prototype._remove = function remove(tx, map, callback) { batch.put(prefix + 'u/t/' + input.prevout.hash + '/' + input.prevout.index, - input.output.toRaw()); + input.output.toExtended()); batch.del(prefix + 'o/' + input.prevout.hash + '/' + input.prevout.index); }); @@ -784,7 +784,7 @@ TXPool.prototype._unconfirm = function unconfirm(tx, map, callback) { coin.height = tx.height; - batch.put(prefix + 'u/t/' + hash + '/' + i, coin.toRaw()); + batch.put(prefix + 'u/t/' + hash + '/' + i, coin.toExtended()); next(); });