From 9ee385482842d8fb93ef002ba1c135b6c9f52993 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Fri, 10 Aug 2018 06:04:01 -0700 Subject: [PATCH] bdb: upgrade bdb usage for keys. --- lib/blockchain/chaindb.js | 132 ++++++++++++++++----------------- lib/blockchain/layout.js | 22 +++--- lib/mempool/layout.js | 2 +- lib/mempool/mempool.js | 26 +++---- lib/wallet/layout.js | 34 ++++----- lib/wallet/txdb.js | 138 +++++++++++++++++------------------ lib/wallet/walletdb.js | 150 +++++++++++++++++++------------------- migrate/chaindb3to4.js | 10 +-- migrate/walletdb6to7.js | 54 +++++++------- 9 files changed, 284 insertions(+), 284 deletions(-) diff --git a/lib/blockchain/chaindb.js b/lib/blockchain/chaindb.js index ce216ab8..c51e6e7c 100644 --- a/lib/blockchain/chaindb.js +++ b/lib/blockchain/chaindb.js @@ -62,7 +62,7 @@ class ChainDB { this.logger.info('Opening ChainDB...'); await this.db.open(); - await this.db.verify(layout.V.build(), 'chain', 4); + await this.db.verify(layout.V.encode(), 'chain', 4); const state = await this.getState(); @@ -264,7 +264,7 @@ class ChainDB { if (entry) return entry.height; - const height = await this.db.get(layout.h.build(hash)); + const height = await this.db.get(layout.h.encode(hash)); if (!height) return -1; @@ -293,7 +293,7 @@ class ChainDB { if (entry) return entry.hash; - return this.db.get(layout.H.build(height)); + return this.db.get(layout.H.encode(height)); } /** @@ -313,7 +313,7 @@ class ChainDB { if (cache) return cache; - const hash = await this.db.get(layout.H.build(height)); + const hash = await this.db.get(layout.H.encode(height)); if (!hash) return null; @@ -350,7 +350,7 @@ class ChainDB { if (cache) return cache; - const raw = await this.db.get(layout.e.build(hash)); + const raw = await this.db.get(layout.e.encode(hash)); if (!raw) return null; @@ -488,7 +488,7 @@ class ChainDB { */ async getState() { - const data = await this.db.get(layout.R.build()); + const data = await this.db.get(layout.R.encode()); if (!data) return null; @@ -517,7 +517,7 @@ class ChainDB { */ async getFlags() { - const data = await this.db.get(layout.O.build()); + const data = await this.db.get(layout.O.encode()); if (!data) return null; @@ -615,7 +615,7 @@ class ChainDB { }); for (const item of items) { - const [bit, hash] = layout.v.parse(item.key); + const [bit, hash] = layout.v.decode(item.key); const state = item.value[0]; stateCache.insert(bit, hash, state); } @@ -652,7 +652,7 @@ class ChainDB { bw.writeI32(deployment.window); } - b.put(layout.D.build(), bw.render()); + b.put(layout.D.encode(), bw.render()); } /** @@ -662,7 +662,7 @@ class ChainDB { */ async checkDeployments() { - const raw = await this.db.get(layout.D.build()); + const raw = await this.db.get(layout.D.encode()); assert(raw, 'No deployment table found.'); @@ -774,8 +774,8 @@ class ChainDB { if (!hash) throw new Error(`Cannot find hash for ${i}.`); - b.del(layout.b.build(hash)); - b.del(layout.u.build(hash)); + b.del(layout.b.encode(hash)); + b.del(layout.u.encode(hash)); } try { @@ -784,7 +784,7 @@ class ChainDB { const flags = ChainFlags.fromOptions(options); assert(flags.prune); - b.put(layout.O.build(), flags.toRaw()); + b.put(layout.O.encode(), flags.toRaw()); await b.write(); } catch (e) { @@ -804,7 +804,7 @@ class ChainDB { */ async getNextHash(hash) { - return this.db.get(layout.n.build(hash)); + return this.db.get(layout.n.encode(hash)); } /** @@ -908,7 +908,7 @@ class ChainDB { return this.db.keys({ gte: layout.p.min(), lte: layout.p.max(), - parse: key => layout.p.parse(key) + parse: key => layout.p.decode(key)[0] }); } @@ -932,7 +932,7 @@ class ChainDB { if (cache) return CoinEntry.fromRaw(cache); - const raw = await this.db.get(layout.c.build(hash, index)); + const raw = await this.db.get(layout.c.encode(hash, index)); if (!raw) return null; @@ -969,7 +969,7 @@ class ChainDB { async hasCoins(tx) { for (let i = 0; i < tx.outputs.length; i++) { - const key = layout.c.build(tx.hash(), i); + const key = layout.c.encode(tx.hash(), i); if (await this.db.has(key)) return true; } @@ -1030,7 +1030,7 @@ class ChainDB { */ async getUndoCoins(hash) { - const data = await this.db.get(layout.u.build(hash)); + const data = await this.db.get(layout.u.encode(hash)); if (!data) return new UndoCoins(); @@ -1068,7 +1068,7 @@ class ChainDB { if (!hash) return null; - return this.db.get(layout.b.build(hash)); + return this.db.get(layout.b.encode(hash)); } /** @@ -1109,7 +1109,7 @@ class ChainDB { if (!this.options.indexTX) return null; - const data = await this.db.get(layout.t.build(hash)); + const data = await this.db.get(layout.t.encode(hash)); if (!data) return null; @@ -1141,7 +1141,7 @@ class ChainDB { if (!this.options.indexTX) return false; - return this.db.has(layout.t.build(hash)); + return this.db.has(layout.t.encode(hash)); } /** @@ -1166,7 +1166,7 @@ class ChainDB { gte: layout.C.min(hash), lte: layout.C.max(hash), parse: (key) => { - const [, txid, index] = layout.C.parse(key); + const [, txid, index] = layout.C.decode(key); return [txid, index]; } }); @@ -1200,7 +1200,7 @@ class ChainDB { gte: layout.T.min(hash), lte: layout.T.max(hash), parse: (key) => { - const [, txid] = layout.T.parse(key); + const [, txid] = layout.T.decode(key); set.add(txid); } }); @@ -1373,15 +1373,15 @@ class ChainDB { const hash = block.hash(); // Hash->height index. - this.put(layout.h.build(hash), fromU32(entry.height)); + this.put(layout.h.encode(hash), fromU32(entry.height)); // Entry data. - this.put(layout.e.build(hash), entry.toRaw()); + this.put(layout.e.encode(hash), entry.toRaw()); this.cacheHash.push(entry.hash, entry); // Tip index. - this.del(layout.p.build(entry.prevBlock)); - this.put(layout.p.build(hash), null); + this.del(layout.p.encode(entry.prevBlock)); + this.put(layout.p.encode(hash), null); // Update state caches. this.saveUpdates(); @@ -1394,17 +1394,17 @@ class ChainDB { // Hash->next-block index. if (!entry.isGenesis()) - this.put(layout.n.build(entry.prevBlock), hash); + this.put(layout.n.encode(entry.prevBlock), hash); // Height->hash index. - this.put(layout.H.build(entry.height), hash); + this.put(layout.H.encode(entry.height), hash); this.cacheHeight.push(entry.height, entry); // Connect block and save data. await this.saveBlock(entry, block, view); // Commit new chain state. - this.put(layout.R.build(), this.pending.commit(hash)); + this.put(layout.R.encode(), this.pending.commit(hash)); } /** @@ -1441,10 +1441,10 @@ class ChainDB { assert(!entry.isGenesis()); // We can now add a hash->next-block index. - this.put(layout.n.build(entry.prevBlock), hash); + this.put(layout.n.encode(entry.prevBlock), hash); // We can now add a height->hash index. - this.put(layout.H.build(entry.height), hash); + this.put(layout.H.encode(entry.height), hash); this.cacheHeight.push(entry.height, entry); // Re-insert into cache. @@ -1457,7 +1457,7 @@ class ChainDB { await this.connectBlock(entry, block, view); // Update chain state. - this.put(layout.R.build(), this.pending.commit(hash)); + this.put(layout.R.encode(), this.pending.commit(hash)); } /** @@ -1493,10 +1493,10 @@ class ChainDB { async _disconnect(entry, block) { // Remove hash->next-block index. - this.del(layout.n.build(entry.prevBlock)); + this.del(layout.n.encode(entry.prevBlock)); // Remove height->hash index. - this.del(layout.H.build(entry.height)); + this.del(layout.H.encode(entry.height)); this.cacheHeight.unpush(entry.height); // Update state caches. @@ -1506,7 +1506,7 @@ class ChainDB { const view = await this.disconnectBlock(entry, block); // Revert chain state to previous tip. - this.put(layout.R.build(), this.pending.commit(entry.prevBlock)); + this.put(layout.R.encode(), this.pending.commit(entry.prevBlock)); return view; } @@ -1526,7 +1526,7 @@ class ChainDB { for (const update of updates) { const {bit, hash} = update; - this.put(layout.v.build(bit, hash), update.toRaw()); + this.put(layout.v.encode(bit, hash), update.toRaw()); } } @@ -1565,7 +1565,7 @@ class ChainDB { // Stop once we hit our target tip. if (tip.hash.equals(entry.hash)) { - this.put(layout.R.build(), this.pending.commit(tip.hash)); + this.put(layout.R.encode(), this.pending.commit(tip.hash)); await this.commit(); break; } @@ -1573,15 +1573,15 @@ class ChainDB { assert(!tip.isGenesis()); // Revert the tip index. - this.del(layout.p.build(tip.hash)); - this.put(layout.p.build(tip.prevBlock), null); + this.del(layout.p.encode(tip.hash)); + this.put(layout.p.encode(tip.prevBlock), null); // Remove all records (including // main-chain-only records). - this.del(layout.H.build(tip.height)); - this.del(layout.h.build(tip.hash)); - this.del(layout.e.build(tip.hash)); - this.del(layout.n.build(tip.prevBlock)); + this.del(layout.H.encode(tip.height)); + this.del(layout.h.encode(tip.hash)); + this.del(layout.e.encode(tip.hash)); + this.del(layout.n.encode(tip.prevBlock)); // Disconnect and remove block data. try { @@ -1592,7 +1592,7 @@ class ChainDB { } // Revert chain state to previous tip. - this.put(layout.R.build(), this.pending.commit(tip.prevBlock)); + this.put(layout.R.encode(), this.pending.commit(tip.prevBlock)); await this.commit(); @@ -1652,10 +1652,10 @@ class ChainDB { assert(!tip.isGenesis()); // Remove all non-main-chain records. - this.del(layout.p.build(tip.hash)); - this.del(layout.h.build(tip.hash)); - this.del(layout.e.build(tip.hash)); - this.del(layout.b.build(tip.hash)); + this.del(layout.p.encode(tip.hash)); + this.del(layout.h.encode(tip.hash)); + this.del(layout.e.encode(tip.hash)); + this.del(layout.b.encode(tip.hash)); // Queue up hash to be removed // on successful write. @@ -1683,7 +1683,7 @@ class ChainDB { // Write actual block data (this may be // better suited to flat files in the future). - this.put(layout.b.build(hash), block.toRaw()); + this.put(layout.b.encode(hash), block.toRaw()); if (!view) return; @@ -1707,7 +1707,7 @@ class ChainDB { if (!block) throw new Error('Block not found.'); - this.del(layout.b.build(block.hash())); + this.del(layout.b.encode(block.hash())); return this.disconnectBlock(entry, block); } @@ -1722,7 +1722,7 @@ class ChainDB { for (const [hash, coins] of view.map) { for (const [index, coin] of coins.outputs) { if (coin.spent) { - this.del(layout.c.build(hash, index)); + this.del(layout.c.encode(hash, index)); if (this.coinCache.capacity > 0) this.coinCache.unpush(Outpoint.toKey(hash, index)); continue; @@ -1730,7 +1730,7 @@ class ChainDB { const raw = coin.toRaw(); - this.put(layout.c.build(hash, index), raw); + this.put(layout.c.encode(hash, index), raw); if (this.coinCache.capacity > 0) this.coinCache.push(Outpoint.toKey(hash, index), raw); @@ -1783,7 +1783,7 @@ class ChainDB { // Write undo coins (if there are any). if (!view.undo.isEmpty()) - this.put(layout.u.build(hash), view.undo.commit()); + this.put(layout.u.encode(hash), view.undo.commit()); // Prune height-288 if pruning is enabled. return this.pruneBlock(entry); @@ -1842,7 +1842,7 @@ class ChainDB { this.saveView(view); // Remove undo coins. - this.del(layout.u.build(hash)); + this.del(layout.u.encode(hash)); return view; } @@ -1872,8 +1872,8 @@ class ChainDB { if (!hash) return; - this.del(layout.b.build(hash)); - this.del(layout.u.build(hash)); + this.del(layout.b.encode(hash)); + this.del(layout.u.encode(hash)); } /** @@ -1884,7 +1884,7 @@ class ChainDB { saveFlags() { const flags = ChainFlags.fromOptions(this.options); const b = this.db.batch(); - b.put(layout.O.build(), flags.toRaw()); + b.put(layout.O.encode(), flags.toRaw()); return b.write(); } @@ -1903,11 +1903,11 @@ class ChainDB { if (this.options.indexTX) { const meta = TXMeta.fromTX(tx, entry, index); - this.put(layout.t.build(hash), meta.toRaw()); + this.put(layout.t.encode(hash), meta.toRaw()); if (this.options.indexAddress) { for (const addr of tx.getHashes(view)) - this.put(layout.T.build(addr, hash), null); + this.put(layout.T.encode(addr, hash), null); } } @@ -1925,7 +1925,7 @@ class ChainDB { if (!addr) continue; - this.del(layout.C.build(addr, hash, index)); + this.del(layout.C.encode(addr, hash, index)); } } @@ -1936,7 +1936,7 @@ class ChainDB { if (!addr) continue; - this.put(layout.C.build(addr, hash, i), null); + this.put(layout.C.encode(addr, hash, i), null); } } @@ -1951,10 +1951,10 @@ class ChainDB { const hash = tx.hash(); if (this.options.indexTX) { - this.del(layout.t.build(hash)); + this.del(layout.t.encode(hash)); if (this.options.indexAddress) { for (const addr of tx.getHashes(view)) - this.del(layout.T.build(addr, hash)); + this.del(layout.T.encode(addr, hash)); } } @@ -1972,7 +1972,7 @@ class ChainDB { if (!addr) continue; - this.put(layout.C.build(addr, hash, index), null); + this.put(layout.C.encode(addr, hash, index), null); } } @@ -1983,7 +1983,7 @@ class ChainDB { if (!addr) continue; - this.del(layout.C.build(addr, hash, i)); + this.del(layout.C.encode(addr, hash, i)); } } } diff --git a/lib/blockchain/layout.js b/lib/blockchain/layout.js index 3a65c0fb..1bc801d5 100644 --- a/lib/blockchain/layout.js +++ b/lib/blockchain/layout.js @@ -33,18 +33,18 @@ const layout = { O: bdb.key('O'), R: bdb.key('R'), D: bdb.key('D'), - e: bdb.key('e', ['bhash256']), - h: bdb.key('h', ['bhash256']), + e: bdb.key('e', ['hash256']), + h: bdb.key('h', ['hash256']), H: bdb.key('H', ['uint32']), - n: bdb.key('n', ['bhash256']), - p: bdb.key('p', ['bhash256']), - b: bdb.key('b', ['bhash256']), - t: bdb.key('t', ['bhash256']), - c: bdb.key('c', ['bhash256', 'uint32']), - u: bdb.key('u', ['bhash256']), - v: bdb.key('v', ['uint8', 'bhash256']), - T: bdb.key('T', ['bhash', 'bhash256']), - C: bdb.key('C', ['bhash', 'bhash256', 'uint32']) + n: bdb.key('n', ['hash256']), + p: bdb.key('p', ['hash256']), + b: bdb.key('b', ['hash256']), + t: bdb.key('t', ['hash256']), + c: bdb.key('c', ['hash256', 'uint32']), + u: bdb.key('u', ['hash256']), + v: bdb.key('v', ['uint8', 'hash256']), + T: bdb.key('T', ['hash', 'hash256']), + C: bdb.key('C', ['hash', 'hash256', 'uint32']) }; /* diff --git a/lib/mempool/layout.js b/lib/mempool/layout.js index 4982c9ea..81fddc34 100644 --- a/lib/mempool/layout.js +++ b/lib/mempool/layout.js @@ -21,7 +21,7 @@ const layout = { v: bdb.key('v'), R: bdb.key('R'), F: bdb.key('F'), - e: bdb.key('e', ['bhash256']) + e: bdb.key('e', ['hash256']) }; /* diff --git a/lib/mempool/mempool.js b/lib/mempool/mempool.js index 4973d567..2264c538 100644 --- a/lib/mempool/mempool.js +++ b/lib/mempool/mempool.js @@ -2389,7 +2389,7 @@ class MempoolCache { } async getVersion() { - const data = await this.db.get(layout.v.build()); + const data = await this.db.get(layout.v.encode()); if (!data) return -1; @@ -2398,11 +2398,11 @@ class MempoolCache { } async getTip() { - return this.db.get(layout.R.build()); + return this.db.get(layout.R.encode()); } async getFees() { - const data = await this.db.get(layout.F.build()); + const data = await this.db.get(layout.F.encode()); if (!data) return null; @@ -2440,7 +2440,7 @@ class MempoolCache { return; await this.db.open(); - await this.db.verify(layout.V.build(), 'mempool', 0); + await this.db.verify(layout.V.encode(), 'mempool', 0); await this.verify(); @@ -2460,28 +2460,28 @@ class MempoolCache { if (!this.db) return; - this.batch.put(layout.e.build(entry.hash()), entry.toRaw()); + this.batch.put(layout.e.encode(entry.hash()), entry.toRaw()); } remove(hash) { if (!this.db) return; - this.batch.del(layout.e.build(hash)); + this.batch.del(layout.e.encode(hash)); } sync(tip) { if (!this.db) return; - this.batch.put(layout.R.build(), tip); + this.batch.put(layout.R.encode(), tip); } writeFees(fees) { if (!this.db) return; - this.batch.put(layout.F.build(), fees.toRaw()); + this.batch.put(layout.F.encode(), fees.toRaw()); } clear() { @@ -2500,8 +2500,8 @@ class MempoolCache { async init(hash) { const batch = this.db.batch(); - batch.put(layout.v.build(), fromU32(MempoolCache.VERSION)); - batch.put(layout.R.build(), hash); + batch.put(layout.v.encode(), fromU32(MempoolCache.VERSION)); + batch.put(layout.R.encode(), hash); await batch.write(); } @@ -2552,9 +2552,9 @@ class MempoolCache { for (const key of keys) batch.del(key); - batch.put(layout.v.build(), fromU32(MempoolCache.VERSION)); - batch.put(layout.R.build(), this.chain.tip.hash); - batch.del(layout.F.build()); + batch.put(layout.v.encode(), fromU32(MempoolCache.VERSION)); + batch.put(layout.R.encode(), this.chain.tip.hash); + batch.del(layout.F.encode()); await batch.write(); diff --git a/lib/wallet/layout.js b/lib/wallet/layout.js index 2c0a66c6..dfaebf5d 100644 --- a/lib/wallet/layout.js +++ b/lib/wallet/layout.js @@ -35,9 +35,9 @@ exports.wdb = { O: bdb.key('O'), R: bdb.key('R'), D: bdb.key('D'), - p: bdb.key('p', ['bhash']), - P: bdb.key('P', ['uint32', 'bhash']), - r: bdb.key('r', ['uint32', 'uint32', 'bhash']), + p: bdb.key('p', ['hash']), + P: bdb.key('P', ['uint32', 'hash']), + r: bdb.key('r', ['uint32', 'uint32', 'hash']), w: bdb.key('w', ['uint32']), W: bdb.key('W', ['uint32']), l: bdb.key('l', ['ascii']), @@ -46,8 +46,8 @@ exports.wdb = { n: bdb.key('n', ['uint32', 'uint32']), h: bdb.key('h', ['uint32']), b: bdb.key('b', ['uint32']), - o: bdb.key('o', ['bhash256', 'uint32']), - T: bdb.key('T', ['bhash256']), + o: bdb.key('o', ['hash256', 'uint32']), + T: bdb.key('T', ['hash256']), t: bdb.key('t', ['uint32']) }; @@ -74,17 +74,17 @@ exports.txdb = { prefix: bdb.key('t', ['uint32']), R: bdb.key('R'), r: bdb.key('r', ['uint32']), - t: bdb.key('t', ['bhash256']), - c: bdb.key('c', ['bhash256', 'uint32']), - d: bdb.key('d', ['bhash256', 'uint32']), - s: bdb.key('s', ['bhash256', 'uint32']), - p: bdb.key('p', ['bhash256']), - m: bdb.key('m', ['uint32', 'bhash256']), - h: bdb.key('h', ['uint32', 'bhash256']), - T: bdb.key('T', ['uint32', 'bhash256']), - P: bdb.key('P', ['uint32', 'bhash256']), - M: bdb.key('M', ['uint32', 'uint32', 'bhash256']), - H: bdb.key('H', ['uint32', 'uint32', 'bhash256']), - C: bdb.key('C', ['uint32', 'bhash256', 'uint32']), + t: bdb.key('t', ['hash256']), + c: bdb.key('c', ['hash256', 'uint32']), + d: bdb.key('d', ['hash256', 'uint32']), + s: bdb.key('s', ['hash256', 'uint32']), + p: bdb.key('p', ['hash256']), + m: bdb.key('m', ['uint32', 'hash256']), + h: bdb.key('h', ['uint32', 'hash256']), + T: bdb.key('T', ['uint32', 'hash256']), + P: bdb.key('P', ['uint32', 'hash256']), + M: bdb.key('M', ['uint32', 'uint32', 'hash256']), + H: bdb.key('H', ['uint32', 'uint32', 'hash256']), + C: bdb.key('C', ['uint32', 'hash256', 'uint32']), b: bdb.key('b', ['uint32']) }; diff --git a/lib/wallet/txdb.js b/lib/wallet/txdb.js index 38f79fa0..3af8c81b 100644 --- a/lib/wallet/txdb.js +++ b/lib/wallet/txdb.js @@ -50,7 +50,7 @@ class TXDB { */ async open(wallet) { - const prefix = layout.prefix.build(wallet.wid); + const prefix = layout.prefix.encode(wallet.wid); this.wid = wallet.wid; this.bucket = this.db.bucket(prefix); @@ -109,8 +109,8 @@ class TXDB { async saveCredit(b, credit, path) { const {coin} = credit; - b.put(layout.c.build(coin.hash, coin.index), credit.toRaw()); - b.put(layout.C.build(path.account, coin.hash, coin.index), null); + b.put(layout.c.encode(coin.hash, coin.index), credit.toRaw()); + b.put(layout.C.encode(path.account, coin.hash, coin.index), null); return this.addOutpointMap(b, coin.hash, coin.index); } @@ -124,8 +124,8 @@ class TXDB { async removeCredit(b, credit, path) { const {coin} = credit; - b.del(layout.c.build(coin.hash, coin.index)); - b.del(layout.C.build(path.account, coin.hash, coin.index)); + b.del(layout.c.encode(coin.hash, coin.index)); + b.del(layout.C.encode(path.account, coin.hash, coin.index)); return this.removeOutpointMap(b, coin.hash, coin.index); } @@ -140,8 +140,8 @@ class TXDB { spendCredit(b, credit, tx, index) { const prevout = tx.inputs[index].prevout; const spender = Outpoint.fromTX(tx, index); - b.put(layout.s.build(prevout.hash, prevout.index), spender.toRaw()); - b.put(layout.d.build(spender.hash, spender.index), credit.coin.toRaw()); + b.put(layout.s.encode(prevout.hash, prevout.index), spender.toRaw()); + b.put(layout.d.encode(spender.hash, spender.index), credit.coin.toRaw()); } /** @@ -153,8 +153,8 @@ class TXDB { unspendCredit(b, tx, index) { const prevout = tx.inputs[index].prevout; const spender = Outpoint.fromTX(tx, index); - b.del(layout.s.build(prevout.hash, prevout.index)); - b.del(layout.d.build(spender.hash, spender.index)); + b.del(layout.s.encode(prevout.hash, prevout.index)); + b.del(layout.d.encode(spender.hash, spender.index)); } /** @@ -166,7 +166,7 @@ class TXDB { async writeInput(b, tx, index) { const prevout = tx.inputs[index].prevout; const spender = Outpoint.fromTX(tx, index); - b.put(layout.s.build(prevout.hash, prevout.index), spender.toRaw()); + b.put(layout.s.encode(prevout.hash, prevout.index), spender.toRaw()); return this.addOutpointMap(b, prevout.hash, prevout.index); } @@ -178,7 +178,7 @@ class TXDB { async removeInput(b, tx, index) { const prevout = tx.inputs[index].prevout; - b.del(layout.s.build(prevout.hash, prevout.index)); + b.del(layout.s.encode(prevout.hash, prevout.index)); return this.removeOutpointMap(b, prevout.hash, prevout.index); } @@ -190,7 +190,7 @@ class TXDB { async updateBalance(b, state) { const balance = await this.getWalletBalance(); state.applyTo(balance); - b.put(layout.R.build(), balance.toRaw()); + b.put(layout.R.encode(), balance.toRaw()); return balance; } @@ -203,7 +203,7 @@ class TXDB { async updateAccountBalance(b, acct, delta) { const balance = await this.getAccountBalance(acct); delta.applyTo(balance); - b.put(layout.r.build(acct), balance.toRaw()); + b.put(layout.r.encode(acct), balance.toRaw()); return balance; } @@ -215,7 +215,7 @@ class TXDB { */ async getSpent(hash, index) { - const data = await this.bucket.get(layout.s.build(hash, index)); + const data = await this.bucket.get(layout.s.encode(hash, index)); if (!data) return null; @@ -231,7 +231,7 @@ class TXDB { */ isSpent(hash, index) { - return this.bucket.has(layout.s.build(hash, index)); + return this.bucket.has(layout.s.encode(hash, index)); } /** @@ -305,7 +305,7 @@ class TXDB { return this.bucket.keys({ gte: layout.b.min(), lte: layout.b.max(), - parse: key => layout.b.parse(key) + parse: key => layout.b.decode(key)[0] }); } @@ -316,7 +316,7 @@ class TXDB { */ async getBlock(height) { - const data = await this.bucket.get(layout.b.build(height)); + const data = await this.bucket.get(layout.b.encode(height)); if (!data) return null; @@ -332,7 +332,7 @@ class TXDB { */ async addBlock(b, hash, block) { - const key = layout.b.build(block.height); + const key = layout.b.encode(block.height); const data = await this.bucket.get(key); if (!data) { @@ -360,7 +360,7 @@ class TXDB { */ async removeBlock(b, hash, height) { - const key = layout.b.build(height); + const key = layout.b.encode(height); const data = await this.bucket.get(key); if (!data) @@ -399,11 +399,11 @@ class TXDB { return; if (block.hashes.size === 0) { - b.del(layout.b.build(height)); + b.del(layout.b.encode(height)); return; } - b.put(layout.b.build(height), block.toRaw()); + b.put(layout.b.encode(height), block.toRaw()); } /** @@ -555,13 +555,13 @@ class TXDB { return null; // Save and index the transaction record. - b.put(layout.t.build(hash), wtx.toRaw()); - b.put(layout.m.build(wtx.mtime, hash), null); + b.put(layout.t.encode(hash), wtx.toRaw()); + b.put(layout.m.encode(wtx.mtime, hash), null); if (!block) - b.put(layout.p.build(hash), null); + b.put(layout.p.encode(hash), null); else - b.put(layout.h.build(height, hash), null); + b.put(layout.h.encode(height, hash), null); // Do some secondary indexing for account-based // queries. This saves us a lot of time for @@ -569,13 +569,13 @@ class TXDB { for (const [acct, delta] of state.accounts) { await this.updateAccountBalance(b, acct, delta); - b.put(layout.T.build(acct, hash), null); - b.put(layout.M.build(acct, wtx.mtime, hash), null); + b.put(layout.T.encode(acct, hash), null); + b.put(layout.M.encode(acct, wtx.mtime, hash), null); if (!block) - b.put(layout.P.build(acct, hash), null); + b.put(layout.P.encode(acct, hash), null); else - b.put(layout.H.build(acct, height, hash), null); + b.put(layout.H.encode(acct, height, hash), null); } // Update block records. @@ -710,15 +710,15 @@ class TXDB { // Save the new serialized transaction as // the block-related properties have been // updated. Also reindex for height. - b.put(layout.t.build(hash), wtx.toRaw()); - b.del(layout.p.build(hash)); - b.put(layout.h.build(height, hash), null); + b.put(layout.t.encode(hash), wtx.toRaw()); + b.del(layout.p.encode(hash)); + b.put(layout.h.encode(height, hash), null); // Secondary indexing also needs to change. for (const [acct, delta] of state.accounts) { await this.updateAccountBalance(b, acct, delta); - b.del(layout.P.build(acct, hash)); - b.put(layout.H.build(acct, height, hash), null); + b.del(layout.P.encode(acct, hash)); + b.put(layout.H.encode(acct, height, hash), null); } await this.removeTXMap(b, hash); @@ -832,25 +832,25 @@ class TXDB { // Remove the transaction data // itself as well as unindex. - b.del(layout.t.build(hash)); - b.del(layout.m.build(wtx.mtime, hash)); + b.del(layout.t.encode(hash)); + b.del(layout.m.encode(wtx.mtime, hash)); if (!block) - b.del(layout.p.build(hash)); + b.del(layout.p.encode(hash)); else - b.del(layout.h.build(height, hash)); + b.del(layout.h.encode(height, hash)); // Remove all secondary indexing. for (const [acct, delta] of state.accounts) { await this.updateAccountBalance(b, acct, delta); - b.del(layout.T.build(acct, hash)); - b.del(layout.M.build(acct, wtx.mtime, hash)); + b.del(layout.T.encode(acct, hash)); + b.del(layout.M.encode(acct, wtx.mtime, hash)); if (!block) - b.del(layout.P.build(acct, hash)); + b.del(layout.P.encode(acct, hash)); else - b.del(layout.H.build(acct, height, hash)); + b.del(layout.H.encode(acct, height, hash)); } // Update block records. @@ -1030,15 +1030,15 @@ class TXDB { // We need to update the now-removed // block properties and reindex due // to the height change. - b.put(layout.t.build(hash), wtx.toRaw()); - b.put(layout.p.build(hash), null); - b.del(layout.h.build(height, hash)); + b.put(layout.t.encode(hash), wtx.toRaw()); + b.put(layout.p.encode(hash), null); + b.del(layout.h.encode(height, hash)); // Secondary indexing also needs to change. for (const [acct, delta] of state.accounts) { await this.updateAccountBalance(b, acct, delta); - b.put(layout.P.build(acct, hash), null); - b.del(layout.H.build(acct, height, hash)); + b.put(layout.P.encode(acct, hash), null); + b.del(layout.H.encode(acct, height, hash)); } // Commit state due to unconfirmed @@ -1229,7 +1229,7 @@ class TXDB { gte: layout.T.min(acct), lte: layout.T.max(acct), parse: (key) => { - const [, hash] = layout.T.parse(key); + const [, hash] = layout.T.decode(key); return hash; } }); @@ -1250,7 +1250,7 @@ class TXDB { return this.bucket.keys({ gte: layout.t.min(), lte: layout.t.max(), - parse: key => layout.t.parse(key) + parse: key => layout.t.decode(key)[0] }); } @@ -1266,7 +1266,7 @@ class TXDB { gte: layout.P.min(acct), lte: layout.P.max(acct), parse: (key) => { - const [, hash] = layout.P.parse(key); + const [, hash] = layout.P.decode(key); return hash; } }); @@ -1287,7 +1287,7 @@ class TXDB { return this.bucket.keys({ gte: layout.p.min(), lte: layout.p.max(), - parse: key => layout.p.parse(key) + parse: key => layout.p.decode(key)[0] }); } @@ -1303,7 +1303,7 @@ class TXDB { gte: layout.C.min(acct), lte: layout.C.max(acct), parse: (key) => { - const [, hash, index] = layout.C.parse(key); + const [, hash, index] = layout.C.decode(key); return new Outpoint(hash, index); } }); @@ -1325,7 +1325,7 @@ class TXDB { gte: layout.c.min(), lte: layout.c.max(), parse: (key) => { - const [hash, index] = layout.c.parse(key); + const [hash, index] = layout.c.decode(key); return new Outpoint(hash, index); } }); @@ -1354,7 +1354,7 @@ class TXDB { limit: options.limit, reverse: options.reverse, parse: (key) => { - const [,, hash] = layout.H.parse(key); + const [,, hash] = layout.H.decode(key); return hash; } }); @@ -1386,7 +1386,7 @@ class TXDB { limit: options.limit, reverse: options.reverse, parse: (key) => { - const [, hash] = layout.h.parse(key); + const [, hash] = layout.h.decode(key); return hash; } }); @@ -1425,7 +1425,7 @@ class TXDB { limit: options.limit, reverse: options.reverse, parse: (key) => { - const [,, hash] = layout.M.parse(key); + const [,, hash] = layout.M.decode(key); return hash; } }); @@ -1457,7 +1457,7 @@ class TXDB { limit: options.limit, reverse: options.reverse, parse: (key) => { - const [, hash] = layout.m.parse(key); + const [, hash] = layout.m.decode(key); return hash; } }); @@ -1580,7 +1580,7 @@ class TXDB { gte: layout.c.min(), lte: layout.c.max(), parse: (key, value) => { - const [hash, index] = layout.c.parse(key); + const [hash, index] = layout.c.decode(key); const credit = Credit.fromRaw(value); credit.coin.hash = hash; credit.coin.index = index; @@ -1628,7 +1628,7 @@ class TXDB { gte: layout.d.min(hash), lte: layout.d.max(hash), parse: (key, value) => { - const [, index] = layout.d.parse(key); + const [, index] = layout.d.decode(key); const coin = Coin.fromRaw(value); const input = tx.inputs[index]; assert(input); @@ -1762,7 +1762,7 @@ class TXDB { */ async getTX(hash) { - const raw = await this.bucket.get(layout.t.build(hash)); + const raw = await this.bucket.get(layout.t.encode(hash)); if (!raw) return null; @@ -1849,7 +1849,7 @@ class TXDB { */ hasTX(hash) { - return this.bucket.has(layout.t.build(hash)); + return this.bucket.has(layout.t.encode(hash)); } /** @@ -1876,7 +1876,7 @@ class TXDB { */ async getCredit(hash, index) { - const data = await this.bucket.get(layout.c.build(hash, index)); + const data = await this.bucket.get(layout.c.encode(hash, index)); if (!data) return null; @@ -1896,7 +1896,7 @@ class TXDB { */ async getSpentCoin(spent, prevout) { - const data = await this.bucket.get(layout.d.build(spent.hash, spent.index)); + const data = await this.bucket.get(layout.d.encode(spent.hash, spent.index)); if (!data) return null; @@ -1915,7 +1915,7 @@ class TXDB { */ hasSpentCoin(spent) { - return this.bucket.has(layout.d.build(spent.hash, spent.index)); + return this.bucket.has(layout.d.encode(spent.hash, spent.index)); } /** @@ -1940,7 +1940,7 @@ class TXDB { coin.height = height; - b.put(layout.d.build(spent.hash, spent.index), coin.toRaw()); + b.put(layout.d.encode(spent.hash, spent.index), coin.toRaw()); } /** @@ -1950,7 +1950,7 @@ class TXDB { */ async hasCoin(hash, index) { - return this.bucket.has(layout.c.build(hash, index)); + return this.bucket.has(layout.c.encode(hash, index)); } /** @@ -1974,7 +1974,7 @@ class TXDB { */ async getWalletBalance() { - const data = await this.bucket.get(layout.R.build()); + const data = await this.bucket.get(layout.R.encode()); if (!data) return new Balance(); @@ -1989,7 +1989,7 @@ class TXDB { */ async getAccountBalance(acct) { - const data = await this.bucket.get(layout.r.build(acct)); + const data = await this.bucket.get(layout.r.encode(acct)); if (!data) return new Balance(acct); @@ -2040,7 +2040,7 @@ class TXDB { */ async abandon(hash) { - const result = await this.bucket.has(layout.p.build(hash)); + const result = await this.bucket.has(layout.p.encode(hash)); if (!result) throw new Error('TX not eligible.'); diff --git a/lib/wallet/walletdb.js b/lib/wallet/walletdb.js index 43ad1a2a..6396c99b 100644 --- a/lib/wallet/walletdb.js +++ b/lib/wallet/walletdb.js @@ -178,7 +178,7 @@ class WalletDB extends EventEmitter { async open() { await this.db.open(); - await this.db.verify(layout.V.build(), 'wallet', 7); + await this.db.verify(layout.V.encode(), 'wallet', 7); await this.verifyNetwork(); @@ -215,11 +215,11 @@ class WalletDB extends EventEmitter { */ async verifyNetwork() { - const raw = await this.db.get(layout.O.build()); + const raw = await this.db.get(layout.O.encode()); if (!raw) { const b = this.db.batch(); - b.put(layout.O.build(), fromU32(this.network.magic)); + b.put(layout.O.encode(), fromU32(this.network.magic)); return b.write(); } @@ -262,7 +262,7 @@ class WalletDB extends EventEmitter { let hashes = 0; await piter.each((key) => { - const data = layout.p.parse(key); + const [data] = layout.p.decode(key); this.filter.add(data); @@ -279,7 +279,7 @@ class WalletDB extends EventEmitter { let outpoints = 0; await oiter.each((key) => { - const [hash, index] = layout.o.parse(key); + const [hash, index] = layout.o.decode(key); const outpoint = new Outpoint(hash, index); const data = outpoint.toRaw(); @@ -355,7 +355,7 @@ class WalletDB extends EventEmitter { for (let height = 0; height < hashes.length; height++) { const hash = hashes[height]; const meta = new BlockMeta(hash, height); - b.put(layout.h.build(height), meta.toHash()); + b.put(layout.h.encode(height), meta.toHash()); tip = meta; } @@ -367,7 +367,7 @@ class WalletDB extends EventEmitter { state.height = tip.height; state.marked = false; - b.put(layout.R.build(), state.toRaw()); + b.put(layout.R.encode(), state.toRaw()); await b.write(); @@ -394,7 +394,7 @@ class WalletDB extends EventEmitter { for (let height = 0; height < hashes.length; height++) { const hash = hashes[height]; const meta = new BlockMeta(hash, height); - b.put(layout.h.build(height), meta.toHash()); + b.put(layout.h.encode(height), meta.toHash()); } await b.write(); @@ -604,7 +604,7 @@ class WalletDB extends EventEmitter { */ async getDepth() { - const raw = await this.db.get(layout.D.build()); + const raw = await this.db.get(layout.D.encode()); if (!raw) return 0; @@ -684,7 +684,7 @@ class WalletDB extends EventEmitter { async ensureWID(id) { if (typeof id === 'number') { - if (!await this.db.has(layout.W.build(id))) + if (!await this.db.has(layout.W.encode(id))) return -1; return id; } @@ -699,7 +699,7 @@ class WalletDB extends EventEmitter { */ async getWID(id) { - const data = await this.db.get(layout.l.build(id)); + const data = await this.db.get(layout.l.encode(id)); if (!data) return -1; @@ -716,7 +716,7 @@ class WalletDB extends EventEmitter { */ async getID(wid) { - const data = await this.db.get(layout.W.build(wid)); + const data = await this.db.get(layout.W.encode(wid)); if (!data) return null; @@ -763,7 +763,7 @@ class WalletDB extends EventEmitter { if (!id) return null; - const data = await this.db.get(layout.w.build(wid)); + const data = await this.db.get(layout.w.encode(wid)); assert(data); const wallet = Wallet.fromRaw(this, data); @@ -787,9 +787,9 @@ class WalletDB extends EventEmitter { const wid = wallet.wid; const id = wallet.id; - b.put(layout.w.build(wid), wallet.toRaw()); - b.put(layout.W.build(wid), fromString(id)); - b.put(layout.l.build(id), fromU32(wid)); + b.put(layout.w.encode(wid), wallet.toRaw()); + b.put(layout.W.encode(wid), fromString(id)); + b.put(layout.l.encode(id), fromU32(wid)); } /** @@ -799,7 +799,7 @@ class WalletDB extends EventEmitter { */ increment(b, wid) { - b.put(layout.D.build(), fromU32(wid + 1)); + b.put(layout.D.encode(), fromU32(wid + 1)); } /** @@ -836,13 +836,13 @@ class WalletDB extends EventEmitter { const b = this.db.batch(); // Update wid->id index. - b.put(layout.W.build(wallet.wid), fromString(id)); + b.put(layout.W.encode(wallet.wid), fromString(id)); // Delete old id->wid index. - b.del(layout.l.build(wallet.id)); + b.del(layout.l.encode(wallet.id)); // Add new id->wid index. - b.put(layout.l.build(id), fromU32(wallet.wid)); + b.put(layout.l.encode(id), fromU32(wallet.wid)); await b.write(); @@ -860,13 +860,13 @@ class WalletDB extends EventEmitter { const index = account.accountIndex; // Remove old wid/name->account index. - b.del(layout.i.build(wid, account.name)); + b.del(layout.i.encode(wid, account.name)); // Name->Index lookups - b.put(layout.i.build(wid, name), fromU32(index)); + b.put(layout.i.encode(wid, name), fromU32(index)); // Index->Name lookups - b.put(layout.n.build(wid, index), fromString(name)); + b.put(layout.n.encode(wid, index), fromString(name)); account.name = name; } @@ -915,9 +915,9 @@ class WalletDB extends EventEmitter { const b = this.db.batch(); - b.del(layout.w.build(wid)); - b.del(layout.W.build(wid)); - b.del(layout.l.build(id)); + b.del(layout.w.encode(wid)); + b.del(layout.W.encode(wid)); + b.del(layout.l.encode(id)); const piter = this.db.iterator({ gte: layout.P.min(wid), @@ -925,7 +925,7 @@ class WalletDB extends EventEmitter { }); await piter.each((key, value) => { - const [, hash] = layout.P.parse(key); + const [, hash] = layout.P.decode(key); b.del(key); return this.removePathMap(b, hash, wid); }); @@ -955,11 +955,11 @@ class WalletDB extends EventEmitter { }); await removeRange({ - gt: layout.t.build(wid), - lt: layout.t.build(wid + 1) + gt: layout.t.encode(wid), + lt: layout.t.encode(wid + 1) }); - const bucket = this.db.bucket(layout.t.build(wid)); + const bucket = this.db.bucket(layout.t.encode(wid)); const biter = bucket.iterator({ gte: tlayout.b.min(), @@ -967,7 +967,7 @@ class WalletDB extends EventEmitter { }); await biter.each((key, value) => { - const height = tlayout.b.parse(key); + const [height] = tlayout.b.decode(key); return this.removeBlockMap(b, height, wid); }); @@ -978,7 +978,7 @@ class WalletDB extends EventEmitter { }); await siter.each((key, value) => { - const [hash, index] = tlayout.s.parse(key); + const [hash, index] = tlayout.s.decode(key); return this.removeOutpointMap(b, hash, index, wid); }); @@ -989,7 +989,7 @@ class WalletDB extends EventEmitter { }); await uiter.each((key, value) => { - const hash = tlayout.p.parse(key); + const [hash] = tlayout.p.decode(key); return this.removeTXMap(b, hash, wid); }); @@ -1114,7 +1114,7 @@ class WalletDB extends EventEmitter { if (!name) return null; - const data = await this.db.get(layout.a.build(wid, index)); + const data = await this.db.get(layout.a.encode(wid, index)); assert(data); const account = Account.fromRaw(this, data); @@ -1147,7 +1147,7 @@ class WalletDB extends EventEmitter { */ async getAccountIndex(wid, name) { - const index = await this.db.get(layout.i.build(wid, name)); + const index = await this.db.get(layout.i.encode(wid, name)); if (!index) return -1; @@ -1163,7 +1163,7 @@ class WalletDB extends EventEmitter { */ async getAccountName(wid, index) { - const name = await this.db.get(layout.n.build(wid, index)); + const name = await this.db.get(layout.n.encode(wid, index)); if (!name) return null; @@ -1183,13 +1183,13 @@ class WalletDB extends EventEmitter { const name = account.name; // Account data - b.put(layout.a.build(wid, index), account.toRaw()); + b.put(layout.a.encode(wid, index), account.toRaw()); // Name->Index lookups - b.put(layout.i.build(wid, name), fromU32(index)); + b.put(layout.i.encode(wid, name), fromU32(index)); // Index->Name lookups - b.put(layout.n.build(wid, index), fromString(name)); + b.put(layout.n.encode(wid, index), fromString(name)); } /** @@ -1200,7 +1200,7 @@ class WalletDB extends EventEmitter { */ async hasAccount(wid, index) { - return this.db.has(layout.a.build(wid, index)); + return this.db.has(layout.a.encode(wid, index)); } /** @@ -1232,10 +1232,10 @@ class WalletDB extends EventEmitter { await this.addPathMap(b, path.hash, wid); // Wallet ID + Address Hash -> Path Data - b.put(layout.P.build(wid, path.hash), path.toRaw()); + b.put(layout.P.encode(wid, path.hash), path.toRaw()); // Wallet ID + Account Index + Address Hash -> Dummy - b.put(layout.r.build(wid, path.account, path.hash), null); + b.put(layout.r.encode(wid, path.account, path.hash), null); } /** @@ -1265,7 +1265,7 @@ class WalletDB extends EventEmitter { */ async readPath(wid, hash) { - const data = await this.db.get(layout.P.build(wid, hash)); + const data = await this.db.get(layout.P.encode(wid, hash)); if (!data) return null; @@ -1284,7 +1284,7 @@ class WalletDB extends EventEmitter { */ async hasPath(wid, hash) { - return this.db.has(layout.P.build(wid, hash)); + return this.db.has(layout.P.encode(wid, hash)); } /** @@ -1296,7 +1296,7 @@ class WalletDB extends EventEmitter { return this.db.keys({ gte: layout.p.min(), lte: layout.p.max(), - parse: key => layout.p.parse(key) + parse: key => layout.p.decode(key)[0] }); } @@ -1310,7 +1310,7 @@ class WalletDB extends EventEmitter { gte: layout.o.min(), lte: layout.o.max(), parse: (key) => { - const [hash, index] = layout.o.parse(key); + const [hash, index] = layout.o.decode(key); return new Outpoint(hash, index); } }); @@ -1326,7 +1326,7 @@ class WalletDB extends EventEmitter { return this.db.keys({ gte: layout.P.min(wid), lte: layout.P.max(wid), - parse: key => layout.P.parse(key)[1] + parse: key => layout.P.decode(key)[1] }); } @@ -1341,7 +1341,7 @@ class WalletDB extends EventEmitter { return this.db.keys({ gte: layout.r.min(wid, account), lte: layout.r.max(wid, account), - parse: key => layout.r.parse(key)[2] + parse: key => layout.r.decode(key)[2] }); } @@ -1360,7 +1360,7 @@ class WalletDB extends EventEmitter { const paths = []; for (const {key, value} of items) { - const [, hash] = layout.P.parse(key); + const [, hash] = layout.P.decode(key); const path = Path.fromRaw(value); path.hash = hash; @@ -1401,7 +1401,7 @@ class WalletDB extends EventEmitter { }); await iter.each((k, value) => { - const [, hash] = layout.P.parse(k); + const [, hash] = layout.P.decode(k); const path = Path.fromRaw(value); if (!path.data) @@ -1433,7 +1433,7 @@ class WalletDB extends EventEmitter { }); await iter.each((k, value) => { - const [, hash] = layout.P.parse(k); + const [, hash] = layout.P.decode(k); const path = Path.fromRaw(value); if (!path.data) @@ -1459,7 +1459,7 @@ class WalletDB extends EventEmitter { const wids = await this.db.keys({ gte: layout.w.min(), lte: layout.w.max(), - parse: key => layout.w.parse(key) + parse: key => layout.w.decode(key)[0] }); this.logger.info('Resending from %d wallets.', wids.length); @@ -1476,13 +1476,13 @@ class WalletDB extends EventEmitter { */ async resendPending(wid) { - const prefix = layout.t.build(wid); + const prefix = layout.t.encode(wid); const b = this.db.bucket(prefix); const hashes = await b.keys({ gte: tlayout.p.min(), lte: tlayout.p.max(), - parse: key => tlayout.p.parse(key) + parse: key => tlayout.p.decode(key)[0] }); if (hashes.length === 0) @@ -1496,7 +1496,7 @@ class WalletDB extends EventEmitter { const txs = []; for (const hash of hashes) { - const data = await b.get(tlayout.t.build(hash)); + const data = await b.get(tlayout.t.encode(hash)); if (!data) continue; @@ -1566,7 +1566,7 @@ class WalletDB extends EventEmitter { */ async getState() { - const data = await this.db.get(layout.R.build()); + const data = await this.db.get(layout.R.encode()); if (!data) return null; @@ -1588,7 +1588,7 @@ class WalletDB extends EventEmitter { // Hashes ahead of our new tip // that we need to delete. while (state.height !== tip.height) { - b.del(layout.h.build(state.height)); + b.del(layout.h.encode(state.height)); state.height -= 1; } } else if (tip.height > state.height) { @@ -1603,8 +1603,8 @@ class WalletDB extends EventEmitter { } // Save tip and state. - b.put(layout.h.build(tip.height), tip.toHash()); - b.put(layout.R.build(), state.toRaw()); + b.put(layout.h.encode(tip.height), tip.toHash()); + b.put(layout.R.encode(), state.toRaw()); await b.write(); @@ -1625,7 +1625,7 @@ class WalletDB extends EventEmitter { state.marked = true; const b = this.db.batch(); - b.put(layout.R.build(), state.toRaw()); + b.put(layout.R.encode(), state.toRaw()); await b.write(); this.state = state; @@ -1707,7 +1707,7 @@ class WalletDB extends EventEmitter { */ async getPathMap(hash) { - return this.getMap(layout.p.build(hash)); + return this.getMap(layout.p.encode(hash)); } /** @@ -1719,7 +1719,7 @@ class WalletDB extends EventEmitter { async addPathMap(b, hash, wid) { await this.addHash(hash); - return this.addMap(b, layout.p.build(hash), wid); + return this.addMap(b, layout.p.encode(hash), wid); } /** @@ -1730,7 +1730,7 @@ class WalletDB extends EventEmitter { */ async removePathMap(b, hash, wid) { - return this.removeMap(b, layout.p.build(hash), wid); + return this.removeMap(b, layout.p.encode(hash), wid); } /** @@ -1740,7 +1740,7 @@ class WalletDB extends EventEmitter { */ async getBlockMap(height) { - return this.getMap(layout.b.build(height)); + return this.getMap(layout.b.encode(height)); } /** @@ -1751,7 +1751,7 @@ class WalletDB extends EventEmitter { */ async addBlockMap(b, height, wid) { - return this.addMap(b, layout.b.build(height), wid); + return this.addMap(b, layout.b.encode(height), wid); } /** @@ -1762,7 +1762,7 @@ class WalletDB extends EventEmitter { */ async removeBlockMap(b, height, wid) { - return this.removeMap(b, layout.b.build(height), wid); + return this.removeMap(b, layout.b.encode(height), wid); } /** @@ -1772,7 +1772,7 @@ class WalletDB extends EventEmitter { */ async getTXMap(hash) { - return this.getMap(layout.T.build(hash)); + return this.getMap(layout.T.encode(hash)); } /** @@ -1783,7 +1783,7 @@ class WalletDB extends EventEmitter { */ async addTXMap(b, hash, wid) { - return this.addMap(b, layout.T.build(hash), wid); + return this.addMap(b, layout.T.encode(hash), wid); } /** @@ -1794,7 +1794,7 @@ class WalletDB extends EventEmitter { */ async removeTXMap(b, hash, wid) { - return this.removeMap(b, layout.T.build(hash), wid); + return this.removeMap(b, layout.T.encode(hash), wid); } /** @@ -1804,7 +1804,7 @@ class WalletDB extends EventEmitter { */ async getOutpointMap(hash, index) { - return this.getMap(layout.o.build(hash, index)); + return this.getMap(layout.o.encode(hash, index)); } /** @@ -1816,7 +1816,7 @@ class WalletDB extends EventEmitter { async addOutpointMap(b, hash, index, wid) { await this.addOutpoint(hash, index); - return this.addMap(b, layout.o.build(hash, index), wid); + return this.addMap(b, layout.o.encode(hash, index), wid); } /** @@ -1827,7 +1827,7 @@ class WalletDB extends EventEmitter { */ async removeOutpointMap(b, hash, index, wid) { - return this.removeMap(b, layout.o.build(hash, index), wid); + return this.removeMap(b, layout.o.encode(hash, index), wid); } /** @@ -1837,7 +1837,7 @@ class WalletDB extends EventEmitter { */ async getBlock(height) { - const data = await this.db.get(layout.h.build(height)); + const data = await this.db.get(layout.h.encode(height)); if (!data) return null; @@ -1898,7 +1898,7 @@ class WalletDB extends EventEmitter { async revert(target) { const iter = this.db.iterator({ - gte: layout.b.build(target + 1), + gte: layout.b.encode(target + 1), lte: layout.b.max(), reverse: true, values: true @@ -1907,7 +1907,7 @@ class WalletDB extends EventEmitter { let total = 0; await iter.each(async (key, value) => { - const height = layout.b.parse(key); + const [height] = layout.b.decode(key); const block = MapRecord.fromRaw(value); for (const wid of block.wids) { diff --git a/migrate/chaindb3to4.js b/migrate/chaindb3to4.js index d5085ab0..497940a6 100644 --- a/migrate/chaindb3to4.js +++ b/migrate/chaindb3to4.js @@ -24,7 +24,7 @@ const db = bdb.create({ async function updateVersion() { console.log('Checking version.'); - const data = await db.get(layout.V.build()); + const data = await db.get(layout.V.encode()); assert(data, 'No version.'); const ver = data.readUInt32LE(0, true); @@ -38,7 +38,7 @@ async function updateVersion() { buf.write('chain', 0, 'ascii'); buf.writeUInt32LE(4, 5, true); - parent.put(layout.V.build(), buf); + parent.put(layout.V.encode(), buf); } async function migrateKeys(id, from, to) { @@ -55,7 +55,7 @@ async function migrateKeys(id, from, to) { let items = 0; await iter.each(async (key) => { - batch.put(to.build(...from(key)), null); + batch.put(to.encode(...from(key)), null); batch.del(key); total += (key.length + 80) * 2; @@ -81,10 +81,10 @@ async function updateKeys() { const table = await db.get(v); assert(table); - parent.put(layout.D.build(), table); + parent.put(layout.D.encode(), table); parent.del(v); - const raw = await db.get(layout.O.build()); + const raw = await db.get(layout.O.encode()); assert(raw); const flags = raw.readUInt32LE(8, true); diff --git a/migrate/walletdb6to7.js b/migrate/walletdb6to7.js index 127048e8..1a7e4577 100644 --- a/migrate/walletdb6to7.js +++ b/migrate/walletdb6to7.js @@ -41,7 +41,7 @@ async function updateVersion() { console.log('Checking version.'); - const data = await db.get(layout.V.build()); + const data = await db.get(layout.V.encode()); assert(data, 'No version.'); const ver = data.readUInt32LE(0, true); @@ -58,7 +58,7 @@ async function updateVersion() { buf.write('wallet', 0, 'ascii'); buf.writeUInt32LE(7, 6, true); - parent.put(layout.V.build(), buf); + parent.put(layout.V.encode(), buf); } async function migrateKeys(id, from, to) { @@ -76,7 +76,7 @@ async function migrateKeys(id, from, to) { let items = 0; await iter.each(async (key, value) => { - batch.put(to.build(...from(key)), value); + batch.put(to.encode(...from(key)), value); batch.del(key); total += (key.length + 80) * 2; @@ -108,7 +108,7 @@ async function updateKeys() { } async function updateState() { - const raw = await db.get(layout.R.build()); + const raw = await db.get(layout.R.encode()); if (!raw) return; @@ -119,7 +119,7 @@ async function updateState() { const bw = bio.write(41); bw.writeBytes(raw); bw.writeU8(1); - parent.put(layout.R.build(), bw.render()); + parent.put(layout.R.encode(), bw.render()); console.log('State updated.'); } @@ -128,7 +128,7 @@ async function updateState() { const buf = Buffer.allocUnsafe(4); buf.writeUInt32LE(depth, 0, true); - parent.put(layout.D.build(), buf); + parent.put(layout.D.encode(), buf); } async function updateBlockMap() { @@ -144,7 +144,7 @@ async function updateBlockMap() { let total = 0; await iter.each((key, value) => { - const height = layout.b.parse(key); + const [height] = layout.b.decode(key); const block = BlockMapRecord.fromRaw(height, value); const map = new Set(); @@ -169,7 +169,7 @@ async function updateTXDB() { gte: layout.w.min(), lte: layout.w.max(), keys: true, - parse: key => layout.w.parse(key) + parse: key => layout.w.decode(key)[0] }); console.log('Updating wallets...'); @@ -177,7 +177,7 @@ async function updateTXDB() { let total = 0; for (const wid of wids) { - const bucket = db.bucket(layout.t.build(wid)); + const bucket = db.bucket(layout.t.encode(wid)); const batch = bucket.wrap(parent); await updateInputs(wid, bucket, batch); @@ -205,14 +205,14 @@ async function updateInputs(wid, bucket, batch) { let total = 0; await iter.each(async (key, value) => { - const [, hash] = tlayout.h.parse(key); - const data = await bucket.get(tlayout.t.build(hash)); + const [, hash] = tlayout.h.decode(key); + const data = await bucket.get(tlayout.t.encode(hash)); assert(data); const tx = TX.fromRaw(data); for (const {prevout} of tx.inputs) { const {hash, index} = prevout; - batch.del(tlayout.s.build(hash, index)); + batch.del(tlayout.s.encode(hash, index)); total += 1; } }); @@ -262,8 +262,8 @@ async function updateTX(wid, bucket, batch) { let total = 0; await iter.each(async (key, value) => { - const hash = tlayout.p.parse(key); - const raw = await db.get(layout.T.build(hash)); + const [hash] = tlayout.p.decode(key); + const raw = await db.get(layout.T.encode(hash)); let map = null; @@ -278,7 +278,7 @@ async function updateTX(wid, bucket, batch) { const bw = bio.write(sizeMap(map)); serializeMap(bw, map); - batch.put(layout.T.build(hash), bw.render()); + batch.put(layout.T.encode(hash), bw.render()); total += 1; }); @@ -320,13 +320,13 @@ async function updateWalletBalance(wid, bucket, batch) { bal.unconfirmed += coin.value; }); - batch.put(tlayout.R.build(), serializeBalance(bal)); + batch.put(tlayout.R.encode(), serializeBalance(bal)); console.log('Updated wallet balance for %d.', wid); } async function updateAccountBalances(wid, bucket, batch) { - const raw = await db.get(layout.w.build(wid)); + const raw = await db.get(layout.w.encode(wid)); assert(raw); const br = bio.read(raw, true); @@ -367,8 +367,8 @@ async function updateAccountBalance(wid, acct, bucket, batch) { console.log('Updating account balance for %d/%d...', wid, acct); await iter.each(async (key, value) => { - const [, hash, index] = tlayout.C.parse(key); - const raw = await bucket.get(tlayout.c.build(hash, index)); + const [, hash, index] = tlayout.C.decode(key); + const raw = await bucket.get(tlayout.c.encode(hash, index)); assert(raw); const br = bio.read(raw, true); const coin = Coin.fromReader(br); @@ -383,13 +383,13 @@ async function updateAccountBalance(wid, acct, bucket, batch) { bal.unconfirmed += coin.value; }); - batch.put(tlayout.r.build(acct), serializeBalance(bal)); + batch.put(tlayout.r.encode(acct), serializeBalance(bal)); console.log('Updated account balance for %d/%d.', wid, acct); } async function updateWallet(wid) { - const raw = await db.get(layout.w.build(wid)); + const raw = await db.get(layout.w.encode(wid)); assert(raw); console.log('Updating wallet: %d.', wid); @@ -465,8 +465,8 @@ async function updateWallet(wid) { bw.writeU32(tokenDepth); bw.writeBytes(key); - parent.put(layout.w.build(wid), bw.render()); - parent.put(layout.W.build(wid), fromString(id)); + parent.put(layout.w.encode(wid), bw.render()); + parent.put(layout.W.encode(wid), fromString(id)); console.log('Updating accounts for %d...', wid); @@ -479,7 +479,7 @@ async function updateWallet(wid) { } async function updateAccount(wid, acct) { - const raw = await db.get(layout.a.build(wid, acct)); + const raw = await db.get(layout.a.encode(wid, acct)); assert(raw); console.log('Updating account: %d/%d...', wid, acct); @@ -558,8 +558,8 @@ async function updateAccount(wid, acct) { bw.writeBytes(key.publicKey); } - parent.put(layout.a.build(wid, acct), bw.render()); - parent.put(layout.n.build(wid, acct), fromString(name)); + parent.put(layout.a.encode(wid, acct), bw.render()); + parent.put(layout.n.encode(wid, acct), fromString(name)); console.log('Updated account: %d/%d.', wid, acct); } @@ -669,7 +669,7 @@ async function getDepth() { await iter.end(); - const depth = layout.w.parse(key); + const [depth] = layout.w.decode(key); return depth + 1; }