@@ -23,7 +23,7 @@ import { BusyError, DryRunStore, FileStore, type Store } from './store.ts';
2323import type { Email } from './email.ts' ;
2424import type { Network , Telegram } from './telegram.ts' ;
2525import { ConfigError , type FetchFn , type Log , errText } from './util.ts' ;
26- import { Watcher , parseState } from './watcher.ts' ;
26+ import { type ChainNow , type ProxyKinds , Watcher , parseState } from './watcher.ts' ;
2727
2828type Known = {
2929 delay : string ;
@@ -117,6 +117,8 @@ export class AllWatcher {
117117 calls : PollCalls = { scan : { } , vaults : { } , read : 0 } ;
118118 private opts : AllOptions ;
119119 private probe : Watcher ;
120+ /** Shared by every read of this network, across polls: proxy code does not change. */
121+ private proxyKinds : ProxyKinds = new Map ( ) ;
120122 private store : Store ;
121123 private clock : ( ) => number ;
122124
@@ -126,7 +128,7 @@ export class AllWatcher {
126128 this . clock = opts . clock ?? Date . now ;
127129 this . store = opts . store ?? ( cfg . dryRun ? new DryRunStore ( new FileStore ( cfg . stateDir ) ) : new FileStore ( cfg . stateDir ) ) ;
128130 // Reads the chain only. It never loads or saves a state.
129- this . probe = new Watcher ( { ...cfg , dryRun : true } , { fetch : opts . fetch , log : opts . log } ) ;
131+ this . probe = new Watcher ( { ...cfg , dryRun : true } , { fetch : opts . fetch , log : opts . log , proxyKinds : this . proxyKinds } ) ;
130132 this . log = this . probe . log ;
131133 }
132134
@@ -287,24 +289,24 @@ export class AllWatcher {
287289 redact ( msg : string ) : string { return this . probe . redact ( msg ) ; }
288290
289291 /** The watcher of one vault: the same code as for a single vault. */
290- private watcherOf ( vault : string , known : Known , rpcCounts ?: RpcCounts ) : Watcher {
292+ private watcherOf ( vault : string , known : Known , rpcCounts ?: RpcCounts , chain ?: ChainNow ) : Watcher {
291293 return new Watcher ( { ...this . cfg , chronobox : vault , delay : known . delay } , {
292294 fetch : this . opts . fetch , telegram : this . opts . telegram , email : this . opts . email , store : this . store , key : vault , where : `The state of ${ vault } ` ,
293- log : ( m ) => this . log ( `${ vault . slice ( 0 , 10 ) } : ${ m } ` ) , rpcCounts,
295+ log : ( m ) => this . log ( `${ vault . slice ( 0 , 10 ) } : ${ m } ` ) , rpcCounts, chain , proxyKinds : this . proxyKinds ,
294296 } ) ;
295297 }
296298
297299 /**
298300 * Reads one vault and sends its alerts. False if the chain could not be read. Null if a question of the bot reads
299301 * this vault right now: that read sends its alerts, and this poll goes on with the next vault.
300302 */
301- private async look ( vault : string , known : Known , calls : PollCalls ) : Promise < boolean | null > {
303+ private async look ( vault : string , known : Known , calls : PollCalls , chain : ChainNow | undefined ) : Promise < boolean | null > {
302304 // Saved before the read: if the function is killed during it, this vault goes to the back of the line, and it
303305 // cannot block every poll after this one.
304306 known . lookedAt = this . clock ( ) ;
305307 delete known . waitingSince ;
306308 await this . save ( ) ;
307- const w = this . watcherOf ( vault , known , calls . vaults ) ;
309+ const w = this . watcherOf ( vault , known , calls . vaults , chain ) ;
308310 let ok : boolean ;
309311 try {
310312 ok = await this . store . lock ( `vault:${ vault } ` , ( ) => { calls . read ++ ; return w . poll ( ) ; } , { waitMs : 0 } ) ;
@@ -324,7 +326,7 @@ export class AllWatcher {
324326 * and the queue is saved with the new lastBlock before any of them is checked: a search is never done twice, also
325327 * when the function is killed afterwards. Returns the vaults to look at now.
326328 */
327- private async scan ( probe : Watcher ) : Promise < { chainId : number ; now : number ; touched : Set < string > } > {
329+ private async scan ( probe : Watcher ) : Promise < { chain : ChainNow ; now : number ; touched : Set < string > } > {
328330 const s = this . state ;
329331 const rpc = probe . rpc ;
330332 const chainId = Number ( await rpc . call ( 'eth_chainId' ) ) ;
@@ -377,7 +379,7 @@ export class AllWatcher {
377379 s . chainId = chainId ;
378380 s . lastBlock = to ;
379381 this . announce ( ) ;
380- return { chainId, now, touched } ;
382+ return { chain : { chainId, head , timestamp : BigInt ( block . timestamp ) } , now, touched } ;
381383 }
382384
383385 /**
@@ -441,10 +443,13 @@ export class AllWatcher {
441443 // Counted apart from the bot's questions, which use this.probe at the same time.
442444 const calls : PollCalls = { scan : { } , vaults : { } , read : 0 } ;
443445 this . calls = calls ;
444- const probe = new Watcher ( { ...this . cfg , dryRun : true } , { fetch : this . opts . fetch , log : this . opts . log , rpcCounts : calls . scan } ) ;
446+ const probe = new Watcher ( { ...this . cfg , dryRun : true } , { fetch : this . opts . fetch , log : this . opts . log , rpcCounts : calls . scan , proxyKinds : this . proxyKinds } ) ;
447+ // What the search read of the chain. The reads of the vaults use it: one head for the whole poll.
448+ let chain : ChainNow | undefined ;
445449 try {
446450 const r = await this . scan ( probe ) ;
447451 now = r . now ;
452+ chain = r . chain ;
448453 for ( const vault of r . touched ) s . vaults [ vault ] . active = true ;
449454 await this . save ( ) ;
450455 } catch ( e ) {
@@ -485,7 +490,7 @@ export class AllWatcher {
485490 // answers anything. Its readers are told (Watcher), and it waits longer each time, while the chain can be read.
486491 let read : boolean | null = false ;
487492 try {
488- read = await this . look ( vault , known , calls ) ;
493+ read = await this . look ( vault , known , calls , chain ) ;
489494 } catch ( e ) {
490495 this . log ( `${ vault } : ${ errText ( e ) } ` ) ;
491496 }
0 commit comments