diff --git a/.sqlx/query-3c8d5b4d6e04f23c00b049b2b01f733df151e7870fdf29d23b85e529276151a1.json b/.sqlx/query-3c8d5b4d6e04f23c00b049b2b01f733df151e7870fdf29d23b85e529276151a1.json new file mode 100644 index 000000000..8a06452d3 --- /dev/null +++ b/.sqlx/query-3c8d5b4d6e04f23c00b049b2b01f733df151e7870fdf29d23b85e529276151a1.json @@ -0,0 +1,18 @@ +{ + "db_name": "PostgreSQL", + "query": "INSERT INTO public.neo_accounts (block_number, address, bytecode, balance, nonce)\n SELECT * FROM UNNEST($1::bigint[], $2::bytea[], $3::bytea[], $4::numeric[], $5::numeric[])\n AS t(block_number, address, bytecode, balance, nonce);", + "describe": { + "columns": [], + "parameters": { + "Left": [ + "Int8Array", + "ByteaArray", + "ByteaArray", + "NumericArray", + "NumericArray" + ] + }, + "nullable": [] + }, + "hash": "3c8d5b4d6e04f23c00b049b2b01f733df151e7870fdf29d23b85e529276151a1" +} diff --git a/.sqlx/query-edae8a7534d957fb6f727f9222299b258a4c96059ab55cd6af5477502acd2265.json b/.sqlx/query-edae8a7534d957fb6f727f9222299b258a4c96059ab55cd6af5477502acd2265.json index f76288568..4cfe9beff 100644 --- a/.sqlx/query-edae8a7534d957fb6f727f9222299b258a4c96059ab55cd6af5477502acd2265.json +++ b/.sqlx/query-edae8a7534d957fb6f727f9222299b258a4c96059ab55cd6af5477502acd2265.json @@ -29,8 +29,8 @@ }, "nullable": [ false, - true, - true, + false, + false, true ] }, diff --git a/src/eth/storage/hybrid/hybrid_history.rs b/src/eth/storage/hybrid/hybrid_history.rs index 2028b679c..9bc1fa36c 100644 --- a/src/eth/storage/hybrid/hybrid_history.rs +++ b/src/eth/storage/hybrid/hybrid_history.rs @@ -19,16 +19,16 @@ use crate::eth::primitives::StoragePointInTime; use crate::eth::primitives::Wei; #[derive(Debug)] -struct SlotInfo { +pub struct SlotInfo { value: SlotValue, } #[derive(Debug)] pub struct AccountInfo { - balance: Wei, - nonce: Nonce, - bytecode: Option, - slots: HashMap, + pub balance: Wei, + pub nonce: Nonce, + pub bytecode: Option, + pub slots: HashMap, } #[derive(Debug)] diff --git a/src/eth/storage/hybrid/mod.rs b/src/eth/storage/hybrid/mod.rs index 5b820bf03..80460a611 100644 --- a/src/eth/storage/hybrid/mod.rs +++ b/src/eth/storage/hybrid/mod.rs @@ -42,6 +42,7 @@ use crate::eth::primitives::SlotSample; use crate::eth::primitives::StoragePointInTime; use crate::eth::primitives::TransactionMined; use crate::eth::primitives::Wei; +use crate::eth::storage::hybrid::hybrid_history::AccountInfo; use crate::eth::storage::hybrid::hybrid_history::HybridHistory; use crate::eth::storage::inmemory::InMemoryHistory; use crate::eth::storage::PermanentStorage; @@ -238,7 +239,8 @@ impl PermanentStorage for HybridPermanentStorage { async fn maybe_read_account(&self, address: &Address, point_in_time: &StoragePointInTime) -> anyhow::Result> { //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; + let hybrid_state = self.hybrid_state.read().await; + let account = match point_in_time { StoragePointInTime::Present => { match hybrid_state.hybrid_accounts_slots.get(address) { @@ -378,13 +380,37 @@ impl PermanentStorage for HybridPermanentStorage { async fn save_accounts(&self, accounts: Vec) -> anyhow::Result<()> { tracing::debug!(?accounts, "saving initial accounts"); - let mut state = self.lock_write().await; + let mut state = self.hybrid_state.write().await; + let mut accounts_changes = (Vec::new(), Vec::new(), Vec::new(), Vec::new(), Vec::new()); for account in accounts { - state.accounts.insert( + state.hybrid_accounts_slots.insert( account.address.clone(), - InMemoryPermanentAccount::new_with_balance(account.address, account.balance), + AccountInfo { + balance: account.balance.clone(), + nonce: account.nonce.clone(), + bytecode: account.bytecode.clone(), + slots: HashMap::new(), + }, ); + accounts_changes.0.push(BlockNumber::from(0)); + accounts_changes.1.push(account.address); + accounts_changes.2.push(account.bytecode); + accounts_changes.3.push(account.balance); + accounts_changes.4.push(account.nonce); } + tokio::time::sleep(Duration::from_secs(5)).await; // XXX Waiting for genesis to be commited pg + sqlx::query!( + "INSERT INTO public.neo_accounts (block_number, address, bytecode, balance, nonce) + SELECT * FROM UNNEST($1::bigint[], $2::bytea[], $3::bytea[], $4::numeric[], $5::numeric[]) + AS t(block_number, address, bytecode, balance, nonce);", + accounts_changes.0 as _, + accounts_changes.1 as _, + accounts_changes.2 as _, + accounts_changes.3 as _, + accounts_changes.4 as _, + ) + .execute(&*state.pool) + .await?; Ok(()) } @@ -464,7 +490,7 @@ pub struct InMemoryPermanentAccount { impl InMemoryPermanentAccount { /// Creates a new permanent account with initial balance. - pub fn new_with_balance(address: Address, balance: Wei) -> Self { + pub fn _new_with_balance(address: Address, balance: Wei) -> Self { Self { address, balance: InMemoryHistory::new_at_zero(balance),