From 9f522c5ca4cd76f3205f45d96b045644b20440b1 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Fri, 18 Nov 2016 18:31:07 -0800 Subject: [PATCH] layout: less conditional requires. see #105. --- lib/bip70/pk.js | 2 +- lib/chain/chaindb.js | 144 +-------- lib/chain/{browser.js => layout-browser.js} | 0 lib/chain/layout.js | 153 +++++++++ lib/wallet/{browser.js => layout-browser.js} | 12 +- lib/wallet/layout.js | 312 +++++++++++++++++++ lib/wallet/txdb.js | 185 +---------- lib/wallet/walletdb.js | 136 +------- package.json | 2 + 9 files changed, 481 insertions(+), 465 deletions(-) rename lib/chain/{browser.js => layout-browser.js} (100%) create mode 100644 lib/chain/layout.js rename lib/wallet/{browser.js => layout-browser.js} (97%) create mode 100644 lib/wallet/layout.js diff --git a/lib/bip70/pk.js b/lib/bip70/pk.js index d06de80b..773b8c29 100644 --- a/lib/bip70/pk.js +++ b/lib/bip70/pk.js @@ -1,5 +1,5 @@ /*! - * x509.js - x509 handling for bcoin + * pk.js - public key algorithms for bcoin * Copyright (c) 2016, Christopher Jeffrey (MIT License). * https://github.com/bcoin-org/bcoin */ diff --git a/lib/chain/chaindb.js b/lib/chain/chaindb.js index 5abd4006..8e5f8f17 100644 --- a/lib/chain/chaindb.js +++ b/lib/chain/chaindb.js @@ -17,7 +17,8 @@ var co = require('../utils/co'); var Network = require('../protocol/network'); var CoinView = require('./coinview'); var Coins = require('./coins'); -var ldb = require('../db/ldb'); +var LDB = require('../db/ldb'); +var layout = require('./layout'); var LRU = require('../utils/lru'); var Block = require('../primitives/block'); var Coin = require('../primitives/coin'); @@ -28,125 +29,6 @@ var ChainEntry = require('./chainentry'); var U32 = utils.U32; var DUMMY = new Buffer([0]); -/* - * Database Layout: - * R -> tip hash - * O -> chain options - * e[hash] -> entry - * h[hash] -> height - * H[height] -> hash - * n[hash] -> next hash - * p[hash] -> tip index - * b[hash] -> block - * t[hash] -> extended tx - * c[hash] -> coins - * u[hash] -> undo coins - * T[addr-hash][hash] -> dummy (tx by address) - * C[addr-hash][hash][index] -> dummy (coin by address) - * W+T[witaddr-hash][hash] -> dummy (tx by address) - * W+C[witaddr-hash][hash][index] -> dummy (coin by address) - */ - -var layout = { - R: new Buffer([0x52]), - O: new Buffer([0x4f]), - e: function e(hash) { - return pair(0x65, hash); - }, - h: function h(hash) { - return pair(0x68, hash); - }, - H: function H(height) { - return ipair(0x48, height); - }, - n: function n(hash) { - return pair(0x6e, hash); - }, - p: function p(hash) { - return pair(0x70, hash); - }, - b: function b(hash) { - return pair(0x62, hash); - }, - t: function t(hash) { - return pair(0x74, hash); - }, - c: function c(hash) { - return pair(0x63, hash); - }, - u: function u(hash) { - return pair(0x75, hash); - }, - T: function T(address, hash) { - var len = address.length; - var key; - - if (typeof address === 'string') - len /= 2; - - if (len === 32) { - key = new Buffer(65); - key[0] = 0xab; // W + T - write(key, address, 1); - write(key, hash, 33); - } else { - key = new Buffer(53); - key[0] = 0x54; // T - write(key, address, 1); - write(key, hash, 21); - } - - return key; - }, - C: function C(address, hash, index) { - var len = address.length; - var key; - - if (typeof address === 'string') - len /= 2; - - if (len === 32) { - key = new Buffer(69); - key[0] = 0x9a; // W + C - write(key, address, 1); - write(key, hash, 33); - key.writeUInt32BE(index, 65, true); - } else { - key = new Buffer(57); - key[0] = 0x43; // C - write(key, address, 1); - write(key, hash, 21); - key.writeUInt32BE(index, 53, true); - } - - return key; - }, - pp: function aa(key) { - return key.toString('hex', 1, 33); - }, - Cc: function Cc(key) { - var hash, index; - - if (key.length === 69) { - hash = key.toString('hex', 33, 65); - index = key.readUInt32BE(65, 0); - } else { - hash = key.toString('hex', 21, 53); - index = key.readUInt32BE(53, 0); - } - - return [hash, index]; - }, - Tt: function Tt(key) { - return key.length === 65 - ? key.toString('hex', 33, 65) - : key.toString('hex', 21, 53); - } -}; - -if (utils.isBrowser) - layout = require('./browser'); - /** * The database backend for the {@link Chain} object. * @exports ChainDB @@ -174,7 +56,7 @@ function ChainDB(chain) { this.network = chain.network; this.options = new ChainOptions(chain.options); - this.db = ldb({ + this.db = LDB({ location: chain.options.location, db: chain.options.db, maxOpenFiles: chain.options.maxFiles, @@ -1999,26 +1881,6 @@ ChainState.fromRaw = function fromRaw(data) { * Helpers */ -function write(data, str, off) { - if (Buffer.isBuffer(str)) - return str.copy(data, off); - data.write(str, off, 'hex'); -} - -function pair(prefix, hash) { - var key = new Buffer(33); - key[0] = prefix; - write(key, hash, 1); - return key; -} - -function ipair(prefix, num) { - var key = new Buffer(5); - key[0] = prefix; - key.writeUInt32BE(num, 1, true); - return key; -} - function getSize(value) { return 80 + value.length; } diff --git a/lib/chain/browser.js b/lib/chain/layout-browser.js similarity index 100% rename from lib/chain/browser.js rename to lib/chain/layout-browser.js diff --git a/lib/chain/layout.js b/lib/chain/layout.js new file mode 100644 index 00000000..76e81266 --- /dev/null +++ b/lib/chain/layout.js @@ -0,0 +1,153 @@ +/*! + * layout.js - blockchain data layout for bcoin + * Copyright (c) 2014-2016, Christopher Jeffrey (MIT License). + * https://github.com/bcoin-org/bcoin + */ + +'use strict'; + +/* + * Database Layout: + * R -> tip hash + * O -> chain options + * e[hash] -> entry + * h[hash] -> height + * H[height] -> hash + * n[hash] -> next hash + * p[hash] -> tip index + * b[hash] -> block + * t[hash] -> extended tx + * c[hash] -> coins + * u[hash] -> undo coins + * T[addr-hash][hash] -> dummy (tx by address) + * C[addr-hash][hash][index] -> dummy (coin by address) + * W+T[witaddr-hash][hash] -> dummy (tx by address) + * W+C[witaddr-hash][hash][index] -> dummy (coin by address) + */ + +var layout = { + R: new Buffer([0x52]), + O: new Buffer([0x4f]), + e: function e(hash) { + return pair(0x65, hash); + }, + h: function h(hash) { + return pair(0x68, hash); + }, + H: function H(height) { + return ipair(0x48, height); + }, + n: function n(hash) { + return pair(0x6e, hash); + }, + p: function p(hash) { + return pair(0x70, hash); + }, + b: function b(hash) { + return pair(0x62, hash); + }, + t: function t(hash) { + return pair(0x74, hash); + }, + c: function c(hash) { + return pair(0x63, hash); + }, + u: function u(hash) { + return pair(0x75, hash); + }, + T: function T(address, hash) { + var len = address.length; + var key; + + if (typeof address === 'string') + len /= 2; + + if (len === 32) { + key = new Buffer(65); + key[0] = 0xab; // W + T + write(key, address, 1); + write(key, hash, 33); + } else { + key = new Buffer(53); + key[0] = 0x54; // T + write(key, address, 1); + write(key, hash, 21); + } + + return key; + }, + C: function C(address, hash, index) { + var len = address.length; + var key; + + if (typeof address === 'string') + len /= 2; + + if (len === 32) { + key = new Buffer(69); + key[0] = 0x9a; // W + C + write(key, address, 1); + write(key, hash, 33); + key.writeUInt32BE(index, 65, true); + } else { + key = new Buffer(57); + key[0] = 0x43; // C + write(key, address, 1); + write(key, hash, 21); + key.writeUInt32BE(index, 53, true); + } + + return key; + }, + pp: function aa(key) { + return key.toString('hex', 1, 33); + }, + Cc: function Cc(key) { + var hash, index; + + if (key.length === 69) { + hash = key.toString('hex', 33, 65); + index = key.readUInt32BE(65, 0); + } else { + hash = key.toString('hex', 21, 53); + index = key.readUInt32BE(53, 0); + } + + return [hash, index]; + }, + Tt: function Tt(key) { + return key.length === 65 + ? key.toString('hex', 33, 65) + : key.toString('hex', 21, 53); + } +}; + +/* + * Helpers + */ + +function write(data, str, off) { + if (Buffer.isBuffer(str)) + return str.copy(data, off); + data.write(str, off, 'hex'); +} + +function pair(prefix, hash) { + var key = new Buffer(33); + key[0] = prefix; + write(key, hash, 1); + return key; +} + +function ipair(prefix, num) { + var key = new Buffer(5); + key[0] = prefix; + key.writeUInt32BE(num, 1, true); + return key; +} + +/* + * Expose + */ + +module.exports = layout; diff --git a/lib/wallet/browser.js b/lib/wallet/layout-browser.js similarity index 97% rename from lib/wallet/browser.js rename to lib/wallet/layout-browser.js index 12515125..29752433 100644 --- a/lib/wallet/browser.js +++ b/lib/wallet/layout-browser.js @@ -8,9 +8,9 @@ var utils = require('../utils/utils'); var pad32 = utils.pad32; -var layout = exports; +var layouts = exports; -layout.walletdb = { +layouts.walletdb = { p: function p(hash) { return 'p' + hash; }, @@ -71,7 +71,7 @@ layout.walletdb = { } }; -layout.txdb = { +layouts.txdb = { prefix: function prefix(wid, key) { return 't' + pad32(wid) + key; }, @@ -203,9 +203,3 @@ layout.txdb = { return +key.slice(0); } }; - -/* - * Expose - */ - -module.exports = layout; diff --git a/lib/wallet/layout.js b/lib/wallet/layout.js new file mode 100644 index 00000000..c48cd713 --- /dev/null +++ b/lib/wallet/layout.js @@ -0,0 +1,312 @@ +/*! + * layout.js - data layout for wallets + * Copyright (c) 2014-2016, Christopher Jeffrey (MIT License). + * https://github.com/bcoin-org/bcoin + */ + +'use strict'; + +var layouts = exports; + +/* + * Wallet Database Layout: + * p[addr-hash] -> wallet ids + * P[wid][addr-hash] -> path data + * r[wid][index][hash] -> path account index + * w[wid] -> wallet + * l[id] -> wid + * a[wid][index] -> account + * i[wid][name] -> account index + * n[wid][index] -> account name + * t[wid]* -> txdb + * R -> chain sync state + * h[height] -> recent block hash + * b[height] -> block->wid map + * o[hash][index] -> outpoint->wid map + */ + +layouts.walletdb = { + p: function p(hash) { + var key = new Buffer(1 + (hash.length / 2)); + key[0] = 0x70; + key.write(hash, 1, 'hex'); + return key; + }, + pp: function pp(key) { + return key.toString('hex', 1); + }, + P: function P(wid, hash) { + var key = new Buffer(1 + 4 + (hash.length / 2)); + key[0] = 0x50; + key.writeUInt32BE(wid, 1, true); + key.write(hash, 5, 'hex'); + return key; + }, + Pp: function Pp(key) { + return key.toString('hex', 5); + }, + r: function r(wid, index, hash) { + var key = new Buffer(1 + 4 + 4 + (hash.length / 2)); + key[0] = 0x72; + key.writeUInt32BE(wid, 1, true); + key.writeUInt32BE(index, 5, true); + key.write(hash, 9, 'hex'); + return key; + }, + rr: function rr(key) { + return key.toString('hex', 9); + }, + w: function w(wid) { + var key = new Buffer(5); + key[0] = 0x77; + key.writeUInt32BE(wid, 1, true); + return key; + }, + ww: function ww(key) { + return key.readUInt32BE(1, true); + }, + l: function l(id) { + var len = Buffer.byteLength(id, 'ascii'); + var key = new Buffer(1 + len); + key[0] = 0x6c; + if (len > 0) + key.write(id, 1, 'ascii'); + return key; + }, + ll: function ll(key) { + return key.toString('ascii', 1); + }, + a: function a(wid, index) { + var key = new Buffer(9); + key[0] = 0x61; + key.writeUInt32BE(wid, 1, true); + key.writeUInt32BE(index, 5, true); + return key; + }, + i: function i(wid, name) { + var len = Buffer.byteLength(name, 'ascii'); + var key = new Buffer(5 + len); + key[0] = 0x69; + key.writeUInt32BE(wid, 1, true); + if (len > 0) + key.write(name, 5, 'ascii'); + return key; + }, + ii: function ii(key) { + return [key.readUInt32BE(1, true), key.toString('ascii', 5)]; + }, + n: function n(wid, index) { + var key = new Buffer(9); + key[0] = 0x6e; + key.writeUInt32BE(wid, 1, true); + key.writeUInt32BE(index, 5, true); + return key; + }, + R: new Buffer([0x52]), + h: function h(height) { + var key = new Buffer(5); + key[0] = 0x68; + key.writeUInt32BE(height, 1, true); + return key; + }, + b: function b(height) { + var key = new Buffer(5); + key[0] = 0x62; + key.writeUInt32BE(height, 1, true); + return key; + }, + bb: function bb(key) { + return key.readUInt32BE(1, true); + }, + o: function o(hash, index) { + var key = new Buffer(37); + key[0] = 0x6f; + key.write(hash, 1, 'hex'); + key.writeUInt32BE(index, 33, true); + return key; + }, + oo: function oo(key) { + return [key.toString('hex', 1, 33), key.readUInt32BE(33, true)]; + } +}; + +/* + * TXDB Database Layout: + * t[hash] -> extended tx + * c[hash][index] -> coin + * d[hash][index] -> undo coin + * s[hash][index] -> spent by hash + * o[hash][index] -> orphan inputs + * p[hash] -> dummy (pending flag) + * m[time][hash] -> dummy (tx by time) + * h[height][hash] -> dummy (tx by height) + * T[account][hash] -> dummy (tx by account) + * P[account][hash] -> dummy (pending tx by account) + * M[account][time][hash] -> dummy (tx by time + account) + * H[account][height][hash] -> dummy (tx by height + account) + * C[account][hash][index] -> dummy (coin by account) + * r[hash] -> dummy (replace by fee chain) + */ + +layouts.txdb = { + prefix: function prefix(wid, key) { + var out = new Buffer(5 + key.length); + out[0] = 0x74; + out.writeUInt32BE(wid, 1); + key.copy(out, 5); + return out; + }, + pre: function prefix(key) { + return key.readUInt32BE(1, true); + }, + R: new Buffer([0x52]), + hi: function hi(ch, hash, index) { + var key = new Buffer(37); + key[0] = ch; + key.write(hash, 1, 'hex'); + key.writeUInt32BE(index, 33, true); + return key; + }, + hii: function hii(key) { + key = key.slice(6); + return [key.toString('hex', 0, 32), key.readUInt32BE(32, true)]; + }, + ih: function ih(ch, index, hash) { + var key = new Buffer(37); + key[0] = ch; + key.writeUInt32BE(index, 1, true); + key.write(hash, 5, 'hex'); + return key; + }, + ihh: function ihh(key) { + key = key.slice(6); + return [key.readUInt32BE(0, true), key.toString('hex', 4, 36)]; + }, + iih: function iih(ch, index, num, hash) { + var key = new Buffer(41); + key[0] = ch; + key.writeUInt32BE(index, 1, true); + key.writeUInt32BE(num, 5, true); + key.write(hash, 9, 'hex'); + return key; + }, + iihh: function iihh(key) { + key = key.slice(6); + return [ + key.readUInt32BE(0, true), + key.readUInt32BE(4, true), + key.toString('hex', 8, 40) + ]; + }, + ihi: function ihi(ch, index, hash, num) { + var key = new Buffer(41); + key[0] = ch; + key.writeUInt32BE(index, 1, true); + key.write(hash, 5, 'hex'); + key.writeUInt32BE(num, 37, true); + return key; + }, + ihii: function ihii(key) { + key = key.slice(6); + return [ + key.readUInt32BE(0, true), + key.toString('hex', 4, 36), + key.readUInt32BE(36, true) + ]; + }, + ha: function ha(ch, hash) { + var key = new Buffer(33); + key[0] = ch; + key.write(hash, 1, 'hex'); + return key; + }, + haa: function haa(key) { + key = key.slice(6); + return key.toString('hex', 0); + }, + t: function t(hash) { + return this.ha(0x74, hash); + }, + tt: function tt(key) { + return this.haa(key); + }, + c: function c(hash, index) { + return this.hi(0x63, hash, index); + }, + cc: function cc(key) { + return this.hii(key); + }, + d: function d(hash, index) { + return this.hi(0x64, hash, index); + }, + dd: function dd(key) { + return this.hii(key); + }, + s: function s(hash, index) { + return this.hi(0x73, hash, index); + }, + ss: function ss(key) { + return this.hii(key); + }, + p: function p(hash) { + return this.ha(0x70, hash); + }, + pp: function pp(key) { + return this.haa(key); + }, + m: function m(time, hash) { + return this.ih(0x6d, time, hash); + }, + mm: function mm(key) { + return this.ihh(key); + }, + h: function h(height, hash) { + return this.ih(0x68, height, hash); + }, + hh: function hh(key) { + return this.ihh(key); + }, + T: function T(account, hash) { + return this.ih(0x54, account, hash); + }, + Tt: function Tt(key) { + return this.ihh(key); + }, + P: function P(account, hash) { + return this.ih(0x50, account, hash); + }, + Pp: function Pp(key) { + return this.ihh(key); + }, + M: function M(account, time, hash) { + return this.iih(0x4d, account, time, hash); + }, + Mm: function Mm(key) { + return this.iihh(key); + }, + H: function H(account, height, hash) { + return this.iih(0x48, account, height, hash); + }, + Hh: function Hh(key) { + return this.iihh(key); + }, + C: function C(account, hash, index) { + return this.ihi(0x43, account, hash, index); + }, + Cc: function Cc(key) { + return this.ihii(key); + }, + r: function r(hash) { + return this.ha(0x72, hash); + }, + b: function b(height) { + var key = new Buffer(5); + key[0] = 0x62; + key.writeUInt32BE(height, 1, true); + return key; + }, + bb: function bb(key) { + key = key.slice(6); + return key.readUInt32BE(0, true); + } +}; diff --git a/lib/wallet/txdb.js b/lib/wallet/txdb.js index 0bc4105a..b8f7fa10 100644 --- a/lib/wallet/txdb.js +++ b/lib/wallet/txdb.js @@ -18,194 +18,11 @@ var TX = require('../primitives/tx'); var Coin = require('../primitives/coin'); var Outpoint = require('../primitives/outpoint'); var records = require('./records'); +var layout = require('./layout').txdb; var BlockMapRecord = records.BlockMapRecord; var OutpointMapRecord = records.OutpointMapRecord; var DUMMY = new Buffer([0]); -/* - * Database Layout: - * t[hash] -> extended tx - * c[hash][index] -> coin - * d[hash][index] -> undo coin - * s[hash][index] -> spent by hash - * o[hash][index] -> orphan inputs - * p[hash] -> dummy (pending flag) - * m[time][hash] -> dummy (tx by time) - * h[height][hash] -> dummy (tx by height) - * T[account][hash] -> dummy (tx by account) - * P[account][hash] -> dummy (pending tx by account) - * M[account][time][hash] -> dummy (tx by time + account) - * H[account][height][hash] -> dummy (tx by height + account) - * C[account][hash][index] -> dummy (coin by account) - * r[hash] -> dummy (replace by fee chain) - */ - -var layout = { - prefix: function prefix(wid, key) { - var out = new Buffer(5 + key.length); - out[0] = 0x74; - out.writeUInt32BE(wid, 1); - key.copy(out, 5); - return out; - }, - pre: function prefix(key) { - return key.readUInt32BE(1, true); - }, - R: new Buffer([0x52]), - hi: function hi(ch, hash, index) { - var key = new Buffer(37); - key[0] = ch; - key.write(hash, 1, 'hex'); - key.writeUInt32BE(index, 33, true); - return key; - }, - hii: function hii(key) { - key = key.slice(6); - return [key.toString('hex', 0, 32), key.readUInt32BE(32, true)]; - }, - ih: function ih(ch, index, hash) { - var key = new Buffer(37); - key[0] = ch; - key.writeUInt32BE(index, 1, true); - key.write(hash, 5, 'hex'); - return key; - }, - ihh: function ihh(key) { - key = key.slice(6); - return [key.readUInt32BE(0, true), key.toString('hex', 4, 36)]; - }, - iih: function iih(ch, index, num, hash) { - var key = new Buffer(41); - key[0] = ch; - key.writeUInt32BE(index, 1, true); - key.writeUInt32BE(num, 5, true); - key.write(hash, 9, 'hex'); - return key; - }, - iihh: function iihh(key) { - key = key.slice(6); - return [ - key.readUInt32BE(0, true), - key.readUInt32BE(4, true), - key.toString('hex', 8, 40) - ]; - }, - ihi: function ihi(ch, index, hash, num) { - var key = new Buffer(41); - key[0] = ch; - key.writeUInt32BE(index, 1, true); - key.write(hash, 5, 'hex'); - key.writeUInt32BE(num, 37, true); - return key; - }, - ihii: function ihii(key) { - key = key.slice(6); - return [ - key.readUInt32BE(0, true), - key.toString('hex', 4, 36), - key.readUInt32BE(36, true) - ]; - }, - ha: function ha(ch, hash) { - var key = new Buffer(33); - key[0] = ch; - key.write(hash, 1, 'hex'); - return key; - }, - haa: function haa(key) { - key = key.slice(6); - return key.toString('hex', 0); - }, - t: function t(hash) { - return layout.ha(0x74, hash); - }, - tt: function tt(key) { - return layout.haa(key); - }, - c: function c(hash, index) { - return layout.hi(0x63, hash, index); - }, - cc: function cc(key) { - return layout.hii(key); - }, - d: function d(hash, index) { - return layout.hi(0x64, hash, index); - }, - dd: function dd(key) { - return layout.hii(key); - }, - s: function s(hash, index) { - return layout.hi(0x73, hash, index); - }, - ss: function ss(key) { - return layout.hii(key); - }, - p: function p(hash) { - return layout.ha(0x70, hash); - }, - pp: function pp(key) { - return layout.haa(key); - }, - m: function m(time, hash) { - return layout.ih(0x6d, time, hash); - }, - mm: function mm(key) { - return layout.ihh(key); - }, - h: function h(height, hash) { - return layout.ih(0x68, height, hash); - }, - hh: function hh(key) { - return layout.ihh(key); - }, - T: function T(account, hash) { - return layout.ih(0x54, account, hash); - }, - Tt: function Tt(key) { - return layout.ihh(key); - }, - P: function P(account, hash) { - return layout.ih(0x50, account, hash); - }, - Pp: function Pp(key) { - return layout.ihh(key); - }, - M: function M(account, time, hash) { - return layout.iih(0x4d, account, time, hash); - }, - Mm: function Mm(key) { - return layout.iihh(key); - }, - H: function H(account, height, hash) { - return layout.iih(0x48, account, height, hash); - }, - Hh: function Hh(key) { - return layout.iihh(key); - }, - C: function C(account, hash, index) { - return layout.ihi(0x43, account, hash, index); - }, - Cc: function Cc(key) { - return layout.ihii(key); - }, - r: function r(hash) { - return layout.ha(0x72, hash); - }, - b: function b(height) { - var key = new Buffer(5); - key[0] = 0x62; - key.writeUInt32BE(height, 1, true); - return key; - }, - bb: function bb(key) { - key = key.slice(6); - return key.readUInt32BE(0, true); - } -}; - -if (utils.isBrowser) - layout = require('./browser').txdb; - /** * TXDB * @exports TXDB diff --git a/lib/wallet/walletdb.js b/lib/wallet/walletdb.js index f377b3b4..82710abc 100644 --- a/lib/wallet/walletdb.js +++ b/lib/wallet/walletdb.js @@ -19,146 +19,22 @@ var Network = require('../protocol/network'); var Path = require('./path'); var Wallet = require('./wallet'); var Account = require('./account'); -var ldb = require('../db/ldb'); +var LDB = require('../db/ldb'); var Bloom = require('../utils/bloom'); var Logger = require('../node/logger'); var TX = require('../primitives/tx'); var Outpoint = require('../primitives/outpoint'); +var layouts = require('./layout'); var records = require('./records'); +var layout = layouts.walletdb; var ChainState = records.ChainState; var BlockMapRecord = records.BlockMapRecord; var BlockMeta = records.BlockMeta; var PathMapRecord = records.PathMapRecord; var OutpointMapRecord = records.OutpointMapRecord; -var TXDB = require('./txdb'); var U32 = utils.U32; var DUMMY = new Buffer([0]); -/* - * Database Layout: - * p[addr-hash] -> wallet ids - * P[wid][addr-hash] -> path data - * r[wid][index][hash] -> path account index - * w[wid] -> wallet - * l[id] -> wid - * a[wid][index] -> account - * i[wid][name] -> account index - * n[wid][index] -> account name - * t[wid]* -> txdb - * R -> chain sync state - * h[height] -> recent block hash - * b[height] -> block->wid map - * o[hash][index] -> outpoint->wid map - */ - -var layout = { - p: function p(hash) { - var key = new Buffer(1 + (hash.length / 2)); - key[0] = 0x70; - key.write(hash, 1, 'hex'); - return key; - }, - pp: function pp(key) { - return key.toString('hex', 1); - }, - P: function P(wid, hash) { - var key = new Buffer(1 + 4 + (hash.length / 2)); - key[0] = 0x50; - key.writeUInt32BE(wid, 1, true); - key.write(hash, 5, 'hex'); - return key; - }, - Pp: function Pp(key) { - return key.toString('hex', 5); - }, - r: function r(wid, index, hash) { - var key = new Buffer(1 + 4 + 4 + (hash.length / 2)); - key[0] = 0x72; - key.writeUInt32BE(wid, 1, true); - key.writeUInt32BE(index, 5, true); - key.write(hash, 9, 'hex'); - return key; - }, - rr: function rr(key) { - return key.toString('hex', 9); - }, - w: function w(wid) { - var key = new Buffer(5); - key[0] = 0x77; - key.writeUInt32BE(wid, 1, true); - return key; - }, - ww: function ww(key) { - return key.readUInt32BE(1, true); - }, - l: function l(id) { - var len = Buffer.byteLength(id, 'ascii'); - var key = new Buffer(1 + len); - key[0] = 0x6c; - if (len > 0) - key.write(id, 1, 'ascii'); - return key; - }, - ll: function ll(key) { - return key.toString('ascii', 1); - }, - a: function a(wid, index) { - var key = new Buffer(9); - key[0] = 0x61; - key.writeUInt32BE(wid, 1, true); - key.writeUInt32BE(index, 5, true); - return key; - }, - i: function i(wid, name) { - var len = Buffer.byteLength(name, 'ascii'); - var key = new Buffer(5 + len); - key[0] = 0x69; - key.writeUInt32BE(wid, 1, true); - if (len > 0) - key.write(name, 5, 'ascii'); - return key; - }, - ii: function ii(key) { - return [key.readUInt32BE(1, true), key.toString('ascii', 5)]; - }, - n: function n(wid, index) { - var key = new Buffer(9); - key[0] = 0x6e; - key.writeUInt32BE(wid, 1, true); - key.writeUInt32BE(index, 5, true); - return key; - }, - R: new Buffer([0x52]), - h: function h(height) { - var key = new Buffer(5); - key[0] = 0x68; - key.writeUInt32BE(height, 1, true); - return key; - }, - b: function b(height) { - var key = new Buffer(5); - key[0] = 0x62; - key.writeUInt32BE(height, 1, true); - return key; - }, - bb: function bb(key) { - return key.readUInt32BE(1, true); - }, - o: function o(hash, index) { - var key = new Buffer(37); - key[0] = 0x6f; - key.write(hash, 1, 'hex'); - key.writeUInt32BE(index, 33, true); - return key; - }, - oo: function oo(key) { - return [key.toString('hex', 1, 33), key.readUInt32BE(33, true)]; - } -}; - -if (utils.isBrowser) - layout = require('./browser').walletdb; - /** * WalletDB * @exports WalletDB @@ -209,7 +85,7 @@ function WalletDB(options) { // Memory used: 1.7mb this.filter = Bloom.fromRate(1000000, 0.001, -1); - this.db = ldb({ + this.db = LDB({ location: this.options.location, db: this.options.db, maxOpenFiles: this.options.maxFiles, @@ -1342,7 +1218,7 @@ WalletDB.prototype.decryptKeys = co(function* decryptKeys(wallet, key) { */ WalletDB.prototype.getPendingKeys = co(function* getPendingKeys() { - var layout = TXDB.layout; + var layout = layouts.txdb; var dummy = new Buffer(0); var keys = []; var iter, item; @@ -1372,7 +1248,7 @@ WalletDB.prototype.getPendingKeys = co(function* getPendingKeys() { */ WalletDB.prototype.getPendingTX = co(function* getPendingTX() { - var layout = TXDB.layout; + var layout = layouts.txdb; var keys = yield this.getPendingKeys(); var uniq = {}; var result = []; diff --git a/package.json b/package.json index 9bd8d306..6bed6f1e 100644 --- a/package.json +++ b/package.json @@ -67,6 +67,8 @@ "./lib/http/rpcclient": "./browser/empty.js", "./lib/http/server": "./browser/empty.js", "./lib/http/wallet": "./browser/empty.js", + "./lib/chain/layout": "./lib/chain/layout-browser.js", + "./lib/wallet/layout": "./lib/wallet/layout-browser.js", "fs": "./browser/empty.js", "crypto": "./browser/empty.js", "child_process": "./browser/empty.js",