Removed magicnumber and size as properties of a block.
This commit is contained in:
parent
116ddac345
commit
6aa0d14bb9
29
lib/block.js
29
lib/block.js
|
@ -43,20 +43,6 @@ Block._from = function _from(arg) {
|
||||||
info = Block._fromJSON(arg);
|
info = Block._fromJSON(arg);
|
||||||
} else if (_.isObject(arg)) {
|
} else if (_.isObject(arg)) {
|
||||||
info = {
|
info = {
|
||||||
/**
|
|
||||||
* @name Block#magicnum
|
|
||||||
* @type number
|
|
||||||
*/
|
|
||||||
magicnum: arg.magicnum,
|
|
||||||
/**
|
|
||||||
* @name Block#size
|
|
||||||
* @type number
|
|
||||||
*/
|
|
||||||
size: arg.size,
|
|
||||||
/**
|
|
||||||
* @name Block#header
|
|
||||||
* @type {BlockHeader}
|
|
||||||
*/
|
|
||||||
header: arg.header,
|
header: arg.header,
|
||||||
/**
|
/**
|
||||||
* @name Block#transactions
|
* @name Block#transactions
|
||||||
|
@ -84,8 +70,6 @@ Block._fromJSON = function _fromJSON(data) {
|
||||||
transactions.push(Transaction().fromJSON(data));
|
transactions.push(Transaction().fromJSON(data));
|
||||||
});
|
});
|
||||||
var info = {
|
var info = {
|
||||||
magicnum: data.magicnum,
|
|
||||||
size: data.size,
|
|
||||||
header: BlockHeader.fromJSON(data.header),
|
header: BlockHeader.fromJSON(data.header),
|
||||||
transactions: transactions
|
transactions: transactions
|
||||||
};
|
};
|
||||||
|
@ -106,13 +90,8 @@ Block.fromJSON = function fromJSON(json) {
|
||||||
* @returns {Object} - An object representing the block data
|
* @returns {Object} - An object representing the block data
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
Block._fromBufferReader = function _fromBufferReader(br, options) {
|
Block._fromBufferReader = function _fromBufferReader(br) {
|
||||||
options = options || {};
|
|
||||||
var info = {};
|
var info = {};
|
||||||
if (!options.skipMagic) {
|
|
||||||
info.magicnum = br.readUInt32LE();
|
|
||||||
info.size = br.readUInt32LE();
|
|
||||||
}
|
|
||||||
info.header = BlockHeader.fromBufferReader(br);
|
info.header = BlockHeader.fromBufferReader(br);
|
||||||
var transactions = br.readVarintNum();
|
var transactions = br.readVarintNum();
|
||||||
info.transactions = [];
|
info.transactions = [];
|
||||||
|
@ -157,6 +136,7 @@ Block.fromRawBlock = function fromRawBlock(data) {
|
||||||
data = new Buffer(data, 'binary');
|
data = new Buffer(data, 'binary');
|
||||||
}
|
}
|
||||||
var br = BufferReader(data);
|
var br = BufferReader(data);
|
||||||
|
br.pos = Block.Values.START_OF_BLOCK;
|
||||||
var info = Block._fromBufferReader(br);
|
var info = Block._fromBufferReader(br);
|
||||||
return new Block(info);
|
return new Block(info);
|
||||||
};
|
};
|
||||||
|
@ -170,8 +150,6 @@ Block.prototype.toObject = function toObject() {
|
||||||
transactions.push(tx.toObject());
|
transactions.push(tx.toObject());
|
||||||
});
|
});
|
||||||
return {
|
return {
|
||||||
magicnum: this.magicnum,
|
|
||||||
size: this.size,
|
|
||||||
header: this.header.toObject(),
|
header: this.header.toObject(),
|
||||||
transactions: transactions
|
transactions: transactions
|
||||||
};
|
};
|
||||||
|
@ -206,8 +184,6 @@ Block.prototype.toBufferWriter = function toBufferWriter(bw) {
|
||||||
if (!bw) {
|
if (!bw) {
|
||||||
bw = new BufferWriter();
|
bw = new BufferWriter();
|
||||||
}
|
}
|
||||||
bw.writeUInt32LE(this.magicnum);
|
|
||||||
bw.writeUInt32LE(this.size);
|
|
||||||
bw.write(this.header.toBuffer());
|
bw.write(this.header.toBuffer());
|
||||||
bw.writeVarintNum(this.transactions.length);
|
bw.writeVarintNum(this.transactions.length);
|
||||||
for (var i = 0; i < this.transactions.length; i++) {
|
for (var i = 0; i < this.transactions.length; i++) {
|
||||||
|
@ -311,6 +287,7 @@ Block.prototype.inspect = function inspect() {
|
||||||
};
|
};
|
||||||
|
|
||||||
Block.Values = {
|
Block.Values = {
|
||||||
|
START_OF_BLOCK: 8, // Start of block in raw block data
|
||||||
NULL_HASH: new Buffer('0000000000000000000000000000000000000000000000000000000000000000', 'hex')
|
NULL_HASH: new Buffer('0000000000000000000000000000000000000000000000000000000000000000', 'hex')
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -31,7 +31,7 @@ describe('Block', function() {
|
||||||
});
|
});
|
||||||
var json = dataJson;
|
var json = dataJson;
|
||||||
|
|
||||||
var genesishex = 'f9beb4d91d0100000100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a29ab5f49ffff001d1dac2b7c0101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff4d04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73ffffffff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000';
|
var genesishex = '0100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a29ab5f49ffff001d1dac2b7c0101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff4d04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73ffffffff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000';
|
||||||
var genesisbuf = new Buffer(genesishex, 'hex');
|
var genesisbuf = new Buffer(genesishex, 'hex');
|
||||||
var genesisidhex = '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f';
|
var genesisidhex = '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f';
|
||||||
|
|
||||||
|
@ -50,20 +50,16 @@ describe('Block', function() {
|
||||||
|
|
||||||
it('should set these known values', function() {
|
it('should set these known values', function() {
|
||||||
var b = new Block({
|
var b = new Block({
|
||||||
magicnum: magicnum,
|
|
||||||
size: size,
|
|
||||||
header: bh,
|
header: bh,
|
||||||
transactions: txs
|
transactions: txs
|
||||||
});
|
});
|
||||||
should.exist(b.magicnum);
|
|
||||||
should.exist(b.size);
|
|
||||||
should.exist(b.header);
|
should.exist(b.header);
|
||||||
should.exist(b.transactions);
|
should.exist(b.transactions);
|
||||||
});
|
});
|
||||||
|
|
||||||
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'), {skipMagic: true});
|
var b = Block.fromBuffer(new Buffer(block.data, 'hex'));
|
||||||
b.transactions.length.should.equal(block.transactions);
|
b.transactions.length.should.equal(block.transactions);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -90,8 +86,6 @@ describe('Block', function() {
|
||||||
|
|
||||||
it('should set these known values', function() {
|
it('should set these known values', function() {
|
||||||
var block = Block.fromJSON(json);
|
var block = Block.fromJSON(json);
|
||||||
should.exist(block.magicnum);
|
|
||||||
should.exist(block.size);
|
|
||||||
should.exist(block.header);
|
should.exist(block.header);
|
||||||
should.exist(block.transactions);
|
should.exist(block.transactions);
|
||||||
});
|
});
|
||||||
|
@ -99,8 +93,6 @@ describe('Block', function() {
|
||||||
it('should set these known values', function() {
|
it('should set these known values', function() {
|
||||||
|
|
||||||
var block = Block(json);
|
var block = Block(json);
|
||||||
should.exist(block.magicnum);
|
|
||||||
should.exist(block.size);
|
|
||||||
should.exist(block.header);
|
should.exist(block.header);
|
||||||
should.exist(block.transactions);
|
should.exist(block.transactions);
|
||||||
});
|
});
|
||||||
|
@ -117,8 +109,6 @@ describe('Block', function() {
|
||||||
it('should recover these known values', function() {
|
it('should recover these known values', function() {
|
||||||
var block = Block.fromJSON(json);
|
var block = Block.fromJSON(json);
|
||||||
var b = JSON.parse(block.toJSON());
|
var b = JSON.parse(block.toJSON());
|
||||||
should.exist(b.magicnum);
|
|
||||||
should.exist(b.size);
|
|
||||||
should.exist(b.header);
|
should.exist(b.header);
|
||||||
should.exist(b.transactions);
|
should.exist(b.transactions);
|
||||||
});
|
});
|
||||||
|
|
File diff suppressed because one or more lines are too long
|
@ -1,6 +1,4 @@
|
||||||
{
|
{
|
||||||
"magicnum": 118034699,
|
|
||||||
"size": 8003,
|
|
||||||
"header": {
|
"header": {
|
||||||
"version": 2,
|
"version": 2,
|
||||||
"prevHash": "4baaa9507c3b27908397ea7bc177a998e9f4fe38b9d5130be7b5353c00000000",
|
"prevHash": "4baaa9507c3b27908397ea7bc177a998e9f4fe38b9d5130be7b5353c00000000",
|
||||||
|
|
Loading…
Reference in New Issue