From 2d2f11b5dc23ef5878cdeefc13acecf33e4b2a5c Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Thu, 21 Jul 2016 14:26:42 -0700 Subject: [PATCH] optimize readVarint. --- README.md | 17 +++++++++++- lib/bcoin/utils.js | 65 ++++++++++++++++++++++++---------------------- 2 files changed, 50 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index c934fe63..a962e86f 100644 --- a/README.md +++ b/README.md @@ -25,9 +25,15 @@ Try it in the browser: http://bcoin.io/browser.html ## Install ``` -$ npm install bcoin +$ git clone git://github.com/bcoin-org/bcoin.git +$ cd bcoin +$ npm install ``` +The latest BCoin has not been published to NPM yet, as it is still under fairly +heavy development (which may involve changing serialization formats for the +database). + ## Documentation Read the docs here: http://bcoin.io/docs/ @@ -38,6 +44,7 @@ Read the docs here: http://bcoin.io/docs/ - [Connecting to the P2P network](#connecting-to-the-p2p-network) - [Doing and SPV sync](#doing-an-spv-sync) - [High-level usage with the Node object](#high-level-usage-with-the-node-object) +- [Running the default full node](#running-the-default-full-node) - [Running a full node in the browser](#running-a-full-node-in-the-browser) - [CLI Usage](#cli-usage) @@ -336,6 +343,14 @@ node.chain.on('full', function() { }); ``` +### Running the default full node + +``` bash +$ node bin/node --fast +``` + +`--fast` will enable checkpoints, coin cache, and getheaders. + ### Running a full node in the browser ``` bash diff --git a/lib/bcoin/utils.js b/lib/bcoin/utils.js index 0147a9d1..c5f65040 100644 --- a/lib/bcoin/utils.js +++ b/lib/bcoin/utils.js @@ -1413,37 +1413,40 @@ utils.readVarint = function readVarint(data, off, big) { assert(off < data.length); - if (data[off] < 0xfd) { - size = 1; - value = data[off]; - if (big) - value = new bn(value); - } else if (data[off] === 0xfd) { - size = 3; - assert(off + size <= data.length); - value = data[off + 1] | (data[off + 2] << 8); - assert(value >= 0xfd); - if (big) - value = new bn(value); - } else if (data[off] === 0xfe) { - size = 5; - assert(off + size <= data.length); - value = data.readUInt32LE(off + 1, true); - assert(value > 0xffff); - if (big) - value = new bn(value); - } else if (data[off] === 0xff) { - size = 9; - assert(off + size <= data.length); - if (big) { - value = utils.readU64(data, off + 1); - assert(value.bitLength() > 32); - } else { - value = utils.readU64N(data, off + 1); - assert(value > 0xffffffff); - } - } else { - assert(false, 'Malformed varint.'); + switch (data[off]) { + case 0xff: + size = 9; + assert(off + size <= data.length); + if (big) { + value = utils.readU64(data, off + 1); + assert(value.bitLength() > 32); + } else { + value = utils.readU64N(data, off + 1); + assert(value > 0xffffffff); + } + break; + case 0xfe: + size = 5; + assert(off + size <= data.length); + value = data.readUInt32LE(off + 1, true); + assert(value > 0xffff); + if (big) + value = new bn(value); + break; + case 0xfd: + size = 3; + assert(off + size <= data.length); + value = data[off + 1] | (data[off + 2] << 8); + assert(value >= 0xfd); + if (big) + value = new bn(value); + break; + default: + size = 1; + value = data[off]; + if (big) + value = new bn(value); + break; } return { size: size, value: value };