adding timestamps to Transactions

This commit is contained in:
Manuel Araoz 2014-01-15 10:32:18 -03:00
parent b2425d2af6
commit 89add704c6
5 changed files with 15 additions and 10 deletions

View File

@ -33,8 +33,9 @@
"it",
"inject",
"expect",
"$"
"$",
"io",
"app"
],
"indent": false, // Specify indentation spacing
"devel": true, // Allow development statements e.g. `console.log();`.

View File

@ -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

View File

@ -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();

View File

@ -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) {

View File

@ -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);
}