Skip to content

Commit 0e709dd

Browse files
committed
Remove duplication in transaction data in cache
1 parent 1c1e6db commit 0e709dd

File tree

1 file changed

+38
-42
lines changed

1 file changed

+38
-42
lines changed

lib/localvalidator.ts

Lines changed: 38 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import * as bitcore from 'bitcore-lib-cash';
55
import { BitcoreTransaction } from './global';
66
import BigNumber from 'bignumber.js';
77

8-
export interface Validation { hex: string|null; validity: boolean|null; parents: Parent[], details: SlpTransactionDetails|null, invalidReason: string|null }
9-
export type GetRawTransactionsAsync = (txid: string[]) => Promise<string[]|null>;
8+
export interface Validation { validity: boolean|null; parents: Parent[], details: SlpTransactionDetails|null, invalidReason: string|null }
9+
export type GetRawTransactionsAsync = (txid: string[]) => Promise<(string|null)[]>;
1010

1111
const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms))
1212

@@ -39,7 +39,7 @@ export class LocalValidator implements SlpValidator {
3939
addValidationFromStore(hex: string, isValid: boolean) {
4040
let id = (<Buffer>this.BITBOX.Crypto.sha256(this.BITBOX.Crypto.sha256(Buffer.from(hex, 'hex'))).reverse()).toString('hex');
4141
if(!this.cachedValidations[id])
42-
this.cachedValidations[id] = { hex: hex, validity: isValid, parents: [], details: null, invalidReason: null }
42+
this.cachedValidations[id] = { validity: isValid, parents: [], details: null, invalidReason: null }
4343
if(!this.cachedRawTransactions[id])
4444
this.cachedRawTransactions[id] = hex;
4545
}
@@ -58,15 +58,15 @@ export class LocalValidator implements SlpValidator {
5858
async waitForTransactionPreProcessing(txid: string){
5959
// TODO: Add some timeout?
6060
while(true) {
61-
if(this.cachedValidations[txid].hex && (this.cachedValidations[txid].details || typeof this.cachedValidations.validity === 'boolean'))
61+
if(this.cachedRawTransactions[txid] && (this.cachedValidations[txid].details || typeof this.cachedValidations.validity === 'boolean'))
6262
break
6363
await sleep(10);
6464
}
6565
//await sleep(100); // short wait to make sure parent's properties gets set first.
6666
return
6767
}
6868

69-
async getRawTransaction(txid: string) {
69+
async retrieveRawTransaction(txid: string) {
7070
if(this.cachedRawTransactions[txid])
7171
return this.cachedRawTransactions[txid];
7272
this.cachedRawTransactions[txid] = (await this.getRawTransactions([txid]))[0]
@@ -81,20 +81,20 @@ export class LocalValidator implements SlpValidator {
8181

8282
async isValidSlpTxid(txid: string) {
8383
if(txid && !this.cachedValidations[txid]) {
84-
this.cachedValidations[txid] = { hex: null, validity: null, parents: [], details: null, invalidReason: null }
85-
this.cachedValidations[txid].hex = await this.getRawTransaction(txid);
84+
this.cachedValidations[txid] = { validity: null, parents: [], details: null, invalidReason: null }
85+
await this.retrieveRawTransaction(txid);
8686
}
8787

8888
// Check to see how we should proceed based on the validation-cache state
89-
if(!this.cachedValidations[txid].hex)
89+
if(!this.cachedRawTransactions[txid])
9090
await this.waitForTransactionPreProcessing(txid);
9191
if(typeof this.cachedValidations[txid].validity === 'boolean')
9292
return this.cachedValidations[txid].validity;
9393
if(this.cachedValidations[txid].details)
9494
await this.waitForCurrentValidationProcessing(txid);
9595

9696
// Check SLP message validity
97-
let txn: BitcoreTransaction = new bitcore.Transaction(this.cachedValidations[txid].hex)
97+
let txn: BitcoreTransaction = new bitcore.Transaction(this.cachedRawTransactions[txid])
9898
let slpmsg: SlpTransactionDetails;
9999
try {
100100
slpmsg = this.cachedValidations[txid].details = this.slp.parseSlpOutputScript(txn.outputs[0]._scriptBuffer)
@@ -110,21 +110,19 @@ export class LocalValidator implements SlpValidator {
110110
else if (slpmsg.transactionType === SlpTransactionType.MINT) {
111111
for(let i = 0; i < txn.inputs.length; i++) {
112112
let input_txid = txn.inputs[i].prevTxId.toString('hex')
113-
let input_txhex = await this.getRawTransaction(input_txid)
114-
if (input_txhex) {
115-
let input_tx: BitcoreTransaction = new bitcore.Transaction(input_txhex);
116-
try {
117-
let input_slpmsg = this.slp.parseSlpOutputScript(input_tx.outputs[0]._scriptBuffer)
118-
if(input_slpmsg.transactionType === SlpTransactionType.GENESIS)
119-
input_slpmsg.tokenIdHex = input_txid;
120-
if(input_slpmsg.tokenIdHex === slpmsg.tokenIdHex) {
121-
if(input_slpmsg.transactionType === SlpTransactionType.GENESIS || input_slpmsg.transactionType === SlpTransactionType.MINT) {
122-
if(txn.inputs[i].outputIndex === input_slpmsg.batonVout)
123-
this.cachedValidations[txid].parents.push({ txid: txn.inputs[i].prevTxId.toString('hex'), versionType: input_slpmsg.versionType ,valid: null, inputQty: null })
124-
}
113+
let input_txhex = await this.retrieveRawTransaction(input_txid)
114+
let input_tx: BitcoreTransaction = new bitcore.Transaction(input_txhex);
115+
try {
116+
let input_slpmsg = this.slp.parseSlpOutputScript(input_tx.outputs[0]._scriptBuffer)
117+
if(input_slpmsg.transactionType === SlpTransactionType.GENESIS)
118+
input_slpmsg.tokenIdHex = input_txid;
119+
if(input_slpmsg.tokenIdHex === slpmsg.tokenIdHex) {
120+
if(input_slpmsg.transactionType === SlpTransactionType.GENESIS || input_slpmsg.transactionType === SlpTransactionType.MINT) {
121+
if(txn.inputs[i].outputIndex === input_slpmsg.batonVout)
122+
this.cachedValidations[txid].parents.push({ txid: txn.inputs[i].prevTxId.toString('hex'), versionType: input_slpmsg.versionType ,valid: null, inputQty: null })
125123
}
126-
} catch(_) {}
127-
}
124+
}
125+
} catch(_) {}
128126
}
129127
if(this.cachedValidations[txid].parents.length !== 1) {
130128
this.cachedValidations[txid].invalidReason = "MINT transaction must have 1 valid baton parent."
@@ -136,26 +134,24 @@ export class LocalValidator implements SlpValidator {
136134
let tokenInQty = new BigNumber(0);
137135
for(let i = 0; i < txn.inputs.length; i++) {
138136
let input_txid = txn.inputs[i].prevTxId.toString('hex')
139-
let input_txhex = await this.getRawTransaction(input_txid)
140-
if (input_txhex) {
141-
let input_tx: BitcoreTransaction = new bitcore.Transaction(input_txhex);
142-
try {
143-
let input_slpmsg = this.slp.parseSlpOutputScript(input_tx.outputs[0]._scriptBuffer)
144-
if(input_slpmsg.transactionType === SlpTransactionType.GENESIS)
145-
input_slpmsg.tokenIdHex = input_txid;
146-
if(input_slpmsg.tokenIdHex === slpmsg.tokenIdHex) {
147-
if(input_slpmsg.transactionType === SlpTransactionType.SEND) {
148-
tokenInQty = tokenInQty.plus(input_slpmsg.sendOutputs[txn.inputs[i].outputIndex])
149-
this.cachedValidations[txid].parents.push({txid: txn.inputs[i].prevTxId.toString('hex'), versionType: input_slpmsg.versionType, valid: null, inputQty: input_slpmsg.sendOutputs[txn.inputs[i].outputIndex] })
150-
}
151-
else if(input_slpmsg.transactionType === SlpTransactionType.GENESIS || input_slpmsg.transactionType === SlpTransactionType.MINT) {
152-
if(txn.inputs[i].outputIndex === 1)
153-
tokenInQty = tokenInQty.plus(input_slpmsg.genesisOrMintQuantity)
154-
this.cachedValidations[txid].parents.push({txid: txn.inputs[i].prevTxId.toString('hex'), versionType: input_slpmsg.versionType, valid: null, inputQty: input_slpmsg.genesisOrMintQuantity })
155-
}
137+
let input_txhex = await this.retrieveRawTransaction(input_txid)
138+
let input_tx: BitcoreTransaction = new bitcore.Transaction(input_txhex);
139+
try {
140+
let input_slpmsg = this.slp.parseSlpOutputScript(input_tx.outputs[0]._scriptBuffer)
141+
if(input_slpmsg.transactionType === SlpTransactionType.GENESIS)
142+
input_slpmsg.tokenIdHex = input_txid;
143+
if(input_slpmsg.tokenIdHex === slpmsg.tokenIdHex) {
144+
if(input_slpmsg.transactionType === SlpTransactionType.SEND) {
145+
tokenInQty = tokenInQty.plus(input_slpmsg.sendOutputs[txn.inputs[i].outputIndex])
146+
this.cachedValidations[txid].parents.push({txid: txn.inputs[i].prevTxId.toString('hex'), versionType: input_slpmsg.versionType, valid: null, inputQty: input_slpmsg.sendOutputs[txn.inputs[i].outputIndex] })
156147
}
157-
} catch(_) {}
158-
}
148+
else if(input_slpmsg.transactionType === SlpTransactionType.GENESIS || input_slpmsg.transactionType === SlpTransactionType.MINT) {
149+
if(txn.inputs[i].outputIndex === 1)
150+
tokenInQty = tokenInQty.plus(input_slpmsg.genesisOrMintQuantity)
151+
this.cachedValidations[txid].parents.push({txid: txn.inputs[i].prevTxId.toString('hex'), versionType: input_slpmsg.versionType, valid: null, inputQty: input_slpmsg.genesisOrMintQuantity })
152+
}
153+
}
154+
} catch(_) {}
159155
}
160156

161157
// Check token inputs are greater than token outputs (includes valid and invalid inputs)

0 commit comments

Comments
 (0)