Skip to content
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

feat: read slot from history (hybrid) #388

Merged
merged 4 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

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.

33 changes: 27 additions & 6 deletions src/eth/storage/hybrid/hybrid_history.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::HashMap;
use std::sync::Arc;

use anyhow::Context;
use sqlx::types::BigDecimal;
use sqlx::FromRow;
use sqlx::Pool;
Expand Down Expand Up @@ -170,19 +171,39 @@ impl HybridHistory {
Ok(())
}

pub async fn get_slot_at_point(&self, address: &Address, slot_index: &SlotIndex, point_in_time: &StoragePointInTime) -> Option<Slot> {
match point_in_time {
pub async fn get_slot_at_point(&self, address: &Address, slot_index: &SlotIndex, point_in_time: &StoragePointInTime) -> anyhow::Result<Option<Slot>> {
let slot = match point_in_time {
StoragePointInTime::Present => self.hybrid_accounts_slots.get(address).map(|account_info| {
let value = account_info.slots.get(slot_index).map(|slot_info| slot_info.value.clone()).unwrap_or_default();
Slot {
index: slot_index.clone(),
value,
}
}),
StoragePointInTime::Past(_number) => {
None //XXX TODO use postgres query
}
}
StoragePointInTime::Past(number) => sqlx::query_as!(
Slot,
r#"
SELECT
slot_index as "index: _",
value as "value: _"
FROM neo_account_slots
WHERE account_address = $1
AND slot_index = $2
AND block_number = (SELECT MAX(block_number)
FROM historical_slots
WHERE account_address = $1
AND idx = $2
AND block_number <= $3)
"#,
address as _,
slot_index as _,
number as _
)
.fetch_optional(&*self.pool)
.await
.context("failed to select slot")?,
};
Ok(slot)
}
}

Expand Down
4 changes: 1 addition & 3 deletions src/eth/storage/hybrid/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,7 @@ impl PermanentStorage for HybridPermanentStorage {

async fn maybe_read_slot(&self, address: &Address, slot_index: &SlotIndex, point_in_time: &StoragePointInTime) -> anyhow::Result<Option<Slot>> {
tracing::debug!(%address, %slot_index, ?point_in_time, "reading slot");
let hybrid_state = self.hybrid_state.write().await;

Ok(hybrid_state.get_slot_at_point(address, slot_index, point_in_time).await)
self.hybrid_state.read().await.get_slot_at_point(address, slot_index, point_in_time).await
}

async fn read_block(&self, selection: &BlockSelection) -> anyhow::Result<Option<Block>> {
Expand Down
2 changes: 1 addition & 1 deletion static/schema/001-init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,7 @@ CREATE TABLE public.neo_account_slots (
block_number BIGINT NOT NULL,
slot_index BYTEA NOT NULL,
account_address BYTEA NOT NULL,
value BYTEA,
value BYTEA NOT NULL,
created_at TIMESTAMP WITHOUT TIME ZONE DEFAULT now(),
PRIMARY KEY (account_address, slot_index, block_number),
FOREIGN KEY (block_number) REFERENCES public.neo_blocks(block_number)
Expand Down
Loading