-
Couldn't load subscription status.
- Fork 7
Fix/handle sent snapshots #435
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from all commits
4b1d83d
3440f8c
4adf134
df5821b
cb0b6e6
deeb431
7d472b2
6ddc49f
9e9db78
1ecb72e
f794917
f61dccf
4a35176
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,21 @@ | ||
| import { Network } from "../consts/bridgeRoutes"; | ||
| import { ZeroHash } from "ethers"; | ||
| import { Network, snapshotSavingPeriod } from "../consts/bridgeRoutes"; | ||
| import { getLastMessageSaved } from "../utils/graphQueries"; | ||
| import { BotEvents } from "../utils/botEvents"; | ||
| import { defaultEmitter } from "../utils/emitter"; | ||
|
|
||
| interface SnapshotCheckParams { | ||
| epochPeriod: number; | ||
| chainId: number; | ||
| veaInbox: any; | ||
| veaOutbox: any; | ||
| count: number; | ||
| fetchLastSavedMessage?: typeof getLastMessageSaved; | ||
| } | ||
|
|
||
| export interface SaveSnapshotParams { | ||
| chainId: number; | ||
| veaInbox: any; | ||
| veaOutbox: any; | ||
| network: Network; | ||
| epochPeriod: number; | ||
| count: number; | ||
|
|
@@ -25,6 +28,7 @@ export interface SaveSnapshotParams { | |
| export const saveSnapshot = async ({ | ||
| chainId, | ||
| veaInbox, | ||
| veaOutbox, | ||
| network, | ||
| epochPeriod, | ||
| count, | ||
|
|
@@ -33,18 +37,18 @@ export const saveSnapshot = async ({ | |
| toSaveSnapshot = isSnapshotNeeded, | ||
| now = Math.floor(Date.now() / 1000), | ||
| }: SaveSnapshotParams): Promise<any> => { | ||
| if (network != Network.DEVNET) { | ||
| const timeElapsed = now % epochPeriod; | ||
| const timeLeftForEpoch = epochPeriod - timeElapsed; | ||
| // Saving snapshots in last 10 minutes of the epoch on testnet | ||
| if (timeLeftForEpoch > 600) { | ||
| emitter.emit(BotEvents.SNAPSHOT_WAITING, timeLeftForEpoch); | ||
| return { transactionHandler, latestCount: count }; | ||
| } | ||
| const timeElapsed = now % epochPeriod; | ||
| const timeLeftForEpoch = epochPeriod - timeElapsed; | ||
|
|
||
| if (timeLeftForEpoch > snapshotSavingPeriod[network]) { | ||
| emitter.emit(BotEvents.SNAPSHOT_WAITING, timeLeftForEpoch); | ||
| return { transactionHandler, latestCount: count }; | ||
| } | ||
| const { snapshotNeeded, latestCount } = await toSaveSnapshot({ | ||
| epochPeriod, | ||
| chainId, | ||
| veaInbox, | ||
| veaOutbox, | ||
| count, | ||
| }); | ||
| if (!snapshotNeeded) return { transactionHandler, latestCount }; | ||
|
|
@@ -53,28 +57,42 @@ export const saveSnapshot = async ({ | |
| }; | ||
|
|
||
| export const isSnapshotNeeded = async ({ | ||
| epochPeriod, | ||
| chainId, | ||
| veaInbox, | ||
| veaOutbox, | ||
| count, | ||
| fetchLastSavedMessage = getLastMessageSaved, | ||
| }: SnapshotCheckParams): Promise<{ snapshotNeeded: boolean; latestCount: number }> => { | ||
| const currentCount = Number(await veaInbox.count()); | ||
|
|
||
| if (count == currentCount) { | ||
| return { snapshotNeeded: false, latestCount: currentCount }; | ||
| } | ||
| let lastSavedCount: number; | ||
| let lastSavedSnapshot: string; | ||
| try { | ||
| const saveSnapshotLogs = await veaInbox.queryFilter(veaInbox.filters.SnapshotSaved()); | ||
| lastSavedCount = Number(saveSnapshotLogs[saveSnapshotLogs.length - 1].args[2]); | ||
| lastSavedSnapshot = saveSnapshotLogs[saveSnapshotLogs.length - 1].args[1]; | ||
| } catch { | ||
| const veaInboxAddress = await veaInbox.getAddress(); | ||
| const lastSavedMessageId = await fetchLastSavedMessage(veaInboxAddress, chainId); | ||
| const { id: lastSavedMessageId, stateRoot: lastSavedStateRoot } = await fetchLastSavedMessage( | ||
| veaInboxAddress, | ||
| chainId | ||
| ); | ||
| const messageIndex = extractMessageIndex(lastSavedMessageId); | ||
| lastSavedSnapshot = lastSavedStateRoot; | ||
| // adding 1 to the message index to get the last saved count | ||
| lastSavedCount = messageIndex + 1; | ||
| lastSavedCount = messageIndex; | ||
| } | ||
|
Comment on lines
+73
to
88
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chainHandle empty Graph response and align comment with logic.
Apply this diff: } catch {
const veaInboxAddress = await veaInbox.getAddress();
- const { id: lastSavedMessageId, stateRoot: lastSavedStateRoot } = await fetchLastSavedMessage(
- veaInboxAddress,
- chainId
- );
- const messageIndex = extractMessageIndex(lastSavedMessageId);
- lastSavedSnapshot = lastSavedStateRoot;
- // adding 1 to the message index to get the last saved count
- lastSavedCount = messageIndex;
+ const lastSaved = await fetchLastSavedMessage(veaInboxAddress, chainId);
+ if (!lastSaved) {
+ lastSavedSnapshot = ZeroHash;
+ lastSavedCount = 0;
+ } else {
+ const messageIndex = extractMessageIndex(lastSaved.id);
+ lastSavedSnapshot = lastSaved.stateRoot;
+ // If the index is zero-based, keep as-is; otherwise, add +1.
+ lastSavedCount = messageIndex; // update comment accordingly if needed
+ }
}Confirm whether the graph’s message index is zero- or one-based so we can fix the comment or add I need to verify the concerns raised in the review comment. Let me explore the codebase to understand the context. 🏁 Script executed: #!/bin/bash
# Find and display the snapshot.ts file content around the target lines
fd snapshot.ts --type f | head -5Length of output: 37 🏁 Script executed: #!/bin/bash
# Look for fetchLastSavedMessage function definition
rg -n "fetchLastSavedMessage" --type ts -A 10 -B 2Length of output: 12707 🏁 Script executed: #!/bin/bash
# Search for ZeroHash constant
rg -n "ZeroHash" --type ts -B 2 -A 2Length of output: 5106 🏁 Script executed: #!/bin/bash
# Search for tests related to snapshot
fd -e test.ts -e spec.ts | xargs grep -l "snapshot" 2>/dev/null | head -3Length of output: 128 🏁 Script executed: #!/bin/bash
# Check for Graph-related imports and message index usage
rg -n "messageIndex\|message.*index" --type ts -C 3Length of output: 0 🏁 Script executed: #!/bin/bash
# Find getLastMessageSaved implementation
rg -n "getLastMessageSaved" --type ts -A 15 -B 2Length of output: 5164 🏁 Script executed: #!/bin/bash
# Find extractMessageIndex implementation
rg -n "extractMessageIndex" --type ts -A 10 -B 2Length of output: 2109 🏁 Script executed: #!/bin/bash
# Check graphQueries file
fd graphQueries --type fLength of output: 39 Fix unsafe destructuring and misleading comment in catch block. The code at lines 80-88 has two issues:
Apply the suggested diff to add a null check and provide safe defaults (using the already-imported |
||
| const epochNow = Math.floor(Date.now() / (1000 * epochPeriod)); | ||
| const currentSnapshot = await veaInbox.snapshots(epochNow); | ||
| const currentStateRoot = await veaOutbox.stateRoot(); | ||
| if (currentCount > lastSavedCount) { | ||
| return { snapshotNeeded: true, latestCount: currentCount }; | ||
| } else if (currentSnapshot == ZeroHash && lastSavedSnapshot != currentStateRoot) { | ||
| return { snapshotNeeded: true, latestCount: currentCount }; | ||
| } | ||
| return { snapshotNeeded: false, latestCount: currentCount }; | ||
| }; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
Remove it.only to avoid running a single test only.
This will cripple the test suite.
Run this to detect any lingering “only” in tests:
🏁 Script executed:
Length of output: 132
Remove
it.only()from test at line 117 to restore full test suite execution.The
it.only()prevents other tests from running and will break the test suite. Change line 117 fromit.only(...)toit(...).📝 Committable suggestion
🤖 Prompt for AI Agents