Skip to content

Commit

Permalink
Hybrid pre load slots and accounts (#384)
Browse files Browse the repository at this point in the history
* chore: create structures for hybrid pre-load

* chore: add queries to load history into memory

* lint

* lint

* chore: load slot and account onto memory

* chore: make queries leaner

* lint
  • Loading branch information
renancloudwalk authored Mar 18, 2024
1 parent 0b79850 commit 97fbfd8
Show file tree
Hide file tree
Showing 7 changed files with 292 additions and 38 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.

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

41 changes: 41 additions & 0 deletions src/eth/primitives/slot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,23 @@ impl Display for SlotIndex {

gen_newtype_from!(self = SlotIndex, other = u64, U256, [u8; 32]);

impl From<Vec<u8>> for SlotIndex {
fn from(bytes: Vec<u8>) -> Self {
// Initialize U256 to zero
// Assuming the byte array is in big-endian format,
let u256: U256 = if bytes.len() <= 32 {
let mut padded_bytes = [0u8; 32];
padded_bytes[32 - bytes.len()..].copy_from_slice(&bytes);
U256::from_big_endian(&padded_bytes)
} else {
// Handle the error or truncate the Vec<u8> as needed
// For simplicity, this example will only load the first 32 bytes if the Vec is too large
U256::from_big_endian(&bytes[0..32])
};
SlotIndex(u256)
}
}

impl From<RevmU256> for SlotIndex {
fn from(value: RevmU256) -> Self {
Self(value.to_be_bytes().into())
Expand Down Expand Up @@ -187,6 +204,30 @@ impl From<SlotValue> for [u8; 32] {
}
}

impl From<SlotValue> for Vec<u8> {
fn from(value: SlotValue) -> Self {
let mut vec = vec![0u8; 32]; // Initialize a vector with 32 bytes set to 0
value.0.to_big_endian(&mut vec);
vec
}
}
impl From<Vec<u8>> for SlotValue {
fn from(bytes: Vec<u8>) -> Self {
// Initialize U256 to zero
// Assuming the byte array is in big-endian format,
let u256: U256 = if bytes.len() <= 32 {
let mut padded_bytes = [0u8; 32];
padded_bytes[32 - bytes.len()..].copy_from_slice(&bytes);
U256::from_big_endian(&padded_bytes)
} else {
// Handle the error or truncate the Vec<u8> as needed
// For simplicity, this example will only load the first 32 bytes if the Vec is too large
U256::from_big_endian(&bytes[0..32])
};
SlotValue(u256)
}
}

gen_newtype_from!(self = SlotValue, other = u64, U256, [u8; 32]);

impl From<RevmU256> for SlotValue {
Expand Down
166 changes: 166 additions & 0 deletions src/eth/storage/hybrid/hybrid_history.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
use std::collections::HashMap;
use std::sync::Arc;

use sqlx::types::BigDecimal;
use sqlx::FromRow;
use sqlx::Pool;
use sqlx::Postgres;

use crate::eth::primitives::Account;
use crate::eth::primitives::Address;
use crate::eth::primitives::Bytes;
use crate::eth::primitives::Nonce;
use crate::eth::primitives::Slot;
use crate::eth::primitives::SlotIndex;
use crate::eth::primitives::SlotValue;
use crate::eth::primitives::StoragePointInTime;
use crate::eth::primitives::Wei;

#[derive(Debug)]
struct SlotInfo {
value: SlotValue,
}

#[derive(Debug)]
pub struct AccountInfo {
balance: Wei,
nonce: Nonce,
bytecode: Option<Bytes>,
slots: HashMap<SlotIndex, SlotInfo>,
}

#[derive(Debug)]
pub struct HybridHistory {
pub hybrid_accounts_slots: HashMap<Address, AccountInfo>,
pool: Arc<Pool<Postgres>>,
}

#[derive(FromRow)]
struct AccountRow {
address: Vec<u8>,
nonce: Option<BigDecimal>,
balance: Option<BigDecimal>,
bytecode: Option<Vec<u8>>,
}

#[derive(FromRow)]
struct SlotRow {
account_address: Vec<u8>,
slot_index: SlotIndex,
value: Option<Vec<u8>>,
}

impl HybridHistory {
pub async fn new(pool: Arc<Pool<Postgres>>) -> Result<Self, sqlx::Error> {
// Initialize the structure
let mut history = HybridHistory {
hybrid_accounts_slots: HashMap::new(),
pool,
};

history.load_latest_data().await?;

Ok(history)
}

//XXX TODO use a fixed block_number during load, in order to avoid sync problem
// e.g other instance moving forward and this query getting incongruous data
async fn load_latest_data(&mut self) -> Result<(), sqlx::Error> {
let account_rows = sqlx::query_as!(
AccountRow,
"
SELECT DISTINCT ON (address)
address,
nonce,
balance,
bytecode
FROM
neo_accounts
ORDER BY
address,
block_number DESC
"
)
.fetch_all(&*self.pool)
.await?;

let mut accounts: HashMap<Address, AccountInfo> = HashMap::new();

for account_row in account_rows {
let addr: Address = account_row.address.try_into().unwrap_or_default(); //XXX add alert
accounts.insert(
addr,
AccountInfo {
balance: account_row.balance.map(|b| b.try_into().unwrap_or_default()).unwrap_or_default(),
nonce: account_row.nonce.map(|n| n.try_into().unwrap_or_default()).unwrap_or_default(),
bytecode: account_row.bytecode.map(Bytes::from),
slots: HashMap::new(),
},
);
}

// Load slots
let slot_rows = sqlx::query_as!(
SlotRow,
"
SELECT DISTINCT ON (account_address, slot_index)
account_address,
slot_index,
value
FROM
neo_account_slots
ORDER BY
account_address,
slot_index,
block_number DESC
"
)
.fetch_all(&*self.pool)
.await?;

for slot_row in slot_rows {
let addr = &slot_row.account_address.try_into().unwrap_or_default(); //XXX add alert
if let Some(account_info) = accounts.get_mut(addr) {
account_info.slots.insert(
slot_row.slot_index,
SlotInfo {
value: slot_row.value.unwrap_or_default().into(),
},
);
}
}

self.hybrid_accounts_slots = accounts;

Ok(())
}

pub async fn get_slot_at_point(&self, address: &Address, slot_index: &SlotIndex, point_in_time: &StoragePointInTime) -> Option<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
}
}
}
}

impl AccountInfo {
pub async fn to_account(&self, point_in_time: &StoragePointInTime, address: &Address) -> Account {
match point_in_time {
StoragePointInTime::Present => Account {
address: address.clone(),
nonce: self.nonce.clone(),
balance: self.balance.clone(),
bytecode: self.bytecode.clone(),
},
StoragePointInTime::Past(_number) => Account::default(),
}
}
}
Loading

0 comments on commit 97fbfd8

Please sign in to comment.