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
use ethereum::Block as EthereumBlock;
use ethereum_types::{H160, H256, U256};
use std::{marker::PhantomData, sync::Arc};
use sc_client_api::backend::{StorageProvider, Backend, StateBackend, AuxStore};
use sp_blockchain::{Error as BlockChainError, HeaderMetadata, HeaderBackend};
use sp_storage::StorageKey;
use codec::Decode;
use sp_runtime::traits::{Block as BlockT, BlakeTwo256};
use sp_api::BlockId;
use fp_rpc::TransactionStatus;
use super::{StorageOverride, storage_prefix_build, blake2_128_extend};
pub struct SchemaV1Override<B: BlockT, C, BE> {
client: Arc<C>,
_marker: PhantomData<(B, BE)>,
}
impl<B: BlockT, C, BE> SchemaV1Override<B, C, BE> {
pub fn new(client: Arc<C>) -> Self {
Self { client, _marker: PhantomData }
}
}
impl<B, C, BE> SchemaV1Override<B, C, BE> where
C: StorageProvider<B, BE> + AuxStore,
C: HeaderBackend<B> + HeaderMetadata<B, Error=BlockChainError> + 'static,
BE: Backend<B> + 'static,
BE::State: StateBackend<BlakeTwo256>,
B: BlockT<Hash=H256> + Send + Sync + 'static,
C: Send + Sync + 'static,
{
fn query_storage<T: Decode>(&self, id: &BlockId<B>, key: &StorageKey) -> Option<T> {
if let Ok(Some(data)) = self.client.storage(
id,
key
) {
if let Ok(result) = Decode::decode(&mut &data.0[..]) {
return Some(result);
}
}
None
}
}
impl<Block, C, BE> StorageOverride<Block> for SchemaV1Override<Block, C, BE>
where
C: StorageProvider<Block, BE>,
C: AuxStore,
C: HeaderBackend<Block>,
C: HeaderMetadata<Block, Error=BlockChainError> + 'static,
BE: Backend<Block> + 'static,
BE::State: StateBackend<BlakeTwo256>,
Block: BlockT<Hash=H256> + Send + Sync + 'static,
C: Send + Sync + 'static,
{
fn account_code_at(&self, block: &BlockId<Block>, address: H160) -> Option<Vec<u8>> {
let mut key: Vec<u8> = storage_prefix_build(b"EVM", b"AccountCodes");
key.extend(blake2_128_extend(address.as_bytes()));
self.query_storage::<Vec<u8>>(
block,
&StorageKey(key)
)
}
fn storage_at(&self, block: &BlockId<Block>, address: H160, index: U256) -> Option<H256> {
let tmp: &mut [u8; 32] = &mut [0; 32];
index.to_big_endian(tmp);
let mut key: Vec<u8> = storage_prefix_build(b"EVM", b"AccountStorages");
key.extend(blake2_128_extend(address.as_bytes()));
key.extend(blake2_128_extend(tmp));
self.query_storage::<H256>(
block,
&StorageKey(key)
)
}
fn current_block(&self, block: &BlockId<Block>) -> Option<EthereumBlock> {
self.query_storage::<ethereum::Block>(
block,
&StorageKey(
storage_prefix_build(b"Ethereum", b"CurrentBlock")
)
)
}
fn current_receipts(&self, block: &BlockId<Block>) -> Option<Vec<ethereum::Receipt>> {
self.query_storage::<Vec<ethereum::Receipt>>(
block,
&StorageKey(
storage_prefix_build(b"Ethereum", b"CurrentReceipts")
)
)
}
fn current_transaction_statuses(&self, block: &BlockId<Block>) -> Option<Vec<TransactionStatus>> {
self.query_storage::<Vec<TransactionStatus>>(
block,
&StorageKey(
storage_prefix_build(b"Ethereum", b"CurrentTransactionStatuses")
)
)
}
}