@@ -48,29 +48,48 @@ const database = drizzle(new Database(env.SQLITE_FILENAME));
48
48
49
49
let startBlock = env . START_BLOCK ;
50
50
51
- // Resume from latest block stored in DB. This will throw if the DB doesn't exist yet, so we wrap in a try/catch and ignore the error.
52
- try {
53
- const currentChainStates = database . select ( ) . from ( chainState ) . where ( eq ( chainState . chainId , chainId ) ) . all ( ) ;
54
- // TODO: replace this type workaround with `noUncheckedIndexedAccess: true` when we can fix all the issues related (https://github.com/latticexyz/mud/issues/1212)
55
- const currentChainState : ( typeof currentChainStates ) [ number ] | undefined = currentChainStates [ 0 ] ;
56
-
57
- if ( currentChainState != null ) {
58
- if ( currentChainState . schemaVersion != schemaVersion ) {
59
- console . log (
60
- "schema version changed from" ,
61
- currentChainState . schemaVersion ,
62
- "to" ,
63
- schemaVersion ,
64
- "recreating database" ,
65
- ) ;
66
- fs . truncateSync ( env . SQLITE_FILENAME ) ;
67
- } else if ( currentChainState . lastUpdatedBlockNumber != null ) {
68
- console . log ( "resuming from block number" , currentChainState . lastUpdatedBlockNumber + 1n ) ;
69
- startBlock = currentChainState . lastUpdatedBlockNumber + 1n ;
51
+ async function getCurrentChainState ( ) : Promise <
52
+ | {
53
+ schemaVersion: number ;
54
+ chainId: number ;
55
+ lastUpdatedBlockNumber: bigint | null ;
56
+ lastError: string | null ;
70
57
}
58
+ | undefined
59
+ > {
60
+ // This will throw if the DB doesn't exist yet, so we wrap in a try/catch and ignore the error.
61
+ try {
62
+ const currentChainStates = database . select ( ) . from ( chainState ) . where ( eq ( chainState . chainId , chainId ) ) . all ( ) ;
63
+ // TODO: replace this type workaround with `noUncheckedIndexedAccess: true` when we can fix all the issues related (https://github.com/latticexyz/mud/issues/1212)
64
+ const currentChainState : ( typeof currentChainStates ) [ number ] | undefined = currentChainStates [ 0 ] ;
65
+ return currentChainState ;
66
+ } catch ( error ) {
67
+ // ignore errors, this is optional
68
+ }
69
+ }
70
+
71
+ async function getLatestStoredBlockNumber ( ) : Promise < bigint | undefined > {
72
+ const currentChainState = await getCurrentChainState ( ) ;
73
+ return currentChainState ?. lastUpdatedBlockNumber ?? undefined ;
74
+ }
75
+
76
+ const currentChainState = await getCurrentChainState ( ) ;
77
+ if ( currentChainState ) {
78
+ // Reset the db if the version changed
79
+ if ( currentChainState . schemaVersion != schemaVersion ) {
80
+ console . log (
81
+ "schema version changed from" ,
82
+ currentChainState . schemaVersion ,
83
+ "to" ,
84
+ schemaVersion ,
85
+ "recreating database" ,
86
+ ) ;
87
+ fs . truncateSync ( env . SQLITE_FILENAME ) ;
88
+ } else if ( currentChainState . lastUpdatedBlockNumber != null ) {
89
+ // Resume from latest block stored in DB. This will throw if the DB doesn't exist yet, so we wrap in a try/catch and ignore the error.
90
+ console . log ( "resuming from block number" , currentChainState . lastUpdatedBlockNumber + 1n ) ;
91
+ startBlock = currentChainState . lastUpdatedBlockNumber + 1n ;
71
92
}
72
- } catch ( error ) {
73
- // ignore errors, this is optional
74
93
}
75
94
76
95
const { latestBlockNumber$, storedBlockLogs$ } = await syncToSqlite ( {
@@ -112,6 +131,8 @@ server.use(
112
131
metrics ( {
113
132
isHealthy : ( ) => true ,
114
133
isReady : ( ) => isCaughtUp ,
134
+ getLatestStoredBlockNumber,
135
+ followBlockTag : env . FOLLOW_BLOCK_TAG ,
115
136
} ) ,
116
137
) ;
117
138
server . use ( helloWorld ( ) ) ;
0 commit comments