-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Track event indexing progress (#3075)
# Description Our event indexing logic currently can't handle some cases well during restarts. Let's say a contract we index doesn't emit an event for 1 month our current logic would fetch the last indexed event from that contract and continue indexing from there. That means on restarts we might index ~1 month (theoretically unbounded) of blocks before we can start building new auctions (we recently started to require the indexing logic to have seen the tip of the chain before we can start building auctions). Especially on fast chains like `arbitrum` this is a huge issue. Dusts off @squadgazzz's previous [PR](#2965) # Changes * introduced a new table `last_processed_blocks` which associates a block number for an arbitrary string * populated table with data for `settlements`, `ethflow_refunds`, and `onchain_orders` ## How to test - 1 database specific test - actually not sure how to best test this e2e. We could spawn an autopilot, index an event, mint a ton of empty blocks, and restart the autopilot to trigger the code we are interested in. But it's not super clear to me what would be a good assertion that can be tested with reasonable effort. 🤔 --------- Co-authored-by: ilya <[email protected]>
- Loading branch information
1 parent
11df525
commit fad84f1
Showing
14 changed files
with
195 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,26 @@ | ||
use { | ||
anyhow::{Context, Result}, | ||
sqlx::PgPool, | ||
}; | ||
|
||
pub mod settlement; | ||
|
||
pub async fn write_last_block_to_db(db: &PgPool, last_block: u64, contract: &str) -> Result<()> { | ||
let mut ex = db.acquire().await?; | ||
database::last_indexed_blocks::update( | ||
&mut ex, | ||
contract, | ||
i64::try_from(last_block).context("new value of counter is not i64")?, | ||
) | ||
.await?; | ||
Ok(()) | ||
} | ||
|
||
pub async fn read_last_block_from_db(db: &PgPool, contract: &str) -> Result<u64> { | ||
let mut ex = db.acquire().await?; | ||
database::last_indexed_blocks::fetch(&mut ex, contract) | ||
.await? | ||
.unwrap_or_default() | ||
.try_into() | ||
.context("last block is not u64") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use sqlx::{Executor, PgConnection}; | ||
|
||
pub async fn update( | ||
ex: &mut PgConnection, | ||
contract: &str, | ||
last_indexed_block: i64, | ||
) -> Result<(), sqlx::Error> { | ||
const QUERY: &str = r#" | ||
INSERT INTO last_indexed_blocks (contract, block_number) | ||
VALUES ($1, $2) | ||
ON CONFLICT (contract) | ||
DO UPDATE SET block_number = EXCLUDED.block_number; | ||
"#; | ||
|
||
ex.execute(sqlx::query(QUERY).bind(contract).bind(last_indexed_block)) | ||
.await?; | ||
Ok(()) | ||
} | ||
|
||
pub async fn fetch(ex: &mut PgConnection, contract: &str) -> Result<Option<i64>, sqlx::Error> { | ||
const QUERY: &str = r#" | ||
SELECT block_number | ||
FROM last_indexed_blocks | ||
WHERE contract = $1; | ||
"#; | ||
|
||
sqlx::query_scalar(QUERY) | ||
.bind(contract) | ||
.fetch_optional(ex) | ||
.await | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use {super::*, sqlx::Connection}; | ||
|
||
#[tokio::test] | ||
#[ignore] | ||
async fn postgres_last_indexed_block_roundtrip() { | ||
let mut db = PgConnection::connect("postgresql://").await.unwrap(); | ||
let mut db = db.begin().await.unwrap(); | ||
crate::clear_DANGER_(&mut db).await.unwrap(); | ||
|
||
assert_eq!(fetch(&mut db, "test").await.unwrap(), None); | ||
|
||
update(&mut db, "test", 42).await.unwrap(); | ||
assert_eq!(fetch(&mut db, "test").await.unwrap(), Some(42)); | ||
|
||
update(&mut db, "test", 43).await.unwrap(); | ||
assert_eq!(fetch(&mut db, "test").await.unwrap(), Some(43)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.