rename. fix testing functions.

This commit is contained in:
Christopher Jeffrey 2016-06-17 04:09:16 -07:00
parent 2d70636fb0
commit 54d3b350e9
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
17 changed files with 57 additions and 92 deletions

View File

@ -81,7 +81,7 @@ Block.fromOptions = function fromOptions(data) {
* @returns {Buffer}
*/
Block.prototype.render = function render(writer) {
Block.prototype.toRaw = function toRaw(writer) {
var raw = this.getRaw();
if (writer) {
writer.writeBytes(raw);
@ -95,7 +95,7 @@ Block.prototype.render = function render(writer) {
* @returns {Buffer}
*/
Block.prototype.renderNormal = function renderNormal(writer) {
Block.prototype.toNormal = function toNormal(writer) {
var raw;
if (!this.hasWitness()) {
raw = this.getRaw();
@ -113,7 +113,7 @@ Block.prototype.renderNormal = function renderNormal(writer) {
* @returns {Buffer}
*/
Block.prototype.renderWitness = function renderWitness(writer) {
Block.prototype.toWitness = function toWitness(writer) {
var raw;
if (this.hasWitness()) {
raw = this.getRaw();
@ -178,7 +178,7 @@ Block.prototype.getSizes = function getSizes() {
}
writer = new bcoin.writer();
this.render(writer);
this.toRaw(writer);
return {
size: writer.written,
@ -685,17 +685,6 @@ Block.fromJSON = function fromJSON(json) {
return new Block().fromJSON(json);
};
/**
* Serialize the block.
* @see {Block#render}
* @param {String?} enc - Encoding, can be `'hex'` or null.
* @returns {Buffer|String}
*/
Block.prototype.toRaw = function toRaw(writer) {
return this.render(writer);
};
/**
* Parse a serialized block.
* @param {Buffer} data
@ -772,18 +761,18 @@ Block.prototype.frame = function frame(witness, writer) {
for (i = 0; i < this.txs.length; i++) {
tx = this.txs[i];
if (witness) {
tx.render(p);
tx.toRaw(p);
witnessSize += tx._lastWitnessSize;
} else {
tx.renderNormal(p);
tx.toNormal(p);
}
}
this._lastWitnessSize = witnessSize;
if (!writer)
p = p.render();
this._lastWitnessSize = witnessSize;
return p;
};
@ -803,7 +792,7 @@ Block.prototype.frameWitness = function frameWitness(writer) {
Block.isBlock = function isBlock(obj) {
return obj
&& typeof obj.merkleRoot === 'string'
&& obj.merkleRoot !== undefined
&& typeof obj.getCommitmentHash === 'function';
};

View File

@ -798,7 +798,7 @@ ChainDB.prototype.saveBlock = function saveBlock(block, view, batch, connect, ca
if (this.options.spv)
return utils.nextTick(callback);
batch.put(layout.b(block.hash()), block.render());
batch.put(layout.b(block.hash()), block.toRaw());
if (!connect)
return utils.nextTick(callback);

View File

@ -300,8 +300,8 @@ Coin.fromTX = function fromTX(tx, index) {
Coin.isCoin = function isCoin(obj) {
return obj
&& typeof obj.version === 'number'
&& typeof obj.script === 'object'
&& obj.version !== undefined
&& obj.script !== undefined
&& typeof obj.getConfirmations === 'function';
};

View File

@ -39,15 +39,6 @@ function Headers(data) {
utils.inherits(Headers, bcoin.abstractblock);
/**
* Serialize the header into a `headers` packet.
* @returns {Buffer}
*/
Headers.prototype.render = function render(writer) {
return this.getRaw(writer);
};
/**
* Do non-contextual verification on the headers.
* @alias Headers#verify

View File

@ -502,9 +502,9 @@ Input.fromExtended = function fromExtended(data, enc) {
Input.isInput = function isInput(obj) {
return obj
&& obj.prevout
&& obj.script
&& obj.witness
&& obj.prevout !== undefined
&& obj.script !== undefined
&& obj.witness !== undefined
&& typeof obj.getAddress === 'function';
};

View File

@ -144,8 +144,13 @@ MemBlock.fromRaw = function fromRaw(data) {
return new MemBlock().fromRaw(data);
};
MemBlock.prototype.toRaw = function toRaw() {
return this.raw;
};
/**
* Parse the serialized block data and create an actual {@link Block}.
* Parse the serialized block data
* and create an actual {@link Block}.
* @returns {Block}
* @throws Parse error
*/

View File

@ -76,15 +76,6 @@ MerkleBlock.fromOptions = function fromOptions(data) {
return new MerkleBlock().fromOptions(data);
};
/**
* Serialize the merkleblock.
* @returns {Buffer}
*/
MerkleBlock.prototype.render = function render(writer) {
return this.toRaw(writer);
};
/**
* Get merkleblock size.
* @returns {Number} Size.
@ -563,7 +554,7 @@ MerkleBlock.fromBlock = function fromBlock(block, filter) {
MerkleBlock.isMerkleBlock = function isMerkleBlock(obj) {
return obj
&& Buffer.isBuffer(obj.flags)
&& obj.flags !== undefined
&& typeof obj.verifyPartial === 'function';
};

View File

@ -689,7 +689,7 @@ MinerBlock.prototype.toRaw = function toRaw(writer) {
p.writeVarint(this.block.txs.length - 1);
for (i = 1; i < this.block.txs.length; i++)
p.writeBytes(this.block.txs[i].render());
p.writeBytes(this.block.txs[i].toRaw());
if (!writer)
p = p.render();

View File

@ -279,8 +279,8 @@ Output.fromRaw = function fromRaw(data, enc) {
Output.isOutput = function isOutput(obj) {
return obj
&& obj.value
&& obj.script
&& obj.value !== undefined
&& obj.script !== undefined
&& typeof obj.getAddress === 'function';
};

View File

@ -653,53 +653,53 @@ Framer._getBlocks = function _getBlocks(data, writer, headers) {
/**
* Serialize transaction without witness.
* @param {NakedTX|TX} tx
* @param {TX} tx
* @param {BufferWriter?} writer - A buffer writer to continue writing from.
* @returns {Buffer} Returns a BufferWriter if `writer` was passed in.
*/
Framer.tx = function _tx(tx, writer) {
return tx.renderNormal(writer);
return tx.toNormal(writer);
};
/**
* Serialize transaction with witness. Calculates the witness
* size as it is framing (exposed on return value as `_witnessSize`).
* @param {NakedTX|TX} tx
* @param {TX} tx
* @param {BufferWriter?} writer - A buffer writer to continue writing from.
* @returns {Buffer} Returns a BufferWriter if `writer` was passed in.
*/
Framer.witnessTX = function _witnessTX(tx, writer) {
return tx.renderWitness(writer);
return tx.toWitness(writer);
};
/**
* Serialize a block without witness data.
* @param {NakedBlock|Block} block
* @param {Block} block
* @param {BufferWriter?} writer - A buffer writer to continue writing from.
* @returns {Buffer} Returns a BufferWriter if `writer` was passed in.
*/
Framer.block = function _block(block, writer) {
return block.renderNormal(block);
return block.toNormal(block);
};
/**
* Serialize a block with witness data. Calculates the witness
* size as it is framing (exposed on return value as `_witnessSize`).
* @param {NakedBlock|Block} block
* @param {Block} block
* @param {BufferWriter?} writer - A buffer writer to continue writing from.
* @returns {Buffer} Returns a BufferWriter if `writer` was passed in.
*/
Framer.witnessBlock = function _witnessBlock(block, writer) {
return block.renderWitness(writer);
return block.toWitness(writer);
};
/**
* Serialize a merkle block.
* @param {NakedBlock|MerkleBlock} block
* @param {MerkleBlock} block
* @param {BufferWriter?} writer - A buffer writer to continue writing from.
* @returns {Buffer} Returns a BufferWriter if `writer` was passed in.
*/
@ -710,7 +710,7 @@ Framer.merkleBlock = function _merkleBlock(block, writer) {
/**
* Serialize headers.
* @param {NakedBlock[]|Headers[]} headers
* @param {Headers[]} headers
* @param {BufferWriter?} writer - A buffer writer to continue writing from.
* @returns {Buffer} Returns a BufferWriter if `writer` was passed in.
*/

View File

@ -634,7 +634,7 @@ Parser.parseInv = function parseInv(p) {
/**
* Parse merkleblock packet.
* @param {Buffer|BufferReader} p
* @returns {NakedBlock}
* @returns {Block}
*/
Parser.parseMerkleBlock = function parseMerkleBlock(p) {
@ -644,7 +644,7 @@ Parser.parseMerkleBlock = function parseMerkleBlock(p) {
/**
* Parse headers packet.
* @param {Buffer|BufferReader} p
* @returns {NakedBlock[]}
* @returns {Headers[]}
*/
Parser.parseHeaders = function parseHeaders(p) {
@ -664,7 +664,7 @@ Parser.parseHeaders = function parseHeaders(p) {
/**
* Parse block packet.
* @param {Buffer|BufferReader} p
* @returns {NakedBlock}
* @returns {Block}
*/
Parser.parseBlock = function parseBlock(p) {
@ -674,7 +674,7 @@ Parser.parseBlock = function parseBlock(p) {
/**
* Parse block packet.
* @param {Buffer|BufferReader} p
* @returns {NakedBlock}
* @returns {Block}
*/
Parser.parseMemBlock = function parseMemBlock(p) {
@ -685,7 +685,7 @@ Parser.parseMemBlock = function parseMemBlock(p) {
* Parse tx packet (will automatically switch to witness
* parsing if a witness transaction is detected).
* @param {Buffer|BufferReader} p
* @returns {NakedTX}
* @returns {TX}
*/
Parser.parseTX = function parseTX(p) {

View File

@ -197,7 +197,7 @@ TX.prototype.hash = function _hash(enc) {
var hash = this._hash;
if (!hash) {
hash = utils.dsha256(this.renderNormal());
hash = utils.dsha256(this.toNormal());
if (!this.mutable)
this._hash = hash;
}
@ -227,7 +227,7 @@ TX.prototype.witnessHash = function witnessHash(enc) {
return this.hash(enc);
if (!hash) {
hash = utils.dsha256(this.renderWitness());
hash = utils.dsha256(this.toWitness());
if (!this.mutable)
this._whash = hash;
}
@ -243,7 +243,7 @@ TX.prototype.witnessHash = function witnessHash(enc) {
* @returns {Buffer} Serialized transaction.
*/
TX.prototype.render = function render(writer) {
TX.prototype.toRaw = function toRaw(writer) {
var raw = this.getRaw();
if (writer) {
writer.writeBytes(raw);
@ -259,7 +259,7 @@ TX.prototype.render = function render(writer) {
* @returns {Buffer} Serialized transaction.
*/
TX.prototype.renderNormal = function renderNormal(writer) {
TX.prototype.toNormal = function toNormal(writer) {
var raw = this.getRaw();
if (!TX.isWitness(raw)) {
if (writer) {
@ -278,7 +278,7 @@ TX.prototype.renderNormal = function renderNormal(writer) {
* @returns {Buffer} Serialized transaction.
*/
TX.prototype.renderWitness = function renderWitness(writer) {
TX.prototype.toWitness = function toWitness(writer) {
var raw = this.getRaw();
if (TX.isWitness(raw)) {
if (writer) {
@ -1918,17 +1918,6 @@ TX.fromJSON = function fromJSON(json) {
return new TX().fromJSON(json);
};
/**
* Serialize the transaction.
* @see {TX#render}
* @param {String?} enc - Encoding, can be `'hex'` or null.
* @returns {Buffer|String}
*/
TX.prototype.toRaw = function toRaw(writer) {
return this.render(writer);
};
/**
* Instantiate a transaction from a serialized Buffer.
* @param {Buffer} data
@ -2124,11 +2113,11 @@ TX.prototype.frameWitness = function frameWitness(writer) {
p.writeU32(this.locktime);
this._lastWitnessSize = witnessSize + 2;
if (!writer)
p = p.render();
this._lastWitnessSize = witnessSize + 2;
return p;
};

View File

@ -190,7 +190,7 @@
/**
* @typedef {Object} SubmitOrderPacket
* @property {Hash} hash
* @property {NakedTX} tx
* @property {TX} tx
* @global
*/
@ -246,7 +246,7 @@
* @property {Buffer?} data.map - Hit map.
* @property {Object} data.height - Chain height.
* @property {Hash} data.tip - Chain tip hash.
* @property {NakedCoin[]} data.coins
* @property {Coin[]} data.coins
* @global
*/

View File

@ -817,7 +817,7 @@ Framer.item = function _item(item, p) {
} else {
if (item instanceof bcoin.block) {
p.writeU8(40);
item.render(p);
item.toRaw(p);
} else if (item instanceof bcoin.tx) {
p.writeU8(41);
item.toExtended(true, p);

View File

@ -70,7 +70,7 @@ describe('Block', function() {
it('should decode/encode with parser/framer', function() {
var b = bcoin.merkleblock.fromRaw(raw, 'hex');
assert.equal(b.render().toString('hex'), raw);
assert.equal(b.toRaw().toString('hex'), raw);
assert.equal(raw, raw2);
});
@ -82,7 +82,7 @@ describe('Block', function() {
it('should be serialized and deserialized and still verify', function() {
var raw = mblock.toRaw();
var b = bcoin.merkleblock.fromRaw(raw);
assert.deepEqual(b.render(), raw);
assert.deepEqual(b.toRaw(), raw);
assert(b.verify());
});
@ -129,7 +129,7 @@ describe('Block', function() {
filter.add(item2, 'hex');
var mblock2 = bcoin.merkleblock.fromBlock(block, filter);
assert(mblock2.verifyPartial());
assert.deepEqual(mblock2.render(), mblock.render());
assert.deepEqual(mblock2.toRaw(), mblock.toRaw());
});
it('should verify a historical block', function() {

View File

@ -113,7 +113,7 @@ describe('TX', function() {
it('should decode/encode with parser/framer' + suffix, function() {
var tx = bcoin.tx.fromRaw(raw, 'hex');
clearCache(tx, nocache);
assert.equal(tx.render().toString('hex'), raw);
assert.equal(tx.toRaw().toString('hex'), raw);
});
it('should be verifiable' + suffix, function() {
@ -163,9 +163,9 @@ describe('TX', function() {
assert.equal(wtx.getSize(), 62138);
assert.equal(wtx.getVirtualSize(), 61813);
assert.equal(wtx.getCost(), 247250);
var raw1 = wtx.render();
var raw1 = wtx.toRaw();
clearCache(wtx, true);
var raw2 = wtx.render();
var raw2 = wtx.toRaw();
assert.deepEqual(raw1, raw2);
var wtx2 = bcoin.tx.fromRaw(raw2);
clearCache(wtx2, nocache);

View File

@ -177,7 +177,7 @@ describe('Wallet', function() {
var maxSize = tx.maxSize();
w.sign(tx, function(err) {
assert.ifError(err);
assert(tx.render().length <= maxSize);
assert(tx.toRaw().length <= maxSize);
assert(tx.verify());
});
});