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