Skip to content

Commit

Permalink
Implement storage_at Runtime API (#30)
Browse files Browse the repository at this point in the history
* Implement storage_at Runtime API

* Fix encoding 30#discussion_r441482104
  • Loading branch information
tgmichel authored Jun 18, 2020
1 parent 2017dd5 commit 520948d
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 3 deletions.
1 change: 1 addition & 0 deletions frame/ethereum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use sp_runtime::{
traits::UniqueSaturatedInto,
transaction_validity::{TransactionValidity, TransactionSource, ValidTransaction}
};
use rlp;
use sha3::{Digest, Keccak256};

pub use frontier_rpc_primitives::TransactionStatus;
Expand Down
2 changes: 1 addition & 1 deletion rpc/core/src/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<BlockNumber>) -> BoxFuture<H256>;
fn storage_at(&self, _: H160, _: U256, _: Option<BlockNumber>) -> Result<H256>;

/// Returns block with given hash.
#[rpc(name = "eth_getBlockByHash")]
Expand Down
1 change: 1 addition & 0 deletions rpc/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ sp_api::decl_runtime_apis! {
fn gas_price() -> U256;
fn account_code_at(address: H160) -> Vec<u8>;
fn author() -> H160;
fn storage_at(address: H160, index: U256) -> H256;
fn block_by_number(number: u32) -> Option<EthereumBlock>;
fn block_transaction_count_by_number(number: u32) -> Option<U256>;
fn block_by_hash(hash: H256) -> Option<EthereumBlock>;
Expand Down
19 changes: 17 additions & 2 deletions rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,23 @@ impl<B, C, SC, P, CT, BE> EthApiT for EthApi<B, C, SC, P, CT, BE> where
unimplemented!("proof");
}

fn storage_at(&self, _: H160, _: U256, _: Option<BlockNumber>) -> BoxFuture<H256> {
unimplemented!("storage_at");
fn storage_at(&self, address: H160, index: U256, number: Option<BlockNumber>) -> Result<H256> {
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, hash: H256, _: bool) -> Result<Option<RichBlock>> {
Expand Down
6 changes: 6 additions & 0 deletions template/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,12 @@ impl_runtime_apis! {
}
}

fn storage_at(address: H160, index: U256) -> H256 {
let mut tmp = [0u8; 32];
index.to_big_endian(&mut tmp);
evm::Module::<Runtime>::account_storages(address, H256::from_slice(&tmp[..]))
}

fn block_by_number(number: u32) -> Option<EthereumBlock> {
<ethereum::Module<Runtime>>::block_by_number(number)
}
Expand Down

0 comments on commit 520948d

Please sign in to comment.