Skip to content

Commit

Permalink
refactor(config): Cleanup & permit selective override (#1940)
Browse files Browse the repository at this point in the history
Permit certain config objects (i.e. MAX_BLOCK_LOOK_BACK) to be
selectively overridden, rather than requiring a complete definition. 
This will slightly improve new chain automation.

Also simplify a few instances of config parsing, adding some basic type
validation along the way.
  • Loading branch information
pxrl authored Jan 2, 2025
1 parent 3429663 commit 38e159d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
30 changes: 19 additions & 11 deletions src/common/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ export class CommonConfig {
HUB_CHAIN_ID,
POLLING_DELAY,
MAX_BLOCK_LOOK_BACK,
MAX_TX_WAIT_DURATION,
SEND_TRANSACTIONS,
SPOKE_POOL_CHAINS_OVERRIDE,
ACROSS_BOT_VERSION,
Expand All @@ -44,7 +43,20 @@ export class CommonConfig {
ARWEAVE_GATEWAY,
} = env;

const mergeConfig = <T>(config: T, envVar: string): T => {
const shallowCopy = { ...config };
Object.entries(JSON.parse(envVar ?? "{}")).forEach(([k, v]) => {
assert(
typeof v === typeof shallowCopy[k] || !isDefined(shallowCopy[k]),
`Invalid ${envVar} configuration on key ${k} (${typeof v} != ${typeof shallowCopy[k]})`
);
shallowCopy[k] = v;
});
return shallowCopy;
};

this.version = ACROSS_BOT_VERSION ?? "unknown";
this.hubPoolChainId = Number(HUB_CHAIN_ID ?? CHAIN_IDs.MAINNET);

this.timeToCache = Number(HUB_POOL_TIME_TO_CACHE ?? 60 * 60); // 1 hour by default.
if (Number.isNaN(this.timeToCache) || this.timeToCache < 0) {
Expand All @@ -57,22 +69,18 @@ export class CommonConfig {
this.maxConfigVersion = Number(ACROSS_MAX_CONFIG_VERSION ?? Constants.CONFIG_STORE_VERSION);
assert(!isNaN(this.maxConfigVersion), `Invalid maximum config version: ${this.maxConfigVersion}`);

this.blockRangeEndBlockBuffer = BLOCK_RANGE_END_BLOCK_BUFFER
? JSON.parse(BLOCK_RANGE_END_BLOCK_BUFFER)
: Constants.BUNDLE_END_BLOCK_BUFFERS;
this.blockRangeEndBlockBuffer = mergeConfig(Constants.BUNDLE_END_BLOCK_BUFFERS, BLOCK_RANGE_END_BLOCK_BUFFER);

this.ignoredAddresses = JSON.parse(IGNORED_ADDRESSES ?? "[]").map((address) => ethers.utils.getAddress(address));

// `maxRelayerLookBack` is how far we fetch events from, modifying the search config's 'fromBlock'
this.maxRelayerLookBack = Number(MAX_RELAYER_DEPOSIT_LOOK_BACK ?? Constants.MAX_RELAYER_DEPOSIT_LOOK_BACK);
this.hubPoolChainId = Number(HUB_CHAIN_ID ?? CHAIN_IDs.MAINNET);
this.pollingDelay = Number(POLLING_DELAY ?? 60);
this.spokePoolChainsOverride = SPOKE_POOL_CHAINS_OVERRIDE ? JSON.parse(SPOKE_POOL_CHAINS_OVERRIDE) : [];
this.maxBlockLookBack = MAX_BLOCK_LOOK_BACK ? JSON.parse(MAX_BLOCK_LOOK_BACK) : {};
if (Object.keys(this.maxBlockLookBack).length === 0) {
this.maxBlockLookBack = Constants.CHAIN_MAX_BLOCK_LOOKBACK;
}
this.maxTxWait = Number(MAX_TX_WAIT_DURATION ?? 180); // 3 minutes
this.spokePoolChainsOverride = JSON.parse(SPOKE_POOL_CHAINS_OVERRIDE ?? "[]");

// Inherit the default eth_getLogs block range config, then sub in any env-based overrides.
this.maxBlockLookBack = mergeConfig(Constants.CHAIN_MAX_BLOCK_LOOKBACK, MAX_BLOCK_LOOK_BACK);

this.sendingTransactionsEnabled = SEND_TRANSACTIONS === "true";

// Load the Arweave gateway from the environment.
Expand Down
8 changes: 2 additions & 6 deletions src/relayer/RelayerConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,8 @@ export class RelayerConfig extends CommonConfig {
this.relayerDestinationChains = JSON.parse(RELAYER_DESTINATION_CHAINS ?? "[]");

// Empty means all tokens.
this.relayerTokens = RELAYER_TOKENS
? JSON.parse(RELAYER_TOKENS).map((token) => ethers.utils.getAddress(token))
: [];
this.slowDepositors = SLOW_DEPOSITORS
? JSON.parse(SLOW_DEPOSITORS).map((depositor) => ethers.utils.getAddress(depositor))
: [];
this.relayerTokens = JSON.parse(RELAYER_TOKENS ?? "[]").map((token) => ethers.utils.getAddress(token));
this.slowDepositors = JSON.parse(SLOW_DEPOSITORS ?? "[]").map((depositor) => ethers.utils.getAddress(depositor));

this.minRelayerFeePct = toBNWei(MIN_RELAYER_FEE_PCT || Constants.RELAYER_MIN_FEE_PCT);

Expand Down

0 comments on commit 38e159d

Please sign in to comment.