Skip to content

Commit

Permalink
Fix storing settlement observations (#2070)
Browse files Browse the repository at this point in the history
# Description
Fixes current issue we have in gnosis chain staging, where deep
reindexing happened and on_settlement_event_handler doesn't handle it
properly.
  • Loading branch information
sunce86 authored Nov 22, 2023
1 parent d31e240 commit 04feb02
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
5 changes: 3 additions & 2 deletions crates/autopilot/src/database/on_settlement_event_updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,9 @@ impl super::Postgres {
.await
.context("failed to insert auction_transaction")?;

// It's always fine to store a settlement observation.
database::settlement_observations::insert(
// in case of deep reindexing we might already have the observation, so just
// overwrite it
database::settlement_observations::upsert(
ex,
Observation {
block_number: settlement_update.block_number,
Expand Down
27 changes: 25 additions & 2 deletions crates/database/src/settlement_observations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ pub struct Observation {
pub log_index: i64,
}

pub async fn insert(ex: &mut PgConnection, observation: Observation) -> Result<(), sqlx::Error> {
pub async fn upsert(ex: &mut PgConnection, observation: Observation) -> Result<(), sqlx::Error> {
const QUERY: &str = r#"
INSERT INTO settlement_observations (gas_used, effective_gas_price, surplus, fee, block_number, log_index)
VALUES ($1, $2, $3, $4, $5, $6)
ON CONFLICT (block_number, log_index) DO UPDATE
SET gas_used = $1, effective_gas_price = $2, surplus = $3, fee = $4
;"#;
sqlx::query(QUERY)
.bind(observation.gas_used)
Expand Down Expand Up @@ -78,7 +80,7 @@ mod tests {
log_index: 1,
};

insert(&mut db, input.clone()).await.unwrap();
upsert(&mut db, input.clone()).await.unwrap();
let output = fetch(
&mut db,
&EventIndex {
Expand All @@ -90,5 +92,26 @@ mod tests {
.unwrap()
.unwrap();
assert_eq!(input, output);

let new_input = Observation {
gas_used: 5.into(),
effective_gas_price: 6.into(),
surplus: 7.into(),
fee: 8.into(),
block_number: 1,
log_index: 1,
};
upsert(&mut db, new_input.clone()).await.unwrap();
let output = fetch(
&mut db,
&EventIndex {
block_number: 1,
log_index: 1,
},
)
.await
.unwrap()
.unwrap();
assert_eq!(new_input, output);
}
}

0 comments on commit 04feb02

Please sign in to comment.