Browse Source

Refactor: Create cache in constructor

psbt
junderw 6 years ago
parent
commit
6e447b1f1b
No known key found for this signature in database GPG Key ID: B256185D3A971908
  1. 12
      src/psbt.js
  2. 20
      ts_src/psbt.ts

12
src/psbt.js

@ -62,16 +62,14 @@ const DEFAULT_OPTS = {
class Psbt { class Psbt {
constructor(opts = {}, data = new bip174_1.Psbt(new PsbtTransaction())) { constructor(opts = {}, data = new bip174_1.Psbt(new PsbtTransaction())) {
this.data = data; this.data = data;
// set defaults
this.opts = Object.assign({}, DEFAULT_OPTS, opts);
this.__CACHE = { this.__CACHE = {
__NON_WITNESS_UTXO_TX_CACHE: [], __NON_WITNESS_UTXO_TX_CACHE: [],
__NON_WITNESS_UTXO_BUF_CACHE: [], __NON_WITNESS_UTXO_BUF_CACHE: [],
__TX_IN_CACHE: {}, __TX_IN_CACHE: {},
__TX: new transaction_1.Transaction(), __TX: this.data.globalMap.unsignedTx.tx,
}; };
// set defaults
this.opts = Object.assign({}, DEFAULT_OPTS, opts);
const c = this.__CACHE;
c.__TX = this.data.globalMap.unsignedTx.tx;
if (this.data.inputs.length === 0) this.setVersion(2); if (this.data.inputs.length === 0) this.setVersion(2);
// Make data hidden when enumerating // Make data hidden when enumerating
const dpew = (obj, attr, enumerable, writable) => const dpew = (obj, attr, enumerable, writable) =>
@ -92,10 +90,8 @@ class Psbt {
} }
static fromBuffer(buffer, opts = {}) { static fromBuffer(buffer, opts = {}) {
const psbtBase = bip174_1.Psbt.fromBuffer(buffer, transactionFromBuffer); const psbtBase = bip174_1.Psbt.fromBuffer(buffer, transactionFromBuffer);
const tx = psbtBase.globalMap.unsignedTx.tx;
const psbt = new Psbt(opts, psbtBase); const psbt = new Psbt(opts, psbtBase);
psbt.__CACHE.__TX = tx; checkTxForDupeIns(psbt.__CACHE.__TX, psbt.__CACHE);
checkTxForDupeIns(tx, psbt.__CACHE);
return psbt; return psbt;
} }
get inputCount() { get inputCount() {

20
ts_src/psbt.ts

@ -88,20 +88,12 @@ export class Psbt {
static fromBuffer(buffer: Buffer, opts: PsbtOptsOptional = {}): Psbt { static fromBuffer(buffer: Buffer, opts: PsbtOptsOptional = {}): Psbt {
const psbtBase = PsbtBase.fromBuffer(buffer, transactionFromBuffer); const psbtBase = PsbtBase.fromBuffer(buffer, transactionFromBuffer);
const tx: Transaction = (psbtBase.globalMap.unsignedTx as PsbtTransaction)
.tx;
const psbt = new Psbt(opts, psbtBase); const psbt = new Psbt(opts, psbtBase);
psbt.__CACHE.__TX = tx; checkTxForDupeIns(psbt.__CACHE.__TX, psbt.__CACHE);
checkTxForDupeIns(tx, psbt.__CACHE);
return psbt; return psbt;
} }
private __CACHE: PsbtCache = { private __CACHE: PsbtCache;
__NON_WITNESS_UTXO_TX_CACHE: [],
__NON_WITNESS_UTXO_BUF_CACHE: [],
__TX_IN_CACHE: {},
__TX: new Transaction(),
};
private opts: PsbtOpts; private opts: PsbtOpts;
constructor( constructor(
@ -110,8 +102,12 @@ export class Psbt {
) { ) {
// set defaults // set defaults
this.opts = Object.assign({}, DEFAULT_OPTS, opts); this.opts = Object.assign({}, DEFAULT_OPTS, opts);
const c = this.__CACHE; this.__CACHE = {
c.__TX = (this.data.globalMap.unsignedTx as PsbtTransaction).tx; __NON_WITNESS_UTXO_TX_CACHE: [],
__NON_WITNESS_UTXO_BUF_CACHE: [],
__TX_IN_CACHE: {},
__TX: (this.data.globalMap.unsignedTx as PsbtTransaction).tx,
};
if (this.data.inputs.length === 0) this.setVersion(2); if (this.data.inputs.length === 0) this.setVersion(2);
// Make data hidden when enumerating // Make data hidden when enumerating

Loading…
Cancel
Save