@@ -15,29 +15,42 @@ import { initialize as initializeEmitter } from "./utils/logger";
1515import { BotEvents } from "./utils/botEvents" ;
1616import { getEpochPeriod , Network } from "./consts/bridgeRoutes" ;
1717import { runHashiExecutor } from "./utils/hashi" ;
18+ import { sendHeartbeat } from "./utils/heartbeat" ;
1819
1920interface RelayerConfig {
2021 networkConfigs : RelayerNetworkConfig [ ] ;
2122 shutdownManager : ShutdownManager ;
2223 emitter : EventEmitter ;
2324}
2425
26+ const HASHI_CYCLE_TIME_MS = 5 * 60 * 1000 ; // 5 minutes
27+
2528/**
2629 * Start the relayer
2730 * @param config.networkConfigs The network configurations retrieved from the env.
2831 * @param config.shutdownManager The shutdown manager
2932 * @param config.emitter The event emitter
3033 */
3134export async function start ( { networkConfigs, shutdownManager, emitter } : RelayerConfig ) {
35+ const HEARTBEAT_URL = process . env . HEARTBEAT_URL ;
36+ await sendHeartbeat ( "started" , HEARTBEAT_URL ) ;
3237 initializeEmitter ( emitter ) ;
33- let delayAmount = 7200 * 1000 ; // 2 hours in ms
38+ const executeTimes : number [ ] = networkConfigs . map ( ( ) => 0 ) ;
3439 while ( ! shutdownManager . getIsShuttingDown ( ) ) {
35- for ( const networkConfig of networkConfigs ) {
36- delayAmount = await processNetworkConfig ( networkConfig , shutdownManager , emitter , delayAmount ) ;
40+ let executeTime : number = HASHI_CYCLE_TIME_MS + Date . now ( ) ;
41+ await sendHeartbeat ( "running" , HEARTBEAT_URL ) ;
42+ for ( let i = 0 ; i < networkConfigs . length ; i ++ ) {
43+ if ( executeTimes [ i ] > Date . now ( ) ) {
44+ continue ;
45+ }
46+ executeTimes [ i ] = await processNetworkConfig ( networkConfigs [ i ] , shutdownManager , emitter , executeTimes [ i ] ) ;
47+ executeTime = Math . min ( executeTime , executeTimes [ i ] ) ;
3748 }
38- emitter . emit ( BotEvents . WAITING , delayAmount ) ;
39- await delay ( delayAmount ) ;
49+ const delayMs = executeTime - Date . now ( ) ;
50+ emitter . emit ( BotEvents . WAITING , delayMs ) ;
51+ await delay ( delayMs ) ;
4052 }
53+ await sendHeartbeat ( "stopped" , HEARTBEAT_URL ) ;
4154}
4255
4356/**
@@ -54,37 +67,40 @@ async function processNetworkConfig(
5467 emitter : EventEmitter ,
5568 currentDelay : number
5669) : Promise < number > {
57- const { chainId, network, senders } = networkConfig ;
58- emitter . emit ( BotEvents . STARTED , chainId , network ) ;
70+ const { chainId, network, senders, sourceChainId } = networkConfig ;
71+ const logNetwork = sourceChainId ? `${ sourceChainId } ->${ chainId } Hashi` : network ;
72+ emitter . emit ( BotEvents . STARTED , chainId , logNetwork ) ;
5973 const maxBatchSize = 10 ; // 10 messages per batch
6074
6175 await setupExitHandlers ( chainId , shutdownManager , network , emitter ) ;
6276
63- let { nonce, hashiNonce } = await initializeNonces ( chainId , network , emitter ) ;
64- if ( nonce == null ) return currentDelay ;
65-
66- const hashiExecutorEnabled = process . env . HASHI_EXECUTOR_ENABLED === "true" ;
67- if ( hashiExecutorEnabled ) {
77+ let { nonce, hashiBlockNumber } = await initializeNonces ( chainId , network , emitter ) ;
78+ if ( sourceChainId ) {
6879 // Execute messages on Hashi
69- hashiNonce = await runHashiExecutor ( { chainId, network, nonce : hashiNonce , emitter } ) ;
80+ hashiBlockNumber = await runHashiExecutor ( {
81+ sourceChainId,
82+ targetChainId : chainId ,
83+ network,
84+ blockNumber : hashiBlockNumber ,
85+ emitter,
86+ } ) ;
87+ await updateStateFile ( chainId , Math . floor ( Date . now ( ) / 1000 ) , nonce , hashiBlockNumber , network , emitter ) ;
88+ return Date . now ( ) + HASHI_CYCLE_TIME_MS ;
7089 }
71-
7290 const toRelayAll = senders [ 0 ] === ethers . ZeroAddress ;
7391 nonce = toRelayAll
7492 ? await relayBatch ( { chainId, network, nonce, maxBatchSize, emitter } )
7593 : await relayAllFrom ( chainId , network , nonce , senders , emitter ) ;
7694
77- if ( nonce == null ) return currentDelay ;
78-
79- await updateStateFile ( chainId , Math . floor ( Date . now ( ) / 1000 ) , nonce , hashiNonce , network , emitter ) ;
95+ await updateStateFile ( chainId , Math . floor ( Date . now ( ) / 1000 ) , nonce , hashiBlockNumber , network , emitter ) ;
8096
8197 if ( network === Network . DEVNET ) {
82- return 1000 * 10 ; // 10 seconds for devnet
98+ return Date . now ( ) + 1000 * 60 * 2 ; // 2 min for devnet
8399 } else {
84100 const currentTS = Math . floor ( Date . now ( ) / 1000 ) ;
85101 const epochPeriod = getEpochPeriod ( chainId ) ;
86102 const timeLeft = ( epochPeriod - ( Math . floor ( currentTS / 1000 ) % epochPeriod ) ) * 1000 + 100 * 1000 ;
87- return Math . min ( currentDelay , timeLeft ) ;
103+ return Date . now ( ) + timeLeft ;
88104 }
89105}
90106
@@ -97,6 +113,5 @@ if (require.main === module) {
97113 shutdownManager,
98114 emitter,
99115 } ;
100-
101116 start ( testnetRelayerConfig ) ;
102117}
0 commit comments