From 613643495942e07812d57845d3ac6e1233018a26 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sun, 20 Mar 2016 09:19:43 -0700 Subject: [PATCH] refactor. --- lib/bcoin/ldb.js | 9 +++++---- lib/bcoin/pool.js | 10 ++++++++-- lib/bcoin/protocol/network.js | 24 ++++-------------------- lib/bcoin/script.js | 10 ++++++---- lib/bcoin/utils.js | 35 +++++++++++++++-------------------- 5 files changed, 38 insertions(+), 50 deletions(-) diff --git a/lib/bcoin/ldb.js b/lib/bcoin/ldb.js index 05bd42ef..c1840a96 100644 --- a/lib/bcoin/ldb.js +++ b/lib/bcoin/ldb.js @@ -24,10 +24,10 @@ module.exports = function ldb(name, options) { if (bcoin.isBrowser) { backend = require('level-js'); } else { - if (!backend || backend === 'rocksdb') - backend = 'rocksdown'; - else if (backend === 'leveldb') + if (!backend || backend === 'leveldb') backend = 'leveldown'; + else if (backend === 'rocksdb') + backend = 'rocksdown'; else if (backend === 'lmdb') backend = 'lmdb'; @@ -52,7 +52,8 @@ module.exports = function ldb(name, options) { writeMap: options.writeMap || false, // For RocksDB - memtableBudget: 512 << 20, + // optimizeCompaction: 'level', + // memtableBudget: 512 << 20, db: backend }); diff --git a/lib/bcoin/pool.js b/lib/bcoin/pool.js index 1bfa6ea3..4948033d 100644 --- a/lib/bcoin/pool.js +++ b/lib/bcoin/pool.js @@ -18,6 +18,7 @@ var constants = bcoin.protocol.constants; function Pool(node, options) { var self = this; + var seeds; if (!(this instanceof Pool)) return new Pool(node, options); @@ -28,6 +29,7 @@ function Pool(node, options) { options = {}; this.options = options; + this.network = node.network; options.spv = options.spv !== false; @@ -41,8 +43,12 @@ function Pool(node, options) { ? (!options.spv ? true : false) : options.relay; - this.network = node.network; - this.originalSeeds = (options.seeds || network.seeds).map(utils.parseHost); + seeds = (options.seeds || network.seeds).slice(); + + if (process.env.BCOIN_SEED) + seeds.unshift(process.env.BCOIN_SEED); + + this.originalSeeds = seeds.map(utils.parseHost); this.setSeeds([]); this.server = null; diff --git a/lib/bcoin/protocol/network.js b/lib/bcoin/protocol/network.js index 5265b80f..108a2cd5 100644 --- a/lib/bcoin/protocol/network.js +++ b/lib/bcoin/protocol/network.js @@ -111,11 +111,7 @@ main.genesis = { hash: utils.revHex( '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f' ), - prevBlock: utils.toHex( - [ 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0 ]), + prevBlock: '0000000000000000000000000000000000000000000000000000000000000000', merkleRoot: utils.revHex( '4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b' ), @@ -234,11 +230,7 @@ testnet.genesis = { hash: utils.revHex( '000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943' ), - prevBlock: utils.toHex( - [ 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0 ]), + prevBlock: '0000000000000000000000000000000000000000000000000000000000000000', merkleRoot: utils.revHex( '4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b' ), @@ -339,11 +331,7 @@ regtest.genesis = { hash: utils.revHex( '0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206' ), - prevBlock: utils.toHex( - [ 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0 ]), + prevBlock: '0000000000000000000000000000000000000000000000000000000000000000', merkleRoot: utils.revHex( '4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b' ), @@ -457,11 +445,7 @@ segnet.genesis = { hash: utils.revHex( '0d5b9c518ddf053fcac71730830df4526a9949c08f34acf6a1d30464d22f02aa' ), - prevBlock: utils.toHex( - [ 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0 ]), + prevBlock: '0000000000000000000000000000000000000000000000000000000000000000', merkleRoot: utils.revHex( '4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b' ), diff --git a/lib/bcoin/script.js b/lib/bcoin/script.js index 9b1ac547..68cb0e13 100644 --- a/lib/bcoin/script.js +++ b/lib/bcoin/script.js @@ -2473,6 +2473,8 @@ Script.decode = function decode(buf) { // script parsing was originally non-strict/ridiculous. // Something could do a direct push of 30 bytes with // only 20 bytes after it. + // NOTE 2: We use reference Buffer slices here. Larger + // buffer slices should _never_ be passed in here. while (off < buf.length) { b = buf[off++]; @@ -2485,7 +2487,7 @@ Script.decode = function decode(buf) { // Direct Push // Next `b` bytes should be pushed to stack if (b >= 0x01 && b <= 0x4b) { - code.push(utils.slice(buf, off, off + b)); + code.push(buf.slice(off, off + b)); off += b; utils.hidden(code[code.length - 1], 'pushdata', { opcode: null, @@ -2512,7 +2514,7 @@ Script.decode = function decode(buf) { if (opcode === 'pushdata1') { len = buf[off]; off += 1; - code.push(utils.slice(buf, off, off + len)); + code.push(buf.slice(off, off + len)); off += len; utils.hidden(code[code.length - 1], 'pushdata', { opcode: opcode, @@ -2521,7 +2523,7 @@ Script.decode = function decode(buf) { } else if (opcode === 'pushdata2') { len = utils.readU16(buf, off); off += 2; - code.push(utils.slice(buf, off, off + len)); + code.push(buf.slice(off, off + len)); off += len; utils.hidden(code[code.length - 1], 'pushdata', { opcode: opcode, @@ -2530,7 +2532,7 @@ Script.decode = function decode(buf) { } else if (opcode === 'pushdata4') { len = utils.readU32(buf, off); off += 4; - code.push(utils.slice(buf, off, off + len)); + code.push(buf.slice(off, off + len)); off += len; utils.hidden(code[code.length - 1], 'pushdata', { opcode: opcode, diff --git a/lib/bcoin/utils.js b/lib/bcoin/utils.js index cb7b2616..078eb5e8 100644 --- a/lib/bcoin/utils.js +++ b/lib/bcoin/utils.js @@ -28,7 +28,8 @@ utils.nop = function() {}; utils.slice = function slice(buf, start, end) { var clone; - buf = buf.slice(start || 0, end || buf.length); + if (start != null) + buf = buf.slice(start, end); clone = new Buffer(buf.length); @@ -61,18 +62,18 @@ utils.toBuffer = function toBuffer(msg, enc) { }; utils.toArray = function toArray(msg, enc) { + var res = []; + var i, c, hi, lo, slice, num; + + if (!msg) + return res; + if (Buffer.isBuffer(msg)) return Array.prototype.slice.call(msg); if (Array.isArray(msg)) return msg.slice(); - if (!msg) - return []; - - var res = []; - var i, c, hi, lo, slice, num; - if (typeof msg === 'string') { if (!enc) { for (i = 0; i < msg.length; i++) { @@ -165,6 +166,7 @@ utils.fromBase58 = function fromBase58(str) { for (i = 0; i < str.length; i++) if (str[i] !== '1') break; + zeroes = i; // Read 4-char words and add them to bignum @@ -374,7 +376,7 @@ utils.toHex = function toHex(msg) { if (typeof msg === 'string') return msg; - return new Buffer(msg).toString('hex'); + return msg.toString('hex'); }; utils.isHex = function isHex(msg) { @@ -611,22 +613,14 @@ utils.isSatoshi = function isSatoshi(val) { return val; if (utils.isInt(val)) return new bn(val, 10); - if (utils.isHex(val)) - return new bn(val, 'hex'); if (Buffer.isBuffer(val)) return new bn(val); - if (Array.isArray(val)) - return new bn(val); return false; }; utils.isBTC = function isBTC(val) { if (utils.isFloat(val)) return val; - // For user input strings. Might cause overlap - // with isSatoshi if not used carefully. - // if (utils.isInt(val)) - // return val; return false; }; @@ -680,12 +674,13 @@ utils.ip2version = function ip2version(ip, version) { // Check to see if this an // ipv4-mapped ipv6 address. if (ip.length > 4) { - while (ip[0] === 0) - ip = ip.slice(1); + i = 0; + while (ip[i] === 0) + i++; // Found an ipv4 address - if (ip.length === 6 && ip[0] === 0xff && ip[1] === 0xff) - return ip.slice(-4); + if (ip.length - i === 6 && ip[i] === 0xff && ip[i + 1] === 0xff) + return utils.slice(ip, -4); // No ipv4 address return new Buffer([0, 0, 0, 0]);