-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathscanner.js
177 lines (174 loc) · 7.31 KB
/
scanner.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
require('colors');
const deasync = require('deasync');
const debug = require('./debug');
const bitcoin = require('./bitcoin');
const payment = require('./payment');
const assert = require('assert');
const async = require('async');
const { lpad, rpad, numpad } = require('./utils');
const model = {
invoice: require('./models/invoice'),
state: require('./models/state'),
payment: require('./models/payment'),
};
const txArrayString = (vtx) => {
let s = '';
for (const tx of vtx) {
s += ` ${tx.address} id=${tx.txid} c=${lpad(tx.category, 8)} v=${numpad(''+tx.amount,2,8)}\n`;
}
return s;
}
const scanner = {
findGenesisBlock(cb) {
bitcoin.getBlockHash(0, (err, info) => {
assert.equal(null, err);
cb(info.result);
});
},
checkWalletConflicts(tx, affectedInvoices, cb) {
if (!cb) { cb = affectedInvoices; affectedInvoices = {}; }
if (typeof(tx) === 'string') {
try {
tx = bitcoin.getTransactionS(tx).result;
} catch (e) {
return cb(e);
}
}
// console.log(`*** ${JSON.stringify(tx)} ***`);
if (tx.walletconflicts.length > 0) {
// console.log(`- checking wallet conflict txs ${JSON.stringify(tx.walletconflicts)}`);
async.eachSeries(
tx.walletconflicts,
(txid, escb) => {
let ctx;
try {
ctx = bitcoin.getTransactionS(txid).result;
} catch (ce) {
// console.log(`ERROR FETCHING TXID=${txid}: ${JSON.stringify(ce)}`);
return escb();
}
// console.log(`ctx = ${JSON.stringify(ctx)}`);
payment.updatePayment(ctx, (u2err, u2invoiceId) => {
if (!u2err && u2invoiceId) affectedInvoices[u2invoiceId] = true;
escb(u2err);
});
},
cb
);
} else cb(null);
},
checkTransactions(transactions, cb) {
const affectedInvoices = {};
async.eachSeries(
transactions,
(transaction, transactionCallback) => {
const paymentCallback = (err, invoiceId) => {
if (invoiceId) affectedInvoices[invoiceId] = true;
transactionCallback();
};
let tx = transaction;
if (typeof(transaction) === 'string') {
try {
tx = bitcoin.getTransactionS(transaction).result;
} catch (e) {
model.payment.findByTxId(transaction, (pmtErr, payments) => {
if (pmtErr || payments.length === 0) return paymentCalback(); // unrelated transaction
const [ payment ] = payments;
paymentCallback(null, payment.invoice);
});
return;
}
}
// console.log(`updating payment for ${tx.txid} with walletconflicts=${tx.walletconflicts}`);
payment.updatePayment(tx, (e,r) => {
if (e) return paymentCallback(e);
this.checkWalletConflicts(tx, affectedInvoices, paymentCallback);
});
},
() => cb(null, Object.keys(affectedInvoices))
);
},
checkWatchedInvoices(cb) {
model.invoice.getWatchedInvoices((err, invoices) => {
if (err) return cb(err);
// console.log(`checking ${invoices.length} watched invoices`);
async.eachSeries(
invoices,
(invoice, invoiceSeriesCallback) => {
// find all payments and check for wallet conflicts
model.payment.findByAddr(invoice.addr, (err, payments) => {
if (err) return invoiceSeriesCallback(err);
async.eachSeries(
payments,
(payment, paymentSeriesCallback) => {
// console.log(`checkWalletConflicts(${payment.txid})`);
this.checkWalletConflicts(payment.txid, paymentSeriesCallback);
},
() => payment.updateInvoice(''+invoice._id, invoiceSeriesCallback)
);
});
},
cb
);
});
},
scanFromHash(blockHash, cb) {
bitcoin.listSinceBlock(blockHash, (err, info) => {
if (err) return cb(err);
const result = info.result;
// anything new?
let z = '';
for (const tx of result.transactions) {
z += `\n${tx.txid}.${tx.confirmations}`;
}
if (result.lastblock === blockHash && z === this.lsbcached) return cb(null, blockHash, []);
this.lsbcached = z;
debug(`listSinceBlock ${blockHash} (depth=${result.depth}) [new last=${result.lastblock}] -> \n${txArrayString(info.result.transactions)}`);
let autoCheckWatched = false;
for (const tx of info.result.transactions) {
// if (tx.txid !== this.lastconsoleloggedtxtxid) {
// console.log(`- ${tx.txid}`.yellow);
// this.lastconsoleloggedtxtxid = tx.txid;
// }
if (tx.category === 'orphan') {
// console.log('found orphan transaction; auto-check watched flag set');
autoCheckWatched = true;
break;
}
}
const newBlockHash = result.lastblock;
this.checkTransactions(result.transactions, (err2, affectedInvoices) => {
// agggregate
debug(`affected invoices: ${JSON.stringify(affectedInvoices)}`);
// if (this.lastprintedset !== newBlockHash) {
// this.lastprintedset = newBlockHash;
// console.log(`lastblockhash => ${newBlockHash}`);
// }
model.state.set('lastblockhash', newBlockHash, () => {
debug(` :: ${newBlockHash} [${JSON.stringify(affectedInvoices)}]`);
if (autoCheckWatched) {
setTimeout(() => this.checkWatchedInvoices(() => cb(null, newBlockHash, affectedInvoices)), 2000);
} else cb(null, newBlockHash, affectedInvoices);
});
});
});
},
scan(cb) {
model.state.get('lastblockhash', (lastblockhash) => {
if (this.lastprintedscanfrom !== lastblockhash) {
this.lastprintedscanfrom = lastblockhash;
debug('scanning from last block hash');
// console.log(`scan from ${lastblockhash}`);
}
if (!lastblockhash) {
debug('no last block hash; scanning from genesis block');
this.findGenesisBlock((genesishash) => {
this.scanFromHash(genesishash, cb);
});
return;
}
this.scanFromHash(lastblockhash, cb);
});
},
};
module.exports = scanner;