Skip to content

Conversation

@squadgazzz
Copy link
Contributor

@squadgazzz squadgazzz commented Sep 19, 2025

Description

Currently, on each service restart, CoW AMM indexing starts from the AMM Factory SC's deployment block, since all the indexed events are stored in in-memory cache. This has become problematic on some chains that actively use AMMs, since on each restart, the service has to re-index all the events. As a result, this consumes a lot of RPC resources, and the overall protocol performance drops drastically. On Base, for example, it might take 2-3h.

Changes

This PR introduces a persistence layer for the CoW AMM indexing, which effectively stores all the indexed AMMs and the last indexed block, so on the next restart, it can continue from the last indexed block.

  • When the registry is initialized, fetch CoW AMMs from the DB to populate the cache. Since we have an indexer per factory, the factory address is stored in the db.
  • The last_indexed_blocks will now contain information per CoW AMM factory, which is required for initialization of each indexer. If no data is found, use the configured deployment block.
  • When another CoW AMM is indexed, it is stored in both in-memory cache and the db.
  • On revert, all the CoW AMMs with the affected blocks are removed.

How to test

Existing tests. Updated postgres tests. Staging.

@github-actions
Copy link

github-actions bot commented Sep 19, 2025

Reminder: Please update the DB Readme and comment whether migrations are reversible (include rollback scripts if applicable).
If creating new tables, update the tables list.
When adding a new index, consider using CREATE INDEX CONCURRENTLY for tables involved in the critical execution path.


Caused by:

# Conflicts:
#	crates/driver/src/domain/competition/pre_processing.rs
#	crates/driver/src/infra/api/routes/solve/dto/solve_request.rs
#	crates/e2e/src/setup/deploy.rs
# Conflicts:
#	crates/driver/src/infra/blockchain/mod.rs
#	crates/driver/src/run.rs
#	crates/driver/src/tests/setup/solver.rs
@github-actions
Copy link

Reminder: Please update the DB Readme and comment whether migrations are reversible (include rollback scripts if applicable).

  • If creating new tables, update the tables list.
  • When adding a new index, consider using CREATE INDEX CONCURRENTLY for tables involved in the critical execution path.
  • For breaking changes, remember that during rollout k8s starts the new autopilot, runs the Flyway migration, and only then shuts down the old pod. That overlap means the previous version can still be processing requests on the migrated schema, so make it compatible first and ship the breaking DB change in the following release.

Caused by:

Copy link
Contributor

@MartinquaXD MartinquaXD left a comment

Choose a reason for hiding this comment

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

Looks already good over all. Just a couple small comments.

limit | Long lived order that may receive surplus. Users sign a static fee of 0 upfront and either the backend or the solvers compute a dynamic fee that gets taken from the surplus (while still respecting the user's limit price!).


### cow\_amms
Copy link
Contributor

Choose a reason for hiding this comment

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

cow_amms is a new table but shows up in the enum section and doesn't follow the same format as other tables.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addressed all the comments.

Comment on lines +195 to +196
let mut ex = self.0.db.begin().await?;
database::cow_amms::upsert_batched(&mut ex, &db_amms).await?;
Copy link
Contributor

Choose a reason for hiding this comment

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

Good that you do the DB stuff first to avoid getting the in-memory cache into a weird state when inserting there works but the DB query fails. 👍

Copy link
Contributor

@MartinquaXD MartinquaXD left a comment

Choose a reason for hiding this comment

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

Approved assuming the one new comment gets addressed.

database::last_indexed_blocks::update(
&mut ex,
&self.0.factory_address.to_string(),
i64::try_from(latest_block).context("last block is not u64")?,
Copy link
Contributor

Choose a reason for hiding this comment

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

This error is confusing, the integer may be a u64 but it's bigger than 1 << 32 - 1 (the positive side of the integer)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We have it all around the code base. If the function signature changes, the error message won't be valid anymore with your suggestion.

cache.entry(block_number).or_default().push(amm);
}
tracing::info!(
count,
Copy link
Contributor

Choose a reason for hiding this comment

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

This would allow you to remove the count variable above

Suggested change
count,
count = %processed_amms.len(),

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It won't work since the processed_amms is being moved above.

let mut ex = self.0.db.acquire().await?;
database::last_indexed_blocks::fetch(&mut ex, &self.0.factory_address.to_string())
.await?
.map(|block| block.try_into().context("last block is not u64"))
Copy link
Contributor

@jmg-duarte jmg-duarte Oct 22, 2025

Choose a reason for hiding this comment

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

Ditto

Base automatically changed from remove-driver-cow-amm-indexer to main October 22, 2025 12:09
@squadgazzz squadgazzz enabled auto-merge (squash) October 22, 2025 14:50
@squadgazzz squadgazzz merged commit 1280f95 into main Oct 22, 2025
17 checks passed
@squadgazzz squadgazzz deleted the cow-amm-indexer-db-storage branch October 22, 2025 15:15
@github-actions github-actions bot locked and limited conversation to collaborators Oct 22, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants