Browse Source

spv transaction history use one time caching

v0.25
pbca26 7 years ago
parent
commit
8f2efde629
  1. 140
      routes/shepherd/electrum/transactions.js

140
routes/shepherd/electrum/transactions.js

@ -60,18 +60,33 @@ module.exports = (shepherd) => {
shepherd.log(json.length, true);
let index = 0;
const _getTransaction = (txid) => {
return new shepherd.Promise((resolve, reject) => {
if (!_inputTxs[txid]) {
ecl.blockchainTransactionGet(txid)
.then((_rawtxJSON) => {
resolve(_rawtxJSON);
});
} else {
resolve(_inputTxs[txid]);
}
});
} ;
async.eachOfSeries(json, (transaction, ind, callback) => {
console.log(`index ${index}`);
//return new shepherd.Promise((resolve, reject) => {
ecl.blockchainBlockGetHeader(transaction.height)
.then((blockInfo) => {
if (blockInfo &&
blockInfo.timestamp) {
ecl.blockchainTransactionGet(transaction['tx_hash'])
_getTransaction(transaction['tx_hash'])
.then((_rawtxJSON) => {
if (!_inputTxs[transaction['tx_hash']]) {
_inputTxs[transaction['tx_hash']] = _rawtxJSON;
}
shepherd.log('electrum gettransaction ==>', true);
shepherd.log((index + ' | ' + (_rawtxJSON.length - 1)), true);
shepherd.log(_rawtxJSON, true);
// shepherd.log(_rawtxJSON, true);
// decode tx
const _network = shepherd.getNetworkData(network);
@ -81,57 +96,18 @@ module.exports = (shepherd) => {
shepherd.log(`decodedtx network ${network}`, true);
shepherd.log('decodedtx =>', true);
shepherd.log(decodedTx.outputs, true);
// shepherd.log(decodedTx.outputs, true);
let index2 = 0;
if (decodedTx &&
decodedTx.inputs) {
async.eachOfSeries(decodedTx.inputs, (_decodedInput, index2, callback2) => {
//shepherd.Promise.all(decodedTx.inputs.map((_decodedInput, index) => {
//return new shepherd.Promise((_resolve, _reject) => {
if (_decodedInput.txid !== '0000000000000000000000000000000000000000000000000000000000000000') {
if (!_inputTxs[_decodedInput.txid]) {
ecl.blockchainTransactionGet(_decodedInput.txid)
.then((rawInput) => {
_inputTxs[_decodedInput.txid] = rawInput;
const decodedVinVout = shepherd.electrumJSTxDecoder(rawInput, network, _network);
shepherd.log(`electrum raw input tx ${_decodedInput.txid} ==>`, true);
if (decodedVinVout) {
shepherd.log(decodedVinVout.outputs[_decodedInput.n], true);
txInputs.push(decodedVinVout.outputs[_decodedInput.n]);
index2++;
callback2();
} else {
decodedTx.inputs &&
decodedTx.inputs.length) {
async.eachOfSeries(decodedTx.inputs, (_decodedInput, ind2, callback2) => {
function checkLoop() {
index2++;
callback2();
}
});
} else {
const decodedVinVout = shepherd.electrumJSTxDecoder(_inputTxs[_decodedInput.txid], network, _network);
shepherd.log(`electrum raw one time cached input tx ${_decodedInput.txid} ==>`, true);
if (decodedVinVout) {
shepherd.log(decodedVinVout.outputs[_decodedInput.n], true);
txInputs.push(decodedVinVout.outputs[_decodedInput.n]);
index2++;
callback2();
} else {
index2++;
callback2();
}
}
} else {
index2++;
callback2();
}
console.log(`${index2} | ${decodedTx.inputs.length - 1} => main callback`);
if (index2 === decodedTx.inputs.length - 1) {
index++;
if (index2 === decodedTx.inputs.length) {
shepherd.log(`tx history decode inputs ${decodedTx.inputs.length} | ${index2} => main callback`, true);
const _parsedTx = {
network: decodedTx.network,
format: decodedTx.format,
@ -171,8 +147,7 @@ module.exports = (shepherd) => {
_rawtx.push(formattedTx[0]);
_rawtx.push(formattedTx[1]);
}
callback();
}
index++;
if (index === json.length) {
ecl.close();
@ -184,12 +159,44 @@ module.exports = (shepherd) => {
res.end(JSON.stringify(successObj));
}
//});
callback();
shepherd.log(`tx history main loop ${json.length} | ${index}`, true);
}
callback2();
}
if (_decodedInput.txid !== '0000000000000000000000000000000000000000000000000000000000000000') {
if (!_inputTxs[_decodedInput.txid]) {
ecl.blockchainTransactionGet(_decodedInput.txid)
.then((rawInput) => {
_inputTxs[_decodedInput.txid] = rawInput;
const decodedVinVout = shepherd.electrumJSTxDecoder(rawInput, network, _network);
shepherd.log(`electrum raw input tx ${_decodedInput.txid} ==>`, true);
if (decodedVinVout) {
shepherd.log(decodedVinVout.outputs[_decodedInput.n], true);
txInputs.push(decodedVinVout.outputs[_decodedInput.n]);
}
checkLoop();
});
/*.then(promiseResult => {
} else {
const decodedVinVout = shepherd.electrumJSTxDecoder(_inputTxs[_decodedInput.txid], network, _network);
resolve(true);
});*/
shepherd.log(`electrum raw one time cached input tx ${_decodedInput.txid} ==>`, true);
if (decodedVinVout) {
shepherd.log(decodedVinVout.outputs[_decodedInput.n], true);
txInputs.push(decodedVinVout.outputs[_decodedInput.n]);
}
checkLoop();
}
} else {
checkLoop();
}
});
} else {
const _parsedTx = {
network: decodedTx.network,
@ -204,7 +211,6 @@ module.exports = (shepherd) => {
const formattedTx = shepherd.parseTransactionAddresses(_parsedTx, req.query.address, network);
_rawtx.push(formattedTx);
index++;
callback();
if (index === json.length) {
ecl.close();
@ -215,6 +221,8 @@ module.exports = (shepherd) => {
};
res.end(JSON.stringify(successObj));
} else {
callback();
}
}
});
@ -230,11 +238,7 @@ module.exports = (shepherd) => {
};
const formattedTx = shepherd.parseTransactionAddresses(_parsedTx, req.query.address, network);
_rawtx.push(formattedTx);
callback();
index++;
}
});
//});
if (index === json.length) {
ecl.close();
@ -245,18 +249,12 @@ module.exports = (shepherd) => {
};
res.end(JSON.stringify(successObj));
} else {
callback();
}
}
});
/*.then(promiseResult => {
ecl.close();
const successObj = {
msg: 'success',
result: _rawtx,
};
res.end(JSON.stringify(successObj));
});*/
});
} else {
ecl.close();

Loading…
Cancel
Save