Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion watcher/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ How it finds the Chronoboxes (on Vercel too):
- It reads logs only from contracts it chose: the factory, and the Delays of the Chronoboxes it knows. Anybody can deploy contracts that emit any event, so a search over every contract would let anybody fill a poll. That is why an older Chronobox is not found when it asks for something. Set `START_BLOCK` to the block of the first Chronobox to find them all from the factory.
- A new Delay waits in a queue until it is checked. The queue is saved together with the search, before any check, so a search is never done twice. Each poll checks a batch of 8 at least, and new Delays get a quarter of its time at most, so a flood of Delays from the factory cannot take the time of the Chronoboxes that are known. The queue holds 1000 at most; more is dropped and logged. A Chronobox among them is still found when somebody presses Start for it.
- It accepts an address only if it is a Safe whose single owner is the burn address and whose modules are that Delay, or that Delay and a Roles modifier.
- A Chronobox where nothing waits cannot change until somebody asks for something. So the watcher reads a Chronobox only when something waits there, when something new was asked for, or when its "All quiet" message is due and a chat follows it. One log request per poll (per 100 Chronoboxes) covers all quiet ones.
- A Chronobox changes only through its contracts, and they log each change. So each poll reads the logs of all of them at once: its Safe, its Delay, its Recovery Delay, its cancel contract and its Council Safe (one request per poll for every 100 addresses). The watcher reads a Chronobox only when one of them logged something, when an alert that only time brings is due (a wait that ends, a request that ends soon or is void, a reminder before a recovery can be carried out), when its "All quiet" message is due and a chat follows it, and once an hour while something waits. That hourly read sees what comes without a log: the Delay removes void requests in `skipExpired()` without one, so "Removed" can come up to an hour late. A Chronobox that is new, has alerts left to send, or could not be read is read in each poll. When the search is behind the head (after an outage), every Chronobox where something waits is read in each poll, as if each of them had logged something.
- The first start begins at the newest block. Set `START_BLOCK` to search from an older block.

How a person subscribes (on Vercel too):
Expand Down
76 changes: 55 additions & 21 deletions watcher/src/all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// every question of the bot reads it again under the lock "all", because on Vercel a cron run and the webhook
// work at the same time, in different places.
import {
MASTER_COPY, MODULE_PROXY_FACTORY, RevertError, TOPIC_DISABLED_MODULE, TOPIC_ENABLED_MODULE, TOPIC_MODULE_PROXY_CREATION, TOPIC_TRANSACTION_ADDED,
MASTER_COPY, MODULE_PROXY_FACTORY, RevertError, TOPIC_MODULE_PROXY_CREATION,
type RpcCounts, callsByMethod, encAddr, hex, strip, toAddr, totalCalls,
} from './chain.ts';
import { predictRecoveryDelay } from './guardian.ts';
Expand All @@ -23,11 +23,18 @@ import { BusyError, DryRunStore, FileStore, type Store } from './store.ts';
import type { Email } from './email.ts';
import type { Network, Telegram } from './telegram.ts';
import { ConfigError, type FetchFn, type Log, errText } from './util.ts';
import { type ChainNow, type ProxyKinds, Watcher, parseState } from './watcher.ts';
import { type ChainNow, type ProxyKinds, Watcher, nextAlertAt, parseState, watchedBy } from './watcher.ts';

type Known = {
delay: string;
/** Read it in the next poll: it is new, a log of one of its contracts came, an alert waits to be sent, or its last read failed. */
active: boolean;
/** Something waits in its line or in the line of its recovery. */
waiting?: boolean;
/** The chain time (s) of its next alert that only time brings (nextAlertAt). The poll reads it then. */
dueAt?: number;
/** The contracts whose logs mean it changed, besides its Delay and Recovery Delay (watchedBy). From its last read. */
watch?: string[];
heartbeatAt?: number;
/** When the last read of this vault began (ms). The poll reads the vaults that waited longest first. */
lookedAt?: number;
Expand Down Expand Up @@ -58,8 +65,13 @@ const CHECK_TRIES = 3;
/** A vault whose read fails while the chain can be read waits 1, 2, 4 ... minutes before the next read, an hour at most. */
const RETRY_FIRST_MS = 60_000;
const RETRY_MAX_MS = 3_600_000;
/** Delays per eth_getLogs request for the requests of known Chronoboxes. */
/** Addresses per eth_getLogs request for the logs of known Chronoboxes. */
const ADDRESSES_PER_QUERY = 100;
/**
* A vault in which something waits is read at least this often, even without a log or an alert due (ms). Some changes
* come without a log: the Delay 1.0.1 removes void requests in skipExpired() without one.
*/
export const RECHECK_MS = 3_600_000;

export type AllOptions = {
fetch?: FetchFn;
Expand Down Expand Up @@ -300,7 +312,7 @@ export class AllWatcher {
* Reads one vault and sends its alerts. False if the chain could not be read. Null if a question of the bot reads
* this vault right now: that read sends its alerts, and this poll goes on with the next vault.
*/
private async look(vault: string, known: Known, calls: PollCalls, chain: ChainNow | undefined): Promise<boolean | null> {
private async look(vault: string, known: Known, calls: PollCalls, chain: ChainNow | undefined, now: number): Promise<boolean | null> {
// Saved before the read: if the function is killed during it, this vault goes to the back of the line, and it
// cannot block every poll after this one.
known.lookedAt = this.clock();
Expand All @@ -315,8 +327,13 @@ export class AllWatcher {
throw e;
}
const ws = w.state;
known.active = Object.keys(ws.items).length > 0 || Object.keys(ws.recoveryItems ?? {}).length > 0 ||
ws.outbox.length > 0 || ws.failCount > 0 || ws.lastBlock === undefined;
// Read again in the next poll only for what a log does not show: an alert that waits to be sent, a failed read.
known.active = ws.outbox.length > 0 || ws.failCount > 0 || ws.lastBlock === undefined;
known.waiting = Object.keys(ws.items).length > 0 || Object.keys(ws.recoveryItems ?? {}).length > 0;
const due = nextAlertAt(ws, BigInt(chain?.timestamp ?? now));
if (due === undefined) delete known.dueAt;
else known.dueAt = Number(due);
if (ws.settings) known.watch = watchedBy(ws);
known.heartbeatAt = ws.lastHeartbeat;
return ok;
}
Expand All @@ -326,7 +343,7 @@ export class AllWatcher {
* and the queue is saved with the new lastBlock before any of them is checked: a search is never done twice, also
* when the function is killed afterwards. Returns the vaults to look at now.
*/
private async scan(probe: Watcher): Promise<{ chain: ChainNow; now: number; touched: Set<string> }> {
private async scan(probe: Watcher): Promise<{ chain: ChainNow; now: number; touched: Set<string>; covered: boolean }> {
const s = this.state;
const rpc = probe.rpc;
const chainId = Number(await rpc.call('eth_chainId'));
Expand All @@ -345,17 +362,24 @@ export class AllWatcher {
// A long way behind (the first start with START_BLOCK, or after an outage): one part per poll.
const to = this.cfg.maxScanBlocks > 0 ? Math.min(head, from + this.cfg.maxScanBlocks - 1) : head;
if (from <= to) {
const byDelay = new Map(Object.entries(s.vaults).map(([vault, k]) => [k.delay, vault]));
// The requests of the Chronoboxes it knows, and a wallet that comes or goes without a request: wallet and
// guardian together, or a recovery. Only their Delays can emit these logs. The Recovery Delay of a vault may
// not be made: then it has no logs.
const watched = new Map(byDelay);
for (const vault of Object.keys(s.vaults)) watched.set(predictRecoveryDelay(vault), vault);
// Every log of the contracts of the Chronoboxes it knows: a request, a wallet that comes or goes, a change of
// a setting, a call of a module of the Safe, a change of the Council Safe. A vault is read when one of its
// contracts logs something (or when an alert of it is due), so these must be all the contracts whose change it
// tells of. The Recovery Delay of a vault may not be made: then it has no logs. Logs that anybody can cause (money
// sent to the Safe) cost a read, never more than one in each poll.
const watched = new Map<string, Set<string>>();
const watch = (address: string, vault: string) => {
const a = address.toLowerCase();
if (!watched.has(a)) watched.set(a, new Set());
watched.get(a)!.add(vault);
};
for (const [vault, k] of Object.entries(s.vaults)) {
for (const a of [k.delay, predictRecoveryDelay(vault), ...(k.watch ?? [])]) watch(a, vault);
}
const all = [...watched.keys()];
for (let i = 0; i < all.length; i += ADDRESSES_PER_QUERY) {
for (const l of await probe.getLogs(from, to, [[TOPIC_TRANSACTION_ADDED, TOPIC_ENABLED_MODULE, TOPIC_DISABLED_MODULE]], all.slice(i, i + ADDRESSES_PER_QUERY))) {
const vault = watched.get(String(l.address).toLowerCase());
if (vault) touched.add(vault);
for (const l of await probe.getLogs(from, to, [], all.slice(i, i + ADDRESSES_PER_QUERY))) {
for (const vault of watched.get(String(l.address).toLowerCase()) ?? []) touched.add(vault);
}
}
// New Delays, from the factory. Every Chronobox deploys its Delay there.
Expand All @@ -379,7 +403,7 @@ export class AllWatcher {
s.chainId = chainId;
s.lastBlock = to;
this.announce();
return { chain: { chainId, head, timestamp: BigInt(block.timestamp) }, now, touched };
return { chain: { chainId, head, timestamp: BigInt(block.timestamp) }, now, touched, covered: to === head };
}

/**
Expand Down Expand Up @@ -428,8 +452,11 @@ export class AllWatcher {
}

/**
* One poll. A vault with an empty queue cannot change until it queues something, so only vaults
* with something pending, with a new queued transaction or with a heartbeat due are read.
* One poll. A vault changes only through its contracts, and they log each change. So a vault is read when one of
* them logged something, when an alert that only time brings is due (a wait that ends, a request that ends soon),
* when its heartbeat is due, and at least once in RECHECK_MS while something waits in it. A vault that is new, has
* alerts to send or could not be read is read in each poll. When the search did not reach the head, each vault in
* which something waits is read, as if every one of them had logged something.
* Throws BusyError at once if another poll or a question of the bot holds the lock.
*/
poll(opts: PollOptions = {}): Promise<boolean> {
Expand All @@ -446,10 +473,13 @@ export class AllWatcher {
const probe = new Watcher({ ...this.cfg, dryRun: true }, { fetch: this.opts.fetch, log: this.opts.log, rpcCounts: calls.scan, proxyKinds: this.proxyKinds });
// What the search read of the chain. The reads of the vaults use it: one head for the whole poll.
let chain: ChainNow | undefined;
// The search read the logs up to the head: a vault without a log and without an alert due has not changed.
let covered = false;
try {
const r = await this.scan(probe);
now = r.now;
chain = r.chain;
covered = r.covered;
for (const vault of r.touched) s.vaults[vault].active = true;
await this.save();
} catch (e) {
Expand All @@ -471,7 +501,11 @@ export class AllWatcher {
// An "all quiet" message makes sense only where somebody can hear it. Email gets none, so an address is no reason.
const heard = Boolean(this.cfg.webhookUrl || this.cfg.dryRun) || (this.opts.telegram?.subscribers(chainId, vault).length ?? 0) > 0;
if (known.retryAt !== undefined && known.retryAt > this.clock()) return false;
return known.active || (heard && beat > 0 && now - (known.heartbeatAt ?? 0) >= beat);
return known.active ||
(heard && beat > 0 && now - (known.heartbeatAt ?? 0) >= beat) ||
(known.dueAt !== undefined && now >= known.dueAt) ||
// Without a search up to the head, a vault in which something waits is read in each poll, as before.
(Boolean(known.waiting) && (!covered || this.clock() - (known.lookedAt ?? 0) >= RECHECK_MS));
});
// The vaults that waited longest first. A poll that runs out of time goes on there the next time, so vaults
// that are always busy cannot keep the others from being read.
Expand All @@ -490,7 +524,7 @@ export class AllWatcher {
// answers anything. Its readers are told (Watcher), and it waits longer each time, while the chain can be read.
let read: boolean | null = false;
try {
read = await this.look(vault, known, calls, chain);
read = await this.look(vault, known, calls, chain, now);
} catch (e) {
this.log(`${vault}: ${errText(e)}`);
}
Expand Down
42 changes: 42 additions & 0 deletions watcher/src/watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,48 @@ function remindersDue(readyAt: bigint, now: bigint): number {
return RECOVERY_REMINDERS.filter((lead) => now >= readyAt - lead).length;
}

/**
* The earliest chain time (s) at which a read of this state can send an alert that only time brings: a wait that
* ends, a request or a recovery that ends soon or is void, a reminder before a recovery can be carried out. Undefined
* when none can come. Each alert of that kind in observe() has its time here. A time too early costs one read; a time
* too late would make an alert late, so when in doubt it is earlier.
*/
export function nextAlertAt(s: State, now: bigint): bigint | undefined {
const times: bigint[] = [];
const line = (items: Record<string, Item> | undefined, cooldown: bigint, expiration: bigint, reminders: boolean) => {
for (const it of Object.values(items ?? {})) {
const { readyAt, expiresAt } = itemStatus(BigInt(it.createdAt), cooldown, expiration, now);
if (!it.sentReady) times.push(readyAt);
if (expiresAt !== null && !it.sentExpiring) {
const soon = expiresAt - expiryLead(expiration);
times.push(soon > readyAt ? soon : readyAt);
}
if (expiresAt !== null && !it.sentExpired) times.push(expiresAt + 1n);
// A reminder is sent only while the step waits. Once the wait is over, the reminders that were not sent never come.
if (reminders && now < readyAt) RECOVERY_REMINDERS.forEach((lead, i) => { if ((it.remindersSent ?? 0) <= i) times.push(readyAt - lead); });
}
};
const x = s.settings;
if (!x) return undefined;
line(s.items, BigInt(x.cooldown), BigInt(x.expiration), false);
if (x.recovery) line(s.recoveryItems, BigInt(x.recovery.cooldown), BigInt(x.recovery.expiration), true);
return times.length ? times.reduce((a, b) => (b < a ? b : a)) : undefined;
}

/**
* The contracts whose logs show a change of this vault, besides its Delay and its Recovery Delay: the Safe of the
* vault (every call of a module, and every change of its owners, modules and guard), its cancel contract, the Council
* Safe, and the other addresses that can cancel (a Council Safe that is made later is one of them before it is made).
* Empty before the vault was read to the end.
*/
export function watchedBy(s: State): string[] {
const x = s.settings;
if (!x || !s.vault) return [];
const wallets = new Set(x.controllers.map((a) => a.toLowerCase()));
const all = [s.vault, s.delay, x.recovery?.delay, x.roles, x.council, ...(x.cancellers ?? []).filter((a) => !wallets.has(a.toLowerCase()))];
return [...new Set(all.filter((a): a is string => Boolean(a)).map((a) => a.toLowerCase()))];
}

export function emptyState(): State {
return { version: 1, items: {}, outbox: [], failCount: 0, problemOpen: false, seq: 0 };
}
Expand Down
Loading
Loading