Skip to content

Commit

Permalink
Sload pre cache (#322)
Browse files Browse the repository at this point in the history
* chore: add rwlock cache

* lint: type "simplification"

* chore: create a slot cache at initialization

* chore: lint

* fix: add missing file back into sqlx
  • Loading branch information
renancloudwalk authored Mar 7, 2024
1 parent a64e100 commit 64c54cd
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions src/eth/storage/postgres/queries/select_slot_cache.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
SELECT
idx as "index: _",
value as "value: _",
account_address as "address: _",
creation_block as "block: _"
FROM account_slots
WHERE creation_block >= $1;
25 changes: 23 additions & 2 deletions src/infra/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::time::Duration;

use anyhow::anyhow;
use sqlx::postgres::PgPoolOptions;
use sqlx::types::BigDecimal;
use sqlx::PgPool;
use tokio::sync::RwLock;

Expand Down Expand Up @@ -42,13 +43,26 @@ impl Postgres {
})?;

let postgres = Self {
connection_pool,
sload_cache: Arc::new(RwLock::new(HashMap::new())),
connection_pool: connection_pool.clone(),
sload_cache: Arc::new(RwLock::new(Self::new_sload_cache(connection_pool).await?)),
};

Ok(postgres)
}

async fn new_sload_cache(connection_pool: PgPool) -> anyhow::Result<HashMap<(Address, SlotIndex), (SlotValue, BlockNumber)>> {
let raw_sload = sqlx::query_file_as!(SlotCache, "src/eth/storage/postgres/queries/select_slot_cache.sql", BigDecimal::from(0))
.fetch_optional(&connection_pool)
.await?;
let mut sload_cache = HashMap::new();

raw_sload.into_iter().for_each(|s| {
sload_cache.insert((s.address, s.index), (s.value, s.block));
});

Ok(sload_cache)
}

/// Starts a new database transaction.
pub async fn start_transaction(&self) -> anyhow::Result<sqlx::Transaction<'_, sqlx::Postgres>> {
tracing::debug!("starting postgres transaction");
Expand All @@ -67,3 +81,10 @@ impl Postgres {
}
}
}

struct SlotCache {
pub index: SlotIndex,
pub value: SlotValue,
pub address: Address,
pub block: BlockNumber,
}

0 comments on commit 64c54cd

Please sign in to comment.