1
1
import { JsonRpcProvider } from "@ethersproject/providers" ;
2
2
import { getBridgeConfig , Network } from "./consts/bridgeRoutes" ;
3
- import { getVeaInbox , getVeaOutbox } from "./utils/ethers" ;
3
+ import { getTransactionHandler , getVeaInbox , getVeaOutbox } from "./utils/ethers" ;
4
4
import { getBlockFromEpoch , setEpochRange } from "./utils/epochHandler" ;
5
5
import { getClaimValidator , getClaimer } from "./utils/ethers" ;
6
6
import { defaultEmitter } from "./utils/emitter" ;
@@ -12,6 +12,7 @@ import { getClaim } from "./utils/claim";
12
12
import { MissingEnvError } from "./utils/errors" ;
13
13
import { CheckAndClaimParams } from "./ArbToEth/claimer" ;
14
14
import { ChallengeAndResolveClaimParams } from "./ArbToEth/validator" ;
15
+ import { saveSnapshot , SaveSnapshotParams } from "./utils/snapshot" ;
15
16
16
17
const RPC_BLOCK_LIMIT = 500 ; // RPC_BLOCK_LIMIT is the limit of blocks that can be queried at once
17
18
@@ -31,24 +32,25 @@ export const watch = async (
31
32
const privKey = process . env . PRIVATE_KEY ;
32
33
if ( ! privKey ) throw new MissingEnvError ( "PRIVATE_KEY" ) ;
33
34
const cliCommand = process . argv ;
34
- const path = getBotPath ( { cliCommand } ) ;
35
+ const { path, toSaveSnapshot } = getBotPath ( { cliCommand } ) ;
35
36
const networkConfigs = getNetworkConfig ( ) ;
36
37
emitter . emit ( BotEvents . STARTED , path , networkConfigs [ 0 ] . networks ) ;
37
38
const transactionHandlers : { [ epoch : number ] : any } = { } ;
38
- const toWatch : { [ key : string ] : number [ ] } = { } ;
39
+ const toWatch : { [ key : string ] : { count : number ; epochs : number [ ] } } = { } ;
39
40
while ( ! shutDownSignal . getIsShutdownSignal ( ) ) {
40
41
for ( const networkConfig of networkConfigs ) {
41
- await processNetwork ( path , networkConfig , transactionHandlers , toWatch , emitter ) ;
42
+ await processNetwork ( path , toSaveSnapshot , networkConfig , transactionHandlers , toWatch , emitter ) ;
42
43
}
43
44
await wait ( 1000 * 10 ) ;
44
45
}
45
46
} ;
46
47
47
48
async function processNetwork (
48
49
path : number ,
50
+ toSaveSnapshot : boolean ,
49
51
networkConfig : NetworkConfig ,
50
52
transactionHandlers : { [ epoch : number ] : any } ,
51
- toWatch : { [ key : string ] : number [ ] } ,
53
+ toWatch : { [ key : string ] : { count : number ; epochs : number [ ] } } ,
52
54
emitter : typeof defaultEmitter
53
55
) : Promise < void > {
54
56
const { chainId, networks } = networkConfig ;
@@ -57,27 +59,27 @@ async function processNetwork(
57
59
emitter . emit ( BotEvents . WATCHING , chainId , network ) ;
58
60
const networkKey = `${ chainId } _${ network } ` ;
59
61
if ( ! toWatch [ networkKey ] ) {
60
- toWatch [ networkKey ] = [ ] ;
62
+ toWatch [ networkKey ] = { count : - 1 , epochs : [ ] } ;
61
63
}
62
-
63
64
const veaOutboxProvider = new JsonRpcProvider ( outboxRPC ) ;
64
65
let veaOutboxLatestBlock = await veaOutboxProvider . getBlock ( "latest" ) ;
65
66
66
67
// If the watcher has already started, only check the latest epoch
67
68
if ( network == Network . DEVNET ) {
68
- toWatch [ networkKey ] = [ Math . floor ( veaOutboxLatestBlock . timestamp / routeConfig [ network ] . epochPeriod ) ] ;
69
- } else if ( toWatch [ networkKey ] . length == 0 ) {
69
+ toWatch [ networkKey ] . epochs = [ Math . floor ( veaOutboxLatestBlock . timestamp / routeConfig [ network ] . epochPeriod ) ] ;
70
+ } else if ( toWatch [ networkKey ] . epochs . length == 0 ) {
70
71
const epochRange = setEpochRange ( {
71
72
chainId,
72
73
currentTimestamp : veaOutboxLatestBlock . timestamp ,
73
74
epochPeriod : routeConfig [ network ] . epochPeriod ,
74
75
} ) ;
75
- toWatch [ networkKey ] = epochRange ;
76
+ toWatch [ networkKey ] . epochs = epochRange ;
76
77
}
77
78
78
79
await processEpochsForNetwork ( {
79
80
chainId,
80
81
path,
82
+ toSaveSnapshot,
81
83
networkKey,
82
84
network,
83
85
routeConfig,
@@ -88,30 +90,33 @@ async function processNetwork(
88
90
emitter,
89
91
} ) ;
90
92
const currentLatestBlock = await veaOutboxProvider . getBlock ( "latest" ) ;
91
- const currentLatestEpoch = Math . floor ( currentLatestBlock . timestamp / routeConfig [ network ] . epochPeriod ) ;
93
+ const currentClaimableEpoch = Math . floor ( currentLatestBlock . timestamp / routeConfig [ network ] . epochPeriod ) - 1 ;
94
+
92
95
const toWatchEpochs = toWatch [ networkKey ] ;
93
- const lastEpochInToWatch = toWatchEpochs [ toWatchEpochs . length - 1 ] ;
94
- if ( currentLatestEpoch > lastEpochInToWatch ) {
95
- toWatch [ networkKey ] . push ( currentLatestEpoch ) ;
96
+ const lastEpochInToWatch = toWatchEpochs [ toWatchEpochs . epochs . length - 1 ] ;
97
+ if ( currentClaimableEpoch > lastEpochInToWatch ) {
98
+ toWatch [ networkKey ] . epochs . push ( currentClaimableEpoch ) ;
96
99
}
97
100
}
98
101
}
99
102
100
103
interface ProcessEpochParams {
101
104
chainId : number ;
102
105
path : number ;
106
+ toSaveSnapshot : boolean ;
103
107
networkKey : string ;
104
108
network : Network ;
105
109
routeConfig : any ;
106
110
inboxRPC : string ;
107
111
outboxRPC : string ;
108
- toWatch : { [ key : string ] : number [ ] } ;
112
+ toWatch : { [ key : string ] : { count : number ; epochs : number [ ] } } ;
109
113
transactionHandlers : { [ epoch : number ] : any } ;
110
114
emitter : typeof defaultEmitter ;
111
115
}
112
116
async function processEpochsForNetwork ( {
113
117
chainId,
114
118
path,
119
+ toSaveSnapshot,
115
120
networkKey,
116
121
network,
117
122
routeConfig,
@@ -126,10 +131,39 @@ async function processEpochsForNetwork({
126
131
const veaOutbox = getVeaOutbox ( routeConfig [ network ] . veaOutbox . address , privKey , outboxRPC , chainId , network ) ;
127
132
const veaInboxProvider = new JsonRpcProvider ( inboxRPC ) ;
128
133
const veaOutboxProvider = new JsonRpcProvider ( outboxRPC ) ;
129
- let i = toWatch [ networkKey ] . length - 1 ;
130
- const latestEpoch = toWatch [ networkKey ] [ i ] ;
134
+ let i = toWatch [ networkKey ] . epochs . length - 1 ;
135
+ const latestEpoch = toWatch [ networkKey ] . epochs [ i ] ;
136
+ const currentEpoch = Math . floor ( Date . now ( ) / ( 1000 * routeConfig [ network ] . epochPeriod ) ) ;
137
+ // Checks and saves the snapshot if needed
138
+ if ( toSaveSnapshot ) {
139
+ const TransactionHandler = getTransactionHandler ( chainId , network ) as any ;
140
+ const transactionHandler =
141
+ transactionHandlers [ currentEpoch ] ||
142
+ new TransactionHandler ( {
143
+ network,
144
+ epoch : currentEpoch ,
145
+ veaInbox,
146
+ veaOutbox,
147
+ veaInboxProvider,
148
+ veaOutboxProvider,
149
+ emitter,
150
+ } ) ;
151
+ const { updatedTransactionHandler, latestCount } = await saveSnapshot ( {
152
+ veaInbox,
153
+ network,
154
+ epochPeriod : routeConfig [ network ] . epochPeriod ,
155
+ count : toWatch [ networkKey ] . count ,
156
+ transactionHandler,
157
+ } as SaveSnapshotParams ) ;
158
+ const count = toWatch [ networkKey ] . count ;
159
+ if ( count == - 1 || count != latestCount ) {
160
+ transactionHandlers [ currentEpoch ] = updatedTransactionHandler ;
161
+ toWatch [ networkKey ] . count = latestCount ;
162
+ }
163
+ }
164
+
131
165
while ( i >= 0 ) {
132
- const epoch = toWatch [ networkKey ] [ i ] ;
166
+ const epoch = toWatch [ networkKey ] . epochs [ i ] ;
133
167
const epochBlock = await getBlockFromEpoch ( epoch , routeConfig [ network ] . epochPeriod , veaOutboxProvider ) ;
134
168
const latestBlock = await veaOutboxProvider . getBlock ( "latest" ) ;
135
169
let toBlock : number | string = "latest" ;
@@ -142,6 +176,7 @@ async function processEpochsForNetwork({
142
176
const checkAndChallengeResolve = getClaimValidator ( chainId , network ) ;
143
177
const checkAndClaim = getClaimer ( chainId , network ) ;
144
178
let updatedTransactions ;
179
+
145
180
if ( path > BotPaths . CLAIMER && claim != null ) {
146
181
const checkAndChallengeResolveDeps : ChallengeAndResolveClaimParams = {
147
182
claim,
@@ -177,7 +212,7 @@ async function processEpochsForNetwork({
177
212
transactionHandlers [ epoch ] = updatedTransactions ;
178
213
} else if ( epoch != latestEpoch ) {
179
214
delete transactionHandlers [ epoch ] ;
180
- toWatch [ networkKey ] . splice ( i , 1 ) ;
215
+ toWatch [ networkKey ] . epochs . splice ( i , 1 ) ;
181
216
}
182
217
i -- ;
183
218
}
0 commit comments