Skip to content

Commit

Permalink
feat: read only slot value from database and reuse index from memory (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
dinhani-cw authored Apr 9, 2024
1 parent 142852b commit 2974441
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 84 deletions.

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

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

This file was deleted.

This file was deleted.

18 changes: 11 additions & 7 deletions src/eth/storage/postgres_permanent/postgres_permanent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use crate::eth::primitives::LogTopic;
use crate::eth::primitives::Slot;
use crate::eth::primitives::SlotIndex;
use crate::eth::primitives::SlotSample;
use crate::eth::primitives::SlotValue;
use crate::eth::primitives::StoragePointInTime;
use crate::eth::primitives::TransactionMined;
use crate::eth::storage::postgres_permanent::types::AccountBatch;
Expand Down Expand Up @@ -164,10 +165,9 @@ impl PermanentStorage for PostgresPermanentStorage {
let slot_index_u8: [u8; 32] = slot_index.clone().into();

let mut conn = PoolOrThreadConnection::take(&self.pool).await?;
let slot = match point_in_time {
let slot_value_vec: Option<Vec<u8>> = match point_in_time {
StoragePointInTime::Present =>
sqlx::query_file_as!(
Slot,
sqlx::query_file_scalar!(
"src/eth/storage/postgres_permanent/sql/select_slot.sql",
address.as_ref(),
slot_index_u8.as_ref(),
Expand All @@ -176,8 +176,7 @@ impl PermanentStorage for PostgresPermanentStorage {
.await?,
StoragePointInTime::Past(number) => {
let block_number: i64 = (*number).try_into()?;
sqlx::query_file_as!(
Slot,
sqlx::query_file_scalar!(
"src/eth/storage/postgres_permanent/sql/select_slot_at_block.sql",
address.as_ref(),
slot_index_u8.as_ref(),
Expand All @@ -190,8 +189,13 @@ impl PermanentStorage for PostgresPermanentStorage {

// If there is no slot, we return
// an "empty slot"
match slot {
Some(slot) => {
match slot_value_vec {
Some(slot_value_vec) => {
let slot_value = SlotValue::from(slot_value_vec);
let slot = Slot {
index: slot_index.clone(),
value: slot_value,
};
tracing::trace!(?address, ?slot_index, %slot, "slot found");
Ok(Some(slot))
}
Expand Down
4 changes: 1 addition & 3 deletions src/eth/storage/postgres_permanent/sql/select_slot.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
SELECT
idx as "index: _",
value as "value: _"
SELECT value
FROM account_slots
WHERE account_address = $1
AND idx = $2
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
SELECT
idx as "index: _",
value as "value: _"
SELECT value
FROM historical_slots
WHERE account_address = $1
AND idx = $2
Expand Down
34 changes: 22 additions & 12 deletions tests/test_import_offline_snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,40 +25,50 @@ mod m {
}

#[cfg(feature = "metrics")]
const METRIC_QUERIES: [&str; 30] = [
const METRIC_QUERIES: [&str; 43] = [
// EVM
"",
"* EVM",
m::formatcp!("{}_count", m::METRIC_EVM_EXECUTION),
m::formatcp!("{}_sum", m::METRIC_EVM_EXECUTION),
m::formatcp!("{}{{quantile='1'}}", m::METRIC_EVM_EXECUTION),
// STORAGE ACCOUNTS
"",
m::formatcp!("{}{{quantile='0.95'}}", m::METRIC_EVM_EXECUTION),
"* ACCOUNTS (count)",
m::formatcp!("sum({}_count)", m::METRIC_STORAGE_READ_ACCOUNT),
m::formatcp!("{}_count{{found_at='temporary'}}", m::METRIC_STORAGE_READ_ACCOUNT),
m::formatcp!("{}_count{{found_at='permanent'}}", m::METRIC_STORAGE_READ_ACCOUNT),
m::formatcp!("{}_count{{found_at='default'}}", m::METRIC_STORAGE_READ_ACCOUNT),
"* ACCOUNTS (cumulative)",
m::formatcp!("sum({}_sum)", m::METRIC_STORAGE_READ_ACCOUNT),
m::formatcp!("{}_sum{{found_at='temporary'}}", m::METRIC_STORAGE_READ_ACCOUNT),
m::formatcp!("{}_sum{{found_at='permanent'}}", m::METRIC_STORAGE_READ_ACCOUNT),
m::formatcp!("{}_sum{{found_at='default'}}", m::METRIC_STORAGE_READ_ACCOUNT),
"* ACCOUNTS (P100)",
m::formatcp!("{}{{found_at='temporary', quantile='1'}}", m::METRIC_STORAGE_READ_ACCOUNT),
m::formatcp!("{}{{found_at='permanent', quantile='1'}}", m::METRIC_STORAGE_READ_ACCOUNT),
m::formatcp!("{}{{found_at='default', quantile='1'}}", m::METRIC_STORAGE_READ_ACCOUNT),
// STORAGE SLOTS
"",
"* ACCOUNTS (P95)",
m::formatcp!("{}{{found_at='temporary', quantile='0.95'}}", m::METRIC_STORAGE_READ_ACCOUNT),
m::formatcp!("{}{{found_at='permanent', quantile='0.95'}}", m::METRIC_STORAGE_READ_ACCOUNT),
m::formatcp!("{}{{found_at='default', quantile='0.95'}}", m::METRIC_STORAGE_READ_ACCOUNT),
"* STORAGE SLOTS (count)",
m::formatcp!("sum({}_count)", m::METRIC_STORAGE_READ_SLOT),
m::formatcp!("{}_count{{found_at='temporary'}}", m::METRIC_STORAGE_READ_SLOT),
m::formatcp!("{}_count{{found_at='permanent'}}", m::METRIC_STORAGE_READ_SLOT),
m::formatcp!("{}_count{{found_at='default'}}", m::METRIC_STORAGE_READ_SLOT),
"* STORAGE SLOTS (cumulative)",
m::formatcp!("sum({}_sum)", m::METRIC_STORAGE_READ_SLOT),
m::formatcp!("{}_sum{{found_at='temporary'}}", m::METRIC_STORAGE_READ_SLOT),
m::formatcp!("{}_sum{{found_at='permanent'}}", m::METRIC_STORAGE_READ_SLOT),
m::formatcp!("{}_sum{{found_at='default'}}", m::METRIC_STORAGE_READ_SLOT),
"* STORAGE SLOTS (P100)",
m::formatcp!("{}{{found_at='temporary', quantile='1'}}", m::METRIC_STORAGE_READ_SLOT),
m::formatcp!("{}{{found_at='permanent', quantile='1'}}", m::METRIC_STORAGE_READ_SLOT),
m::formatcp!("{}{{found_at='default', quantile='1'}}", m::METRIC_STORAGE_READ_SLOT),
// STORAGE COMMIT
"",
"* STORAGE SLOTS (P95)",
m::formatcp!("{}{{found_at='temporary', quantile='0.95'}}", m::METRIC_STORAGE_READ_SLOT),
m::formatcp!("{}{{found_at='permanent', quantile='0.95'}}", m::METRIC_STORAGE_READ_SLOT),
m::formatcp!("{}{{found_at='default', quantile='0.95'}}", m::METRIC_STORAGE_READ_SLOT),
"* COMMIT",
m::formatcp!("{}{{quantile='1'}}", m::METRIC_STORAGE_COMMIT),
];

Expand Down Expand Up @@ -106,8 +116,8 @@ async fn test_import_offline_snapshot() {

for query in METRIC_QUERIES {
// formatting between query groups
if query.is_empty() {
println!("\n--------------------");
if query.starts_with('*') {
println!("\n{}\n--------------------", query.replace("* ", ""));
continue;
}

Expand All @@ -124,10 +134,10 @@ async fn test_import_offline_snapshot() {
let value: f64 = value.parse().unwrap();

if query.contains("_count") {
println!("{:<64} = {}", query, value);
println!("{:<70} = {}", query, value);
} else {
let secs = Duration::from_secs_f64(value);
println!("{:<64} = {}", query, secs.fancy_duration().truncate(2));
println!("{:<70} = {}", query, secs.fancy_duration().truncate(2));
}
}
}
Expand Down

0 comments on commit 2974441

Please sign in to comment.