fix timestamp and rounding in TXs

This commit is contained in:
Matias Alejo Garcia 2014-02-05 03:34:46 -03:00
parent 71b27aede0
commit e4095cbf8e
8 changed files with 76 additions and 55 deletions

View File

@ -9,8 +9,8 @@ function spec() {
var BitcoreUtil = require('bitcore/util/util'); var BitcoreUtil = require('bitcore/util/util');
var TransactionDb = require('../../lib/TransactionDb').class(); var TransactionDb = require('../../lib/TransactionDb').class();
function Address(addrStr) { function Address(addrStr, txDb) {
this.tdb = new TransactionDb(); this.txDb = txDb || new TransactionDb();
this.balanceSat = 0; this.balanceSat = 0;
this.totalReceivedSat = 0; this.totalReceivedSat = 0;
this.totalSentSat = 0; this.totalSentSat = 0;
@ -58,35 +58,24 @@ function spec() {
Address.prototype.update = function(next) { Address.prototype.update = function(next) {
var self = this; var self = this;
async.series([ async.series([
/* function (cb) {
TransactionIn.find({addr:self.addrStr}).exec(function(err,txIn){
if (err) return cb(err);
txIn.forEach(function(txItem){
self.balanceSat += txItem.value_sat;
self.totalReceivedSat += txItem.value_sat;
});
return cb();
});
},
*/
function (cb) { function (cb) {
self.tdb.fromAddr(self.addrStr, function(err,txOut){ self.txDb.fromAddr(self.addrStr, function(err,txOut){
if (err) return cb(err); if (err) return cb(err);
txOut.forEach(function(txItem){ txOut.forEach(function(txItem){
self.totalReceivedSat += txItem.value_sat; var v = parseInt(txItem.value_sat);
self.totalReceivedSat += v;
self.transactions.push(txItem.txid); self.transactions.push(txItem.txid);
if (! txItem.spendTxId) { if (! txItem.spendTxId) {
// unspent // unspent
self.balanceSat += txItem.value_sat; self.balanceSat += v;
self.txApperances +=1; self.txApperances +=1;
} }
else { else {
// spent // spent
self.totalSentSat += txItem.value_sat; self.totalSentSat += v;
self.transactions.push(txItem.spendTxid); self.transactions.push(txItem.spendTxid);
self.txApperances +=2; self.txApperances +=2;
} }

View File

@ -2,9 +2,7 @@
'use strict'; 'use strict';
var util = require('util'); var util = require('util');
var T = require('../app/models/Transaction').class(); var T = require('../lib/TransactionDb').class();
var mongoose= require('mongoose'),
config = require('../config/config');
process.env.NODE_ENV = process.env.NODE_ENV || 'development'; process.env.NODE_ENV = process.env.NODE_ENV || 'development';
@ -14,21 +12,15 @@ var hash = process.argv[2] || 'e2253359458db3e732c82a43fc62f56979ff59928f25a2df3
var t = new T();
t.fromIdWithInfo(hash, function(err, ret) {
mongoose.connect(config.db); console.log('Err:');
mongoose.connection.on('error', function(err) { console.log('error!', err); }); console.log(err);
mongoose.connection.on('open', function() {
T.fromIdWithInfo(hash, function(err, ret) {
console.log('Err:');
console.log(err);
console.log('Ret:'); console.log('Ret:');
console.log(util.inspect(ret,{depth:null})); console.log(util.inspect(ret,{depth:null}));
mongoose.connection.close();
});
}); });

17
dev-util/sync-level.js Normal file
View File

@ -0,0 +1,17 @@
#!/usr/bin/env node
'use strict';
var Sync = require('../lib/Sync').class();
var s = new Sync();
s.setOrphan(
'0000000000c2b1e8dab92a72741289e5ef0d4f375fd1b26f729da2ba979c028a',
'000000000228f9d02654459e09998c7557afa9082784c11226853f5feb805df9',
function (err) {
console.log('[sync-level.js.15]',err); //TODO
});

19
dev-util/tx-level.js Executable file
View File

@ -0,0 +1,19 @@
#!/usr/bin/env node
var
config = require('../config/config'),
levelup = require('levelup');
db = levelup(config.leveldb + '/txs');
var s = 'txouts-addr-mgqvRGJMwR9JU5VhJ3x9uX9MTkzTsmmDgQ';
db.createReadStream({start: s, end: s+'~'})
.on('data', function (data) {
console.log('[block-level.js.11:data:]',data); //TODO
if (data==false) c++;
})
.on('end', function () {
});

View File

@ -278,7 +278,13 @@ function spec() {
var ti=0; var ti=0;
// Get TX Address // Get TX Address
b.txs.forEach(function(t) { b.txs.forEach(function(t) {
var objTx = blockInfo.tx[ti++]; var objTx = blockInfo.tx[ti++];
//add time from block
objTx.time = blockInfo.time;
var to=0; var to=0;
t.outs.forEach( function(o) { t.outs.forEach( function(o) {

View File

@ -221,7 +221,6 @@ function spec() {
}); });
}, },
function(err) { function(err) {
console.log('[TransactionDb.js.165]', ret); //TODO console.log('[TransactionDb.js.165]', ret); //TODO
return cb(err,ret); return cb(err,ret);
}); });
@ -341,7 +340,7 @@ console.log('[TransactionDb.js.165]', ret); //TODO
// } // }
var addr = o.scriptPubKey.addresses[0]; var addr = o.scriptPubKey.addresses[0];
var sat = o.value * util.COIN; var sat = Math.round(o.value * util.COIN);
self.db.batch() self.db.batch()
.put( OUTS_ROOT + tx.txid + '-' + o.n, addr + ':' + sat) .put( OUTS_ROOT + tx.txid + '-' + o.n, addr + ':' + sat)
.put( ADDR_ROOT + addr + '-' + ts + '-' + tx.txid + .put( ADDR_ROOT + addr + '-' + ts + '-' + tx.txid +

View File

@ -1,16 +1,23 @@
#!/usr/bin/env node #!/usr/bin/env node
'use strict';
process.env.NODE_ENV = process.env.NODE_ENV || 'development'; process.env.NODE_ENV = process.env.NODE_ENV || 'development';
var var assert = require('assert'),
assert = require('assert'),
fs = require('fs'), fs = require('fs'),
config = require('../../config/config'), Address = require('../../app/models/Address').class(),
Address = require('../../app/models/Address').class(); TransactionDb = require('../../lib/TransactionDb').class(),
addrValid = JSON.parse(fs.readFileSync('test/integration/addr.json')); addrValid = JSON.parse(fs.readFileSync('test/integration/addr.json'));
var txDb;
describe('Address balances', function(){ describe('Address balances', function(){
before(function(c) {
txDb = new TransactionDb();
return c();
});
addrValid.forEach( function(v) { addrValid.forEach( function(v) {
if (v.disabled) { if (v.disabled) {
console.log(v.addr + " => disabled in JSON"); console.log(v.addr + " => disabled in JSON");
@ -19,7 +26,7 @@ describe('Address balances', function(){
it('Info for: ' + v.addr, function(done) { it('Info for: ' + v.addr, function(done) {
this.timeout(5000); this.timeout(5000);
var a = new Address(v.addr); var a = new Address(v.addr, txDb);
a.update(function(err) { a.update(function(err) {
if (err) done(err); if (err) done(err);

View File

@ -1,12 +1,6 @@
[ [
{ {
"disabled":1,
"addr": "mgqvRGJMwR9JU5VhJ3x9uX9MTkzTsmmDgQ",
"balance": 43.1,
"txApperances": 19
},
{
"disabled":1,
"addr": "mp3Rzxx9s1A21SY3sjJ3CQoa2Xjph7e5eS", "addr": "mp3Rzxx9s1A21SY3sjJ3CQoa2Xjph7e5eS",
"balance": 0, "balance": 0,
"totalReceived": 50, "totalReceived": 50,
@ -14,7 +8,6 @@
"txApperances": 2 "txApperances": 2
}, },
{ {
"disabled":1,
"addr": "muyg1K5WsHkfMVCkUXU2y7Xp5ZD6RGzCeH", "addr": "muyg1K5WsHkfMVCkUXU2y7Xp5ZD6RGzCeH",
"balance": 0.38571339, "balance": 0.38571339,
"totalReceived": 0.38571339, "totalReceived": 0.38571339,
@ -29,7 +22,6 @@
"txApperances": 13 "txApperances": 13
}, },
{ {
"disabled":1,
"addr": "n47CfqnKWdNwqY1UWxTmNJAqYutFxdH3zY", "addr": "n47CfqnKWdNwqY1UWxTmNJAqYutFxdH3zY",
"balance": 0, "balance": 0,
"totalReceived":26.4245, "totalReceived":26.4245,
@ -37,7 +29,6 @@
"txApperances": 4 "txApperances": 4
}, },
{ {
"disabled":1,
"addr": "mzSyyXgofoBxpr6gYcU3cV345G8hJpixRd", "addr": "mzSyyXgofoBxpr6gYcU3cV345G8hJpixRd",
"balance": 0, "balance": 0,
"totalReceived":3.9775, "totalReceived":3.9775,
@ -45,7 +36,11 @@
"txApperances": 2 "txApperances": 2
}, },
{ {
"disabled":1, "addr": "mgqvRGJMwR9JU5VhJ3x9uX9MTkzTsmmDgQ",
"balance": 43.1,
"txApperances": 19
},
{
"addr": "mzW2hdZN2um7WBvTDerdahKqRgj3md9C29", "addr": "mzW2hdZN2um7WBvTDerdahKqRgj3md9C29",
"txApperances": 2034, "txApperances": 2034,
"balance": 1049.69744099, "balance": 1049.69744099,
@ -53,7 +48,6 @@
"totalSent": 0 "totalSent": 0
}, },
{ {
"disabled":1,
"addr": "mjRmkmYzvZN3cA3aBKJgYJ65epn3WCG84H", "addr": "mjRmkmYzvZN3cA3aBKJgYJ65epn3WCG84H",
"txApperances": 13343, "txApperances": 13343,
"balance": 46413.0, "balance": 46413.0,
@ -61,7 +55,6 @@
"totalSent": 310717.17644359 "totalSent": 310717.17644359
}, },
{ {
"disabled":1,
"addr": "mgKY35SXqxFpcKK3Dq9mW9919N7wYXvcFM", "addr": "mgKY35SXqxFpcKK3Dq9mW9919N7wYXvcFM",
"txApperances": 1, "txApperances": 1,
"balance": 0.01979459, "balance": 0.01979459,
@ -70,7 +63,6 @@
"transactions": [ "91800d80bb4c69b238c9bfd94eb5155ab821e6b25cae5c79903d12853bbb4ed5" ] "transactions": [ "91800d80bb4c69b238c9bfd94eb5155ab821e6b25cae5c79903d12853bbb4ed5" ]
}, },
{ {
"disabled":1,
"addr": "mmvP3mTe53qxHdPqXEvdu8WdC7GfQ2vmx5", "addr": "mmvP3mTe53qxHdPqXEvdu8WdC7GfQ2vmx5",
"balance": 10580.50027254, "balance": 10580.50027254,
"totalReceived": 12157.65075053, "totalReceived": 12157.65075053,