From 3da0779d70193020b38961f12ac15bf094287f4a Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Tue, 27 May 2014 18:02:42 -0300 Subject: [PATCH] fix confirmed balance --- dev-util/getAddr.js | 25 +++++++++++++++++++++++ dev-util/get_outs.js | 41 -------------------------------------- dev-util/get_outs_addr.js | 39 ------------------------------------ lib/BlockDb.js | 32 ++++++++++++++--------------- test/integration/addr.json | 6 ++++++ 5 files changed, 47 insertions(+), 96 deletions(-) create mode 100755 dev-util/getAddr.js delete mode 100755 dev-util/get_outs.js delete mode 100755 dev-util/get_outs_addr.js diff --git a/dev-util/getAddr.js b/dev-util/getAddr.js new file mode 100755 index 00000000..e0f086ce --- /dev/null +++ b/dev-util/getAddr.js @@ -0,0 +1,25 @@ +#!/usr/bin/env node +'use strict'; + +var util = require('util'), + config = require('../config/config'); + +process.env.NODE_ENV = process.env.NODE_ENV || 'development'; + +var A = require('../app/models/Address'); + +// var hash = process.argv[2] || '0000000000b6288775bbd326bedf324ca8717a15191da58391535408205aada4'; +var hash = process.argv[2] || 'mp3Rzxx9s1A21SY3sjJ3CQoa2Xjph7e5eS'; + +var a= new A(hash); +a.update(function(err) { + console.log('Err:'); + console.log(err); + + console.log('Ret:'); + console.log(util.inspect(a,{depth:null})); + +}) + + + diff --git a/dev-util/get_outs.js b/dev-util/get_outs.js deleted file mode 100755 index d93be4bd..00000000 --- a/dev-util/get_outs.js +++ /dev/null @@ -1,41 +0,0 @@ -#!/usr/bin/env node -'use strict'; - -var util = require('util'); -var mongoose= require('mongoose'), - config = require('../config/config'); - -process.env.NODE_ENV = process.env.NODE_ENV || 'development'; - -var T = require('../app/models/TransactionOut'); - - -// var hash = process.argv[2] || '0000000000b6288775bbd326bedf324ca8717a15191da58391535408205aada4'; -var hash = process.argv[2] || 'e2253359458db3e732c82a43fc62f56979ff59928f25a2df34dfa443e9a41160'; - - - - -mongoose.connect(config.db); - -mongoose.connection.on('error', function(err) { console.log(err); }); - - -mongoose.connection.on('open', function() { - - var b = new Buffer(hash,'hex'); - - T.find({txidBuf: b}, function(err, ret) { - - console.log('Err:'); - console.log(err); - - - console.log('Ret:'); - console.log(util.inspect(ret,{depth:null})); - mongoose.connection.close(); - }); -}); - - - diff --git a/dev-util/get_outs_addr.js b/dev-util/get_outs_addr.js deleted file mode 100755 index b9935d56..00000000 --- a/dev-util/get_outs_addr.js +++ /dev/null @@ -1,39 +0,0 @@ -#!/usr/bin/env node -'use strict'; - -var util = require('util'); -var mongoose= require('mongoose'), - config = require('../config/config'); - -process.env.NODE_ENV = process.env.NODE_ENV || 'development'; - -var T = require('../app/models/TransactionOut'); - - -// var hash = process.argv[2] || '0000000000b6288775bbd326bedf324ca8717a15191da58391535408205aada4'; -var hash = process.argv[2] || 'mp3Rzxx9s1A21SY3sjJ3CQoa2Xjph7e5eS'; - - - - -mongoose.connect(config.db); - -mongoose.connection.on('error', function(err) { console.log(err); }); - - -mongoose.connection.on('open', function() { - - T.find({addr: hash}, function(err, ret) { - - console.log('Err:'); - console.log(err); - - - console.log('Ret:'); - console.log(util.inspect(ret,{depth:null})); - mongoose.connection.close(); - }); -}); - - - diff --git a/lib/BlockDb.js b/lib/BlockDb.js index b8e67b04..9f2d5d8f 100644 --- a/lib/BlockDb.js +++ b/lib/BlockDb.js @@ -142,16 +142,10 @@ BlockDb.prototype.setBlockNotMain = function(hash, cb) { // the block prev to the new block, nor TIP pointer // BlockDb.prototype.add = function(b, height, cb) { - d('adding block %s #d', b,height); + var txs = typeof b.tx[0] === 'string' ? b.tx : b.tx.map( function(o){ return o.txid; }); + var dbScript = this._addBlockScript(b,height); - dbScript = dbScript.concat(this._addTxsScript( - b.tx.map( - function(o){ - return o.txid; - }), - b.hash, - height - )); + dbScript = dbScript.concat(this._addTxsScript(txs, b.hash, height)); this.txDb.addMany(b.tx, function(err) { if (err) return cb(err); db.batch(dbScript,cb); @@ -328,6 +322,7 @@ BlockDb.prototype.blockIndex = function(height, cb) { BlockDb.prototype._fillConfirmationsOneSpent = function(o, chainHeight, cb) { var self = this; if (!o.spentTxId) return cb(); + if (o.multipleSpentAttempts) { async.eachLimit(o.multipleSpentAttempts, CONCURRENCY, function(oi, e_c) { @@ -336,7 +331,7 @@ BlockDb.prototype._fillConfirmationsOneSpent = function(o, chainHeight, cb) { if (height>=0) { o.spentTxId = oi.spentTxId; o.index = oi.index; - o.spentIsConfirmed = chainHeight - height >= self.safeConfirmations ? 1 : 0; + o.spentIsConfirmed = chainHeight >= height; o.spentConfirmations = chainHeight - height; } return e_c(); @@ -345,8 +340,10 @@ BlockDb.prototype._fillConfirmationsOneSpent = function(o, chainHeight, cb) { } else { self.getBlockForTx(o.spentTxId, function(err, hash, height) { if (err) return cb(err); - o.spentIsConfirmed = chainHeight - height >= self.safeConfirmations ? 1 : 0; - o.spentConfirmations = chainHeight - height; + if (height >=0 ) { + o.spentIsConfirmed = chainHeight >= height; + o.spentConfirmations = chainHeight - height; + } return cb(); }); } @@ -356,9 +353,12 @@ BlockDb.prototype._fillConfirmationsOne = function(o, chainHeight, cb) { var self = this; self.getBlockForTx(o.txid, function(err, hash, height) { if (err) return cb(err); - o.isConfirmed = chainHeight - height >= self.safeConfirmations ? 1 : 0; - o.confirmations = chainHeight - height; - return self._fillConfirmationsOneSpent(o,chainHeight,cb); + if (height>=0) { + o.isConfirmed = chainHeight >= height; + o.confirmations = chainHeight - height; + return self._fillConfirmationsOneSpent(o,chainHeight,cb); + } + else return cb(); }); }; @@ -369,6 +369,7 @@ BlockDb.prototype.fillConfirmations = function(txouts, cb) { return !x.spentIsConfirmedCached // not 100%cached && !(x.isConfirmedCached && !x.spentTxId); // and not 50%cached but not spent }); + async.eachLimit(txs, CONCURRENCY, function(txout, e_c) { if(txout.isConfirmedCached) { self._fillConfirmationsOneSpent(txout,height, e_c); @@ -394,7 +395,6 @@ BlockDb.prototype.migrateV02 = function(cb) { }; BlockDb.prototype.migrateV02cleanup = function(cb) { - var self = this; console.log('## deleting txb- from txs db'); //todo diff --git a/test/integration/addr.json b/test/integration/addr.json index df83e9ef..b646a2db 100644 --- a/test/integration/addr.json +++ b/test/integration/addr.json @@ -1,4 +1,10 @@ [ + { + "addr": "mm8CDNJnk8PtQPEtTns2ah5nmxN63ENHtY", + "balance": 1.401, + "txApperances": 2 + }, + { "addr": "mp3Rzxx9s1A21SY3sjJ3CQoa2Xjph7e5eS", "balance": 0,