forked from 0i0/bitcoinjs-color
-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.js
178 lines (155 loc) · 4.56 KB
/
query.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
var bitcoin = require('../bitcoin');
var Util = require('../util');
var Step = require('step');
var Bignum = bitcoin.bignum;
function getOutpoints(blockChain, txStore, txs, callback) {
if (!Array.isArray(txs)) {
txs = [txs];
}
Step(
function fetchTxInputsStep() {
var group = this.group();
txs.forEach(function (tx) {
if (tx.isCoinBase()) return;
var callback = group();
tx.cacheInputs(blockChain, txStore, false, function (err, cache) {
if (err) {
callback(err);
return;
}
tx.ins.forEach(function (txin) {
var prevTx = cache.txIndex[txin.getOutpointHash().toString('base64')];
txin.source = prevTx[txin.getOutpointIndex()];
});
callback();
});
});
},
function calculateStep(err) {
if (err) throw err;
txs.forEach(function (tx, i) {
tx.totalIn = Bignum(0);
tx.totalOut = Bignum(0);
tx.ins.forEach(function (txin, j) {
if (txin.isCoinBase()) return;
tx.totalIn = tx.totalIn.add(Util.valueToBigInt(txin.source.v));
});
// TODO: Add block value to totalIn for coinbase
tx.outs.forEach(function (txout, j) {
tx.totalOut = tx.totalOut.add(Util.valueToBigInt(txout.v));
});
if (!tx.isCoinBase()) tx.fee = tx.totalIn.sub(tx.totalOut);
});
this(null, txs);
},
callback
);
};
function formatTx(tx) {
var standard = tx.getStandardizedObject();
var data =
{ 'hash' : standard.hash
, 'totalIn': Util.formatValue(tx.totalIn)
, 'totalOut': Util.formatValue(tx.totalOut)
, 'fee': (tx.fee) ? Util.formatValue(tx.fee) : "0.0"
, 'in' : []
, 'out' : []
}
tx.ins.forEach(function (txin, j) {
if (txin.isCoinBase()) {
console.log(txin);
data.in.push(
{ type : 'coinBase'
}
)
return;
}
if (txin.source) {
data.in.push(
{ sourceAddr : Util.pubKeyHashToAddress(txin.source.getScript().simpleOutPubKeyHash())
, value : Util.formatValue(txin.source.v)
, prev_tx : standard.in[j].prev_out.hash
, outIndex : txin.getOutpointIndex()
, n : standard.in[j].prev_out.n
}
)
}
})
tx.outs.forEach(function (txout, j) {
data.out.push(
{ toAddr : Util.pubKeyHashToAddress(txout.getScript().simpleOutPubKeyHash())
, value : Util.formatValue(txout.v)
}
)
})
return data;
}
exports.txquery = function (args, opt, callback) {
var hash64 = args[0];
var hash = new Buffer(hash64, 'base64');
var storage = this.node.getStorage();
var blockChain = this.node.getBlockChain();
var txStore = this.node.getTxStore();
var result = {};
Step(
function getMemTxStep() {
txStore.get(hash, this);
},
function getChainTxStep(err, tx) {
if (tx) {
// We already found the transaction, just store it
result.tx = tx;
// And then skip to fetchOutpointsStep
throw 'fetchop';
} else {
// The transaction isn't in the memory pool, check the database
storage.getTransactionByHash(hash, this);
}
},
function getBlockHashStep(err, tx) {
if (err) throw err;
if (tx) {
// We found the transaction, store it
result.tx = tx;
} else {
// We still couldn't find the transaction, return undefined
callback(null, undefined);
return;
}
// Get the containing block
storage.getContainingBlock(hash, this);
},
function getBlockStep(err, blockHash) {
if (err) throw err;
if (!blockHash) {
// TODO: Strange orphan (corrupt db)
callback(null, new Error('Block containing transaction not indexed, '
+ 'db corrupt.'));
return;
}
// Get the containing block
storage.getBlockByHash(blockHash, this);
},
function storeBlockStep(err, block) {
if (err) throw err;
if (!block) {
// TODO: Strange orphan (corrupt db)
callback(null, new Error('Block containing transaction not found, '
+ 'db corrupt.'));
return;
}
result.block = block.getStandardizedObject();
this(null);
},
function fetchOutpointsStep(err) {
if (err && err !== 'fetchop') throw err;
getOutpoints(blockChain, txStore, result.tx, this);
},
function returnResultStep(err) {
if (err) throw err;
result.tx = formatTx(result.tx);
this(null, result);
},
callback
);
};