From 89add704c614c2acafe7cf617e1367d9450f1e17 Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Wed, 15 Jan 2014 10:32:18 -0300 Subject: [PATCH] adding timestamps to Transactions --- .jshintrc | 5 +++-- app/models/Block.js | 6 ++++++ app/models/Transaction.js | 6 ++++-- lib/PeerSync.js | 6 +----- lib/Sync.js | 2 +- 5 files changed, 15 insertions(+), 10 deletions(-) diff --git a/.jshintrc b/.jshintrc index 9e1a154e..b3549407 100644 --- a/.jshintrc +++ b/.jshintrc @@ -33,8 +33,9 @@ "it", "inject", "expect", - "$" - + "$", + "io", + "app" ], "indent": false, // Specify indentation spacing "devel": true, // Allow development statements e.g. `console.log();`. diff --git a/app/models/Block.js b/app/models/Block.js index 46af5907..4ef0ed91 100644 --- a/app/models/Block.js +++ b/app/models/Block.js @@ -43,6 +43,12 @@ BlockSchema.path('title').validate(function(title) { * Statics */ +BlockSchema.statics.createTimestamped = function(block, cb) { + var now = Math.round(new Date().getTime() / 1000); + block.time = now; + this.create(block, cb); +}; + BlockSchema.statics.load = function(id, cb) { this.findOne({ _id: id diff --git a/app/models/Transaction.js b/app/models/Transaction.js index 0e71ec7c..219e79a7 100644 --- a/app/models/Transaction.js +++ b/app/models/Transaction.js @@ -39,6 +39,7 @@ var TransactionSchema = new Schema({ type: Boolean, default: false, }, + time: Number, }); /** @@ -93,9 +94,10 @@ TransactionSchema.statics.createFromArray = function(txs, next) { var that = this; if (!txs) return next(); var mongo_txs = []; - async.forEach( txs, + async.forEach(txs, function(tx, cb) { - that.create({ txid: tx }, function(err, new_tx) { + var now = Math.round(new Date().getTime() / 1000); + that.create({ txid: tx, time: now }, function(err, new_tx) { if (err) { if (err.toString().match(/E11000/)) { return cb(); diff --git a/lib/PeerSync.js b/lib/PeerSync.js index 14d3acdd..b0f0cc39 100644 --- a/lib/PeerSync.js +++ b/lib/PeerSync.js @@ -46,13 +46,11 @@ function spec() { }; PeerSync.prototype.handle_inv = function(info) { - // TODO: should limit the invs to objects we haven't seen yet var invs = info.message.invs; invs.forEach(function(inv) { console.log('[p2p_sync] Handle inv for a ' + CoinConst.MSG.to_str(inv.type)); }); - // this is not needed right now, but it's left in case - // we need to store more info in the future + // TODO: should limit the invs to objects we haven't seen yet info.conn.sendGetData(invs); }; @@ -69,12 +67,10 @@ function spec() { PeerSync.prototype.handle_block = function(info) { var self = this; var block = info.message.block; - var now = Math.round(new Date().getTime() / 1000); var blockHash = coinUtil.formatHashFull(block.calcHash()); console.log('[p2p_sync] Handle block: ' + blockHash); this.sync.storeBlock({ 'hash': blockHash, - 'time': now, 'fromP2P': true, }, function(err) { diff --git a/lib/Sync.js b/lib/Sync.js index 588e8d6e..c09804ee 100644 --- a/lib/Sync.js +++ b/lib/Sync.js @@ -59,7 +59,7 @@ function spec() { Sync.prototype.storeBlock = function(block, cb) { var that = this; - Block.create(block, function(err, b){ + Block.createTimestamped(block, function(err, b){ if (b && that.opts.broadcast_blocks) { sockets.broadcast_block(b); }