Skip to content

Commit

Permalink
chore: use metadata inline instead of having a dedicated database (#510)
Browse files Browse the repository at this point in the history
* chore: use metadata inline instead of having a dedicated database

* fix: only store current_block when there is one

* lint

* chore: add current block during load

* lint

* fix: return next instead of evaluating it

---------

Co-authored-by: Maycon Amaro <[email protected]>
  • Loading branch information
renancloudwalk and mayconamaroCW authored Apr 3, 2024
1 parent 5aa0203 commit b921c54
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 61 deletions.

This file was deleted.

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.

60 changes: 34 additions & 26 deletions src/eth/storage/hybrid/hybrid_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,21 +92,25 @@ impl HybridStorageState {

// Spawn the first blocking task for accounts
let accounts_task = tokio::spawn(async move {
let current_block_number = self_clone_accounts.get_current_block_number();
let account_rows = sqlx::query_as!(
AccountRow,
"
SELECT DISTINCT ON (address)
address,
nonce,
balance,
bytecode,
code_hash
FROM
neo_accounts
ORDER BY
address,
block_number DESC
"
SELECT DISTINCT ON (address)
address,
nonce,
balance,
bytecode,
code_hash
FROM
neo_accounts
WHERE
block_number > $1
ORDER BY
address,
block_number DESC
",
current_block_number,
)
.fetch_all(&pool_clone_accounts)
.await?;
Expand All @@ -131,20 +135,24 @@ impl HybridStorageState {

// Spawn the second blocking task for slots
let slots_task = tokio::spawn(async move {
let current_block_number = self_clone_slots.get_current_block_number();
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
"
SELECT DISTINCT ON (account_address, slot_index)
account_address,
slot_index,
value
FROM
neo_account_slots
WHERE
block_number > $1
ORDER BY
account_address,
slot_index,
block_number DESC
",
current_block_number,
)
.fetch_all(&pool_clone_slots)
.await?;
Expand All @@ -162,9 +170,9 @@ impl HybridStorageState {
// Check the results of both tasks
for result in results {
match result {
Ok(Ok(())) => continue, // Successfully completed task
Ok(Err(e)) => return Err(e), // Task completed with an error
Err(e) => return Err(anyhow::Error::new(e)), // Task panicked
Ok(Ok(())) => continue,
Ok(Err(e)) => return Err(e),
Err(e) => return Err(anyhow::Error::new(e)),
}
}

Expand Down
13 changes: 13 additions & 0 deletions src/eth/storage/hybrid/rocks_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ impl<K: Serialize + for<'de> Deserialize<'de> + std::hash::Hash + Eq, V: Seriali
bincode::deserialize(&value_bytes).ok()
}

pub fn get_current_block_number(&self) -> i64 {
let Ok(serialized_key) = bincode::serialize(&"current_block") else {
return -1;
};
let Ok(Some(value_bytes)) = self.db.get(serialized_key) else { return -1 };

bincode::deserialize(&value_bytes).ok().unwrap_or(-1)
}

// Mimics the 'insert' functionality of a HashMap
pub fn insert(&self, key: K, value: V) {
let serialized_key = bincode::serialize(&key).unwrap();
Expand Down Expand Up @@ -153,6 +162,10 @@ impl<'a, K: Serialize + for<'de> Deserialize<'de> + std::hash::Hash + Eq, V: Ser
Some(key_value) => {
let (key, value) = key_value.unwrap(); // XXX deal with the result

if key == bincode::serialize(&"current_block").unwrap().into_boxed_slice() {
return self.next();
}

let key: K = bincode::deserialize(&key).unwrap();
let value: V = bincode::deserialize(&value).unwrap();
Some((key, value))
Expand Down

0 comments on commit b921c54

Please sign in to comment.