From 83e1de2e9892c74b8f00a1a5b8097d6655f845ee Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Tue, 5 Dec 2017 02:32:54 -0800 Subject: [PATCH] db: stop relying on encoding. --- lib/blockchain/chaindb.js | 14 ++++++++++---- lib/wallet/walletdb.js | 26 +++++++++++++++++++------- test/wallet-test.js | 13 +++++++++---- 3 files changed, 38 insertions(+), 15 deletions(-) diff --git a/lib/blockchain/chaindb.js b/lib/blockchain/chaindb.js index f5fda9e8..fed78df6 100644 --- a/lib/blockchain/chaindb.js +++ b/lib/blockchain/chaindb.js @@ -24,8 +24,6 @@ const ChainEntry = require('./chainentry'); const TXMeta = require('../primitives/txmeta'); const CoinEntry = require('../coins/coinentry'); const {encoding} = bio; -const u8 = encoding.u8; -const u32 = encoding.u32; /** * ChainDB @@ -1386,7 +1384,7 @@ class ChainDB { const hash = block.hash(); // Hash->height index. - this.put(layout.h.build(hash), u32(entry.height)); + this.put(layout.h.build(hash), fromU32(entry.height)); // Entry data. this.put(layout.e.build(hash), entry.toRaw()); @@ -2288,7 +2286,9 @@ class CacheUpdate { } toRaw() { - return u8(this.state); + const data = Buffer.allocUnsafe(1); + data[0] = this.state; + return data; } } @@ -2300,6 +2300,12 @@ function getSize(value) { return value.length + 80; } +function fromU32(num) { + const data = Buffer.allocUnsafe(4); + data.writeUInt32LE(num, 0, true); + return data; +} + /* * Expose */ diff --git a/lib/wallet/walletdb.js b/lib/wallet/walletdb.js index 5c5eaaee..afd36073 100644 --- a/lib/wallet/walletdb.js +++ b/lib/wallet/walletdb.js @@ -27,8 +27,6 @@ const Outpoint = require('../primitives/outpoint'); const layouts = require('./layout'); const records = require('./records'); const NullClient = require('./nullclient'); -const {encoding} = bio; -const {u32} = encoding; const layout = layouts.wdb; const tlayout = layouts.txdb; @@ -216,7 +214,7 @@ class WalletDB extends EventEmitter { if (!raw) { const b = this.db.batch(); - b.put(layout.O.build(), u32(this.network.magic)); + b.put(layout.O.build(), fromU32(this.network.magic)); return b.write(); } @@ -750,7 +748,7 @@ class WalletDB extends EventEmitter { const id = wallet.id; b.put(layout.w.build(wid), wallet.toRaw()); - b.put(layout.l.build(id), u32(wid)); + b.put(layout.l.build(id), fromU32(wid)); } /** @@ -760,7 +758,7 @@ class WalletDB extends EventEmitter { */ increment(b, wid) { - b.put(layout.D.build(), u32(wid + 1)); + b.put(layout.D.build(), fromU32(wid + 1)); } /** @@ -1125,10 +1123,10 @@ class WalletDB extends EventEmitter { b.put(layout.a.build(wid, index), account.toRaw()); // Name->Index lookups - b.put(layout.i.build(wid, name), u32(index)); + b.put(layout.i.build(wid, name), fromU32(index)); // Index->Name lookups - b.put(layout.n.build(wid, index), Buffer.from(name, 'ascii')); + b.put(layout.n.build(wid, index), fromString(name)); } /** @@ -2264,6 +2262,20 @@ class WalletOptions { } } +/* + * Helpers + */ + +function fromU32(num) { + const data = Buffer.allocUnsafe(4); + data.writeUInt32LE(num, 0, true); + return data; +} + +function fromString(str) { + return Buffer.from(str, 'ascii'); +} + /* * Expose */ diff --git a/test/wallet-test.js b/test/wallet-test.js index de0f209a..f5d045b4 100644 --- a/test/wallet-test.js +++ b/test/wallet-test.js @@ -19,7 +19,6 @@ const Input = require('../lib/primitives/input'); const Outpoint = require('../lib/primitives/outpoint'); const Script = require('../lib/script/script'); const HD = require('../lib/hd'); -const u32 = encoding.u32; const KEY1 = 'xprv9s21ZrQH143K3Aj6xQBymM31Zb4BVc7wxqfUhMZrzewdDVCt' + 'qUP9iWfcHgJofs25xbaUpCps9GDXj83NiWvQCAkWQhVj5J4CorfnpKX94AZ'; @@ -37,6 +36,12 @@ let importedKey = null; let doubleSpendWallet = null; let doubleSpendCoin = null; +function fromU32(num) { + const data = Buffer.allocUnsafe(4); + data.writeUInt32LE(num, 0, true); + return data; +} + function curBlock(wdb) { return fakeBlock(wdb.state.height); }; @@ -46,9 +51,9 @@ function nextBlock(wdb) { } function fakeBlock(height) { - const prev = hash256.digest(u32((height - 1) >>> 0)); - const hash = hash256.digest(u32(height >>> 0)); - const root = hash256.digest(u32((height | 0x80000000) >>> 0)); + const prev = hash256.digest(fromU32((height - 1) >>> 0)); + const hash = hash256.digest(fromU32(height >>> 0)); + const root = hash256.digest(fromU32((height | 0x80000000) >>> 0)); return { hash: hash.toString('hex'),