Skip to content

Commit

Permalink
feat: read account from history (hybrid) (#395)
Browse files Browse the repository at this point in the history
  • Loading branch information
carneiro-cw authored Mar 19, 2024
1 parent 29bc4ad commit 894c401
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 14 deletions.

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

2 changes: 1 addition & 1 deletion src/eth/storage/hybrid/hybrid_history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub struct AccountInfo {
#[derive(Debug)]
pub struct HybridHistory {
pub hybrid_accounts_slots: HashMap<Address, AccountInfo>,
pool: Arc<Pool<Postgres>>,
pub pool: Arc<Pool<Postgres>>,
}

#[derive(FromRow)]
Expand Down
47 changes: 36 additions & 11 deletions src/eth/storage/hybrid/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::sync::atomic::Ordering;
use std::sync::Arc;
use std::time::Duration;

use anyhow::Context;
use async_trait::async_trait;
use indexmap::IndexMap;
use metrics::atomics::AtomicU64;
Expand Down Expand Up @@ -238,19 +239,43 @@ impl PermanentStorage for HybridPermanentStorage {
async fn maybe_read_account(&self, address: &Address, point_in_time: &StoragePointInTime) -> anyhow::Result<Option<Account>> {
//XXX TODO deal with point_in_time first, e.g create to_account at hybrid_accounts_slots
let hybrid_state = self.hybrid_state.write().await;
match hybrid_state.hybrid_accounts_slots.get(address) {
Some(inmemory_account) => {
let account = inmemory_account.to_account(point_in_time, address).await;
tracing::trace!(%address, ?account, "account found");
Ok(Some(account))
}
let account = match point_in_time {
StoragePointInTime::Present => {
match hybrid_state.hybrid_accounts_slots.get(address) {
Some(inmemory_account) => {
let account = inmemory_account.to_account(point_in_time, address).await;
tracing::trace!(%address, ?account, "account found");
Some(account)
}

None => {
//XXX TODO start a inmemory account from the database maybe using to_account on a empty account
tracing::trace!(%address, "account not found");
Ok(None)
None => {
//XXX TODO start a inmemory account from the database maybe using to_account on a empty account
tracing::trace!(%address, "account not found");
None
}
}
}
}
StoragePointInTime::Past(block_number) => sqlx::query_as!(
Account,
r#"
SELECT address as "address: _",
nonce as "nonce: _",
balance as "balance: _",
bytecode as "bytecode: _"
FROM neo_accounts
WHERE address = $1
AND block_number = (SELECT MAX(block_number)
FROM historical_slots
WHERE address = $1
AND block_number <= $2)"#,
address as _,
block_number as _
)
.fetch_optional(&*hybrid_state.pool)
.await
.context("failed to select account")?,
};
Ok(account)
}

async fn maybe_read_slot(&self, address: &Address, slot_index: &SlotIndex, point_in_time: &StoragePointInTime) -> anyhow::Result<Option<Slot>> {
Expand Down
4 changes: 2 additions & 2 deletions static/schema/001-init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -734,8 +734,8 @@ CREATE TABLE public.neo_accounts (
block_number BIGINT NOT NULL,
address BYTEA NOT NULL,
bytecode BYTEA,
balance NUMERIC(38, 0),
nonce NUMERIC,
balance NUMERIC(38, 0) NOT NULL,
nonce NUMERIC NOT NULL,
created_at TIMESTAMP WITHOUT TIME ZONE DEFAULT now(),
PRIMARY KEY (address, block_number),
FOREIGN KEY (block_number) REFERENCES public.neo_blocks(block_number)
Expand Down

0 comments on commit 894c401

Please sign in to comment.