Skip to content
Closed
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
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ members = [
"template/runtime",
]
resolver = "2"

[patch.crates-io]
evm = { git = "https://github.com/purestake/evm", branch = "jeremy-precompile-handle-for-subcalls" }
79 changes: 43 additions & 36 deletions client/rpc/src/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ use std::{
time,
};

use crate::cache::LRUCacheByteLimited;
use ethereum::{BlockV2 as EthereumBlock, TransactionV2 as EthereumTransaction};
use ethereum_types::{H160, H256, H512, H64, U256, U64};
use evm::{ExitError, ExitReason};
use futures::{future::TryFutureExt, StreamExt};
use jsonrpc_core::{futures::future, BoxFuture, Error, Result};
use crate::cache::LRUCacheByteLimited;
use tokio::sync::{mpsc, oneshot};

use codec::{Decode, Encode};
Expand All @@ -42,8 +42,7 @@ use sc_transaction_pool::{ChainApi, Pool};
use sc_transaction_pool_api::{InPoolTransaction, TransactionPool};
use sp_api::{ApiExt, BlockId, Core, HeaderT, ProvideRuntimeApi};
use sp_block_builder::BlockBuilder;
use sp_blockchain::BlockStatus;
use sp_blockchain::HeaderBackend;
use sp_blockchain::{BlockStatus, HeaderBackend};
use sp_core::hashing::keccak_256;
use sp_runtime::{
traits::{BlakeTwo256, Block as BlockT, NumberFor, One, Saturating, UniqueSaturatedInto, Zero},
Expand All @@ -63,8 +62,8 @@ use fp_rpc::{ConvertTransactionRuntimeApi, EthereumRuntimeRPCApi, TransactionSta
use fp_storage::EthereumStorageSchema;

use crate::{
error_on_execution_failure, format::Formatter, frontier_backend_client, internal_err, overrides::OverrideHandle,
public_key, EthSigner, StorageOverride,
error_on_execution_failure, format::Formatter, frontier_backend_client, internal_err,
overrides::OverrideHandle, public_key, EthSigner, StorageOverride,
};

/// Default JSONRPC error code return by geth
Expand All @@ -89,7 +88,9 @@ pub struct EthApi<B: BlockT, C, P, CT, BE, H: ExHashT, A: ChainApi, F: Formatter
_marker: PhantomData<(B, BE, F)>,
}

impl<B: BlockT, C, P, CT, BE, H: ExHashT, A: ChainApi, F: Formatter> EthApi<B, C, P, CT, BE, H, A, F> {
impl<B: BlockT, C, P, CT, BE, H: ExHashT, A: ChainApi, F: Formatter>
EthApi<B, C, P, CT, BE, H, A, F>
{
pub fn new(
client: Arc<C>,
pool: Arc<P>,
Expand Down Expand Up @@ -349,18 +350,13 @@ where
let address_bloom_filter = FilteredParams::adresses_bloom_filter(&filter.address);
let topics_bloom_filter = FilteredParams::topics_bloom_filter(&topics_input);



while current_number <= to {
let id = BlockId::Number(current_number);
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,
id,
);
let schema = frontier_backend_client::onchain_storage_schema::<B, C, BE>(client, id);

let block = block_data_cache.current_block(schema, substrate_hash).await;

Expand Down Expand Up @@ -706,24 +702,29 @@ where
// Indexers heavily rely on the parent hash.
// Moonbase client-level patch for inconsistent runtime 1200 state.
let number = rich_block.inner.header.number.unwrap_or_default();
if rich_block.inner.header.parent_hash == H256::default()
&& number > U256::zero() {
let id = BlockId::Hash(substrate_hash);
if let Ok(Some(header)) = client.header(id) {
let parent_hash = *header.parent_hash();

let parent_id = BlockId::Hash(parent_hash);
let schema =
frontier_backend_client::onchain_storage_schema::<B, C, BE>(client.as_ref(), parent_id);
if let Some(block) = block_data_cache.current_block(schema, parent_hash).await {
rich_block.inner.header.parent_hash =
H256::from_slice(keccak_256(&rlp::encode(&block.header)).as_slice());
}
if rich_block.inner.header.parent_hash == H256::default()
&& number > U256::zero()
{
let id = BlockId::Hash(substrate_hash);
if let Ok(Some(header)) = client.header(id) {
let parent_hash = *header.parent_hash();

let parent_id = BlockId::Hash(parent_hash);
let schema = frontier_backend_client::onchain_storage_schema::<B, C, BE>(
client.as_ref(),
parent_id,
);
if let Some(block) =
block_data_cache.current_block(schema, parent_hash).await
{
rich_block.inner.header.parent_hash = H256::from_slice(
keccak_256(&rlp::encode(&block.header)).as_slice(),
);
}
}
}
Ok(Some(rich_block))

},
}
_ => Ok(None),
}
})
Expand Down Expand Up @@ -782,19 +783,24 @@ where
// Indexers heavily rely on the parent hash.
// Moonbase client-level patch for inconsistent runtime 1200 state.
let number = rich_block.inner.header.number.unwrap_or_default();
if rich_block.inner.header.parent_hash == H256::default()
&& number > U256::zero() {
if rich_block.inner.header.parent_hash == H256::default()
&& number > U256::zero()
{
let id = BlockId::Hash(substrate_hash);
if let Ok(Some(header)) = client.header(id) {
let parent_hash = *header.parent_hash();

let parent_id = BlockId::Hash(parent_hash);
let schema =
frontier_backend_client::onchain_storage_schema::<B, C, BE>(client.as_ref(), parent_id);
if let Some(block) = block_data_cache.current_block(schema, parent_hash).await {
rich_block.inner.header.parent_hash =
H256::from_slice(keccak_256(&rlp::encode(&block.header)).as_slice());
let schema = frontier_backend_client::onchain_storage_schema::<B, C, BE>(
client.as_ref(),
parent_id,
);
if let Some(block) =
block_data_cache.current_block(schema, parent_hash).await
{
rich_block.inner.header.parent_hash = H256::from_slice(
keccak_256(&rlp::encode(&block.header)).as_slice(),
);
}
}
}
Expand Down Expand Up @@ -2876,7 +2882,8 @@ where
// Make sure only block hashes marked as best are referencing cache checkpoints.
if notification.block == client.info().best_hash {
// Just map the change set to the actual data.
let storage: Vec<Option<StorageData>> = notification.changes
let storage: Vec<Option<StorageData>> = notification
.changes
.iter()
.filter_map(|(o_sk, _k, v)| {
if o_sk.is_none() {
Expand Down
26 changes: 12 additions & 14 deletions frame/ethereum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ impl<T: Config> Pallet<T> {
(Some(gas_price), None, None) => {
priority = gas_price.saturating_sub(base_fee).unique_saturated_into();
gas_price
},
}
// EIP-1559 transaction without tip.
(None, Some(max_fee_per_gas), None) => max_fee_per_gas,
// EIP-1559 transaction with tip.
Expand All @@ -555,7 +555,7 @@ impl<T: Config> Pallet<T> {
.min(max_priority_fee_per_gas)
.unique_saturated_into();
max_fee_per_gas
},
}
_ => return Err(InvalidTransaction::Payment.into()),
};

Expand Down Expand Up @@ -747,18 +747,16 @@ impl<T: Config> Pallet<T> {
match transaction {
// max_fee_per_gas and max_priority_fee_per_gas in legacy and 2930 transactions is
// the provided gas_price.
Transaction::Legacy(t) => {
(
t.input.clone(),
t.value,
t.gas_limit,
Some(t.gas_price),
Some(t.gas_price),
Some(t.nonce),
t.action,
Vec::new(),
)
}
Transaction::Legacy(t) => (
t.input.clone(),
t.value,
t.gas_limit,
Some(t.gas_price),
Some(t.gas_price),
Some(t.nonce),
t.action,
Vec::new(),
),
Transaction::EIP2930(t) => {
let access_list: Vec<(H160, Vec<H256>)> = t
.access_list
Expand Down
9 changes: 5 additions & 4 deletions frame/evm/precompile/blake2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ mod eip_152;

use core::mem::size_of;
use fp_evm::{
Context, ExitError, ExitSucceed, Precompile, PrecompileFailure, PrecompileOutput,
PrecompileResult,
Context, ExitError, ExitSucceed, Precompile, PrecompileFailure, PrecompileHandle,
PrecompileOutput, PrecompileResult,
};

pub struct Blake2F;
Expand All @@ -37,6 +37,7 @@ impl Precompile for Blake2F {
/// Format of `input`:
/// [4 bytes for rounds][64 bytes for h][128 bytes for m][8 bytes for t_0][8 bytes for t_1][1 byte for f]
fn execute(
handle: &mut impl PrecompileHandle,
input: &[u8],
target_gas: Option<u64>,
_context: &Context,
Expand Down Expand Up @@ -65,6 +66,8 @@ impl Precompile for Blake2F {
}
}

handle.record_cost(gas_cost)?;

// we use from_le_bytes below to effectively swap byte order to LE if architecture is BE

let mut h_buf: [u8; 64] = [0; 64];
Expand Down Expand Up @@ -116,9 +119,7 @@ impl Precompile for Blake2F {

Ok(PrecompileOutput {
exit_status: ExitSucceed::Returned,
cost: gas_cost,
output: output_buf.to_vec(),
logs: Default::default(),
})
}
}
Expand Down
19 changes: 11 additions & 8 deletions frame/evm/precompile/bn128/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ extern crate alloc;

use alloc::vec::Vec;
use fp_evm::{
Context, ExitError, ExitSucceed, Precompile, PrecompileFailure, PrecompileOutput,
PrecompileResult,
Context, ExitError, ExitSucceed, Precompile, PrecompileFailure, PrecompileHandle,
PrecompileOutput, PrecompileResult,
};
use sp_core::U256;

Expand Down Expand Up @@ -77,13 +77,16 @@ impl Bn128Add {

impl Precompile for Bn128Add {
fn execute(
handle: &mut impl PrecompileHandle,
input: &[u8],
_target_gas: Option<u64>,
_context: &Context,
_is_static: bool,
) -> PrecompileResult {
use bn::AffineG1;

handle.record_cost(Bn128Add::GAS_COST)?;

let p1 = read_point(input, 0)?;
let p2 = read_point(input, 64)?;

Expand All @@ -108,9 +111,7 @@ impl Precompile for Bn128Add {

Ok(PrecompileOutput {
exit_status: ExitSucceed::Returned,
cost: Bn128Add::GAS_COST,
output: buf.to_vec(),
logs: Default::default(),
})
}
}
Expand All @@ -124,13 +125,16 @@ impl Bn128Mul {

impl Precompile for Bn128Mul {
fn execute(
handle: &mut impl PrecompileHandle,
input: &[u8],
_target_gas: Option<u64>,
_context: &Context,
_is_static: bool,
) -> PrecompileResult {
use bn::AffineG1;

handle.record_cost(Bn128Mul::GAS_COST)?;

let p = read_point(input, 0)?;
let fr = read_fr(input, 64)?;

Expand All @@ -155,9 +159,7 @@ impl Precompile for Bn128Mul {

Ok(PrecompileOutput {
exit_status: ExitSucceed::Returned,
cost: Bn128Mul::GAS_COST,
output: buf.to_vec(),
logs: Default::default(),
})
}
}
Expand All @@ -173,6 +175,7 @@ impl Bn128Pairing {

impl Precompile for Bn128Pairing {
fn execute(
handle: &mut impl PrecompileHandle,
input: &[u8],
target_gas: Option<u64>,
_context: &Context,
Expand Down Expand Up @@ -282,14 +285,14 @@ impl Precompile for Bn128Pairing {
}
};

handle.record_cost(gas_cost)?;

let mut buf = [0u8; 32];
ret_val.to_big_endian(&mut buf);

Ok(PrecompileOutput {
exit_status: ExitSucceed::Returned,
cost: gas_cost,
output: buf.to_vec(),
logs: Default::default(),
})
}
}
Expand Down
10 changes: 6 additions & 4 deletions frame/evm/precompile/dispatch/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ extern crate alloc;

use core::marker::PhantomData;
use fp_evm::{
Context, ExitError, ExitSucceed, Precompile, PrecompileFailure, PrecompileOutput,
PrecompileResult,
Context, ExitError, ExitSucceed, Precompile, PrecompileFailure, PrecompileHandle,
PrecompileOutput, PrecompileResult,
};
use frame_support::{
codec::Decode,
Expand All @@ -42,6 +42,7 @@ where
<T::Call as Dispatchable>::Origin: From<Option<T::AccountId>>,
{
fn execute(
handle: &mut impl PrecompileHandle,
input: &[u8],
target_gas: Option<u64>,
context: &Context,
Expand Down Expand Up @@ -75,11 +76,12 @@ where
let cost = T::GasWeightMapping::weight_to_gas(
post_info.actual_weight.unwrap_or(info.weight),
);

handle.record_cost(cost)?;

Ok(PrecompileOutput {
exit_status: ExitSucceed::Stopped,
cost,
output: Default::default(),
logs: Default::default(),
})
}
Err(_) => Err(PrecompileFailure::Error {
Expand Down
Loading