diff --git a/.sqlx/query-3aa3f853d45b3fadcc9eb1a80cc45233482a14c82ac8a6630922bdcefae3cfd8.json b/.sqlx/query-3aa3f853d45b3fadcc9eb1a80cc45233482a14c82ac8a6630922bdcefae3cfd8.json deleted file mode 100644 index ebdf6112e..000000000 --- a/.sqlx/query-3aa3f853d45b3fadcc9eb1a80cc45233482a14c82ac8a6630922bdcefae3cfd8.json +++ /dev/null @@ -1,32 +0,0 @@ -{ - "db_name": "PostgreSQL", - "query": "\n SELECT DISTINCT ON (account_address, slot_index)\n account_address,\n slot_index,\n value\n FROM\n neo_account_slots\n ORDER BY\n account_address,\n slot_index,\n block_number DESC\n ", - "describe": { - "columns": [ - { - "ordinal": 0, - "name": "account_address", - "type_info": "Bytea" - }, - { - "ordinal": 1, - "name": "slot_index", - "type_info": "Bytea" - }, - { - "ordinal": 2, - "name": "value", - "type_info": "Bytea" - } - ], - "parameters": { - "Left": [] - }, - "nullable": [ - false, - false, - false - ] - }, - "hash": "3aa3f853d45b3fadcc9eb1a80cc45233482a14c82ac8a6630922bdcefae3cfd8" -} diff --git a/.sqlx/query-2317a0db261ebe5274550bfd83028fab15194d30ff46de0488113858562eb5f5.json b/.sqlx/query-e0ee78b433b1534cb9bdda76c879f87eeff0cbc426765dd616f70a8015196aa3.json similarity index 54% rename from .sqlx/query-2317a0db261ebe5274550bfd83028fab15194d30ff46de0488113858562eb5f5.json rename to .sqlx/query-e0ee78b433b1534cb9bdda76c879f87eeff0cbc426765dd616f70a8015196aa3.json index a112e109d..b0a6a7d63 100644 --- a/.sqlx/query-2317a0db261ebe5274550bfd83028fab15194d30ff46de0488113858562eb5f5.json +++ b/.sqlx/query-e0ee78b433b1534cb9bdda76c879f87eeff0cbc426765dd616f70a8015196aa3.json @@ -1,6 +1,6 @@ { "db_name": "PostgreSQL", - "query": "\n SELECT DISTINCT ON (address)\n address,\n nonce,\n balance,\n bytecode,\n code_hash\n FROM\n neo_accounts\n ORDER BY\n address,\n block_number DESC\n ", + "query": "\n SELECT DISTINCT ON (address)\n address,\n nonce,\n balance,\n bytecode,\n code_hash\n FROM\n neo_accounts\n WHERE\n block_number > $1\n ORDER BY\n address,\n block_number DESC\n ", "describe": { "columns": [ { @@ -30,7 +30,9 @@ } ], "parameters": { - "Left": [] + "Left": [ + "Int8" + ] }, "nullable": [ false, @@ -40,5 +42,5 @@ false ] }, - "hash": "2317a0db261ebe5274550bfd83028fab15194d30ff46de0488113858562eb5f5" + "hash": "e0ee78b433b1534cb9bdda76c879f87eeff0cbc426765dd616f70a8015196aa3" } diff --git a/.sqlx/query-fc28957dbf90502fd28723241c2f686e23c971a3b67391d63e5dadd690808e11.json b/.sqlx/query-fc28957dbf90502fd28723241c2f686e23c971a3b67391d63e5dadd690808e11.json new file mode 100644 index 000000000..9440abd91 --- /dev/null +++ b/.sqlx/query-fc28957dbf90502fd28723241c2f686e23c971a3b67391d63e5dadd690808e11.json @@ -0,0 +1,34 @@ +{ + "db_name": "PostgreSQL", + "query": "\n SELECT DISTINCT ON (account_address, slot_index)\n account_address,\n slot_index,\n value\n FROM\n neo_account_slots\n WHERE\n block_number > $1\n ORDER BY\n account_address,\n slot_index,\n block_number DESC\n ", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "account_address", + "type_info": "Bytea" + }, + { + "ordinal": 1, + "name": "slot_index", + "type_info": "Bytea" + }, + { + "ordinal": 2, + "name": "value", + "type_info": "Bytea" + } + ], + "parameters": { + "Left": [ + "Int8" + ] + }, + "nullable": [ + false, + false, + false + ] + }, + "hash": "fc28957dbf90502fd28723241c2f686e23c971a3b67391d63e5dadd690808e11" +} diff --git a/src/eth/storage/hybrid/hybrid_state.rs b/src/eth/storage/hybrid/hybrid_state.rs index 354d2e3d5..b9ad34b9a 100644 --- a/src/eth/storage/hybrid/hybrid_state.rs +++ b/src/eth/storage/hybrid/hybrid_state.rs @@ -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?; @@ -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?; @@ -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)), } } diff --git a/src/eth/storage/hybrid/rocks_db.rs b/src/eth/storage/hybrid/rocks_db.rs index 63bc67cf6..4aef9dfc0 100644 --- a/src/eth/storage/hybrid/rocks_db.rs +++ b/src/eth/storage/hybrid/rocks_db.rs @@ -69,6 +69,15 @@ impl 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(); @@ -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))