Skip to content

Ethereum bridge milestone 1.5: upgrade get_resume_cursor() to avoid full scan on listener start#505

Merged
side2k merged 14 commits intomainfrom
ethereum-bridge-1-5-get-resume-cursor
Mar 5, 2026
Merged

Ethereum bridge milestone 1.5: upgrade get_resume_cursor() to avoid full scan on listener start#505
side2k merged 14 commits intomainfrom
ethereum-bridge-1-5-get-resume-cursor

Conversation

@side2k
Copy link
Contributor

@side2k side2k commented Mar 3, 2026

Depends on #504

  • when emitting event, use eventId as indexed topic1
  • on listener startup - scan for a block with next_event_id, using event id as indexed search parameter
  • listener now explicitly fails if during sync stage it encounters event_id higher than next_event_id in the internal state
  • a warning is issues when the next_event_id is not 0, but during sync scanning listener didn't find a matching event_id

/// will both remain pending.
pub pending_events: MerkleMap<BridgeEventId, PendingBridgeEvent>,
/// Last known location of an external-chain event log for this chain.
pub last_event_location: Option<LastEventLocation>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't look correct. The chain state itself shouldn't be tracking information related to the processing handled by the listener itself. The listener should know what event to look for based on:

  • next_event_id
  • Which proposals for new events are pending

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the best way I've found so far to avoid full chain scan while catching up missed events during listener startup.

Copy link
Contributor Author

@side2k side2k Mar 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also thought about scanning backwards, but Ethereum is badly suited for that, and we'll have to make lots of RPC calls. This can be optimized with binary search, but it will still be less effective than using this cursor information.
If you know about something I miss here (some Ethereum mechanism that can be helpful, etc) - please let me know.

Also, apparently there is a thing I didn't know about - ethereum reorgs. To adapt to that we may also want to store block hash along with other cursor info to verify it wasn't a subject of reorg.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know what problem you're trying to solve for. Can you clarify?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If Ethereum is a series of blocks G..D..L..C, where

  • G - genesis
  • D - bridge contract deployment block
  • L - block containing last event that was processed by kolme
  • C - current (last) block in the chain

When listener starts, and it has non-zero next_event_id it has to scan blocks in L..C to find:

  • event with next_event_id
  • all events that has been emitted by bridge since that for catching up

Right now, we don't have a way to know location of L or D - only G. Storing location is D is to be implemented in a milestone 3, but the most efficient strategy would be to know L and start scan from it.

So, I've added last_event_location that stores L's block height and log index of the event in it, to the kolme state.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Is there an Ethereum method for searching for an event based on its metadata?
  • The problem is storing the last block info within the Kolme chain itself. Introducing a listener storage mechanism is possible, though it would be better if there's a way to query "find the event for this contract with ID X".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there an Ethereum method for searching for an event based on its metadata?

Yes, and we use it. It's not O(1).
On second thought, I could set event id as indexed topic1, which will make it more efficient. Still - the best efficiency(as far, as I understand) would be to limit a search with L, but I can implement the "topic1 way" in a couple of hours, likely.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • changes that added location info to the sidechain state reverted (I think it would be worth to keep them in history)
  • I've added event id to indexed topics and use it to sync listener on startup
  • Search's lower bound is genesis block at the moment, I plan to fix that and start from contract deployment block in milestone 3.

Base automatically changed from ethereum-bridge-1-5-validators-verification to main March 4, 2026 06:00
@side2k side2k marked this pull request as ready for review March 4, 2026 06:02
@side2k side2k requested a review from snoyberg March 4, 2026 14:07
Leonid Amirov added 3 commits March 5, 2026 11:02
Revert "feat(listener): ethereum: store event location in Message"

This reverts commit 9df3665.

Revert "feat(ethereum): resume listener from last processed event location"

This reverts commit 91987ea.

Revert "feat(listener): maintain event location in the state"

This reverts commit 59307b6.

Revert "feat(tests): core::types: cover location's versioned [de]serialization"

This reverts commit ca9204e.

Revert "feat(types): Message::Listener: add location field"

This reverts commit fed06f9.

Revert "feat(types): core::PendingBridgeEvent: add location and create serialization v1"

This reverts commit 02a2949.

Revert "feat(types): core::ChainConfig: add last_event_location"

This reverts commit c888b0c.

Revert "feat(types): core::ChainState: add last_event_location and create serialization v1"

This reverts commit b18b775.

Revert "feat(types): add LastEventLocation"

This reverts commit 3786ee9.
@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Mar 5, 2026

Deploying kolme with  Cloudflare Pages  Cloudflare Pages

Latest commit: 2bf2d70
Status: ✅  Deploy successful!
Preview URL: https://e765e512.kolme.pages.dev
Branch Preview URL: https://ethereum-bridge-1-5-get-resu.kolme.pages.dev

View logs

@side2k side2k merged commit 4f677bd into main Mar 5, 2026
4 checks passed
@side2k side2k deleted the ethereum-bridge-1-5-get-resume-cursor branch March 5, 2026 09:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants