optimize readVarint.

This commit is contained in:
Christopher Jeffrey 2016-07-21 14:26:42 -07:00
parent ee08278654
commit 2d2f11b5dc
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 50 additions and 32 deletions

View File

@ -25,9 +25,15 @@ Try it in the browser: http://bcoin.io/browser.html
## Install ## 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 ## Documentation
Read the docs here: http://bcoin.io/docs/ 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) - [Connecting to the P2P network](#connecting-to-the-p2p-network)
- [Doing and SPV sync](#doing-an-spv-sync) - [Doing and SPV sync](#doing-an-spv-sync)
- [High-level usage with the Node object](#high-level-usage-with-the-node-object) - [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) - [Running a full node in the browser](#running-a-full-node-in-the-browser)
- [CLI Usage](#cli-usage) - [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 ### Running a full node in the browser
``` bash ``` bash

View File

@ -1413,37 +1413,40 @@ utils.readVarint = function readVarint(data, off, big) {
assert(off < data.length); assert(off < data.length);
if (data[off] < 0xfd) { switch (data[off]) {
size = 1; case 0xff:
value = data[off]; size = 9;
if (big) assert(off + size <= data.length);
value = new bn(value); if (big) {
} else if (data[off] === 0xfd) { value = utils.readU64(data, off + 1);
size = 3; assert(value.bitLength() > 32);
assert(off + size <= data.length); } else {
value = data[off + 1] | (data[off + 2] << 8); value = utils.readU64N(data, off + 1);
assert(value >= 0xfd); assert(value > 0xffffffff);
if (big) }
value = new bn(value); break;
} else if (data[off] === 0xfe) { case 0xfe:
size = 5; size = 5;
assert(off + size <= data.length); assert(off + size <= data.length);
value = data.readUInt32LE(off + 1, true); value = data.readUInt32LE(off + 1, true);
assert(value > 0xffff); assert(value > 0xffff);
if (big) if (big)
value = new bn(value); value = new bn(value);
} else if (data[off] === 0xff) { break;
size = 9; case 0xfd:
assert(off + size <= data.length); size = 3;
if (big) { assert(off + size <= data.length);
value = utils.readU64(data, off + 1); value = data[off + 1] | (data[off + 2] << 8);
assert(value.bitLength() > 32); assert(value >= 0xfd);
} else { if (big)
value = utils.readU64N(data, off + 1); value = new bn(value);
assert(value > 0xffffffff); break;
} default:
} else { size = 1;
assert(false, 'Malformed varint.'); value = data[off];
if (big)
value = new bn(value);
break;
} }
return { size: size, value: value }; return { size: size, value: value };