block: rename "abbr" methods to "head".

This commit is contained in:
Christopher Jeffrey 2017-07-25 00:08:28 -07:00
parent 23397dd753
commit 7ed36ec2ba
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
8 changed files with 28 additions and 54 deletions

View File

@ -1027,7 +1027,7 @@ RPC.prototype._submitWork = async function _submitWork(data) {
if (data.length !== 128) if (data.length !== 128)
throw new RPCError(errs.INVALID_PARAMETER, 'Invalid work size.'); throw new RPCError(errs.INVALID_PARAMETER, 'Invalid work size.');
header = Headers.fromAbbr(data); header = Headers.fromHead(data);
data = data.slice(0, 80); data = data.slice(0, 80);
data = swap32(data); data = swap32(data);

View File

@ -257,7 +257,7 @@ CompactBlock.prototype.getSize = function getSize(witness) {
*/ */
CompactBlock.prototype.writeRaw = function writeRaw(bw, witness) { CompactBlock.prototype.writeRaw = function writeRaw(bw, witness) {
this.writeAbbr(bw); this.writeHead(bw);
bw.writeBytes(this.keyNonce); bw.writeBytes(this.keyNonce);
@ -397,7 +397,7 @@ CompactBlock.prototype.hasIndex = function hasIndex(index) {
*/ */
CompactBlock.prototype.getKey = function getKey() { CompactBlock.prototype.getKey = function getKey() {
let data = Buffer.concat([this.abbr(), this.keyNonce]); let data = Buffer.concat([this.toHead(), this.keyNonce]);
let hash = digest.sha256(data); let hash = digest.sha256(data);
return hash.slice(0, 16); return hash.slice(0, 16);
}; };

View File

@ -155,7 +155,7 @@ AbstractBlock.prototype.hash = function _hash(enc) {
let hash = this._hash; let hash = this._hash;
if (!hash) { if (!hash) {
hash = digest.hash256(this.abbr()); hash = digest.hash256(this.toHead());
if (!this.mutable) if (!this.mutable)
this._hash = hash; this._hash = hash;
} }
@ -178,8 +178,8 @@ AbstractBlock.prototype.hash = function _hash(enc) {
* @returns {Buffer} * @returns {Buffer}
*/ */
AbstractBlock.prototype.abbr = function abbr() { AbstractBlock.prototype.toHead = function toHead() {
return this.writeAbbr(new StaticWriter(80)).render(); return this.writeHead(new StaticWriter(80)).render();
}; };
/** /**
@ -187,7 +187,7 @@ AbstractBlock.prototype.abbr = function abbr() {
* @param {BufferWriter} bw * @param {BufferWriter} bw
*/ */
AbstractBlock.prototype.writeAbbr = function writeAbbr(bw) { AbstractBlock.prototype.writeHead = function writeHead(bw) {
bw.writeU32(this.version); bw.writeU32(this.version);
bw.writeHash(this.prevBlock); bw.writeHash(this.prevBlock);
bw.writeHash(this.merkleRoot); bw.writeHash(this.merkleRoot);
@ -202,7 +202,7 @@ AbstractBlock.prototype.writeAbbr = function writeAbbr(bw) {
* @param {BufferReader} br * @param {BufferReader} br
*/ */
AbstractBlock.prototype.parseAbbr = function parseAbbr(br) { AbstractBlock.prototype.parseHead = function parseHead(br) {
this.version = br.readU32(); this.version = br.readU32();
this.prevBlock = br.readHash('hex'); this.prevBlock = br.readHash('hex');
this.merkleRoot = br.readHash('hex'); this.merkleRoot = br.readHash('hex');

View File

@ -644,7 +644,7 @@ Block.prototype.fromReader = function fromReader(br) {
br.start(); br.start();
this.parseAbbr(br); this.parseHead(br);
count = br.readVarint(); count = br.readVarint();
@ -718,14 +718,12 @@ Block.prototype.toMerkle = function toMerkle(filter) {
*/ */
Block.prototype.writeNormal = function writeNormal(bw) { Block.prototype.writeNormal = function writeNormal(bw) {
this.writeAbbr(bw); this.writeHead(bw);
bw.writeVarint(this.txs.length); bw.writeVarint(this.txs.length);
for (let i = 0; i < this.txs.length; i++) { for (let tx of this.txs)
let tx = this.txs[i];
tx.toNormalWriter(bw); tx.toNormalWriter(bw);
}
return bw; return bw;
}; };
@ -739,14 +737,12 @@ Block.prototype.writeNormal = function writeNormal(bw) {
*/ */
Block.prototype.writeWitness = function writeWitness(bw) { Block.prototype.writeWitness = function writeWitness(bw) {
this.writeAbbr(bw); this.writeHead(bw);
bw.writeVarint(this.txs.length); bw.writeVarint(this.txs.length);
for (let i = 0; i < this.txs.length; i++) { for (let tx of this.txs)
let tx = this.txs[i];
tx.toWriter(bw); tx.toWriter(bw);
}
return bw; return bw;
}; };
@ -802,10 +798,8 @@ Block.prototype.getNormalSizes = function getNormalSizes() {
size += 80; size += 80;
size += encoding.sizeVarint(this.txs.length); size += encoding.sizeVarint(this.txs.length);
for (let i = 0; i < this.txs.length; i++) { for (let tx of this.txs)
let tx = this.txs[i];
size += tx.getBaseSize(); size += tx.getBaseSize();
}
return new RawBlock(size, 0); return new RawBlock(size, 0);
}; };
@ -822,8 +816,7 @@ Block.prototype.getWitnessSizes = function getWitnessSizes() {
size += 80; size += 80;
size += encoding.sizeVarint(this.txs.length); size += encoding.sizeVarint(this.txs.length);
for (let i = 0; i < this.txs.length; i++) { for (let tx of this.txs) {
let tx = this.txs[i];
let sizes = tx.getSizes(); let sizes = tx.getSizes();
size += sizes.total; size += sizes.total;
witness += sizes.witness; witness += sizes.witness;

View File

@ -58,7 +58,7 @@ Headers.prototype.getSize = function getSize() {
*/ */
Headers.prototype.toWriter = function toWriter(bw) { Headers.prototype.toWriter = function toWriter(bw) {
this.writeAbbr(bw); this.writeHead(bw);
bw.writeVarint(0); bw.writeVarint(0);
return bw; return bw;
}; };
@ -80,7 +80,7 @@ Headers.prototype.toRaw = function toRaw() {
*/ */
Headers.prototype.fromReader = function fromReader(br) { Headers.prototype.fromReader = function fromReader(br) {
this.parseAbbr(br); this.parseHead(br);
br.readVarint(); br.readVarint();
return this; return this;
}; };
@ -118,34 +118,15 @@ Headers.fromRaw = function fromRaw(data, enc) {
return new Headers().fromRaw(data); return new Headers().fromRaw(data);
}; };
/**
* Inject properties from buffer reader.
* @private
* @param {BufferReader} br
*/
Headers.prototype.fromAbbrReader = function fromAbbrReader(br) {
return this.parseAbbr(br);
};
/** /**
* Inject properties from serialized data. * Inject properties from serialized data.
* TODO: MOVE.
* @private * @private
* @param {Buffer} data * @param {Buffer} data
*/ */
Headers.prototype.fromAbbr = function fromAbbr(data) { Headers.prototype.fromHead = function fromHead(data) {
return this.fromAbbrReader(new BufferReader(data)); return this.parseHead(new BufferReader(data));
};
/**
* Instantiate headers from buffer reader.
* @param {BufferReader} br
* @returns {Headers}
*/
Headers.fromAbbrReader = function fromAbbrReader(br) {
return new Headers().fromAbbrReader(br);
}; };
/** /**
@ -155,10 +136,10 @@ Headers.fromAbbrReader = function fromAbbrReader(br) {
* @returns {Headers} * @returns {Headers}
*/ */
Headers.fromAbbr = function fromAbbr(data, enc) { Headers.fromHead = function fromHead(data, enc) {
if (typeof data === 'string') if (typeof data === 'string')
data = Buffer.from(data, enc); data = Buffer.from(data, enc);
return new Headers().fromAbbr(data); return new Headers().fromHead(data);
}; };
/** /**
@ -291,7 +272,7 @@ Headers.prototype.format = function format(view, height) {
Headers.isHeaders = function isHeaders(obj) { Headers.isHeaders = function isHeaders(obj) {
return obj return obj
&& !obj.txs && !obj.txs
&& typeof obj.abbr === 'function' && typeof obj.toHead === 'function'
&& typeof obj.toBlock !== 'function'; && typeof obj.toBlock !== 'function';
}; };

View File

@ -59,7 +59,7 @@ MemBlock.prototype.memory = true;
* @returns {Buffer} * @returns {Buffer}
*/ */
MemBlock.prototype.abbr = function abbr() { MemBlock.prototype.toHead = function toHead() {
return this._raw.slice(0, 80); return this._raw.slice(0, 80);
}; };
@ -144,7 +144,7 @@ MemBlock.prototype.parseCoinbaseHeight = function parseCoinbaseHeight() {
MemBlock.prototype.fromRaw = function fromRaw(data) { MemBlock.prototype.fromRaw = function fromRaw(data) {
let br = new BufferReader(data, true); let br = new BufferReader(data, true);
this.parseAbbr(br); this.parseHead(br);
this._raw = br.data; this._raw = br.data;

View File

@ -346,7 +346,7 @@ MerkleBlock.prototype.getSize = function getSize() {
*/ */
MerkleBlock.prototype.toWriter = function toWriter(bw) { MerkleBlock.prototype.toWriter = function toWriter(bw) {
this.writeAbbr(bw); this.writeHead(bw);
bw.writeU32(this.totalTX); bw.writeU32(this.totalTX);
@ -380,7 +380,7 @@ MerkleBlock.prototype.toRaw = function toRaw() {
MerkleBlock.prototype.fromReader = function fromReader(br) { MerkleBlock.prototype.fromReader = function fromReader(br) {
let count; let count;
this.parseAbbr(br); this.parseHead(br);
this.totalTX = br.readU32(); this.totalTX = br.readU32();

View File

@ -314,7 +314,7 @@ function parseEntry(data, enc) {
if (typeof data === 'string') if (typeof data === 'string')
data = Buffer.from(data, 'hex'); data = Buffer.from(data, 'hex');
block = Headers.fromAbbr(data); block = Headers.fromHead(data);
br = new BufferReader(data); br = new BufferReader(data);
br.seek(80); br.seek(80);