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
```
$ 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

View File

@ -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 };