Refactor: nonWitnessUtxo cache

This commit is contained in:
junderw 2019-07-09 11:29:20 +09:00
parent 36a966cfcd
commit 88de1e7b0e
No known key found for this signature in database
GPG key ID: B256185D3A971908
2 changed files with 55 additions and 38 deletions

View file

@ -252,12 +252,9 @@ class Psbt extends bip174_1.Psbt {
if (input.witnessUtxo) { if (input.witnessUtxo) {
inputAmount += input.witnessUtxo.value; inputAmount += input.witnessUtxo.value;
} else if (input.nonWitnessUtxo) { } else if (input.nonWitnessUtxo) {
const cache = this.__CACHE; const nwTx = nonWitnessUtxoTxFromCache(this.__CACHE, input, idx);
if (!cache.__NON_WITNESS_UTXO_TX_CACHE[idx]) {
addNonWitnessTxCache(this.__CACHE, input, idx);
}
const vout = this.__TX.ins[idx].index; const vout = this.__TX.ins[idx].index;
const out = cache.__NON_WITNESS_UTXO_TX_CACHE[idx].outs[vout]; const out = nwTx.outs[vout];
inputAmount += out.value; inputAmount += out.value;
} }
}); });
@ -436,13 +433,14 @@ function addNonWitnessTxCache(cache, input, inputIndex) {
Object.defineProperty(input, 'nonWitnessUtxo', { Object.defineProperty(input, 'nonWitnessUtxo', {
enumerable: true, enumerable: true,
get() { get() {
if (self.__NON_WITNESS_UTXO_BUF_CACHE[selfIndex] !== undefined) { const buf = self.__NON_WITNESS_UTXO_BUF_CACHE[selfIndex];
return self.__NON_WITNESS_UTXO_BUF_CACHE[selfIndex]; const txCache = self.__NON_WITNESS_UTXO_TX_CACHE[selfIndex];
if (buf !== undefined) {
return buf;
} else { } else {
self.__NON_WITNESS_UTXO_BUF_CACHE[ const newBuf = txCache.toBuffer();
selfIndex self.__NON_WITNESS_UTXO_BUF_CACHE[selfIndex] = newBuf;
] = self.__NON_WITNESS_UTXO_TX_CACHE[selfIndex].toBuffer(); return newBuf;
return self.__NON_WITNESS_UTXO_BUF_CACHE[selfIndex];
} }
}, },
set(data) { set(data) {
@ -597,10 +595,11 @@ function getHashForSig(inputIndex, input, unsignedTx, cache) {
let hash; let hash;
let script; let script;
if (input.nonWitnessUtxo) { if (input.nonWitnessUtxo) {
if (!cache.__NON_WITNESS_UTXO_TX_CACHE[inputIndex]) { const nonWitnessUtxoTx = nonWitnessUtxoTxFromCache(
addNonWitnessTxCache(cache, input, inputIndex); cache,
} input,
const nonWitnessUtxoTx = cache.__NON_WITNESS_UTXO_TX_CACHE[inputIndex]; inputIndex,
);
const prevoutHash = unsignedTx.ins[inputIndex].hash; const prevoutHash = unsignedTx.ins[inputIndex].hash;
const utxoHash = nonWitnessUtxoTx.getHash(); const utxoHash = nonWitnessUtxoTx.getHash();
// If a non-witness UTXO is provided, its hash must match the hash specified in the prevout // If a non-witness UTXO is provided, its hash must match the hash specified in the prevout
@ -723,10 +722,11 @@ function getScriptFromInput(inputIndex, input, unsignedTx, cache) {
res.isP2SH = true; res.isP2SH = true;
res.script = input.redeemScript; res.script = input.redeemScript;
} else { } else {
if (!cache.__NON_WITNESS_UTXO_TX_CACHE[inputIndex]) { const nonWitnessUtxoTx = nonWitnessUtxoTxFromCache(
addNonWitnessTxCache(cache, input, inputIndex); cache,
} input,
const nonWitnessUtxoTx = cache.__NON_WITNESS_UTXO_TX_CACHE[inputIndex]; inputIndex,
);
const prevoutIndex = unsignedTx.ins[inputIndex].index; const prevoutIndex = unsignedTx.ins[inputIndex].index;
res.script = nonWitnessUtxoTx.outs[prevoutIndex].script; res.script = nonWitnessUtxoTx.outs[prevoutIndex].script;
} }
@ -841,6 +841,12 @@ function checkInputsForPartialSig(inputs, action) {
} }
}); });
} }
function nonWitnessUtxoTxFromCache(cache, input, inputIndex) {
if (!cache.__NON_WITNESS_UTXO_TX_CACHE[inputIndex]) {
addNonWitnessTxCache(cache, input, inputIndex);
}
return cache.__NON_WITNESS_UTXO_TX_CACHE[inputIndex];
}
function check32Bit(num) { function check32Bit(num) {
if ( if (
typeof num !== 'number' || typeof num !== 'number' ||

View file

@ -305,12 +305,9 @@ export class Psbt extends PsbtBase {
if (input.witnessUtxo) { if (input.witnessUtxo) {
inputAmount += input.witnessUtxo.value; inputAmount += input.witnessUtxo.value;
} else if (input.nonWitnessUtxo) { } else if (input.nonWitnessUtxo) {
const cache = this.__CACHE; const nwTx = nonWitnessUtxoTxFromCache(this.__CACHE, input, idx);
if (!cache.__NON_WITNESS_UTXO_TX_CACHE[idx]) {
addNonWitnessTxCache(this.__CACHE, input, idx);
}
const vout = this.__TX.ins[idx].index; const vout = this.__TX.ins[idx].index;
const out = cache.__NON_WITNESS_UTXO_TX_CACHE[idx].outs[vout] as Output; const out = nwTx.outs[vout] as Output;
inputAmount += out.value; inputAmount += out.value;
} }
}); });
@ -548,13 +545,14 @@ function addNonWitnessTxCache(
Object.defineProperty(input, 'nonWitnessUtxo', { Object.defineProperty(input, 'nonWitnessUtxo', {
enumerable: true, enumerable: true,
get(): Buffer { get(): Buffer {
if (self.__NON_WITNESS_UTXO_BUF_CACHE[selfIndex] !== undefined) { const buf = self.__NON_WITNESS_UTXO_BUF_CACHE[selfIndex];
return self.__NON_WITNESS_UTXO_BUF_CACHE[selfIndex]; const txCache = self.__NON_WITNESS_UTXO_TX_CACHE[selfIndex];
if (buf !== undefined) {
return buf;
} else { } else {
self.__NON_WITNESS_UTXO_BUF_CACHE[ const newBuf = txCache.toBuffer();
selfIndex self.__NON_WITNESS_UTXO_BUF_CACHE[selfIndex] = newBuf;
] = self.__NON_WITNESS_UTXO_TX_CACHE[selfIndex].toBuffer(); return newBuf;
return self.__NON_WITNESS_UTXO_BUF_CACHE[selfIndex];
} }
}, },
set(data: Buffer): void { set(data: Buffer): void {
@ -760,10 +758,11 @@ function getHashForSig(
let script: Buffer; let script: Buffer;
if (input.nonWitnessUtxo) { if (input.nonWitnessUtxo) {
if (!cache.__NON_WITNESS_UTXO_TX_CACHE[inputIndex]) { const nonWitnessUtxoTx = nonWitnessUtxoTxFromCache(
addNonWitnessTxCache(cache, input, inputIndex); cache,
} input,
const nonWitnessUtxoTx = cache.__NON_WITNESS_UTXO_TX_CACHE[inputIndex]; inputIndex,
);
const prevoutHash = unsignedTx.ins[inputIndex].hash; const prevoutHash = unsignedTx.ins[inputIndex].hash;
const utxoHash = nonWitnessUtxoTx.getHash(); const utxoHash = nonWitnessUtxoTx.getHash();
@ -918,10 +917,11 @@ function getScriptFromInput(
res.isP2SH = true; res.isP2SH = true;
res.script = input.redeemScript; res.script = input.redeemScript;
} else { } else {
if (!cache.__NON_WITNESS_UTXO_TX_CACHE[inputIndex]) { const nonWitnessUtxoTx = nonWitnessUtxoTxFromCache(
addNonWitnessTxCache(cache, input, inputIndex); cache,
} input,
const nonWitnessUtxoTx = cache.__NON_WITNESS_UTXO_TX_CACHE[inputIndex]; inputIndex,
);
const prevoutIndex = unsignedTx.ins[inputIndex].index; const prevoutIndex = unsignedTx.ins[inputIndex].index;
res.script = nonWitnessUtxoTx.outs[prevoutIndex].script; res.script = nonWitnessUtxoTx.outs[prevoutIndex].script;
} }
@ -1054,6 +1054,17 @@ function checkInputsForPartialSig(inputs: PsbtInput[], action: string): void {
}); });
} }
function nonWitnessUtxoTxFromCache(
cache: PsbtCache,
input: PsbtInput,
inputIndex: number,
): Transaction {
if (!cache.__NON_WITNESS_UTXO_TX_CACHE[inputIndex]) {
addNonWitnessTxCache(cache, input, inputIndex);
}
return cache.__NON_WITNESS_UTXO_TX_CACHE[inputIndex];
}
function check32Bit(num: number): void { function check32Bit(num: number): void {
if ( if (
typeof num !== 'number' || typeof num !== 'number' ||