From 09e3e1d32f03dac7e47685c1a46071dd10d0ac30 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Wed, 12 Feb 2014 23:56:52 -0300 Subject: [PATCH] new levelDB roots. test updated --- app/controllers/blocks.js | 14 ++++------- app/models/Address.js | 13 ++++++---- lib/BlockDb.js | 18 +++++++------- lib/TransactionDb.js | 45 +++++++++++++++++------------------ public/views/address.html | 25 ++++++++++++++----- test/integration/addr.js | 2 ++ test/integration/addr.json | 4 ++-- test/integration/txitems.json | 8 +++---- util/sync.js | 2 +- 9 files changed, 71 insertions(+), 60 deletions(-) diff --git a/app/controllers/blocks.js b/app/controllers/blocks.js index 04a3746..09e90fe 100644 --- a/app/controllers/blocks.js +++ b/app/controllers/blocks.js @@ -107,22 +107,18 @@ exports.list = function(req, res) { res.status(500).send(err); } else { - var blockshashList = []; var limit = parseInt(req.query.limit || blocks.length); if (blocks.length < limit) { limit = blocks.length; } - for(var i=0;i => - var PREV_PREFIX = 'b-prev-'; // b-prev- => - var NEXT_PREFIX = 'b-next-'; // b-next- => - var MAIN_PREFIX = 'b-main-'; // b-main- => 1/0 - var TIP = 'b-tip-'; // last block on the chain + var TIMESTAMP_PREFIX = 'bts-'; // b-ts- => + var PREV_PREFIX = 'bpr-'; // b-prev- => + var NEXT_PREFIX = 'bne-'; // b-next- => + var MAIN_PREFIX = 'bma-'; // b-main- => 1/0 + var TIP = 'bti-'; // last block on the chain /** @@ -125,14 +125,11 @@ function spec(b) { BlockDb.prototype.has = function(hash, cb) { var k = PREV_PREFIX + hash; db.get(k, function (err,val) { - var ret; + var ret = true; if (err && err.notFound) { err = null; ret = false; } - if (typeof val !== 'undefined') { - ret = true; - } return cb(err, ret); }); }; @@ -170,8 +167,9 @@ function spec(b) { fillCache: true }) .on('data', function (data) { + var k = data.key.split('-'); list.push({ - ts: data.key.replace(TIMESTAMP_PREFIX, ''), + ts: k[1], hash: data.value, }); }) diff --git a/lib/TransactionDb.js b/lib/TransactionDb.js index 50f55a4..607d10c 100644 --- a/lib/TransactionDb.js +++ b/lib/TransactionDb.js @@ -6,17 +6,17 @@ require('classtool'); function spec(b) { // blockHash -> txid mapping - var IN_BLK_PREFIX = 'tx-b-'; //tx-b-- => 1/0 (connected or not) + var IN_BLK_PREFIX = 'txb-'; //txb-- => 1/0 (connected or not) // Only for orphan blocks var FROM_BLK_PREFIX = 'tx-'; //tx-- => 1 // to show tx outs - var OUTS_PREFIX = 'txouts-'; //txouts-- => [addr, btc_sat] + var OUTS_PREFIX = 'txo-'; //txo-- => [addr, btc_sat] + var SPEND_PREFIX = 'txs-'; //txs---- = ts - // to sum up addr balance - var ADDR_PREFIX = 'txouts-addr-'; //txouts-addr---- => + btc_sat - var SPEND_PREFIX = 'txouts-spend-';//txouts-spend---- = ts + // to sum up addr balance (only outs, spends are gotten later) + var ADDR_PREFIX = 'txa-'; //txa--- => + btc_sat:ts // TODO: use bitcore networks module var genesisTXID = '4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b'; @@ -119,12 +119,12 @@ function spec(b) { db.createReadStream({start: k, end: k + '~'}) .on('data', function (data) { var k = data.key.split('-'); - var j = idx[parseInt(k[3])]; + var j = idx[parseInt(k[2])]; assert(typeof j !== 'undefined','Spent could not be stored: tx ' + txid + - 'spend in TX:' + k[2] + ',' + k[3]+ ' j:' + j); + 'spend in TX:' + k[1] + ',' + k[2]+ ' j:' + j); - self._addSpendInfo(ret[j], k[4], k[5], data.value); + self._addSpendInfo(ret[j], k[3], k[4], data.value); }) .on('error', function (err) { return cb(err); @@ -145,7 +145,7 @@ function spec(b) { db.createReadStream({start: k, end: k + '~'}) .on('data', function (data) { var k = data.key.split('-'); - self._addSpendInfo(info.vout[k[3]], k[4], k[5], data.value); + self._addSpendInfo(info.vout[k[2]], k[3], k[4], data.value); }) .on('error', function (err) { return cb(err); @@ -163,7 +163,6 @@ function spec(b) { var valueIn = 0; var incompleteInputs = 0; - var ts; async.eachLimit(info.vin, CONCURRENCY, function(i, c_in) { self.fromTxIdN(i.txid, i.vout, function(err, ret) { @@ -175,6 +174,7 @@ function spec(b) { return c_in(); // error not scalated } + info.firstSeenTs = ret.spendTs; i.unconfirmedInput = i.unconfirmedInput; i.addr = ret.addr; i.valueSat = ret.valueSat; @@ -205,8 +205,6 @@ function spec(b) { i.doubleSpendTxID = null; } - info.firstSeenTs = i.spendTs; - valueIn += i.valueSat; return c_in(); }); @@ -268,7 +266,7 @@ function spec(b) { db.createReadStream({start: k, end: k + '~'}) .on('data', function (data) { var k = data.key.split('-'); - self._addSpendInfo(ret, k[4], k[5], data.value); + self._addSpendInfo(ret, k[3], k[4], data.value); }) .on('error', function (error) { return cb(error); @@ -324,10 +322,10 @@ function spec(b) { var k = data.key.split('-'); var v = data.value.split(':'); ret.push({ + txid: k[2], + index: parseInt(k[3]), value_sat: parseInt(v[0]), - ts: parseInt(k[3]), - txid: k[4], - index: parseInt(k[5]), + ts: parseInt(v[1]), }); }) .on('error', function (err) { @@ -340,7 +338,7 @@ function spec(b) { db.createReadStream({start: k, end: k + '~'}) .on('data', function (data) { var k = data.key.split('-'); - self._addSpendInfo(o, k[4], k[5], data.value); + self._addSpendInfo(o, k[3], k[4], data.value); }) .on('error', function (err) { return e_c(err); @@ -442,7 +440,8 @@ function spec(b) { async.forEachLimit(tx.vin, CONCURRENCY, function(i, next_out) { db.batch() - .put( SPEND_PREFIX + i.txid + '-' + i.vout + '-' + tx.txid + '-' + i.n, ts || 0) + .put( SPEND_PREFIX + i.txid + '-' + i.vout + '-' + tx.txid + '-' + i.n, + ts || 0) .write(next_out); }, function (err) { @@ -467,12 +466,12 @@ function spec(b) { var sat = Math.round(o.value * util.COIN); // existed? - db.get(OUTS_PREFIX + tx.txid + '-' + o.n, function (err,val) { - if (!val) { + var k = OUTS_PREFIX + tx.txid + '-' + o.n; + db.get(k, function(err) { + if (err && err.notFound) { db.batch() - .put( OUTS_PREFIX + tx.txid + '-' + o.n, addr + ':' + sat) - .put( ADDR_PREFIX + addr + '-' + ts + '-' + tx.txid + - '-' + o.n, sat) + .put( k, addr + ':' + sat) + .put( ADDR_PREFIX + addr + '-' + tx.txid + '-' + o.n, sat+':'+ts) .write(next_out); } else { diff --git a/public/views/address.html b/public/views/address.html index 0771f47..d2f050a 100644 --- a/public/views/address.html +++ b/public/views/address.html @@ -12,6 +12,7 @@

Summary

+
Confirmed
@@ -26,17 +27,29 @@ - - - - - - +
Final Balance {{$root.currency.getConvertion(address.balance)}}
Unconfirmed Tx Balance{{$root.currency.getConvertion(address.unconfirmedBalance)}}
No. Transactions {{address.txApperances}}
+
+
Unconfirmed
+ + + + + + + + + + + + +
Unconfirmed Txs Balance{{$root.currency.getConvertion(address.unconfirmedBalance)}}
No. Transactions{{address.unconfirmedTxApperances}}
+
+
diff --git a/test/integration/addr.js b/test/integration/addr.js index 944a60e..89db2b4 100644 --- a/test/integration/addr.js +++ b/test/integration/addr.js @@ -30,6 +30,8 @@ describe('Address balances', function() { a.update(function(err) { if (err) done(err); assert.equal(v.addr, a.addrStr); + assert.equal(a.unconfirmedTxApperances ,0, 'unconfirmedTxApperances: 0'); + assert.equal(a.unconfirmedBalanceSat ,0, 'unconfirmedBalanceSat: 0'); if (v.txApperances) assert.equal(v.txApperances, a.txApperances, 'txApperances: ' + a.txApperances); if (v.totalReceived) assert.equal(v.totalReceived, a.totalReceived, 'received: ' + a.totalReceived); diff --git a/test/integration/addr.json b/test/integration/addr.json index 58dee9f..b985085 100644 --- a/test/integration/addr.json +++ b/test/integration/addr.json @@ -43,10 +43,10 @@ }, { "addr": "mzW2hdZN2um7WBvTDerdahKqRgj3md9C29", - "txApperances": 6049, "balance": 1199.74393853, "totalReceived": 1199.74393853, - "totalSent": 0 + "totalSent": 0, + "txApperances": 5763 }, { "addr": "mjRmkmYzvZN3cA3aBKJgYJ65epn3WCG84H", diff --git a/test/integration/txitems.json b/test/integration/txitems.json index bee3b43..283e873 100644 --- a/test/integration/txitems.json +++ b/test/integration/txitems.json @@ -6,8 +6,8 @@ { "txid": "21798ddc9664ac0ef618f52b151dda82dafaf2e26d2bbef6cdaf55a6957ca237", "toRm": [ - "txouts-spend-86a03cac7d87f596008c6d5a8d3fd8b88842932ea6f0337673eda16f6b472f7f-0", - "txouts-spend-bcd8da8ee847da377f8aaca92502c05e5f914c6a2452753146013b0e642a25a0-0" + "txs-86a03cac7d87f596008c6d5a8d3fd8b88842932ea6f0337673eda16f6b472f7f-0", + "txs-bcd8da8ee847da377f8aaca92502c05e5f914c6a2452753146013b0e642a25a0-0" ], "items": [ { @@ -25,7 +25,7 @@ { "txid": "b633a6249d4a2bc123e7f8a151cae2d4afd17aa94840009f8697270c7818ceee", "toRm": [ - "txouts-spend-01621403689cb4a95699a3dbae029d7031c5667678ef14e2054793954fb27917-0" + "txs-01621403689cb4a95699a3dbae029d7031c5667678ef14e2054793954fb27917-0" ], "items": [ { @@ -43,7 +43,7 @@ { "txid": "ca2f42e44455b8a84434de139efea1fe2c7d71414a8939e0a20f518849085c3b", "toRm": [ - "txouts-spend-2d7b680fb06e4d7eeb65ca49ac7522276586e0090b7fe662fc708129429c5e6a-0" + "txs-2d7b680fb06e4d7eeb65ca49ac7522276586e0090b7fe662fc708129429c5e6a-0" ], "items": [ { diff --git a/util/sync.js b/util/sync.js index 02da660..27aed0d 100755 --- a/util/sync.js +++ b/util/sync.js @@ -15,7 +15,7 @@ program .option('-N --network [livenet]', 'Set bitcoin network [testnet]', 'testnet') .option('-D --destroy', 'Remove current DB (and start from there)', 0) .option('-R --reverse', 'Sync backwards', 0) - .option('-U --uptoexisting', 'Sync only until an existing block is found', 0) + .option('-U --uptoexisting', 'Sync only until old Tip block is found', 0) .option('-F --fromfiles', 'Sync using bitcoind .dat block files (faster)', 0) .option('-S --smart', 'genesis stored? uptoexisting = 1, fromFiles=1 [default]', true) .option('-v --verbose', 'Verbose 0/1', 0)