Skip to content

Commit

Permalink
add flag to disable validator transaction cache
Browse files Browse the repository at this point in the history
to disable cache set "useTransactionCache" to false in the localvaildator constructor
  • Loading branch information
jcramer committed May 12, 2020
1 parent 101607e commit 0508a9a
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions lib/localvalidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ interface Parent {
export class LocalValidator implements SlpValidator {
public BITBOX: BITBOX;
public cachedRawTransactions: { [txid: string]: string };
public useTransactionCache: boolean;
public cachedValidations: { [txid: string]: Validation };
public getRawTransactions: GetRawTransactionsAsync;
public slp: Slp;
public logger: logger = { log: (s: string) => null };

constructor(BITBOX: BITBOX, getRawTransactions: GetRawTransactionsAsync, logger?: logger) {
constructor(BITBOX: BITBOX, getRawTransactions: GetRawTransactionsAsync, logger?: logger, useTransactionCache = true) {
if (!BITBOX) {
throw Error("Must provide BITBOX instance to class constructor.");
}
Expand All @@ -43,14 +44,15 @@ export class LocalValidator implements SlpValidator {
this.slp = new Slp(BITBOX);
this.cachedValidations = {};
this.cachedRawTransactions = {};
this.useTransactionCache = useTransactionCache;
}

public addValidationFromStore(hex: string, isValid: boolean) {
const id = Crypto.txid(Buffer.from(hex, "hex")).toString("hex");
if (!this.cachedValidations[id]) {
this.cachedValidations[id] = { validity: isValid, parents: [], details: null, invalidReason: null, waiting: false };
}
if (!this.cachedRawTransactions[id]) {
if (!this.cachedRawTransactions[id] && this.useTransactionCache) {
this.cachedRawTransactions[id] = hex;
}
}
Expand All @@ -67,7 +69,7 @@ export class LocalValidator implements SlpValidator {
}
}

public async waitForTransactionDownloadToComplete(txid: string){
public async waitForTransactionDownloadToComplete(txid: string) {
while (true) {
if (this.cachedRawTransactions[txid] && this.cachedRawTransactions[txid] !== "waiting") {
break;
Expand All @@ -77,16 +79,18 @@ export class LocalValidator implements SlpValidator {
}

public async retrieveRawTransaction(txid: string) {
if (!this.cachedRawTransactions[txid]) {
if (this.useTransactionCache && !this.cachedRawTransactions[txid]) {
this.cachedRawTransactions[txid] = "waiting";
const txns = await this.getRawTransactions([txid]);
if (!txns || txns.length === 0 || typeof txns[0] !== "string") {
throw Error(`Response error in getRawTransactions, got: ${txns}`);
}
this.cachedRawTransactions[txid] = txns[0];
return txns[0];
} else {
} else if (this.useTransactionCache) {
return this.cachedRawTransactions[txid];
} else {
return await this.getRawTransactions([txid]);
}
}

Expand Down Expand Up @@ -140,12 +144,16 @@ export class LocalValidator implements SlpValidator {
// during the download of a previous request.
//
if (!this.cachedRawTransactions[txid] || this.cachedRawTransactions[txid] === "waiting") {
if (this.cachedRawTransactions[txid] !== "waiting") {
this.retrieveRawTransaction(txid);
}
if (this.useTransactionCache) {
if (this.cachedRawTransactions[txid] !== "waiting") {
this.retrieveRawTransaction(txid);
}

// Wait for previously a initiated download to completed
await this.waitForTransactionDownloadToComplete(txid);
// Wait for previously a initiated download to completed
await this.waitForTransactionDownloadToComplete(txid);
} else {
await this.retrieveRawTransaction(txid);
}
}

// Handle case where txid is already in the process of being validated from a previous call
Expand Down

0 comments on commit 0508a9a

Please sign in to comment.