Skip to content

Commit 5c3f5c0

Browse files
committed
add timestamp to Block
1 parent 377058c commit 5c3f5c0

File tree

4 files changed

+35
-20
lines changed

4 files changed

+35
-20
lines changed

dist/block.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ var _util = require('./util');
55
var _transaction = require('./transaction');var _transaction2 = _interopRequireDefault(_transaction);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}var
66

77
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;
910
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();});
1214
this.merkleTree = null;
1315
}(0, _createClass3.default)(Block, [{ key: 'addTx', value: function addTx(
1416

@@ -60,13 +62,14 @@ Block = function () {
6062
* Returns raw block data size
6163
*/ }, { key: 'getSize', value: function getSize()
6264
{
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);
6567
} }, { key: 'toJSON', value: function toJSON()
6668

6769
{
6870
return {
6971
height: this.height,
72+
timestamp: this.timestamp,
7073
txs: this.txList.map(function (tx) {return tx.toJSON();}) };
7174

7275
} }, { key: 'toRaw',
@@ -79,6 +82,7 @@ Block = function () {
7982
* Returns {Buffer} with raw serialized block bytes
8083
*
8184
* 8 bytes - height
85+
* 4 bytes - timestamp
8286
* [
8387
* 4 bytes - tx 1 length,
8488
* X bytes - tx 1 data,
@@ -92,7 +96,8 @@ Block = function () {
9296
{
9397
var payload = Buffer.alloc(this.getSize(), 0);
9498
(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;
96101
this.txList.forEach(function (tx) {
97102
rawTx = tx.toRaw();
98103
(0, _assert2.default)(rawTx.length <= Math.pow(2, 32));
@@ -119,6 +124,7 @@ Block = function () {
119124

120125

121126

127+
122128
// Returns proof of block inclusion for given tx
123129
// [
124130
// 32b - blockHash
@@ -190,4 +196,4 @@ Block = function () {
190196

191197
// Add slices with proofs and return
192198
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'];

lib/block.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ import { toHexString, writeUint64, readUint64 } from './util';
55
import Tx from './transaction';
66

77
export default class Block {
8-
constructor(height, txs = []) {
8+
constructor(height, options) {
9+
const { timestamp, txs } = options || {};
910
this.height = height;
10-
this.txList = txs;
11-
this.txHashList = txs.map(tx => tx.hash());
11+
this.timestamp = timestamp;
12+
this.txList = txs || [];
13+
this.txHashList = this.txList.map(tx => tx.hash());
1214
this.merkleTree = null;
1315
}
1416

@@ -60,25 +62,27 @@ export default class Block {
6062
* Returns raw block data size
6163
*/
6264
getSize() {
63-
// 8 bytes height + for each tx( X bytes raw tx size + 4 bytes raw tx length )
64-
return 8 + this.txList.reduce((s, tx) => 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((s, tx) => s += tx.getSize() + 4, 0);
6567
}
6668

6769
toJSON() {
6870
return {
6971
height: this.height,
72+
timestamp: this.timestamp,
7073
txs: this.txList.map(tx => tx.toJSON())
7174
};
7275
}
7376

74-
static fromJSON({ height, txs }) {
75-
return new Block(height, txs.map(Tx.fromJSON));
77+
static fromJSON({ height, timestamp, txs }) {
78+
return new Block(height, { timestamp, txs: txs.map(Tx.fromJSON) });
7679
}
7780

7881
/*
7982
* Returns {Buffer} with raw serialized block bytes
8083
*
8184
* 8 bytes - height
85+
* 4 bytes - timestamp
8286
* [
8387
* 4 bytes - tx 1 length,
8488
* X bytes - tx 1 data,
@@ -92,7 +96,8 @@ export default class Block {
9296
toRaw() {
9397
const payload = Buffer.alloc(this.getSize(), 0);
9498
writeUint64(payload, this.height, 0);
95-
let offset = 8, rawTx;
99+
payload.writeUInt32BE(this.timestamp, 8);
100+
let offset = 12, rawTx;
96101
this.txList.forEach(tx => {
97102
rawTx = tx.toRaw();
98103
assert(rawTx.length <= 2 ** 32);
@@ -109,8 +114,9 @@ export default class Block {
109114
*/
110115
static fromRaw(buf) {
111116
const height = readUint64(buf);
112-
const block = new Block(height);
113-
let offset = 8, txSize;
117+
const timestamp = buf.readUInt32BE(8);
118+
const block = new Block(height, { timestamp });
119+
let offset = 12, txSize;
114120
while (offset < buf.length) {
115121
txSize = buf.readUInt32BE(offset);
116122
block.addTx(Tx.fromRaw(buf.slice(offset + 4, offset + 4 + txSize)));

lib/block.spec.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,15 @@ describe('blocks', () => {
137137

138138
transfer.sign([PRIV]);
139139

140-
const block = new Block(height, [transfer]);
141-
140+
const block = new Block(height, {
141+
timestamp: Math.round(Date.now() / 1000),
142+
txs: [transfer]
143+
});
142144
// toJSON
143145
const json = block.toJSON();
144146
expect(json).to.eql({
145147
height: 123000044,
148+
timestamp: block.timestamp,
146149
txs: [transfer.toJSON()],
147150
});
148151
// fromJSON

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "parsec-lib",
3-
"version": "0.2.1",
3+
"version": "0.3.0",
44
"description": "transaction and block implementation",
55
"main": "dist/index.js",
66
"keywords": [

0 commit comments

Comments
 (0)