forked from moonbeam-foundation/frontier
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathblock.rs
More file actions
213 lines (188 loc) · 5.94 KB
/
block.rs
File metadata and controls
213 lines (188 loc) · 5.94 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
// This file is part of Frontier.
//
// Copyright (c) 2022 Parity Technologies (UK) Ltd.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use std::sync::Arc;
use ethereum_types::{H256, U256};
use jsonrpsee::core::RpcResult as Result;
// Substrate
use sc_client_api::backend::{Backend, StateBackend, StorageProvider};
use sc_network_common::ExHashT;
use sc_transaction_pool::ChainApi;
use sp_api::ProvideRuntimeApi;
use sp_blockchain::HeaderBackend;
use sp_core::hashing::keccak_256;
use sp_runtime::traits::{BlakeTwo256, Block as BlockT};
// Frontier
use fc_rpc_core::types::*;
use fp_rpc::EthereumRuntimeRPCApi;
use crate::{
eth::{rich_block_build, Eth},
frontier_backend_client, internal_err,
};
impl<B, C, P, CT, BE, H: ExHashT, A: ChainApi, EGA> Eth<B, C, P, CT, BE, H, A, EGA>
where
B: BlockT<Hash = H256> + Send + Sync + 'static,
C: StorageProvider<B, BE> + HeaderBackend<B> + Send + Sync + 'static,
C: ProvideRuntimeApi<B>,
C::Api: EthereumRuntimeRPCApi<B>,
BE: Backend<B> + 'static,
BE::State: StateBackend<BlakeTwo256>,
{
pub async fn block_by_hash(&self, hash: H256, full: bool) -> Result<Option<RichBlock>> {
let client = Arc::clone(&self.client);
let block_data_cache = Arc::clone(&self.block_data_cache);
let backend = Arc::clone(&self.backend);
let id = match frontier_backend_client::load_hash::<B, C>(
client.as_ref(),
backend.as_ref(),
hash,
)
.map_err(|err| internal_err(format!("{:?}", err)))?
{
Some(hash) => hash,
_ => return Ok(None),
};
let substrate_hash = client
.expect_block_hash_from_id(&id)
.map_err(|_| internal_err(format!("Expect block number from id: {}", id)))?;
let schema =
frontier_backend_client::onchain_storage_schema::<B, C, BE>(client.as_ref(), id);
let block = block_data_cache.current_block(schema, substrate_hash).await;
let statuses = block_data_cache
.current_transaction_statuses(schema, substrate_hash)
.await;
let base_fee = client.runtime_api().gas_price(&id).unwrap_or_default();
match (block, statuses) {
(Some(block), Some(statuses)) => Ok(Some(rich_block_build(
block,
statuses.into_iter().map(Option::Some).collect(),
Some(hash),
full,
Some(base_fee),
))),
_ => Ok(None),
}
}
pub async fn block_by_number(
&self,
number: BlockNumber,
full: bool,
) -> Result<Option<RichBlock>> {
let client = Arc::clone(&self.client);
let block_data_cache = Arc::clone(&self.block_data_cache);
let backend = Arc::clone(&self.backend);
let id = match frontier_backend_client::native_block_id::<B, C>(
client.as_ref(),
backend.as_ref(),
Some(number),
)? {
Some(id) => id,
None => return Ok(None),
};
let substrate_hash = client
.expect_block_hash_from_id(&id)
.map_err(|_| internal_err(format!("Expect block number from id: {}", id)))?;
let schema =
frontier_backend_client::onchain_storage_schema::<B, C, BE>(client.as_ref(), id);
let block = block_data_cache.current_block(schema, substrate_hash).await;
let statuses = block_data_cache
.current_transaction_statuses(schema, substrate_hash)
.await;
let base_fee = client.runtime_api().gas_price(&id).unwrap_or_default();
match (block, statuses) {
(Some(block), Some(statuses)) => {
let hash = H256::from(keccak_256(&rlp::encode(&block.header)));
Ok(Some(rich_block_build(
block,
statuses.into_iter().map(Option::Some).collect(),
Some(hash),
full,
Some(base_fee),
)))
}
_ => Ok(None),
}
}
pub fn block_transaction_count_by_hash(&self, hash: H256) -> Result<Option<U256>> {
let id = match frontier_backend_client::load_hash::<B, C>(
self.client.as_ref(),
self.backend.as_ref(),
hash,
)
.map_err(|err| internal_err(format!("{:?}", err)))?
{
Some(hash) => hash,
_ => return Ok(None),
};
let schema =
frontier_backend_client::onchain_storage_schema::<B, C, BE>(self.client.as_ref(), id);
let block = self
.overrides
.schemas
.get(&schema)
.unwrap_or(&self.overrides.fallback)
.current_block(&id);
match block {
Some(block) => Ok(Some(U256::from(block.transactions.len()))),
None => Ok(None),
}
}
pub fn block_transaction_count_by_number(&self, number: BlockNumber) -> Result<Option<U256>> {
if let BlockNumber::Pending = number {
// get the pending transactions count
return Ok(Some(U256::from(
self.graph.validated_pool().ready().count(),
)));
}
let id = match frontier_backend_client::native_block_id::<B, C>(
self.client.as_ref(),
self.backend.as_ref(),
Some(number),
)? {
Some(id) => id,
None => return Ok(None),
};
let schema =
frontier_backend_client::onchain_storage_schema::<B, C, BE>(self.client.as_ref(), id);
let block = self
.overrides
.schemas
.get(&schema)
.unwrap_or(&self.overrides.fallback)
.current_block(&id);
match block {
Some(block) => Ok(Some(U256::from(block.transactions.len()))),
None => Ok(None),
}
}
pub fn block_uncles_count_by_hash(&self, _: H256) -> Result<U256> {
Ok(U256::zero())
}
pub fn block_uncles_count_by_number(&self, _: BlockNumber) -> Result<U256> {
Ok(U256::zero())
}
pub fn uncle_by_block_hash_and_index(&self, _: H256, _: Index) -> Result<Option<RichBlock>> {
Ok(None)
}
pub fn uncle_by_block_number_and_index(
&self,
_: BlockNumber,
_: Index,
) -> Result<Option<RichBlock>> {
Ok(None)
}
}