Skip to content

Commit

Permalink
add timestamp to Block
Browse files Browse the repository at this point in the history
  • Loading branch information
troggy committed Jun 19, 2018
1 parent 377058c commit 5c3f5c0
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 20 deletions.
20 changes: 13 additions & 7 deletions dist/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ var _util = require('./util');
var _transaction = require('./transaction');var _transaction2 = _interopRequireDefault(_transaction);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}var

Block = function () {
function Block(height) {var txs = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];(0, _classCallCheck3.default)(this, Block);
function Block(height, options) {(0, _classCallCheck3.default)(this, Block);var _ref =
options || {},timestamp = _ref.timestamp,txs = _ref.txs;
this.height = height;
this.txList = txs;
this.txHashList = txs.map(function (tx) {return tx.hash();});
this.timestamp = timestamp;
this.txList = txs || [];
this.txHashList = this.txList.map(function (tx) {return tx.hash();});
this.merkleTree = null;
}(0, _createClass3.default)(Block, [{ key: 'addTx', value: function addTx(

Expand Down Expand Up @@ -60,13 +62,14 @@ Block = function () {
* Returns raw block data size
*/ }, { key: 'getSize', value: function getSize()
{
// 8 bytes height + for each tx( X bytes raw tx size + 4 bytes raw tx length )
return 8 + this.txList.reduce(function (s, tx) {return s += tx.getSize() + 4;}, 0);
// 8 bytes height + 4 bytes timestamp + for each tx( X bytes raw tx size + 4 bytes raw tx length )
return 12 + this.txList.reduce(function (s, tx) {return s += tx.getSize() + 4;}, 0);
} }, { key: 'toJSON', value: function toJSON()

{
return {
height: this.height,
timestamp: this.timestamp,
txs: this.txList.map(function (tx) {return tx.toJSON();}) };

} }, { key: 'toRaw',
Expand All @@ -79,6 +82,7 @@ Block = function () {
* Returns {Buffer} with raw serialized block bytes
*
* 8 bytes - height
* 4 bytes - timestamp
* [
* 4 bytes - tx 1 length,
* X bytes - tx 1 data,
Expand All @@ -92,7 +96,8 @@ Block = function () {
{
var payload = Buffer.alloc(this.getSize(), 0);
(0, _util.writeUint64)(payload, this.height, 0);
var offset = 8,rawTx = void 0;
payload.writeUInt32BE(this.timestamp, 8);
var offset = 12,rawTx = void 0;
this.txList.forEach(function (tx) {
rawTx = tx.toRaw();
(0, _assert2.default)(rawTx.length <= Math.pow(2, 32));
Expand All @@ -119,6 +124,7 @@ Block = function () {




// Returns proof of block inclusion for given tx
// [
// 32b - blockHash
Expand Down Expand Up @@ -190,4 +196,4 @@ Block = function () {

// Add slices with proofs and return
return slices.concat(proofs);
} }], [{ 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'];
} }], [{ 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'];
26 changes: 16 additions & 10 deletions lib/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import { toHexString, writeUint64, readUint64 } from './util';
import Tx from './transaction';

export default class Block {
constructor(height, txs = []) {
constructor(height, options) {
const { timestamp, txs } = options || {};
this.height = height;
this.txList = txs;
this.txHashList = txs.map(tx => tx.hash());
this.timestamp = timestamp;
this.txList = txs || [];
this.txHashList = this.txList.map(tx => tx.hash());
this.merkleTree = null;
}

Expand Down Expand Up @@ -60,25 +62,27 @@ export default class Block {
* Returns raw block data size
*/
getSize() {
// 8 bytes height + for each tx( X bytes raw tx size + 4 bytes raw tx length )
return 8 + this.txList.reduce((s, tx) => s += tx.getSize() + 4, 0);
// 8 bytes height + 4 bytes timestamp + for each tx( X bytes raw tx size + 4 bytes raw tx length )
return 12 + this.txList.reduce((s, tx) => s += tx.getSize() + 4, 0);
}

toJSON() {
return {
height: this.height,
timestamp: this.timestamp,
txs: this.txList.map(tx => tx.toJSON())
};
}

static fromJSON({ height, txs }) {
return new Block(height, txs.map(Tx.fromJSON));
static fromJSON({ height, timestamp, txs }) {
return new Block(height, { timestamp, txs: txs.map(Tx.fromJSON) });
}

/*
* Returns {Buffer} with raw serialized block bytes
*
* 8 bytes - height
* 4 bytes - timestamp
* [
* 4 bytes - tx 1 length,
* X bytes - tx 1 data,
Expand All @@ -92,7 +96,8 @@ export default class Block {
toRaw() {
const payload = Buffer.alloc(this.getSize(), 0);
writeUint64(payload, this.height, 0);
let offset = 8, rawTx;
payload.writeUInt32BE(this.timestamp, 8);
let offset = 12, rawTx;
this.txList.forEach(tx => {
rawTx = tx.toRaw();
assert(rawTx.length <= 2 ** 32);
Expand All @@ -109,8 +114,9 @@ export default class Block {
*/
static fromRaw(buf) {
const height = readUint64(buf);
const block = new Block(height);
let offset = 8, txSize;
const timestamp = buf.readUInt32BE(8);
const block = new Block(height, { timestamp });
let offset = 12, txSize;
while (offset < buf.length) {
txSize = buf.readUInt32BE(offset);
block.addTx(Tx.fromRaw(buf.slice(offset + 4, offset + 4 + txSize)));
Expand Down
7 changes: 5 additions & 2 deletions lib/block.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,15 @@ describe('blocks', () => {

transfer.sign([PRIV]);

const block = new Block(height, [transfer]);

const block = new Block(height, {
timestamp: Math.round(Date.now() / 1000),
txs: [transfer]
});
// toJSON
const json = block.toJSON();
expect(json).to.eql({
height: 123000044,
timestamp: block.timestamp,
txs: [transfer.toJSON()],
});
// fromJSON
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "parsec-lib",
"version": "0.2.1",
"version": "0.3.0",
"description": "transaction and block implementation",
"main": "dist/index.js",
"keywords": [
Expand Down

0 comments on commit 5c3f5c0

Please sign in to comment.