fix standalone p2p

This commit is contained in:
Matias Alejo Garcia 2014-02-06 10:20:49 -03:00
parent 9edf1afcb9
commit 94edb188d0
10 changed files with 121 additions and 39 deletions

View File

@ -118,9 +118,10 @@ exports.list = function(req, res) {
length: allblocks.length,
pagination: {
next: next,
prev: prev,
current: dateStr,
isToday: isToday
prev: prev,
currentTs: lte-1,
current: dateStr,
isToday: isToday
}
});
});

View File

@ -62,21 +62,23 @@ function spec() {
db.fromAddr(self.addrStr, function(err,txOut){
if (err) return cb(err);
txOut.forEach(function(txItem){
var v = txItem.value_sat;
self.totalReceivedSat += v;
self.transactions.push(txItem.txid);
if (! txItem.spendTxId) {
// unspent
self.balanceSat += v;
self.txApperances +=1;
}
else {
// spent
self.totalSentSat += v;
self.transactions.push(txItem.spendTxId);
self.txApperances +=2;
}
if (txItem.isConfirmed) {
var v = txItem.value_sat;
self.totalReceivedSat += v;
self.transactions.push(txItem.txid);
if (! txItem.spendTxId || !txItem.spendIsConfirmed) {
// unspent
self.balanceSat += v;
self.txApperances +=1;
}
else {
// spent
self.totalSentSat += v;
self.transactions.push(txItem.spendTxId);
self.txApperances +=2;
}
};
});
return cb();
});

View File

@ -7,8 +7,8 @@ txindex=1
#rpcallowip=192.168.1.*
#rpcallowip='192.168.1.*'
#rpcallowip=127.0.0.1
#rpcallowip=*
rpcallowip=*
port=18333
rpcport=18332
testnet=3

View File

@ -85,7 +85,6 @@ function spec() {
this.sync.storeBlock({
'hash': blockHash,
'tx': tx_hashes,
// TODO NEXT BLOCK / PREV BLOCK?
},
function(err) {
if (err) {

View File

@ -125,8 +125,7 @@ console.log('[Sync.js.109] WARN: Reach reog depth limit'); //TODO
Sync.prototype.storeTxs = function(txs, cb) {
var self = this;
// TODO
self.txDb.createFromTxs(txs, function(err, inserted_txs, updated_addrs) {
self.txDb.createFromArray(txs, null, function(err, inserted_txs, updated_addrs) {
if (err) return cb(err);
self._handleBroadcast(null, inserted_txs, updated_addrs);

View File

@ -185,12 +185,30 @@ function spec(b) {
});
};
TransactionDb.prototype.fillConfirmations = function(o, cb) {
var self = this;
self.isConfirmed(o.txid, function(err,is) {
if (err) return cb(err);
o.isConfirmed = is;
if (!o.spendTxId) return cb();
self.isConfirmed(o.spendTxId, function(err,is) {
if (err) return cb(err);
o.spendIsConfirmed = is;
return cb();
});
});
};
TransactionDb.prototype.fromAddr = function(addr, cb) {
var self = this;
var k = ADDR_ROOT + addr;
var ret=[];
//
db.createReadStream({start: k, end: k + '~'})
.on('data', function (data) {
var k = data.key.split('-');
@ -221,7 +239,11 @@ function spec(b) {
});
},
function(err) {
return cb(err,ret);
async.each(ret, function(o, e_c){
self.fillConfirmations(o,e_c);
},function(err) {
return cb(err,ret);
});
});
});
};
@ -368,6 +390,45 @@ function spec(b) {
});
};
TransactionDb.prototype.deleteConfirmation = function(txId, blockHash, c) {
if (!blockHash) return c();
db.put(ROOT + txId + '-' + blockHash, 0, function(err) {
return c(err);
});
};
TransactionDb.prototype.addConfirmation = function(txId, blockHash, c) {
if (!blockHash) return c();
db.put(ROOT + txId + '-' + blockHash, 1, function(err) {
return c(err);
});
};
// This slowdown addr balance calculation 100%
TransactionDb.prototype.isConfirmed = function(txId, c) {
var k = ROOT + txId;
var ret = false;
db.createReadStream({start: k, end: k + '~'})
.on('data', function (data) {
if (data.value === '1') ret = true;
})
.on('error', function (err) {
return c(err);
})
.on('end', function (err) {
return c(err,ret);
});
};
// txs can be a [hashes] or [txObjects]
TransactionDb.prototype.createFromArray = function(txs, blockHash, next) {
var self = this;
@ -388,21 +449,23 @@ function spec(b) {
if (!inInfo) return each_cb(err);
self.add(inInfo, function(err) {
if (err || !blockHash) return each_cb(err);
if (err) return each_cb(err);
db.put(ROOT + t + '-' + blockHash, 1, function(err) {
return each_cb(err);
});
// This could mean that the TX was mined since we first received.
if (blockHash && inInfo.blockhash !== blockHash)
console.log('WARN in tx %s: different blockHashses: %s vs %s',
t, blockHash, inInfo.blockhash);
insertedTxs.push(t);
return self.addConfirmation(t,inInfo.blockhash, each_cb);
});
});
}
else {
self.add(t, function(err) {
if (err) return each_cb(err);
if (err || !blockHash) return each_cb(err);
db.put(ROOT + t.txid + '-' + blockHash, 1, function(err) {
return each_cb(err);
});
return self.addConfirmation(t,blockHash, each_cb);
});
}
},
@ -412,7 +475,6 @@ function spec(b) {
};
// txs can be a [hashes] or [txObjects]
TransactionDb.prototype.createFromBlock = function(b, next) {
var self = this;
if (!b || !b.tx) return next();

View File

@ -16,6 +16,14 @@ angular.module('insight.blocks').controller('BlocksController',
});
}
$scope.humanSince = function(time) {
var m = moment.unix(time).startOf('day');
var b = moment().startOf('day');
return m.max().from(b);
};
$scope.list = function() {
$scope.loading = true;

View File

@ -8,10 +8,15 @@
<h3>Blocks <br> mined on:</h3>
</div>
</div>
<p class="lead text-center m20v">{{pagination.current}}</p>
<p class="lead text-center m20v">{{pagination.current}} UTC</p>
<p class="lead text-center m20v" data-ng-show="loading">&nbsp;</p>
<p class="text-center m20v" data-ng-show="pagination.isToday && !loading">Today</p>
<p class="text-center m20v" data-ng-show="!pagination.isToday && !loading">{{humanSince(pagination.currentTs)}}
<p class="text-center m20v" data-ng-show="loading">&nbsp;</p>
<div class="m50v text-center">
<a class="btn btn-primary" href="/blocks-date/{{pagination.prev}}"><small>&larr; {{pagination.prev}}</small></a>
<a class="btn btn-primary" href="/blocks-date/{{pagination.next}}" data-ng-disabled="pagination.isToday"><small>{{pagination.next}} &rarr;</small></a>
<a class="btn btn-primary" href="/blocks-date/{{pagination.next}}" data-ng-show="!pagination.isToday"><small>{{pagination.next}} &rarr;</small></a>
</div>
</div>
<div class="col-xs-12 col-md-9 col-md-offset-3">

View File

@ -37,8 +37,9 @@
},
{
"addr": "mgqvRGJMwR9JU5VhJ3x9uX9MTkzTsmmDgQ",
"txApperances": 27,
"balance": 5.1
"txApperances": 28,
"balance": 0,
"totalReceived": 54.81284116
},
{
"addr": "mzW2hdZN2um7WBvTDerdahKqRgj3md9C29",

View File

@ -14,7 +14,12 @@ program
.parse(process.argv);
var ps = new PeerSync();
ps.init(program);
ps.run();
ps.init(program, function(err){
if (err) {
console.log(err);
process.exit(1);
}
ps.run();
});