bip152: fixes and tests.

This commit is contained in:
Christopher Jeffrey 2016-07-18 18:46:03 -07:00
parent 2feef87331
commit 0ffafa3238
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
3 changed files with 56 additions and 12 deletions

View File

@ -51,7 +51,7 @@ CompactBlock.prototype._verify = function _verify(ret) {
}; };
CompactBlock.prototype.fromOptions = function fromOptions(options) { CompactBlock.prototype.fromOptions = function fromOptions(options) {
assert(bn.isBN(options.keyNonce)); assert(Buffer.isBuffer(options.keyNonce));
assert(Array.isArray(options.ids)); assert(Array.isArray(options.ids));
assert(Array.isArray(options.ptx)); assert(Array.isArray(options.ptx));
@ -92,7 +92,7 @@ CompactBlock.prototype.fromRaw = function fromRaw(data) {
this.bits = p.readU32(); this.bits = p.readU32();
this.nonce = p.readU32(); this.nonce = p.readU32();
this.keyNonce = p.readU64(); this.keyNonce = p.readBytes(8);
this.initKey(); this.initKey();
@ -120,7 +120,9 @@ CompactBlock.prototype.fromRaw = function fromRaw(data) {
return this; return this;
}; };
CompactBlock.fromRaw = function fromRaw(data) { CompactBlock.fromRaw = function fromRaw(data, enc) {
if (typeof data === 'string')
data = new Buffer(data, enc);
return new CompactBlock().fromRaw(data); return new CompactBlock().fromRaw(data);
}; };
@ -135,7 +137,7 @@ CompactBlock.prototype.toRaw = function toRaw(witness, writer) {
p.writeU32(this.bits); p.writeU32(this.bits);
p.writeU32(this.nonce); p.writeU32(this.nonce);
p.writeU64(this.keyNonce); p.writeBytes(this.keyNonce);
p.writeVarint2(this.ids.length); p.writeVarint2(this.ids.length);
@ -239,8 +241,8 @@ CompactBlock.prototype.sid = function sid(hash) {
hash = siphash(hash, this.k0, this.k1); hash = siphash(hash, this.k0, this.k1);
return hash.readUInt32LE(2, true) return hash.readUInt32LE(0, true)
+ hash.readUInt16LE(6, true) + hash.readUInt16LE(4, true)
* 0x100000000; * 0x100000000;
}; };
@ -249,8 +251,7 @@ CompactBlock.prototype.hasIndex = function hasIndex(index) {
}; };
CompactBlock.prototype.initKey = function initKey() { CompactBlock.prototype.initKey = function initKey() {
var nonce = this.keyNonce.toArrayLike(Buffer, 'be', 8); var data = Buffer.concat([this.abbr(), this.keyNonce]);
var data = Buffer.concat([this.abbr(), nonce]);
var hash = utils.sha256(data); var hash = utils.sha256(data);
this.k0 = hash.slice(0, 8); this.k0 = hash.slice(0, 8);
this.k1 = hash.slice(8, 16); this.k1 = hash.slice(8, 16);
@ -322,7 +323,7 @@ CompactBlock.prototype.toBlock = function toBlock() {
return block; return block;
}; };
CompactBlock.prototype.fromBlock = function fromBlock(block) { CompactBlock.prototype.fromBlock = function fromBlock(block, nonce) {
var i, tx, id; var i, tx, id;
this.version = block.version; this.version = block.version;
@ -333,7 +334,10 @@ CompactBlock.prototype.fromBlock = function fromBlock(block) {
this.nonce = block.nonce; this.nonce = block.nonce;
this.totalTX = block.totalTX; this.totalTX = block.totalTX;
this.keyNonce = utils.nonce(); if (!nonce)
nonce = utils.nonce().toArrayLike(Buffer, 'be', 8);
this.keyNonce = nonce;
this.initKey(); this.initKey();
@ -348,8 +352,8 @@ CompactBlock.prototype.fromBlock = function fromBlock(block) {
return this; return this;
}; };
CompactBlock.fromBlock = function fromBlock(block) { CompactBlock.fromBlock = function fromBlock(block, nonce) {
return new CompactBlock().fromBlock(block); return new CompactBlock().fromBlock(block, nonce);
}; };
/** /**

View File

@ -7,6 +7,9 @@ var constants = bcoin.protocol.constants;
var network = bcoin.protocol.network; var network = bcoin.protocol.network;
var assert = require('assert'); var assert = require('assert');
var block300025 = require('./data/block300025.json'); var block300025 = require('./data/block300025.json');
var fs = require('fs');
var cmpct = fs.readFileSync(__dirname + '/data/compactblock.hex', 'utf8').trim().split('\n');
var bip152 = require('../lib/bcoin/bip152');
describe('Block', function() { describe('Block', function() {
var parser = bcoin.protocol.parser; var parser = bcoin.protocol.parser;
@ -204,4 +207,39 @@ describe('Block', function() {
var headers = new bcoin.headers(block); var headers = new bcoin.headers(block);
assert(headers.verify()); assert(headers.verify());
}); });
it('should handle compact block', function(cb) {
var cblock = bip152.CompactBlock.fromRaw(cmpct[0], 'hex');
var block = bcoin.block.fromRaw(cmpct[1], 'hex');
var map = {};
assert.equal(cblock.toRaw().toString('hex'), cmpct[0]);
var cblock2 = bip152.CompactBlock.fromBlock(block, cblock.keyNonce);
assert.equal(cblock2.toRaw().toString('hex'), cmpct[0]);
for (var i = 0; i < block.txs.length; i++) {
var tx = block.txs[i];
map[tx.hash('hex')] = tx;
}
var fakeMempool = {
getSnapshot: function(callback) {
callback(null, Object.keys(map));
},
getTX: function(hash, callback) {
callback(null, map[hash]);
}
};
var realID = 125673511480291;
assert(cblock.sid(block.txs[1].hash('hex')) === realID);
cblock.fillMempool(fakeMempool, function(err, result) {
assert.ifError(err);
assert(result);
for (var i = 0; i < cblock.available.length; i++)
assert(cblock.available[i]);
cb();
});
});
}); });

File diff suppressed because one or more lines are too long