add failing test for Block#toObject
This commit is contained in:
parent
c53b9861d3
commit
dff0891871
@ -55,13 +55,16 @@ Input.prototype._fromObject = function(params) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Input.prototype.toObject = function toObject() {
|
Input.prototype.toObject = function toObject() {
|
||||||
return {
|
var obj = {
|
||||||
prevTxId: this.prevTxId.toString('hex'),
|
prevTxId: this.prevTxId.toString('hex'),
|
||||||
outputIndex: this.outputIndex,
|
outputIndex: this.outputIndex,
|
||||||
sequenceNumber: this.sequenceNumber,
|
sequenceNumber: this.sequenceNumber,
|
||||||
script: this.script.toString(),
|
script: this.script.toString(),
|
||||||
output: this.output ? this.output.toObject() : undefined
|
|
||||||
};
|
};
|
||||||
|
if (this.output) {
|
||||||
|
obj.output = this.output.toObject();
|
||||||
|
}
|
||||||
|
return obj;
|
||||||
};
|
};
|
||||||
|
|
||||||
Input.fromObject = function(obj) {
|
Input.fromObject = function(obj) {
|
||||||
|
|||||||
@ -24,7 +24,7 @@ describe('Block', function() {
|
|||||||
var blockbuf = new Buffer(blockhex, 'hex');
|
var blockbuf = new Buffer(blockhex, 'hex');
|
||||||
var bh = BlockHeader.fromBuffer(new Buffer(data.blockheaderhex, 'hex'));
|
var bh = BlockHeader.fromBuffer(new Buffer(data.blockheaderhex, 'hex'));
|
||||||
var txs = [];
|
var txs = [];
|
||||||
JSON.parse(dataJson).transactions.forEach(function(tx){
|
JSON.parse(dataJson).transactions.forEach(function(tx) {
|
||||||
txs.push(new Transaction().fromJSON(tx));
|
txs.push(new Transaction().fromJSON(tx));
|
||||||
});
|
});
|
||||||
var json = dataJson;
|
var json = dataJson;
|
||||||
@ -32,6 +32,9 @@ describe('Block', function() {
|
|||||||
var genesishex = '0100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a29ab5f49ffff001d1dac2b7c0101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff4d04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73ffffffff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000';
|
var genesishex = '0100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a29ab5f49ffff001d1dac2b7c0101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff4d04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73ffffffff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000';
|
||||||
var genesisbuf = new Buffer(genesishex, 'hex');
|
var genesisbuf = new Buffer(genesishex, 'hex');
|
||||||
var genesisidhex = '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f';
|
var genesisidhex = '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f';
|
||||||
|
var blockOneHex = '010000006fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000982051fd1e4ba744bbbe680e1fee14677ba1a3c3540bf7b1cdb606e857233e0e61bc6649ffff001d01e362990101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0704ffff001d0104ffffffff0100f2052a0100000043410496b538e853519c726a2c91e61ec11600ae1390813a627c66fb8be7947be63c52da7589379515d4e0a604f8141781e62294721166bf621e73a82cbf2342c858eeac00000000';
|
||||||
|
var blockOneBuf = new Buffer(blockOneHex, 'hex');
|
||||||
|
var blockOneId = '00000000839a8e6886ab5951d76f411475428afc90947ee320161bbf18eb6048';
|
||||||
|
|
||||||
it('should make a new block', function() {
|
it('should make a new block', function() {
|
||||||
var b = Block(blockbuf);
|
var b = Block(blockbuf);
|
||||||
@ -56,7 +59,7 @@ describe('Block', function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should properly deserialize blocks', function() {
|
it('should properly deserialize blocks', function() {
|
||||||
dataBlocks.forEach(function(block){
|
dataBlocks.forEach(function(block) {
|
||||||
var b = Block.fromBuffer(new Buffer(block.data, 'hex'));
|
var b = Block.fromBuffer(new Buffer(block.data, 'hex'));
|
||||||
b.transactions.length.should.equal(block.transactions);
|
b.transactions.length.should.equal(block.transactions);
|
||||||
});
|
});
|
||||||
@ -164,6 +167,40 @@ describe('Block', function() {
|
|||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe.only('#toObject', function() {
|
||||||
|
|
||||||
|
it('should recover a block from genesis block buffer', function() {
|
||||||
|
var block = Block.fromBuffer(blockOneBuf);
|
||||||
|
block.id.should.equal(blockOneId);
|
||||||
|
block.toObject().should.deep.equal({
|
||||||
|
header: {
|
||||||
|
version: 1,
|
||||||
|
prevHash: '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f',
|
||||||
|
merkleRoot: '0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098',
|
||||||
|
time: 1231469665,
|
||||||
|
bits: 486604799,
|
||||||
|
nonce: 2573394689
|
||||||
|
},
|
||||||
|
transactions: [{
|
||||||
|
version: 1,
|
||||||
|
inputs: [{
|
||||||
|
prevTxId: '0000000000000000000000000000000000000000000000000000000000000000',
|
||||||
|
outputIndex: 4294967295,
|
||||||
|
sequenceNumber: 4294967295,
|
||||||
|
script: '4 0xffff001d 1 0x04'
|
||||||
|
}],
|
||||||
|
outputs: [{
|
||||||
|
satoshis: 5000000000,
|
||||||
|
script: '65 0x0496b538e853519c726a2c91e61ec11600ae1390813a627c66fb8be7947be' +
|
||||||
|
'63c52da7589379515d4e0a604f8141781e62294721166bf621e73a82cbf2342c858ee OP_CHECKSIG'
|
||||||
|
}],
|
||||||
|
nLockTime: 0
|
||||||
|
}]
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
describe('#_getHash', function() {
|
describe('#_getHash', function() {
|
||||||
|
|
||||||
it('should return the correct hash of the genesis block', function() {
|
it('should return the correct hash of the genesis block', function() {
|
||||||
@ -190,7 +227,7 @@ describe('Block', function() {
|
|||||||
|
|
||||||
it('should return the correct inspect of the genesis block', function() {
|
it('should return the correct inspect of the genesis block', function() {
|
||||||
var block = Block.fromBuffer(genesisbuf);
|
var block = Block.fromBuffer(genesisbuf);
|
||||||
block.inspect().should.equal('<Block '+genesisidhex+'>');
|
block.inspect().should.equal('<Block ' + genesisidhex + '>');
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user