Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement block_by_number Runtime API #28

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion frame/ethereum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use sp_runtime::{
use sha3::{Digest, Keccak256};

pub use frontier_rpc_primitives::TransactionStatus;
pub use ethereum::{Transaction, Log};
pub use ethereum::{Transaction, Log, Block};

/// A type alias for the balance type from this pallet's point of view.
pub type BalanceOf<T> = <T as pallet_balances::Trait>::Balance;
Expand Down Expand Up @@ -226,6 +226,16 @@ impl<T: Trait> Module<T> {
TransactionStatuses::get(hash)
}

pub fn block_by_number(number: T::BlockNumber) -> Option<ethereum::Block> {
if <BlockNumbers<T>>::contains_key(number) {
let hash = <BlockNumbers<T>>::get(number);
if let Some((block, _receipt)) = BlocksAndReceipts::get(hash) {
return Some(block)
}
}
None
}

/// Execute an Ethereum transaction, ignoring transaction signatures.
pub fn execute(source: H160, transaction: ethereum::Transaction) {
let transaction_hash = H256::from_slice(
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 @@ -85,7 +85,7 @@ pub trait EthApi {

/// Returns block with given number.
#[rpc(name = "eth_getBlockByNumber")]
fn block_by_number(&self, _: BlockNumber, _: bool) -> BoxFuture<Option<RichBlock>>;
fn block_by_number(&self, _: BlockNumber, _: bool) -> Result<Option<RichBlock>>;

/// Returns the number of transactions sent from given address at given time (block number).
#[rpc(name = "eth_getTransactionCount")]
Expand Down
3 changes: 2 additions & 1 deletion rpc/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#![cfg_attr(not(feature = "std"), no_std)]

use sp_core::{H160, H256, U256};
use ethereum::Log;
use ethereum::{Log, Block as EthereumBlock};
use ethereum_types::Bloom;
use codec::{Encode, Decode};
use sp_std::vec::Vec;
Expand All @@ -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 block_by_number(number: u32) -> Option<EthereumBlock>;
}
}

Expand Down
52 changes: 49 additions & 3 deletions rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.

use std::{marker::PhantomData, sync::Arc};
use std::collections::BTreeMap;
use ethereum_types::{H160, H256, H64, U256, U64};
use jsonrpc_core::{BoxFuture, Result, ErrorCode, Error, futures::future::{self, Future}};
use futures::future::TryFutureExt;
Expand All @@ -30,7 +31,7 @@ use sp_runtime::traits::BlakeTwo256;
use frontier_rpc_core::EthApi as EthApiT;
use frontier_rpc_core::types::{
BlockNumber, Bytes, CallRequest, EthAccount, Filter, Index, Log, Receipt, RichBlock,
SyncStatus, Transaction, Work,
SyncStatus, Transaction, Work, Rich, Block, BlockTransactions
};
use frontier_rpc_primitives::{EthereumRuntimeApi, ConvertTransaction};

Expand Down Expand Up @@ -168,8 +169,53 @@ impl<B, C, SC, P, CT, BE> EthApiT for EthApi<B, C, SC, P, CT, BE> where
unimplemented!("block_by_hash");
}

fn block_by_number(&self, _: BlockNumber, _: bool) -> BoxFuture<Option<RichBlock>> {
unimplemented!("block_by_number");
fn block_by_number(&self, number: BlockNumber, _: bool) -> Result<Option<RichBlock>> {
let header = self.select_chain.best_chain()
.map_err(|_| internal_err("fetch header failed"))?;

let number_param: u32;

if let Some(block_number) = number.to_min_block_num() {
number_param = block_number.unique_saturated_into();
} else if number == BlockNumber::Latest {
number_param = header.number().clone().unique_saturated_into() as u32;
} else {
unimplemented!("only latest or block number are supported");
}

if let Ok(Some(block)) = self.client.runtime_api().block_by_number(
&BlockId::Hash(header.hash()),
number_param) {
Ok(Some(Rich {
inner: Block {
hash: None, // TODO
parent_hash: block.header.parent_hash,
uncles_hash: H256::zero(), // TODO
author: H160::default(), // TODO
miner: H160::default(), // TODO
state_root: block.header.state_root,
transactions_root: block.header.transactions_root,
receipts_root: block.header.receipts_root,
number: Some(block.header.number),
gas_used: block.header.gas_used,
gas_limit: block.header.gas_limit,
extra_data: Bytes(vec![]), // TODO H256 to Vec<u8>
logs_bloom: Some(block.header.logs_bloom),
timestamp: U256::from(block.header.timestamp),
difficulty: block.header.difficulty,
total_difficulty: None, // TODO
seal_fields: vec![], // TODO
uncles: vec![], // TODO
// TODO expected struct `frontier_rpc_core::types::transaction::Transaction`,
// found struct `ethereum::transaction::Transaction`
transactions: BlockTransactions::Full(vec![]),
size: None // TODO
},
extra_info: BTreeMap::new()
}))
} else {
Ok(None)
}
}

fn transaction_count(&self, address: H160, number: Option<BlockNumber>) -> Result<U256> {
Expand Down
7 changes: 7 additions & 0 deletions template/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ pub use frame_support::{
},
StorageValue,
};
use ethereum::Block as EthereumBlock;


tgmichel marked this conversation as resolved.
Show resolved Hide resolved
#[cfg(any(feature = "std", test))]
pub use sp_runtime::BuildStorage;
pub use sp_runtime::{Perbill, Permill};
Expand Down Expand Up @@ -470,6 +473,10 @@ impl_runtime_apis! {
H160::zero()
}
}

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

impl pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi<
Expand Down