Skip to content

Commit dde89cb

Browse files
committed
extend ethereum api param
1 parent 3898ada commit dde89cb

11 files changed

Lines changed: 86 additions & 46 deletions

File tree

client/cli/src/frontier_db_cmd/mapping_db.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use serde::Deserialize;
2323
// Substrate
2424
use sp_api::ProvideRuntimeApi;
2525
use sp_blockchain::HeaderBackend;
26+
use sp_runtime::SaturatedConversion;
2627
use sp_runtime::traits::Block as BlockT;
2728
// Frontier
2829
use fp_rpc::EthereumRuntimeRPCApi;
@@ -83,10 +84,15 @@ where
8384
.block_hash(ethereum_block_hash)?
8485
.is_none()
8586
{
87+
let substrate_block_num = self
88+
.client
89+
.number(*substrate_block_hash)
90+
.map_err(|e| format!("{:?}", e))?
91+
.ok_or(format!("substrate block hash: {substrate_block_hash:?} query block number return none"))?;
8692
let existing_transaction_hashes: Vec<H256> = if let Some(statuses) = self
8793
.client
8894
.runtime_api()
89-
.current_transaction_statuses(*substrate_block_hash)
95+
.current_transaction_statuses(*substrate_block_hash, substrate_block_num.saturated_into::<u128>().into())
9096
.map_err(|e| format!("{:?}", e))?
9197
{
9298
statuses
@@ -141,10 +147,15 @@ where
141147
.block_hash(ethereum_block_hash)?
142148
.is_some()
143149
{
150+
let substrate_block_num = self
151+
.client
152+
.number(*substrate_block_hash)
153+
.map_err(|e| format!("{:?}", e))?
154+
.ok_or(format!("substrate block hash: {substrate_block_hash:?} query block number return none"))?;
144155
let existing_transaction_hashes: Vec<H256> = if let Some(statuses) = self
145156
.client
146157
.runtime_api()
147-
.current_transaction_statuses(*substrate_block_hash)
158+
.current_transaction_statuses(*substrate_block_hash, substrate_block_num.saturated_into::<u128>().into())
148159
.map_err(|e| format!("{:?}", e))?
149160
{
150161
statuses

client/db/src/sql/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ where
229229
// Read from the runtime and store the block metadata.
230230
let ethereum_block = client
231231
.runtime_api()
232-
.current_block(substrate_genesis_hash)
232+
.current_block(substrate_genesis_hash, Default::default())
233233
.expect("runtime api reachable")
234234
.expect("ethereum genesis block");
235235

client/mapping-sync/src/kv/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use sc_client_api::backend::{Backend, StorageProvider};
2929
use sp_api::{ApiExt, ProvideRuntimeApi};
3030
use sp_blockchain::{Backend as _, HeaderBackend};
3131
use sp_consensus::SyncOracle;
32+
use sp_runtime::SaturatedConversion;
3233
use sp_runtime::traits::{Block as BlockT, Header as HeaderT, Zero};
3334
// Frontier
3435
use fc_storage::OverrideHandle;
@@ -128,7 +129,7 @@ where
128129
let block = if api_version > 1 {
129130
client
130131
.runtime_api()
131-
.current_block(substrate_block_hash)
132+
.current_block(substrate_block_hash, (*header.number()).saturated_into::<u128>().into())
132133
.map_err(|e| format!("{:?}", e))?
133134
} else {
134135
#[allow(deprecated)]

client/rpc/src/eth/block.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ where
128128
let statuses = block_data_cache
129129
.current_transaction_statuses(schema, substrate_hash)
130130
.await;
131-
131+
log::warn!("block_by_number: substrate_hash: {substrate_hash:?}, block: {block:?}, statuses: {statuses:?}");
132132
let base_fee = client.runtime_api().gas_price(substrate_hash).ok();
133133

134134
match (block, statuses) {
@@ -187,6 +187,7 @@ where
187187
.map_err(|_| internal_err(format!("Runtime access error at {}", best_hash)))?;
188188

189189
let base_fee = api.gas_price(best_hash).ok();
190+
log::warn!("block_by_number: best_hash: {best_hash:?}, block: {block:?}, statuses: {statuses:?}");
190191

191192
match (block, statuses) {
192193
(Some(block), Some(statuses)) => Ok(Some(rich_block_build(
@@ -200,7 +201,10 @@ where
200201
_ => Ok(None),
201202
}
202203
}
203-
None => Ok(None),
204+
None => {
205+
log::warn!("block_by_number: none");
206+
Ok(None)
207+
},
204208
}
205209
}
206210

client/rpc/src/eth/execute.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use scale_codec::{Decode, Encode};
2525
// Substrate
2626
use sc_client_api::backend::{Backend, StorageProvider};
2727
use sc_transaction_pool::{ChainApi, RCGroup};
28-
use sp_api::{ApiExt, CallApiAt, CallApiAtParams, ProvideRuntimeApi, StorageTransactionCache};
28+
use sp_api::{ApiExt, BlockId, CallApiAt, CallApiAtParams, ProvideRuntimeApi, StorageTransactionCache};
2929
use sp_block_builder::BlockBuilder as BlockBuilderApi;
3030
use sp_blockchain::HeaderBackend;
3131
use sp_core::ExecutionContext;
@@ -103,7 +103,7 @@ where
103103
)
104104
};
105105

106-
let (substrate_hash, api) = match frontier_backend_client::native_block_id::<B, C>(
106+
let (substrate_hash, substrate_block_num, api) = match frontier_backend_client::native_block_id::<B, C>(
107107
self.client.as_ref(),
108108
self.backend.as_ref(),
109109
number,
@@ -115,13 +115,21 @@ where
115115
.client
116116
.expect_block_hash_from_id(&id)
117117
.map_err(|_| crate::err(JSON_RPC_ERROR_DEFAULT, "header not found", None))?;
118-
(hash, self.client.runtime_api())
118+
let num = match id {
119+
BlockId::Hash(hash) => {
120+
self.client.number(hash).map_err(|e| crate::err(JSON_RPC_ERROR_DEFAULT, e.to_string(), None))?
121+
.ok_or(crate::err(JSON_RPC_ERROR_DEFAULT, "Block number not found", None))?
122+
},
123+
BlockId::Number(num) => num,
124+
};
125+
(hash, num, self.client.runtime_api())
119126
}
120127
None => {
121128
// Not mapped in the db, assume pending.
122129
let hash = self.client.info().best_hash;
130+
let num = self.client.info().best_number;
123131
let api = pending_runtime_api(self.client.as_ref(), self.graph.as_ref())?;
124-
(hash, api)
132+
(hash, num, api)
125133
}
126134
};
127135

@@ -134,7 +142,7 @@ where
134142
};
135143

136144
let block = if api_version > 1 {
137-
api.current_block(substrate_hash)
145+
api.current_block(substrate_hash, substrate_block_num.saturated_into::<u128>().into())
138146
.map_err(|err| internal_err(format!("runtime error: {:?}", err)))?
139147
} else {
140148
#[allow(deprecated)]

client/rpc/src/eth/submit.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use sp_api::{ApiExt, ProvideRuntimeApi};
2727
use sp_block_builder::BlockBuilder as BlockBuilderApi;
2828
use sp_blockchain::HeaderBackend;
2929
use sp_runtime::{
30-
generic::BlockId, traits::Block as BlockT, transaction_validity::TransactionSource,
30+
generic::BlockId, traits::Block as BlockT, transaction_validity::TransactionSource, SaturatedConversion,
3131
};
3232
// Frontier
3333
use fc_rpc_core::types::*;
@@ -81,12 +81,13 @@ where
8181
};
8282

8383
let hash = self.client.info().best_hash;
84+
let number = self.client.info().best_number;
8485

8586
let gas_price = request.gas_price;
8687
let gas_limit = match request.gas {
8788
Some(gas_limit) => gas_limit,
8889
None => {
89-
let block = self.client.runtime_api().current_block(hash);
90+
let block = self.client.runtime_api().current_block(hash, number.saturated_into::<u128>().into());
9091
if let Ok(Some(block)) = block {
9192
block.header.gas_limit
9293
} else {

client/rpc/src/eth/transaction.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ where
376376
}
377377
}
378378
};
379-
379+
log::warn!("");
380380
let status = statuses[index].clone();
381381
let mut cumulative_receipts = receipts;
382382
cumulative_receipts.truncate((status.transaction_index + 1) as usize);

client/storage/src/overrides/mod.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ use ethereum::BlockV2 as EthereumBlock;
2222
use ethereum_types::{H160, H256, U256};
2323
// Substrate
2424
use sp_api::{ApiExt, ProvideRuntimeApi};
25+
use sp_blockchain::HeaderBackend;
2526
use sp_io::hashing::{blake2_128, twox_128};
26-
use sp_runtime::{traits::Block as BlockT, Permill};
27+
use sp_runtime::{traits::Block as BlockT, Permill, SaturatedConversion};
2728
// Frontier
2829
use fp_rpc::{EthereumRuntimeRPCApi, TransactionStatus};
2930
use fp_storage::EthereumStorageSchema;
@@ -96,7 +97,7 @@ impl<B: BlockT, C> RuntimeApiStorageOverride<B, C> {
9697
impl<Block, C> StorageOverride<Block> for RuntimeApiStorageOverride<Block, C>
9798
where
9899
Block: BlockT,
99-
C: ProvideRuntimeApi<Block> + Send + Sync,
100+
C: ProvideRuntimeApi<Block> + Send + Sync + HeaderBackend<Block>,
100101
C::Api: EthereumRuntimeRPCApi<Block>,
101102
{
102103
/// For a given account address, returns pallet_evm::AccountCodes.
@@ -131,7 +132,8 @@ where
131132
let old_block = api.current_block_before_version_2(block_hash).ok()?;
132133
old_block.map(|block| block.into())
133134
} else {
134-
api.current_block(block_hash).ok()?
135+
let number = self.client.number(block_hash).ok()??;
136+
api.current_block(block_hash, number.saturated_into::<u128>().into()).ok()?
135137
}
136138
}
137139

@@ -163,9 +165,10 @@ where
163165
.collect()
164166
})
165167
} else {
168+
let number = self.client.number(block_hash).ok()??;
166169
self.client
167170
.runtime_api()
168-
.current_receipts(block_hash)
171+
.current_receipts(block_hash, number.saturated_into::<u128>().into())
169172
.ok()?
170173
}
171174
}
@@ -175,9 +178,10 @@ where
175178
&self,
176179
block_hash: Block::Hash,
177180
) -> Option<Vec<TransactionStatus>> {
181+
let number = self.client.number(block_hash).ok()??;
178182
self.client
179183
.runtime_api()
180-
.current_transaction_statuses(block_hash)
184+
.current_transaction_statuses(block_hash, number.saturated_into::<u128>().into())
181185
.ok()?
182186
}
183187

frame/ethereum/src/lib.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -332,15 +332,15 @@ pub mod pallet {
332332

333333
/// The current Ethereum block.
334334
#[pallet::storage]
335-
pub type CurrentBlock<T: Config> = StorageValue<_, ethereum::BlockV2>;
335+
pub type CurrentBlock<T: Config> = StorageMap<_, Twox64Concat, U256, ethereum::BlockV2>;
336336

337337
/// The current Ethereum receipts.
338338
#[pallet::storage]
339-
pub type CurrentReceipts<T: Config> = StorageValue<_, Vec<Receipt>>;
339+
pub type CurrentReceipts<T: Config> = StorageMap<_, Twox64Concat, U256, Vec<Receipt>>;
340340

341341
/// The current transaction statuses.
342342
#[pallet::storage]
343-
pub type CurrentTransactionStatuses<T: Config> = StorageValue<_, Vec<TransactionStatus>>;
343+
pub type CurrentTransactionStatuses<T: Config> = StorageMap<_, Twox64Concat, U256, Vec<TransactionStatus>>;
344344

345345
// Mapping for block number and hashes.
346346
#[pallet::storage]
@@ -488,9 +488,9 @@ impl<T: Config> Pallet<T> {
488488

489489
#[cfg(feature = "std")]
490490
let block_time = block_timer.elapsed().as_micros();
491-
CurrentBlock::<T>::put(block.clone());
492-
CurrentReceipts::<T>::put(receipts.clone());
493-
CurrentTransactionStatuses::<T>::put(statuses.clone());
491+
CurrentBlock::<T>::insert(&block_number, block.clone());
492+
CurrentReceipts::<T>::insert(&block_number, receipts.clone());
493+
CurrentTransactionStatuses::<T>::insert(&block_number, statuses.clone());
494494
BlockHash::<T>::insert(block_number, block.header.hash());
495495

496496
match post_log {
@@ -766,8 +766,8 @@ impl<T: Config> Pallet<T> {
766766
}
767767

768768
/// Get current block hash
769-
pub fn current_block_hash() -> Option<H256> {
770-
<CurrentBlock<T>>::get().map(|block| block.header.hash())
769+
pub fn current_block_hash(number: &U256) -> Option<H256> {
770+
<CurrentBlock<T>>::get(number).map(|block| block.header.hash())
771771
}
772772

773773
/// Execute an Ethereum transaction.

primitives/rpc/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,14 +197,14 @@ sp_api::decl_runtime_apis! {
197197
#[changed_in(2)]
198198
fn current_block() -> Option<ethereum::BlockV0>;
199199
/// Return the current block.
200-
fn current_block() -> Option<ethereum::BlockV2>;
200+
fn current_block(block_number: U256) -> Option<ethereum::BlockV2>;
201201
/// Return the current receipt.
202202
#[changed_in(4)]
203203
fn current_receipts() -> Option<Vec<ethereum::ReceiptV0>>;
204204
/// Return the current receipt.
205-
fn current_receipts() -> Option<Vec<ethereum::ReceiptV3>>;
205+
fn current_receipts(block_number: U256) -> Option<Vec<ethereum::ReceiptV3>>;
206206
/// Return the current transaction status.
207-
fn current_transaction_statuses() -> Option<Vec<TransactionStatus>>;
207+
fn current_transaction_statuses(block_number: U256) -> Option<Vec<TransactionStatus>>;
208208
/// Return all the current data for a block in a single runtime call. Legacy.
209209
#[changed_in(2)]
210210
fn current_all() -> (
@@ -219,7 +219,7 @@ sp_api::decl_runtime_apis! {
219219
Option<Vec<ethereum::ReceiptV0>>,
220220
Option<Vec<TransactionStatus>>
221221
);
222-
fn current_all() -> (
222+
fn current_all(block_number: U256) -> (
223223
Option<ethereum::BlockV2>,
224224
Option<Vec<ethereum::ReceiptV3>>,
225225
Option<Vec<TransactionStatus>>

0 commit comments

Comments
 (0)