-
Notifications
You must be signed in to change notification settings - Fork 129
Expand file tree
/
Copy pathcache.rs
More file actions
162 lines (142 loc) · 4.74 KB
/
Copy pathcache.rs
File metadata and controls
162 lines (142 loc) · 4.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use std::{borrow::ToOwned, num::NonZeroUsize, sync::RwLock};
use ethereum::{BlockAny, TransactionV2};
use ethereum_types::{H256, U256};
use lru::LruCache;
use super::traits::{BlockStorage, Rollback, TransactionStorage};
use crate::Result;
#[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 BlockStorage for Cache {
fn get_block_by_number(&self, number: &U256) -> Result<Option<BlockAny>> {
let block = self
.blocks
.write()
.unwrap()
.get(number)
.map(ToOwned::to_owned);
Ok(block)
}
fn get_block_by_hash(&self, block_hash: &H256) -> Result<Option<BlockAny>> {
self.block_hashes
.write()
.unwrap()
.get(block_hash)
.map_or(Ok(None), |block_number| {
self.get_block_by_number(block_number)
})
}
fn put_block(&self, block: &BlockAny) -> Result<()> {
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.clone());
self.block_hashes.write().unwrap().put(hash, block_number);
Ok(())
}
fn get_latest_block(&self) -> Result<Option<BlockAny>> {
let block = self
.latest_block
.read()
.unwrap()
.as_ref()
.map(ToOwned::to_owned);
Ok(block)
}
fn put_latest_block(&self, block: Option<&BlockAny>) -> Result<()> {
let mut cache = self.latest_block.write().unwrap();
*cache = block.cloned();
Ok(())
}
}
impl TransactionStorage for Cache {
fn extend_transactions_from_block(&self, block: &BlockAny) -> Result<()> {
let mut cache = self.transactions.write().unwrap();
for transaction in &block.transactions {
let hash = transaction.hash();
cache.put(hash, transaction.clone());
}
Ok(())
}
fn get_transaction_by_hash(&self, hash: &H256) -> Result<Option<TransactionV2>> {
let transaction = self
.transactions
.write()
.unwrap()
.get(hash)
.map(ToOwned::to_owned);
Ok(transaction)
}
fn get_transaction_by_block_hash_and_index(
&self,
block_hash: &H256,
index: usize,
) -> Result<Option<TransactionV2>> {
self.block_hashes
.write()
.unwrap()
.get(block_hash)
.map_or(Ok(None), |block_number| {
self.get_transaction_by_block_number_and_index(block_number, index)
})
}
fn get_transaction_by_block_number_and_index(
&self,
block_number: &U256,
index: usize,
) -> Result<Option<TransactionV2>> {
let transaction = self
.blocks
.write()
.unwrap()
.get(block_number)
.and_then(|block| block.transactions.get(index).map(ToOwned::to_owned));
Ok(transaction)
}
fn put_transaction(&self, transaction: &TransactionV2) -> Result<()> {
self.transactions
.write()
.unwrap()
.put(transaction.hash(), transaction.clone());
Ok(())
}
}
impl Rollback for Cache {
fn disconnect_latest_block(&self) -> Result<()> {
if let Some(block) = self.get_latest_block()? {
let mut transaction_cache = self.transactions.write().unwrap();
for tx in &block.transactions {
transaction_cache.pop(&tx.hash());
}
self.block_hashes.write().unwrap().pop(&block.header.hash());
self.blocks.write().unwrap().pop(&block.header.number);
let previous_block = self.get_block_by_hash(&block.header.parent_hash)?;
self.put_latest_block(previous_block.as_ref())?;
}
Ok(())
}
}