Merge pull request #49 from maraoz/feature/tx-timestamps
adding timestamps to Transactions
This commit is contained in:
commit
cbee62ccae
|
@ -33,8 +33,9 @@
|
||||||
"it",
|
"it",
|
||||||
"inject",
|
"inject",
|
||||||
"expect",
|
"expect",
|
||||||
"$"
|
"$",
|
||||||
|
"io",
|
||||||
|
"app"
|
||||||
],
|
],
|
||||||
"indent": false, // Specify indentation spacing
|
"indent": false, // Specify indentation spacing
|
||||||
"devel": true, // Allow development statements e.g. `console.log();`.
|
"devel": true, // Allow development statements e.g. `console.log();`.
|
||||||
|
|
|
@ -42,6 +42,12 @@ BlockSchema.path('title').validate(function(title) {
|
||||||
* Statics
|
* 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) {
|
BlockSchema.statics.load = function(id, cb) {
|
||||||
this.findOne({
|
this.findOne({
|
||||||
_id: id
|
_id: id
|
||||||
|
|
|
@ -39,6 +39,7 @@ var TransactionSchema = new Schema({
|
||||||
type: Boolean,
|
type: Boolean,
|
||||||
default: false,
|
default: false,
|
||||||
},
|
},
|
||||||
|
time: Number,
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -95,7 +96,8 @@ TransactionSchema.statics.createFromArray = function(txs, next) {
|
||||||
var mongo_txs = [];
|
var mongo_txs = [];
|
||||||
async.forEach(txs,
|
async.forEach(txs,
|
||||||
function(tx, cb) {
|
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) {
|
||||||
if (err.toString().match(/E11000/)) {
|
if (err.toString().match(/E11000/)) {
|
||||||
return cb();
|
return cb();
|
||||||
|
|
|
@ -46,13 +46,11 @@ function spec() {
|
||||||
};
|
};
|
||||||
|
|
||||||
PeerSync.prototype.handle_inv = function(info) {
|
PeerSync.prototype.handle_inv = function(info) {
|
||||||
// TODO: should limit the invs to objects we haven't seen yet
|
|
||||||
var invs = info.message.invs;
|
var invs = info.message.invs;
|
||||||
invs.forEach(function(inv) {
|
invs.forEach(function(inv) {
|
||||||
console.log('[p2p_sync] Handle inv for a ' + CoinConst.MSG.to_str(inv.type));
|
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
|
// TODO: should limit the invs to objects we haven't seen yet
|
||||||
// we need to store more info in the future
|
|
||||||
info.conn.sendGetData(invs);
|
info.conn.sendGetData(invs);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -69,12 +67,10 @@ function spec() {
|
||||||
PeerSync.prototype.handle_block = function(info) {
|
PeerSync.prototype.handle_block = function(info) {
|
||||||
var self = this;
|
var self = this;
|
||||||
var block = info.message.block;
|
var block = info.message.block;
|
||||||
var now = Math.round(new Date().getTime() / 1000);
|
|
||||||
var blockHash = coinUtil.formatHashFull(block.calcHash());
|
var blockHash = coinUtil.formatHashFull(block.calcHash());
|
||||||
console.log('[p2p_sync] Handle block: ' + blockHash);
|
console.log('[p2p_sync] Handle block: ' + blockHash);
|
||||||
this.sync.storeBlock({
|
this.sync.storeBlock({
|
||||||
'hash': blockHash,
|
'hash': blockHash,
|
||||||
'time': now,
|
|
||||||
'fromP2P': true,
|
'fromP2P': true,
|
||||||
},
|
},
|
||||||
function(err) {
|
function(err) {
|
||||||
|
|
|
@ -59,7 +59,7 @@ function spec() {
|
||||||
|
|
||||||
Sync.prototype.storeBlock = function(block, cb) {
|
Sync.prototype.storeBlock = function(block, cb) {
|
||||||
var that = this;
|
var that = this;
|
||||||
Block.create(block, function(err, b){
|
Block.createTimestamped(block, function(err, b){
|
||||||
if (b && that.opts.broadcast_blocks) {
|
if (b && that.opts.broadcast_blocks) {
|
||||||
sockets.broadcast_block(b);
|
sockets.broadcast_block(b);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue