block/bip152/merkleblock: refactor some block things.
This commit is contained in:
parent
efa0d3eac5
commit
72d5eec710
@ -250,14 +250,17 @@ CompactBlock.prototype.fillMissing = function fillMissing(res) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
CompactBlock.prototype.sid = function sid(hash) {
|
CompactBlock.prototype.sid = function sid(hash) {
|
||||||
|
var lo, hi;
|
||||||
|
|
||||||
if (typeof hash === 'string')
|
if (typeof hash === 'string')
|
||||||
hash = new Buffer(hash, 'hex');
|
hash = new Buffer(hash, 'hex');
|
||||||
|
|
||||||
hash = siphash(hash, this.sipKey);
|
hash = siphash(hash, this.sipKey);
|
||||||
|
|
||||||
return hash.readUInt32LE(0, true)
|
lo = hash.readUInt32LE(0, true);
|
||||||
+ hash.readUInt16LE(4, true)
|
hi = hash.readUInt16LE(4, true);
|
||||||
* 0x100000000;
|
|
||||||
|
return hi * 0x100000000 + lo;
|
||||||
};
|
};
|
||||||
|
|
||||||
CompactBlock.prototype.hasIndex = function hasIndex(index) {
|
CompactBlock.prototype.hasIndex = function hasIndex(index) {
|
||||||
@ -265,14 +268,8 @@ CompactBlock.prototype.hasIndex = function hasIndex(index) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
CompactBlock.prototype.initKey = function initKey() {
|
CompactBlock.prototype.initKey = function initKey() {
|
||||||
var data = new Buffer(88);
|
var data = util.concat(this.abbr(), this.keyNonce);
|
||||||
var hash;
|
var hash = crypto.sha256(data);
|
||||||
|
|
||||||
this.abbr().copy(data, 0);
|
|
||||||
this.keyNonce.copy(data, 80);
|
|
||||||
|
|
||||||
hash = crypto.sha256(data);
|
|
||||||
|
|
||||||
this.sipKey = hash.slice(0, 16);
|
this.sipKey = hash.slice(0, 16);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -332,13 +329,14 @@ CompactBlock.prototype.toBlock = function toBlock() {
|
|||||||
block.bits = this.bits;
|
block.bits = this.bits;
|
||||||
block.nonce = this.nonce;
|
block.nonce = this.nonce;
|
||||||
block.totalTX = this.totalTX;
|
block.totalTX = this.totalTX;
|
||||||
block.txs = new Array(this.ptx.length);
|
|
||||||
block._hash = this._hash;
|
block._hash = this._hash;
|
||||||
|
block._hhash = this._hhash;
|
||||||
|
block._validHeaders = this._validHeaders;
|
||||||
|
|
||||||
for (i = 0; i < this.available.length; i++) {
|
for (i = 0; i < this.available.length; i++) {
|
||||||
tx = this.available[i];
|
tx = this.available[i];
|
||||||
assert(tx, 'Compact block is not full.');
|
assert(tx, 'Compact block is not full.');
|
||||||
block.txs[i] = tx;
|
block.txs.push(tx);
|
||||||
}
|
}
|
||||||
|
|
||||||
return block;
|
return block;
|
||||||
@ -354,6 +352,10 @@ CompactBlock.prototype.fromBlock = function fromBlock(block, witness, nonce) {
|
|||||||
this.bits = block.bits;
|
this.bits = block.bits;
|
||||||
this.nonce = block.nonce;
|
this.nonce = block.nonce;
|
||||||
this.totalTX = block.totalTX;
|
this.totalTX = block.totalTX;
|
||||||
|
this._hash = block._hash;
|
||||||
|
this._hhash = block._hhash;
|
||||||
|
this._validHeaders = true;
|
||||||
|
this._valid = true;
|
||||||
|
|
||||||
if (!nonce)
|
if (!nonce)
|
||||||
nonce = util.nonce();
|
nonce = util.nonce();
|
||||||
|
|||||||
@ -15,7 +15,6 @@ var co = require('../utils/co');
|
|||||||
var Parser = require('./parser');
|
var Parser = require('./parser');
|
||||||
var Framer = require('./framer');
|
var Framer = require('./framer');
|
||||||
var packets = require('./packets');
|
var packets = require('./packets');
|
||||||
var packetTypes = packets.types;
|
|
||||||
var NetworkAddress = require('../primitives/netaddress');
|
var NetworkAddress = require('../primitives/netaddress');
|
||||||
var constants = require('../protocol/constants');
|
var constants = require('../protocol/constants');
|
||||||
var InvItem = require('../primitives/invitem');
|
var InvItem = require('../primitives/invitem');
|
||||||
@ -26,6 +25,9 @@ var BIP150 = require('./bip150');
|
|||||||
var BIP152 = require('./bip152');
|
var BIP152 = require('./bip152');
|
||||||
var Block = require('../primitives/block');
|
var Block = require('../primitives/block');
|
||||||
var TX = require('../primitives/tx');
|
var TX = require('../primitives/tx');
|
||||||
|
var errors = require('../btc/errors');
|
||||||
|
var packetTypes = packets.types;
|
||||||
|
var VerifyResult = errors.VerifyResult;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a remote peer.
|
* Represents a remote peer.
|
||||||
@ -2280,6 +2282,7 @@ Peer.prototype._handleSendCmpct = function _handleSendCmpct(packet) {
|
|||||||
Peer.prototype._handleCmpctBlock = co(function* _handleCmpctBlock(packet) {
|
Peer.prototype._handleCmpctBlock = co(function* _handleCmpctBlock(packet) {
|
||||||
var block = packet.block;
|
var block = packet.block;
|
||||||
var hash = block.hash('hex');
|
var hash = block.hash('hex');
|
||||||
|
var ret = new VerifyResult();
|
||||||
var result;
|
var result;
|
||||||
|
|
||||||
if (!this.options.compact) {
|
if (!this.options.compact) {
|
||||||
@ -2299,6 +2302,14 @@ Peer.prototype._handleCmpctBlock = co(function* _handleCmpctBlock(packet) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!block.verify(ret)) {
|
||||||
|
this.logger.debug(
|
||||||
|
'Peer sent an invalid compact block (%s).',
|
||||||
|
this.hostname);
|
||||||
|
this.reject(block, 'invalid', ret.reason, ret.score);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
result = block.fillMempool(this.options.witness, this.mempool);
|
result = block.fillMempool(this.options.witness, this.mempool);
|
||||||
|
|
||||||
if (result) {
|
if (result) {
|
||||||
|
|||||||
@ -15,8 +15,6 @@ var IP = require('../utils/ip');
|
|||||||
var co = require('../utils/co');
|
var co = require('../utils/co');
|
||||||
var constants = require('../protocol/constants');
|
var constants = require('../protocol/constants');
|
||||||
var errors = require('../btc/errors');
|
var errors = require('../btc/errors');
|
||||||
var VerifyError = errors.VerifyError;
|
|
||||||
var VerifyResult = errors.VerifyResult;
|
|
||||||
var NetworkAddress = require('../primitives/netaddress');
|
var NetworkAddress = require('../primitives/netaddress');
|
||||||
var Address = require('../primitives/address');
|
var Address = require('../primitives/address');
|
||||||
var BIP150 = require('./bip150');
|
var BIP150 = require('./bip150');
|
||||||
@ -29,6 +27,8 @@ var Peer = require('./peer');
|
|||||||
var TX = require('../primitives/tx');
|
var TX = require('../primitives/tx');
|
||||||
var tcp = require('./tcp');
|
var tcp = require('./tcp');
|
||||||
var request = require('../http/request');
|
var request = require('../http/request');
|
||||||
|
var VerifyError = errors.VerifyError;
|
||||||
|
var VerifyResult = errors.VerifyResult;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A pool of peers for handling all network activity.
|
* A pool of peers for handling all network activity.
|
||||||
|
|||||||
@ -677,7 +677,7 @@ Block.prototype.getJSON = function getJSON(network, view, height) {
|
|||||||
nonce: this.nonce,
|
nonce: this.nonce,
|
||||||
totalTX: this.totalTX,
|
totalTX: this.totalTX,
|
||||||
txs: this.txs.map(function(tx, i) {
|
txs: this.txs.map(function(tx, i) {
|
||||||
return tx.getJSON(network, view, this, i);
|
return tx.getJSON(network, view, null, i);
|
||||||
}, this)
|
}, this)
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
@ -205,6 +205,7 @@ Headers.prototype.toHeaders = function toHeaders() {
|
|||||||
Headers.fromBlock = function fromBlock(block) {
|
Headers.fromBlock = function fromBlock(block) {
|
||||||
var headers = new Headers(block);
|
var headers = new Headers(block);
|
||||||
headers._hash = block._hash;
|
headers._hash = block._hash;
|
||||||
|
headers._hhash = block._hhash;
|
||||||
headers._valid = true;
|
headers._valid = true;
|
||||||
return headers;
|
return headers;
|
||||||
};
|
};
|
||||||
|
|||||||
@ -166,6 +166,7 @@ MemBlock.prototype.toNormal = function toNormal() {
|
|||||||
MemBlock.prototype.toBlock = function toBlock() {
|
MemBlock.prototype.toBlock = function toBlock() {
|
||||||
var block = Block.fromRaw(this._raw);
|
var block = Block.fromRaw(this._raw);
|
||||||
block._hash = this._hash;
|
block._hash = this._hash;
|
||||||
|
block._hhash = this._hhash;
|
||||||
block._cbHeight = this._cbHeight;
|
block._cbHeight = this._cbHeight;
|
||||||
block._validHeaders = this._validHeaders;
|
block._validHeaders = this._validHeaders;
|
||||||
this._raw = null;
|
this._raw = null;
|
||||||
|
|||||||
@ -654,6 +654,7 @@ MerkleBlock.fromMatches = function fromMatches(block, matches) {
|
|||||||
|
|
||||||
merkle = new MerkleBlock();
|
merkle = new MerkleBlock();
|
||||||
merkle._hash = block._hash;
|
merkle._hash = block._hash;
|
||||||
|
merkle._hhash = block._hhash;
|
||||||
merkle.version = block.version;
|
merkle.version = block.version;
|
||||||
merkle.prevBlock = block.prevBlock;
|
merkle.prevBlock = block.prevBlock;
|
||||||
merkle.merkleRoot = block.merkleRoot;
|
merkle.merkleRoot = block.merkleRoot;
|
||||||
|
|||||||
@ -154,6 +154,7 @@ describe('Block', function() {
|
|||||||
it('should verify a historical block', function() {
|
it('should verify a historical block', function() {
|
||||||
var view = new CoinView();
|
var view = new CoinView();
|
||||||
var height = block300025.height;
|
var height = block300025.height;
|
||||||
|
var sigops = 0;
|
||||||
var i, j, tx, input, coin, flags;
|
var i, j, tx, input, coin, flags;
|
||||||
|
|
||||||
for (i = 1; i < block300025.txs.length; i++) {
|
for (i = 1; i < block300025.txs.length; i++) {
|
||||||
@ -176,12 +177,14 @@ describe('Block', function() {
|
|||||||
for (i = 1; i < block.txs.length; i++) {
|
for (i = 1; i < block.txs.length; i++) {
|
||||||
tx = block.txs[i];
|
tx = block.txs[i];
|
||||||
assert(tx.isSane());
|
assert(tx.isSane());
|
||||||
assert(tx.checkInputs(view, block300025.height));
|
assert(tx.checkInputs(view, height));
|
||||||
assert(tx.verify(view, flags));
|
assert(tx.verify(view, flags));
|
||||||
assert(!tx.hasWitness());
|
assert(!tx.hasWitness());
|
||||||
|
sigops += tx.getSigopsWeight(view, flags);
|
||||||
view.addTX(tx, height);
|
view.addTX(tx, height);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
assert.equal(sigops, 5280);
|
||||||
assert.equal(block.getReward(view, height), 2507773345);
|
assert.equal(block.getReward(view, height), 2507773345);
|
||||||
assert.equal(block.getReward(view, height), block.txs[0].outputs[0].value);
|
assert.equal(block.getReward(view, height), block.txs[0].outputs[0].value);
|
||||||
});
|
});
|
||||||
@ -198,6 +201,7 @@ describe('Block', function() {
|
|||||||
block2._valid = null;
|
block2._valid = null;
|
||||||
block2._validHeaders = null;
|
block2._validHeaders = null;
|
||||||
block2._hash = null;
|
block2._hash = null;
|
||||||
|
block2._hhash = null;
|
||||||
block2.merkleRoot = block.merkleRoot;
|
block2.merkleRoot = block.merkleRoot;
|
||||||
assert(block2.verify());
|
assert(block2.verify());
|
||||||
});
|
});
|
||||||
@ -213,6 +217,7 @@ describe('Block', function() {
|
|||||||
mblock2._validHeaders = null;
|
mblock2._validHeaders = null;
|
||||||
mblock2._validPartial = null;
|
mblock2._validPartial = null;
|
||||||
mblock2._hash = null;
|
mblock2._hash = null;
|
||||||
|
mblock2._hhash = null;
|
||||||
mblock2.merkleRoot = mblock.merkleRoot;
|
mblock2.merkleRoot = mblock.merkleRoot;
|
||||||
assert(mblock2.verify());
|
assert(mblock2.verify());
|
||||||
});
|
});
|
||||||
@ -227,6 +232,7 @@ describe('Block', function() {
|
|||||||
block2._valid = null;
|
block2._valid = null;
|
||||||
block2._validHeaders = null;
|
block2._validHeaders = null;
|
||||||
block2._hash = null;
|
block2._hash = null;
|
||||||
|
block2._hhash = null;
|
||||||
block2.bits = block.bits;
|
block2.bits = block.bits;
|
||||||
assert(block2.verify());
|
assert(block2.verify());
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user