-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add cache storage layer and GetTransactionBy* RPCs (#1908)
* Add cache storage and RPC impl * Restore block flush()
- Loading branch information
Showing
14 changed files
with
603 additions
and
114 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
use std::{num::NonZeroUsize, sync::RwLock}; | ||
|
||
use ethereum::{BlockAny, TransactionV2}; | ||
use lru::LruCache; | ||
use primitive_types::{H256, U256}; | ||
use std::borrow::ToOwned; | ||
|
||
#[derive(Debug)] | ||
pub struct Cache { | ||
transactions: RwLock<LruCache<H256, TransactionV2>>, | ||
blocks: RwLock<LruCache<U256, BlockAny>>, | ||
block_hashes: RwLock<LruCache<H256, U256>>, | ||
latest_block: RwLock<Option<BlockAny>>, | ||
} | ||
|
||
impl Cache { | ||
const DEFAULT_CACHE_SIZE: usize = 1000; | ||
|
||
pub fn new(cache_size: Option<usize>) -> Self { | ||
Cache { | ||
transactions: RwLock::new(LruCache::new( | ||
NonZeroUsize::new(cache_size.unwrap_or(Self::DEFAULT_CACHE_SIZE)).unwrap(), | ||
)), | ||
blocks: RwLock::new(LruCache::new( | ||
NonZeroUsize::new(cache_size.unwrap_or(Self::DEFAULT_CACHE_SIZE)).unwrap(), | ||
)), | ||
block_hashes: RwLock::new(LruCache::new( | ||
NonZeroUsize::new(cache_size.unwrap_or(Self::DEFAULT_CACHE_SIZE)).unwrap(), | ||
)), | ||
latest_block: RwLock::new(None), | ||
} | ||
} | ||
} | ||
|
||
impl Cache { | ||
pub fn extend_transactions_from_block(&self, block: &BlockAny) { | ||
let mut cache = self.transactions.write().unwrap(); | ||
|
||
for transaction in &block.transactions { | ||
let hash = transaction.hash(); | ||
cache.put(hash, transaction.clone()); | ||
} | ||
} | ||
|
||
pub fn get_transaction_by_hash(&self, hash: &H256) -> Option<TransactionV2> { | ||
self.transactions | ||
.write() | ||
.unwrap() | ||
.get(hash) | ||
.map(ToOwned::to_owned) | ||
} | ||
|
||
pub fn get_transaction_by_block_hash_and_index( | ||
&self, | ||
block_hash: &H256, | ||
index: usize, | ||
) -> Option<TransactionV2> { | ||
self.block_hashes | ||
.write() | ||
.unwrap() | ||
.get(block_hash) | ||
.and_then(|block_number| { | ||
self.get_transaction_by_block_number_and_index(&block_number, index) | ||
}) | ||
} | ||
|
||
pub fn get_transaction_by_block_number_and_index( | ||
&self, | ||
block_number: &U256, | ||
index: usize, | ||
) -> Option<TransactionV2> { | ||
self.blocks | ||
.write() | ||
.unwrap() | ||
.get(block_number)? | ||
.transactions | ||
.get(index) | ||
.map(ToOwned::to_owned) | ||
} | ||
} | ||
|
||
// Block impl | ||
impl Cache { | ||
pub fn get_block_by_number(&self, number: &U256) -> Option<BlockAny> { | ||
self.blocks | ||
.write() | ||
.unwrap() | ||
.get(number) | ||
.map(ToOwned::to_owned) | ||
} | ||
|
||
pub fn get_block_by_hash(&self, block_hash: &H256) -> Option<BlockAny> { | ||
self.block_hashes | ||
.write() | ||
.unwrap() | ||
.get(block_hash) | ||
.and_then(|block_number| self.get_block_by_number(&block_number)) | ||
} | ||
|
||
pub fn put_block(&self, block: BlockAny) { | ||
self.extend_transactions_from_block(&block); | ||
|
||
let block_number = block.header.number; | ||
let hash = block.header.hash(); | ||
self.blocks.write().unwrap().put(block_number, block); | ||
self.block_hashes.write().unwrap().put(hash, block_number); | ||
} | ||
} | ||
|
||
// Latest block impl | ||
impl Cache { | ||
pub fn get_latest_block(&self) -> Option<BlockAny> { | ||
self.latest_block | ||
.read() | ||
.unwrap() | ||
.as_ref() | ||
.map(ToOwned::to_owned) | ||
} | ||
|
||
pub fn put_latest_block(&self, block: BlockAny) { | ||
let mut cache = self.latest_block.write().unwrap(); | ||
*cache = Some(block); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use crate::cache::Cache; | ||
use ethereum::{BlockAny, TransactionV2}; | ||
use primitive_types::{H256, U256}; | ||
|
||
#[derive(Debug)] | ||
pub struct Storage { | ||
cache: Cache, | ||
} | ||
|
||
// TODO : Add DB and pull from DB when cache miss | ||
impl Storage { | ||
pub fn new() -> Self { | ||
Self { | ||
cache: Cache::new(None), | ||
} | ||
} | ||
} | ||
|
||
// Block storage | ||
impl Storage { | ||
pub fn get_block_by_number(&self, number: &U256) -> Option<BlockAny> { | ||
self.cache.get_block_by_number(number) | ||
} | ||
|
||
pub fn get_block_by_hash(&self, block_hash: &H256) -> Option<BlockAny> { | ||
self.cache.get_block_by_hash(block_hash) | ||
} | ||
|
||
pub fn put_block(&self, block: BlockAny) { | ||
self.cache.put_block(block) | ||
} | ||
} | ||
|
||
// Transaction storage | ||
impl Storage { | ||
pub fn get_transaction_by_hash(&self, hash: H256) -> Option<TransactionV2> { | ||
self.cache.get_transaction_by_hash(&hash) | ||
} | ||
|
||
pub fn get_transaction_by_block_hash_and_index( | ||
&self, | ||
hash: H256, | ||
index: usize, | ||
) -> Option<TransactionV2> { | ||
self.cache | ||
.get_transaction_by_block_hash_and_index(&hash, index) | ||
} | ||
|
||
pub fn get_transaction_by_block_number_and_index( | ||
&self, | ||
hash: U256, | ||
index: usize, | ||
) -> Option<TransactionV2> { | ||
self.cache | ||
.get_transaction_by_block_number_and_index(&hash, index) | ||
} | ||
} | ||
|
||
// Latest block storage | ||
impl Storage { | ||
pub fn get_latest_block(&self) -> Option<BlockAny> { | ||
self.cache.get_latest_block() | ||
} | ||
|
||
pub fn put_latest_block(&self, block: BlockAny) { | ||
self.cache.put_latest_block(block) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.