Skip to content

Commit ab901f7

Browse files
committed
Limit the number of txs per address
Avoid requiring too much memory by syncing very large history.
1 parent 3089793 commit ab901f7

2 files changed

Lines changed: 13 additions & 2 deletions

File tree

constants/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,3 +291,6 @@ export const PRICES_CONNECTION_BATCH_SIZE = 1_000
291291
export const PRICES_CONNECTION_TIMEOUT = 30_000
292292

293293
export const CLIENT_PAYMENT_EXPIRATION_TIME = (7) * (24 * 60 * 60 * 1000) // (number of days) * (24 * 60 * 60 * 1000)
294+
295+
// Enough for eCash IFP when created
296+
export const MAX_TXS_PER_ADDRESS = 250000

services/chronikService.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { BlockInfo, ChronikClient, ConnectionStrategy, ScriptUtxo, Tx, WsConfig, WsEndpoint, WsMsgClient, WsSubScriptClient } from 'chronik-client'
22
import { encodeCashAddress, decodeCashAddress } from 'ecashaddrjs'
33
import { AddressWithTransaction, BlockchainInfo, TransactionDetails, ProcessedMessages, SubbedAddressesLog, SyncAndSubscriptionReturn, SubscriptionReturn, SimpleBlockInfo } from 'types/chronikTypes'
4-
import { CHRONIK_MESSAGE_CACHE_DELAY, RESPONSE_MESSAGES, XEC_TIMESTAMP_THRESHOLD, XEC_NETWORK_ID, BCH_NETWORK_ID, BCH_TIMESTAMP_THRESHOLD, CHRONIK_FETCH_N_TXS_PER_PAGE, KeyValueT, NETWORK_IDS_FROM_SLUGS, SOCKET_MESSAGES, NETWORK_IDS, NETWORK_TICKERS, MainNetworkSlugsType, MAX_MEMPOOL_TXS_TO_PROCESS_AT_A_TIME, MEMPOOL_PROCESS_DELAY, CHRONIK_INITIALIZATION_DELAY, LATENCY_TEST_CHECK_DELAY, INITIAL_ADDRESS_SYNC_FETCH_CONCURRENTLY, TX_EMIT_BATCH_SIZE, DB_COMMIT_BATCH_SIZE } from 'constants/index'
4+
import { CHRONIK_MESSAGE_CACHE_DELAY, RESPONSE_MESSAGES, XEC_TIMESTAMP_THRESHOLD, XEC_NETWORK_ID, BCH_NETWORK_ID, BCH_TIMESTAMP_THRESHOLD, CHRONIK_FETCH_N_TXS_PER_PAGE, KeyValueT, NETWORK_IDS_FROM_SLUGS, SOCKET_MESSAGES, NETWORK_IDS, NETWORK_TICKERS, MainNetworkSlugsType, MAX_MEMPOOL_TXS_TO_PROCESS_AT_A_TIME, MEMPOOL_PROCESS_DELAY, CHRONIK_INITIALIZATION_DELAY, LATENCY_TEST_CHECK_DELAY, INITIAL_ADDRESS_SYNC_FETCH_CONCURRENTLY, TX_EMIT_BATCH_SIZE, DB_COMMIT_BATCH_SIZE, MAX_TXS_PER_ADDRESS } from 'constants/index'
55
import { productionAddresses } from 'prisma-local/seeds/addresses'
66
import {
77
TransactionWithAddressAndPrices,
@@ -306,7 +306,15 @@ export class ChronikBlockchainClient {
306306

307307
public async getPaginatedTxs (addressString: string, page: number, pageSize: number): Promise<Tx[]> {
308308
const { type, hash160 } = toHash160(addressString)
309-
return (await this.chronik.script(type, hash160).history(page, pageSize)).txs
309+
const txsPage = (await this.chronik.script(type, hash160).history(page, pageSize))
310+
311+
// If there are too many txs, this might be too expensive to sync. Raise an
312+
// error to skip this address.
313+
if (txsPage.numTxs > MAX_TXS_PER_ADDRESS) {
314+
throw new Error(`Address ${addressString} has too many txs to sync (${txsPage.numTxs} > ${MAX_TXS_PER_ADDRESS}).`)
315+
}
316+
317+
return txsPage.txs
310318
}
311319

312320
/*

0 commit comments

Comments
 (0)