bip152: minor refactor.

This commit is contained in:
Christopher Jeffrey 2017-07-23 20:53:25 -07:00
parent 9d90704b83
commit 69b862df42
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -13,7 +13,6 @@
const assert = require('assert');
const util = require('../utils/util');
const BufferReader = require('../utils/reader');
const BufferWriter = require('../utils/writer');
const StaticWriter = require('../utils/staticwriter');
const encoding = require('../utils/encoding');
const consensus = require('../protocol/consensus');
@ -91,9 +90,7 @@ CompactBlock.prototype.fromOptions = function fromOptions(options) {
if (options.totalTX != null)
this.totalTX = options.totalTX;
this.sipKey = options.sipKey;
this.initKey();
this.sipKey = this.getKey();
return this;
};
@ -135,8 +132,7 @@ CompactBlock.prototype.fromRaw = function fromRaw(data) {
this.nonce = br.readU32();
this.keyNonce = br.readBytes(8);
this.initKey();
this.sipKey = this.getKey();
count = br.readVarint();
@ -397,12 +393,13 @@ CompactBlock.prototype.hasIndex = function hasIndex(index) {
/**
* Initialize the siphash key.
* @private
* @returns {Buffer}
*/
CompactBlock.prototype.initKey = function initKey() {
CompactBlock.prototype.getKey = function getKey() {
let data = Buffer.concat([this.abbr(), this.keyNonce]);
let hash = digest.sha256(data);
this.sipKey = hash.slice(0, 16);
return hash.slice(0, 16);
};
/**
@ -507,8 +504,7 @@ CompactBlock.prototype.fromBlock = function fromBlock(block, witness, nonce) {
nonce = util.nonce();
this.keyNonce = nonce;
this.initKey();
this.sipKey = this.getKey();
for (let i = 1; i < block.txs.length; i++) {
let tx = block.txs[i];
@ -698,7 +694,11 @@ TXRequest.prototype.getSize = function getSize() {
size += encoding.sizeVarint(this.indexes.length);
for (let i = 0; i < this.indexes.length; i++) {
let index = this.indexes[i] - (i === 0 ? 0 : this.indexes[i - 1] + 1);
let index = this.indexes[i];
if (i > 0)
index -= this.indexes[i - 1] + 1;
size += encoding.sizeVarint(index);
}
@ -716,7 +716,11 @@ TXRequest.prototype.toWriter = function toWriter(bw) {
bw.writeVarint(this.indexes.length);
for (let i = 0; i < this.indexes.length; i++) {
let index = this.indexes[i] - (i === 0 ? 0 : this.indexes[i - 1] + 1);
let index = this.indexes[i];
if (i > 0)
index -= this.indexes[i - 1] + 1;
bw.writeVarint(index);
}
@ -729,7 +733,8 @@ TXRequest.prototype.toWriter = function toWriter(bw) {
*/
TXRequest.prototype.toRaw = function toRaw() {
return this.toWriter(new BufferWriter()).render();
let size = this.getSize();
return this.toWriter(new StaticWriter(size)).render();
};
/**