Skip to content

Commit

Permalink
perf(sync): lra cache address encoding of output scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
nitsujlangston committed Oct 30, 2018
1 parent f3dad47 commit 6892f43
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
44 changes: 37 additions & 7 deletions packages/bitcore-node/src/models/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,30 @@ import * as lodash from 'lodash';

const Chain = require('../chain');

const addressCacheStore = {
list: [] as Array<any>,
map: new Map() as Map<any, string>
};

function getCachedAddress(chain: string, network: string, script: string){
const result = addressCacheStore.map.get(`${chain}:${network}:${script}`);
if (result) {
addressCacheStore.list.push(`${chain}:${network}:${script}`);
if (addressCacheStore.list.length > 10000) {
addressCacheStore.map.delete(addressCacheStore.list.shift());
}
}
return result;
}

function setCachedAddress(chain: string, network: string, script: any, address: any) {
addressCacheStore.map.set(`${chain}:${network}:${script}`, address);
addressCacheStore.list.push(`${chain}:${network}:${script}`);
if (addressCacheStore.list.length > 10000) {
addressCacheStore.map.delete(addressCacheStore.list.shift());
}
}

export type ITransaction = {
txid: string;
chain: string;
Expand Down Expand Up @@ -216,12 +240,18 @@ export class Transaction extends BaseModel<ITransaction> {
continue;
}
let address = '';
let scriptBuffer = output.script && output.script.toBuffer();
if (scriptBuffer) {
address = output.script.toAddress(network).toString(true);
if (address === 'false' && output.script.classify() === 'Pay to public key') {
let hash = Chain[chain].lib.crypto.Hash.sha256ripemd160(output.script.chunks[0].buf);
address = Chain[chain].lib.Address(hash, network).toString(true);
if (output.script) {
const hexScript = output.script.toHex();
const cachedAddress = getCachedAddress(chain, network, hexScript);
if (cachedAddress) {
address = cachedAddress;
} else {
address = output.script.toAddress(network).toString(true);
if (address === 'false' && output.script.classify() === 'Pay to public key') {
let hash = Chain[chain].lib.crypto.Hash.sha256ripemd160(output.script.chunks[0].buf);
address = Chain[chain].lib.Address(hash, network).toString(true);
}
setCachedAddress(chain, network, output.script.toHex(), address);
}
}

Expand All @@ -242,7 +272,7 @@ export class Transaction extends BaseModel<ITransaction> {
mintHeight: height,
coinbase: isCoinbase,
value: output.satoshis,
script: scriptBuffer,
script: output.script.toBuffer(),
spentHeight: SpentHeightIndicators.unspent,
wallets: []
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export type BitcoinAddress = {
};
export type BitcoinScript = {
toBuffer: () => Buffer;
toHex: () => string;
classify: () => string;
chunks: Array<{ buf: Buffer }>;
toAddress: (network: string) => BitcoinAddress;
Expand Down

0 comments on commit 6892f43

Please sign in to comment.