-
Notifications
You must be signed in to change notification settings - Fork 93
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
Track event indexing progress #3075
Merged
Merged
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
2719635
Event indexer counters
squadgazzz e7affa1
Add missing field
MartinquaXD 3ab80a1
Change, rename, document a few things
MartinquaXD 56016cb
Remove `sqlx` from `shared`
MartinquaXD 59f44b9
Remove dead code
MartinquaXD 86991f8
Fix migration and db code
MartinquaXD 5d60e9b
Merge branch 'main' into track-event-indexing-progress
MartinquaXD ae4515f
Merge branch 'main' into track-event-indexing-progress
MartinquaXD a086e0a
Rename `last_processed -> last_indexed`
MartinquaXD 6c9c47a
Merge branch 'main' into track-event-indexing-progress
MartinquaXD File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, index_name: &str) -> Result<()> { | ||
let mut ex = db.acquire().await?; | ||
database::last_processed_blocks::update( | ||
&mut ex, | ||
index_name, | ||
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, index_name: &str) -> Result<u64> { | ||
let mut ex = db.acquire().await?; | ||
database::last_processed_blocks::fetch(&mut ex, index_name) | ||
.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, | ||
index: &str, | ||
last_processed_block: i64, | ||
) -> Result<(), sqlx::Error> { | ||
const QUERY: &str = r#" | ||
INSERT INTO last_processed_blocks (index, block_number) | ||
VALUES ($1, $2) | ||
ON CONFLICT (index) | ||
DO UPDATE SET block_number = EXCLUDED.block_number; | ||
"#; | ||
|
||
ex.execute(sqlx::query(QUERY).bind(index).bind(last_processed_block)) | ||
.await?; | ||
Ok(()) | ||
} | ||
|
||
pub async fn fetch(ex: &mut PgConnection, index: &str) -> Result<Option<i64>, sqlx::Error> { | ||
const QUERY: &str = r#" | ||
SELECT block_number | ||
FROM last_processed_blocks | ||
WHERE index = $1; | ||
"#; | ||
|
||
sqlx::query_scalar(QUERY) | ||
.bind(index) | ||
.fetch_optional(ex) | ||
.await | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use {super::*, sqlx::Connection}; | ||
|
||
#[tokio::test] | ||
#[ignore] | ||
async fn postgres_last_processed_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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,7 +76,13 @@ pub trait EventStoring<T>: Send + Sync { | |
/// * `events` the contract events to be appended by the implementer | ||
async fn append_events(&mut self, events: Vec<EthcontractEvent<T>>) -> Result<()>; | ||
|
||
/// Fetches the last processed block to know where to resume indexing after | ||
/// a restart. | ||
async fn last_event_block(&self) -> Result<u64>; | ||
|
||
/// Stores the last processed block to know where to resume indexing after a | ||
/// restart. | ||
async fn persist_last_processed_block(&mut self, last_block: u64) -> Result<()>; | ||
} | ||
|
||
pub trait EventRetrieving { | ||
|
@@ -282,9 +288,12 @@ where | |
if let Some(range) = event_range.history_range { | ||
self.update_events_from_old_blocks(range).await?; | ||
} | ||
if !event_range.latest_blocks.is_empty() { | ||
if let Some(last_block) = event_range.latest_blocks.last() { | ||
self.update_events_from_latest_blocks(&event_range.latest_blocks, event_range.is_reorg) | ||
.await?; | ||
self.store_mut() | ||
.persist_last_processed_block(last_block.0) | ||
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. nit: I'd move this call inside |
||
.await?; | ||
} | ||
Ok(()) | ||
} | ||
|
@@ -652,6 +661,11 @@ mod tests { | |
.map(|event| event.meta.clone().unwrap().block_number) | ||
.unwrap_or_default()) | ||
} | ||
|
||
async fn persist_last_processed_block(&mut self, _last_block: u64) -> Result<()> { | ||
// Nothing to do here since `last_event_block` looks up last stored event. | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[test] | ||
|
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Wouldn't it be better to store in database
u64
type for the last block instead of converting it toi64
?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.
Unfortunately postgres doesn't support
u64
natively. The alternative would be to use a bignumber type or sth like that.