@@ -5,10 +5,12 @@ var _util = require('./util');
5
5
var _transaction = require ( './transaction' ) ; var _transaction2 = _interopRequireDefault ( _transaction ) ; function _interopRequireDefault ( obj ) { return obj && obj . __esModule ? obj : { default : obj } ; } var
6
6
7
7
Block = function ( ) {
8
- function Block ( height ) { var txs = arguments . length > 1 && arguments [ 1 ] !== undefined ? arguments [ 1 ] : [ ] ; ( 0 , _classCallCheck3 . default ) ( this , Block ) ;
8
+ function Block ( height , options ) { ( 0 , _classCallCheck3 . default ) ( this , Block ) ; var _ref =
9
+ options || { } , timestamp = _ref . timestamp , txs = _ref . txs ;
9
10
this . height = height ;
10
- this . txList = txs ;
11
- this . txHashList = txs . map ( function ( tx ) { return tx . hash ( ) ; } ) ;
11
+ this . timestamp = timestamp ;
12
+ this . txList = txs || [ ] ;
13
+ this . txHashList = this . txList . map ( function ( tx ) { return tx . hash ( ) ; } ) ;
12
14
this . merkleTree = null ;
13
15
} ( 0 , _createClass3 . default ) ( Block , [ { key : 'addTx' , value : function addTx (
14
16
@@ -60,13 +62,14 @@ Block = function () {
60
62
* Returns raw block data size
61
63
*/ } , { key : 'getSize' , value : function getSize ( )
62
64
{
63
- // 8 bytes height + for each tx( X bytes raw tx size + 4 bytes raw tx length )
64
- return 8 + this . txList . reduce ( function ( s , tx ) { return s += tx . getSize ( ) + 4 ; } , 0 ) ;
65
+ // 8 bytes height + 4 bytes timestamp + for each tx( X bytes raw tx size + 4 bytes raw tx length )
66
+ return 12 + this . txList . reduce ( function ( s , tx ) { return s += tx . getSize ( ) + 4 ; } , 0 ) ;
65
67
} } , { key : 'toJSON' , value : function toJSON ( )
66
68
67
69
{
68
70
return {
69
71
height : this . height ,
72
+ timestamp : this . timestamp ,
70
73
txs : this . txList . map ( function ( tx ) { return tx . toJSON ( ) ; } ) } ;
71
74
72
75
} } , { key : 'toRaw' ,
@@ -79,6 +82,7 @@ Block = function () {
79
82
* Returns {Buffer} with raw serialized block bytes
80
83
*
81
84
* 8 bytes - height
85
+ * 4 bytes - timestamp
82
86
* [
83
87
* 4 bytes - tx 1 length,
84
88
* X bytes - tx 1 data,
@@ -92,7 +96,8 @@ Block = function () {
92
96
{
93
97
var payload = Buffer . alloc ( this . getSize ( ) , 0 ) ;
94
98
( 0 , _util . writeUint64 ) ( payload , this . height , 0 ) ;
95
- var offset = 8 , rawTx = void 0 ;
99
+ payload . writeUInt32BE ( this . timestamp , 8 ) ;
100
+ var offset = 12 , rawTx = void 0 ;
96
101
this . txList . forEach ( function ( tx ) {
97
102
rawTx = tx . toRaw ( ) ;
98
103
( 0 , _assert2 . default ) ( rawTx . length <= Math . pow ( 2 , 32 ) ) ;
@@ -119,6 +124,7 @@ Block = function () {
119
124
120
125
121
126
127
+
122
128
// Returns proof of block inclusion for given tx
123
129
// [
124
130
// 32b - blockHash
@@ -190,4 +196,4 @@ Block = function () {
190
196
191
197
// Add slices with proofs and return
192
198
return slices . concat ( proofs ) ;
193
- } } ] , [ { key : 'fromJSON' , value : function fromJSON ( _ref ) { var height = _ref . height , txs = _ref . txs ; return new Block ( height , txs . map ( _transaction2 . default . fromJSON ) ) ; } } , { key : 'fromRaw' , value : function fromRaw ( buf ) { var height = ( 0 , _util . readUint64 ) ( buf ) ; var block = new Block ( height ) ; var offset = 8 , txSize = void 0 ; while ( offset < buf . length ) { txSize = buf . readUInt32BE ( offset ) ; block . addTx ( _transaction2 . default . fromRaw ( buf . slice ( offset + 4 , offset + 4 + txSize ) ) ) ; offset += txSize + 4 ; } return block ; } } ] ) ; return Block ; } ( ) ; exports . default = Block ; module . exports = exports [ 'default' ] ;
199
+ } } ] , [ { key : 'fromJSON' , value : function fromJSON ( _ref2 ) { var height = _ref2 . height , timestamp = _ref2 . timestamp , txs = _ref2 . txs ; return new Block ( height , { timestamp : timestamp , txs : txs . map ( _transaction2 . default . fromJSON ) } ) ; } } , { key : 'fromRaw' , value : function fromRaw ( buf ) { var height = ( 0 , _util . readUint64 ) ( buf ) ; var timestamp = buf . readUInt32BE ( 8 ) ; var block = new Block ( height , { timestamp : timestamp } ) ; var offset = 12 , txSize = void 0 ; while ( offset < buf . length ) { txSize = buf . readUInt32BE ( offset ) ; block . addTx ( _transaction2 . default . fromRaw ( buf . slice ( offset + 4 , offset + 4 + txSize ) ) ) ; offset += txSize + 4 ; } return block ; } } ] ) ; return Block ; } ( ) ; exports . default = Block ; module . exports = exports [ 'default' ] ;
0 commit comments