rpc on classtool

This commit is contained in:
Matias Alejo Garcia 2014-02-05 15:08:39 -03:00
parent a66b7e54ce
commit 2f98fe35b3
6 changed files with 27 additions and 33 deletions

View File

@ -61,9 +61,8 @@ function spec() {
function (cb) { function (cb) {
db.fromAddr(self.addrStr, function(err,txOut){ db.fromAddr(self.addrStr, function(err,txOut){
if (err) return cb(err); if (err) return cb(err);
txOut.forEach(function(txItem){ txOut.forEach(function(txItem){
var v = parseInt(txItem.value_sat); var v = txItem.value_sat;
self.totalReceivedSat += v; self.totalReceivedSat += v;
self.transactions.push(txItem.txid); self.transactions.push(txItem.txid);

View File

@ -17,7 +17,8 @@ function spec(b) {
levelup = require('levelup'), levelup = require('levelup'),
BitcoreBlock= require('bitcore/Block').class(), BitcoreBlock= require('bitcore/Block').class(),
config = require('../config/config'); config = require('../config/config');
var db = b.db || levelup(config.leveldb + '/blocks'); var db = b.db || levelup(config.leveldb + '/blocks');
var rpc = b.rpc || new RpcClient(config.bitcoind);
var BlockDb = function() { var BlockDb = function() {
@ -112,8 +113,6 @@ function spec(b) {
BlockDb.prototype.fromHashWithInfo = function(hash, cb) { BlockDb.prototype.fromHashWithInfo = function(hash, cb) {
var rpc = new RpcClient(config.bitcoind);
rpc.getBlock(hash, function(err, info) { rpc.getBlock(hash, function(err, info) {
// Not found? // Not found?
if (err && err.code === -5) return cb(); if (err && err.code === -5) return cb();
@ -135,7 +134,6 @@ function spec(b) {
end: TIMESTAMP_ROOT + end_ts end: TIMESTAMP_ROOT + end_ts
}) })
.on('data', function (data) { .on('data', function (data) {
console.log('[BlockDb.js.137:data:]',data); //TODO
list.push({ list.push({
ts: data.key.replace(TIMESTAMP_ROOT, ''), ts: data.key.replace(TIMESTAMP_ROOT, ''),
hash: data.value, hash: data.value,
@ -149,8 +147,7 @@ console.log('[BlockDb.js.137:data:]',data); //TODO
}); });
}; };
BlockDb.blockIndex = function(height, cb) { BlockDb.prototype.blockIndex = function(height, cb) {
var rpc = new RpcClient(config.bitcoind);
rpc.getBlockHash(height, function(err, bh){ rpc.getBlockHash(height, function(err, bh){
if (err) return cb(err); if (err) return cb(err);

View File

@ -82,8 +82,8 @@ function spec(b) {
var v = data.value.split(':'); var v = data.value.split(':');
ret.push({ ret.push({
addr: v[0], addr: v[0],
value_sat: v[1], value_sat: parseInt(v[1]),
index: k[2], index: parseInt(k[2]),
}); });
}) })
.on('error', function (err) { .on('error', function (err) {
@ -98,9 +98,9 @@ function spec(b) {
var v = data.value.split(':'); var v = data.value.split(':');
var set=0; var set=0;
for(var i=0; i<l; i++) { for(var i=0; i<l; i++) {
if (ret[i].index === k[3]) { if (ret[i].index === parseInt(k[3])) {
ret[i].spendTxId= v[0]; ret[i].spendTxId = v[0];
ret[i].spendIndex=v[1]; ret[i].spendIndex = parseInt(v[1]);
set=1; set=1;
} }
} }
@ -171,7 +171,6 @@ function spec(b) {
self._getInfo(txid, function(err, info) { self._getInfo(txid, function(err, info) {
if (err) return cb(err); if (err) return cb(err);
if (!info ) return cb(); if (!info ) return cb();
return cb(err, {txid: txid, info: info} ); return cb(err, {txid: txid, info: info} );
}); });
}; };
@ -186,7 +185,7 @@ function spec(b) {
err = null; err = null;
} }
var a = val.split(':'); var a = val.split(':');
return cb(err, a[0], a[1]); return cb(err, a[0], parseInt(a[1]));
}); });
}; };
@ -202,16 +201,17 @@ function spec(b) {
var k = data.key.split('-'); var k = data.key.split('-');
var v = data.value.split(':'); var v = data.value.split(':');
ret.push({ ret.push({
value_sat: v[0], value_sat: parseInt(v[0]),
ts: k[3], ts: parseInt(k[3]),
txid: k[4], txid: k[4],
index: k[5], index: parseInt(k[5]),
}); });
}) })
.on('error', function (err) { .on('error', function (err) {
return cb(err); return cb(err);
}) })
.on('end', function () { .on('end', function () {
async.each(ret, function(o, e_c) { async.each(ret, function(o, e_c) {
var k = SPEND_ROOT + o.txid + '-' + o.index; var k = SPEND_ROOT + o.txid + '-' + o.index;
db.get(k, function(err, val) { db.get(k, function(err, val) {
@ -220,7 +220,8 @@ function spec(b) {
var v = val.split(':'); var v = val.split(':');
o.spendTxId= v[0]; o.spendTxId= v[0];
o.spendIndex=v[1]; o.spendIndex=parseInt(v[1]);
o.spendTs=parseInt(v[2]);
return e_c(); return e_c();
}); });
}, },

View File

@ -3,7 +3,7 @@
require('classtool'); require('classtool');
function spec() { function spec(b) {
var RpcClient = require('bitcore/RpcClient').class(), var RpcClient = require('bitcore/RpcClient').class(),
// networks = require('bitcore/network'), // networks = require('bitcore/network'),
BitcoreTransaction = require('bitcore/Transaction').class(), BitcoreTransaction = require('bitcore/Transaction').class(),
@ -11,8 +11,9 @@ function spec() {
util = require('bitcore/util/util'), util = require('bitcore/util/util'),
config = require('../config/config'); config = require('../config/config');
var rpc = b.rpc || new RpcClient(config.bitcoind);
function TransactionRpc() { function TransactionRpc() {
this.dummy = null;
} }
TransactionRpc._parseRpcResult = function(info) { TransactionRpc._parseRpcResult = function(info) {
@ -37,9 +38,9 @@ function spec() {
// Outputs // Outputs
var valueOut = 0; var valueOut = 0;
info.vout.forEach( function(o) { info.vout.forEach( function(o) {
valueOut += o.value * util.COIN; valueOut += o.value;
}); });
info.valueOut = valueOut / util.COIN; info.valueOut = valueOut;
info.size = b.length; info.size = b.length;
return info; return info;
@ -48,8 +49,6 @@ function spec() {
TransactionRpc.getRpcInfo = function(txid, cb) { TransactionRpc.getRpcInfo = function(txid, cb) {
var Self = this; var Self = this;
var rpc = new RpcClient(config.bitcoind);
rpc.getRawTransaction(txid, 1, function(err, txInfo) { rpc.getRawTransaction(txid, 1, function(err, txInfo) {
// Not found? // Not found?

View File

@ -30,14 +30,12 @@ describe('Address balances', function(){
a.update(function(err) { a.update(function(err) {
if (err) done(err); if (err) done(err);
assert.equal(v.addr, a.addrStr); assert.equal(v.addr, a.addrStr);
console.log("TX count:" + a.transactions.length); if (v.txApperances)
assert.equal(v.txApperances, a.txApperances, 'txApperances: ' + a.txApperances );
if (v.balance) assert.equal(v.balance, a.balance, 'balance: ' + a.balance);
if (v.totalReceived) assert.equal(v.totalReceived, a.totalReceived, 'received: ' + a.totalReceived ); if (v.totalReceived) assert.equal(v.totalReceived, a.totalReceived, 'received: ' + a.totalReceived );
if (v.totalSent) assert.equal(v.totalSent, a.totalSent, 'send: ' + a.totalSent); if (v.totalSent) assert.equal(v.totalSent, a.totalSent, 'send: ' + a.totalSent);
if (v.txApperances) if (v.balance) assert.equal(v.balance, a.balance, 'balance: ' + a.balance);
assert.equal(v.txApperances, a.txApperances, 'txApperances: ' + a.txApperances );
if (v.transactions) { if (v.transactions) {

View File

@ -16,10 +16,10 @@
}, },
{ {
"addr": "mhPEfAmeKVwT7arwMYbhwnL2TfwuWbP4r4", "addr": "mhPEfAmeKVwT7arwMYbhwnL2TfwuWbP4r4",
"balance": 1065,
"totalReceived": 1069, "totalReceived": 1069,
"totalSent": 4, "txApperances": 13,
"txApperances": 13 "balance": 1065,
"totalSent": 4
}, },
{ {
"addr": "n47CfqnKWdNwqY1UWxTmNJAqYutFxdH3zY", "addr": "n47CfqnKWdNwqY1UWxTmNJAqYutFxdH3zY",