Skip to content

Commit

Permalink
Fix cache result handling (#2357)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jouzo authored Aug 19, 2023
1 parent bd26493 commit 49525fd
Showing 1 changed file with 54 additions and 34 deletions.
88 changes: 54 additions & 34 deletions lib/ain-evm/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,31 @@ impl Storage {

impl BlockStorage for Storage {
fn get_block_by_number(&self, number: &U256) -> Result<Option<BlockAny>> {
self.cache.get_block_by_number(number).or_else(|_| {
let block = self.blockstore.get_block_by_number(number);
if let Ok(Some(ref block)) = block {
self.cache.put_block(block)?;
match self.cache.get_block_by_number(number) {
Ok(Some(block)) => Ok(Some(block)),
Ok(None) => {
let block = self.blockstore.get_block_by_number(number);
if let Ok(Some(ref block)) = block {
self.cache.put_block(block)?;
}
block
}
block
})
Err(e) => Err(e),
}
}

fn get_block_by_hash(&self, block_hash: &H256) -> Result<Option<BlockAny>> {
self.cache.get_block_by_hash(block_hash).or_else(|_| {
let block = self.blockstore.get_block_by_hash(block_hash);
if let Ok(Some(ref block)) = block {
self.cache.put_block(block)?;
match self.cache.get_block_by_hash(block_hash) {
Ok(Some(block)) => Ok(Some(block)),
Ok(None) => {
let block = self.blockstore.get_block_by_hash(block_hash);
if let Ok(Some(ref block)) = block {
self.cache.put_block(block)?;
}
block
}
block
})
Err(e) => Err(e),
}
}

fn put_block(&self, block: &BlockAny) -> Result<()> {
Expand All @@ -71,14 +79,17 @@ impl BlockStorage for Storage {
}

fn get_latest_block(&self) -> Result<Option<BlockAny>> {
let block = self.cache.get_latest_block().or_else(|_| {
let latest_block = self.blockstore.get_latest_block();
if let Ok(Some(ref block)) = latest_block {
self.cache.put_latest_block(Some(block))?;
match self.cache.get_latest_block() {
Ok(Some(block)) => Ok(Some(block)),
Ok(None) => {
let block = self.blockstore.get_latest_block();
if let Ok(Some(ref block)) = block {
self.cache.put_latest_block(Some(block))?;
}
block
}
latest_block
})?;
Ok(block)
Err(e) => Err(e),
}
}

fn put_latest_block(&self, block: Option<&BlockAny>) -> Result<()> {
Expand All @@ -96,54 +107,63 @@ impl TransactionStorage for Storage {
}

fn get_transaction_by_hash(&self, hash: &H256) -> Result<Option<TransactionV2>> {
let transaction = self.cache.get_transaction_by_hash(hash).or_else(|_| {
let transaction = self.blockstore.get_transaction_by_hash(hash);
if let Ok(Some(ref transaction)) = transaction {
self.cache.put_transaction(transaction)?;
match self.cache.get_transaction_by_hash(hash) {
Ok(Some(transaction)) => Ok(Some(transaction)),
Ok(None) => {
let transaction = self.blockstore.get_transaction_by_hash(hash);
if let Ok(Some(ref transaction)) = transaction {
self.cache.put_transaction(transaction)?;
}
transaction
}
transaction
})?;
Ok(transaction)
Err(e) => Err(e),
}
}

fn get_transaction_by_block_hash_and_index(
&self,
hash: &H256,
index: usize,
) -> Result<Option<TransactionV2>> {
let transaction = self
match self
.cache
.get_transaction_by_block_hash_and_index(hash, index)
.or_else(|_| {
{
Ok(Some(transaction)) => Ok(Some(transaction)),
Ok(None) => {
let transaction = self
.blockstore
.get_transaction_by_block_hash_and_index(hash, index);
if let Ok(Some(ref transaction)) = transaction {
self.cache.put_transaction(transaction)?;
}
transaction
})?;
Ok(transaction)
}
Err(e) => Err(e),
}
}

fn get_transaction_by_block_number_and_index(
&self,
number: &U256,
index: usize,
) -> Result<Option<TransactionV2>> {
let transaction = self
match self
.cache
.get_transaction_by_block_number_and_index(number, index)
.or_else(|_| {
{
Ok(Some(transaction)) => Ok(Some(transaction)),
Ok(None) => {
let transaction = self
.blockstore
.get_transaction_by_block_number_and_index(number, index);
if let Ok(Some(ref transaction)) = transaction {
self.cache.put_transaction(transaction)?;
}
transaction
})?;
Ok(transaction)
}
Err(e) => Err(e),
}
}

fn put_transaction(&self, transaction: &TransactionV2) -> Result<()> {
Expand Down

0 comments on commit 49525fd

Please sign in to comment.