From a196e18c82caf411a5aadbeed09029acc6d651cf Mon Sep 17 00:00:00 2001 From: tgmichel Date: Mon, 8 Jun 2020 14:48:33 +0200 Subject: [PATCH 1/2] Implement storage_at Runtime API --- frame/ethereum/src/lib.rs | 3 ++- rpc/core/src/eth.rs | 2 +- rpc/primitives/src/lib.rs | 1 + rpc/src/lib.rs | 19 +++++++++++++++++-- template/runtime/src/lib.rs | 8 ++++++++ 5 files changed, 29 insertions(+), 4 deletions(-) diff --git a/frame/ethereum/src/lib.rs b/frame/ethereum/src/lib.rs index 2a60b9730e..6106b3b580 100644 --- a/frame/ethereum/src/lib.rs +++ b/frame/ethereum/src/lib.rs @@ -30,7 +30,8 @@ use sp_runtime::{ traits::UniqueSaturatedInto, transaction_validity::{TransactionValidity, TransactionSource, ValidTransaction} }; -use sha3::{Digest, Keccak256}; +pub use rlp; +pub use sha3::{Digest, Keccak256}; pub use frontier_rpc_primitives::TransactionStatus; pub use ethereum::{Transaction, Log}; diff --git a/rpc/core/src/eth.rs b/rpc/core/src/eth.rs index 075e7d4be5..2b7a6bf172 100644 --- a/rpc/core/src/eth.rs +++ b/rpc/core/src/eth.rs @@ -77,7 +77,7 @@ pub trait EthApi { /// Returns content of the storage at given address. #[rpc(name = "eth_getStorageAt")] - fn storage_at(&self, _: H160, _: U256, _: Option) -> BoxFuture; + fn storage_at(&self, _: H160, _: U256, _: Option) -> Result; /// Returns block with given hash. #[rpc(name = "eth_getBlockByHash")] diff --git a/rpc/primitives/src/lib.rs b/rpc/primitives/src/lib.rs index 5cc0721c22..219b894716 100644 --- a/rpc/primitives/src/lib.rs +++ b/rpc/primitives/src/lib.rs @@ -42,6 +42,7 @@ sp_api::decl_runtime_apis! { fn gas_price() -> U256; fn account_code_at(address: H160) -> Vec; fn author() -> H160; + fn storage_at(address: H160, index: U256) -> H256; } } diff --git a/rpc/src/lib.rs b/rpc/src/lib.rs index 41f9213392..953e7a00e6 100644 --- a/rpc/src/lib.rs +++ b/rpc/src/lib.rs @@ -160,8 +160,23 @@ impl EthApiT for EthApi where unimplemented!("proof"); } - fn storage_at(&self, _: H160, _: U256, _: Option) -> BoxFuture { - unimplemented!("storage_at"); + fn storage_at(&self, address: H160, index: U256, number: Option) -> Result { + if let Some(number) = number { + if number != BlockNumber::Latest { + unimplemented!("fetch storage for past blocks is not yet supported"); + } + } + let header = self + .select_chain + .best_chain() + .map_err(|_| internal_err("fetch header failed"))?; + Ok( + self.client + .runtime_api() + .storage_at(&BlockId::Hash(header.hash()), address, index) + .map_err(|_| internal_err("fetch runtime chain id failed"))? + .into(), + ) } fn block_by_hash(&self, _: H256, _: bool) -> BoxFuture> { diff --git a/template/runtime/src/lib.rs b/template/runtime/src/lib.rs index 629ab2152a..c6f2d34e06 100644 --- a/template/runtime/src/lib.rs +++ b/template/runtime/src/lib.rs @@ -62,6 +62,7 @@ pub use frame_support::{ pub use sp_runtime::BuildStorage; pub use sp_runtime::{Perbill, Permill}; pub use timestamp::Call as TimestampCall; +use ethereum::{Digest as Sha3Digest, Keccak256}; /// An index to a block. pub type BlockNumber = u32; @@ -470,6 +471,13 @@ impl_runtime_apis! { H160::zero() } } + + fn storage_at(address: H160, index: U256) -> H256 { + let index = H256::from_slice( + Keccak256::digest(ðereum::rlp::encode(&index)[..]).as_slice(), + ); + evm::Module::::account_storages(address,index) + } } impl pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi< From 76d8e836c4e6a6626afc2bc0e6e9f2e11b975ebe Mon Sep 17 00:00:00 2001 From: tgmichel Date: Wed, 17 Jun 2020 18:40:11 +0200 Subject: [PATCH 2/2] Fix encoding 30#discussion_r441482104 --- frame/ethereum/src/lib.rs | 4 ++-- template/runtime/src/lib.rs | 8 +++----- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/frame/ethereum/src/lib.rs b/frame/ethereum/src/lib.rs index 629a3624a5..28aabdb081 100644 --- a/frame/ethereum/src/lib.rs +++ b/frame/ethereum/src/lib.rs @@ -30,8 +30,8 @@ use sp_runtime::{ traits::UniqueSaturatedInto, transaction_validity::{TransactionValidity, TransactionSource, ValidTransaction} }; -pub use rlp; -pub use sha3::{Digest, Keccak256}; +use rlp; +use sha3::{Digest, Keccak256}; pub use frontier_rpc_primitives::TransactionStatus; pub use ethereum::{Transaction, Log, Block}; diff --git a/template/runtime/src/lib.rs b/template/runtime/src/lib.rs index fae71beb7d..bb6b480e3a 100644 --- a/template/runtime/src/lib.rs +++ b/template/runtime/src/lib.rs @@ -66,7 +66,6 @@ use frontier_rpc_primitives::{TransactionStatus}; pub use sp_runtime::BuildStorage; pub use sp_runtime::{Perbill, Permill}; pub use timestamp::Call as TimestampCall; -use ethereum::{Digest as Sha3Digest, Keccak256}; /// An index to a block. pub type BlockNumber = u32; @@ -477,10 +476,9 @@ impl_runtime_apis! { } fn storage_at(address: H160, index: U256) -> H256 { - let index = H256::from_slice( - Keccak256::digest(ðereum::rlp::encode(&index)[..]).as_slice(), - ); - evm::Module::::account_storages(address,index) + let mut tmp = [0u8; 32]; + index.to_big_endian(&mut tmp); + evm::Module::::account_storages(address, H256::from_slice(&tmp[..])) } fn block_by_number(number: u32) -> Option {