diff --git a/lib/wallet/records.js b/lib/wallet/records.js index 28494c6f..83a693dd 100644 --- a/lib/wallet/records.js +++ b/lib/wallet/records.js @@ -244,14 +244,12 @@ function BlockMapRecord(height) { BlockMapRecord.prototype.fromRaw = function fromRaw(data) { var p = new BufferReader(data); - var i, hash, tx, count; + var count = p.readVarint(); + var i, hash, tx; - while (p.left()) { + for (i = 0; i < count; i++) { hash = p.readHash('hex'); - tx = new TXMapRecord(hash); - count = p.readVarint(); - for (i = 0; i < count; i++) - tx.wids.push(p.readU32()); + tx = TXMapRecord.fromRaw(hash, p); this.txs.push(tx); this.index[tx.hash] = tx; } @@ -278,14 +276,14 @@ BlockMapRecord.fromRaw = function fromRaw(height, data) { BlockMapRecord.prototype.toRaw = function toRaw(writer) { var p = new BufferWriter(writer); - var i, j, tx; + var i, tx; + + p.writeVarint(this.txs.length); for (i = 0; i < this.txs.length; i++) { tx = this.txs[i]; p.writeHash(tx.hash); - p.writeVarint(tx.wids.length); - for (j = 0; j < tx.wids.length; j++) - p.writeU32(tx.wids[j]); + tx.toRaw(p); } if (!writer) @@ -362,6 +360,19 @@ TXMapRecord.prototype.remove = function remove(wid) { return utils.binaryRemove(this.wids, wid, cmp); }; +TXMapRecord.prototype.toRaw = function toRaw(writer) { + return serializeWallets(this.wids, writer); +}; + +TXMapRecord.prototype.fromRaw = function fromRaw(data) { + this.wids = parseWallets(data); + return this; +}; + +TXMapRecord.fromRaw = function fromRaw(hash, data) { + return new TXMapRecord(hash).fromRaw(data); +}; + /** * Outpoint Map * @constructor @@ -381,8 +392,8 @@ OutpointMapRecord.prototype.remove = function remove(wid) { return utils.binaryRemove(this.wids, wid, cmp); }; -OutpointMapRecord.prototype.toRaw = function toRaw() { - return serializeWallets(this.wids); +OutpointMapRecord.prototype.toRaw = function toRaw(writer) { + return serializeWallets(this.wids, writer); }; OutpointMapRecord.prototype.fromRaw = function fromRaw(data) { @@ -412,8 +423,8 @@ PathMapRecord.prototype.remove = function remove(wid) { return utils.binaryRemove(this.wids, wid, cmp); }; -PathMapRecord.prototype.toRaw = function toRaw() { - return serializeWallets(this.wids); +PathMapRecord.prototype.toRaw = function toRaw(writer) { + return serializeWallets(this.wids, writer); }; PathMapRecord.prototype.fromRaw = function fromRaw(data) { @@ -439,24 +450,31 @@ function cmpid(a, b) { function parseWallets(data) { var p = new BufferReader(data); + var count = p.readVarint(); var wids = []; + var i; - while (p.left()) + for (i = 0; i < count; i++) wids.push(p.readU32()); return wids; } -function serializeWallets(wids) { - var p = new BufferWriter(); +function serializeWallets(wids, writer) { + var p = new BufferWriter(writer); var i, wid; + p.writeVarint(wids.length); + for (i = 0; i < wids.length; i++) { wid = wids[i]; p.writeU32(wid); } - return p.render(); + if (!writer) + p = p.render(); + + return p; } /* diff --git a/lib/wallet/txdb.js b/lib/wallet/txdb.js index c0917832..8874c1e4 100644 --- a/lib/wallet/txdb.js +++ b/lib/wallet/txdb.js @@ -3436,13 +3436,15 @@ BlockRecord.prototype.remove = function remove(hash) { BlockRecord.prototype.fromRaw = function fromRaw(data) { var p = new BufferReader(data); - var hash; + var i, hash, count; this.hash = p.readHash('hex'); this.height = p.readU32(); this.ts = p.readU32(); - while (p.left()) { + count = p.readVarint(); + + for (i = 0; i < count; i++) { hash = p.readHash('hex'); this.index[hash] = true; this.hashes.push(hash); @@ -3475,6 +3477,8 @@ BlockRecord.prototype.toRaw = function toRaw(writer) { p.writeU32(this.height); p.writeU32(this.ts); + p.writeVarint(this.hashes.length); + for (i = 0; i < this.hashes.length; i++) p.writeHash(this.hashes[i]); diff --git a/migrate/walletdb5to6.js b/migrate/walletdb5to6.js index 8a819e30..e0a3c552 100644 --- a/migrate/walletdb5to6.js +++ b/migrate/walletdb5to6.js @@ -112,6 +112,50 @@ var indexPaths = co(function* indexPaths() { } }); +var patchPathMaps = co(function* patchPathMaps() { + var i, items, item, hash, wids; + + items = yield db.range({ + gte: new Buffer('70' + constants.NULL_HASH, 'hex'), // p + lte: new Buffer('70' + constants.HIGH_HASH, 'hex') // p + }); + + for (i = 0; i < items.length; i++) { + item = items[i]; + hash = item.key.toString('hex', 1); + wids = parseWallets(item.value); + console.log('p[%s] -> varint(%d)', hash, wids.length); + batch.put(item.key, serializeWallets(wids)); + } +}); + +function parseWallets(data) { + var p = new BufferReader(data); + var wids = []; + + while (p.left()) + wids.push(p.readU32()); + + return wids; +} + +function serializeWallets(wids, writer) { + var p = new BufferWriter(writer); + var i, wid; + + p.writeVarint(wids.length); + + for (i = 0; i < wids.length; i++) { + wid = wids[i]; + p.writeU32(wid); + } + + if (!writer) + p = p.render(); + + return p; +} + function accountToRaw(account) { var p = new BufferWriter(); var i, key; @@ -192,6 +236,7 @@ co.spawn(function* () { yield wipeTXDB(); yield patchAccounts(); yield indexPaths(); + yield patchPathMaps(); yield batch.write(); yield db.close(); }).then(function() {