Skip to content

Commit 357a9e1

Browse files
authored
Merge pull request #1184 from opentensor/fix/evm-gas-estimation
Fix gas estimation issues
2 parents 2633e88 + 00dc331 commit 357a9e1

File tree

6 files changed

+129
-63
lines changed

6 files changed

+129
-63
lines changed

Cargo.lock

Lines changed: 45 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,10 @@ fc-consensus = { git = "https://github.com/opentensor/frontier", rev = "635bdac8
180180
fp-consensus = { git = "https://github.com/opentensor/frontier", rev = "635bdac882", default-features = false }
181181
fp-dynamic-fee = { git = "https://github.com/opentensor/frontier", rev = "635bdac882", default-features = false }
182182
fc-api = { git = "https://github.com/opentensor/frontier", rev = "635bdac882", default-features = false }
183-
fc-rpc = { git = "https://github.com/opentensor/frontier", rev = "635bdac882", default-features = false }
183+
fc-rpc = { git = "https://github.com/opentensor/frontier", rev = "635bdac882", default-features = false, features = ["rpc-binary-search-estimate"]}
184184
fc-rpc-core = { git = "https://github.com/opentensor/frontier", rev = "635bdac882", default-features = false }
185185
fc-mapping-sync = { git = "https://github.com/opentensor/frontier", rev = "635bdac882", default-features = false }
186+
precompile-utils = { git = "https://github.com/opentensor/frontier", rev = "635bdac882", default-features = false }
186187

187188
# Frontier FRAME
188189
pallet-base-fee = { git = "https://github.com/opentensor/frontier", rev = "635bdac882", default-features = false }

node/src/chain_spec/localnet.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,17 @@ fn localnet_genesis(
7777
get_account_id_from_seed::<sr25519::Public>("Ferdie"),
7878
2000000000000u128,
7979
),
80+
// ETH
81+
(
82+
// Alith - 0xf24FF3a9CF04c71Dbc94D0b566f7A27B94566cac
83+
AccountId::from_str("5Fghzk1AJt88PeFEzuRfXzbPchiBbsVGTTXcdx599VdZzkTA").unwrap(),
84+
2000000000000u128,
85+
),
86+
(
87+
// Baltathar - 0x3Cd0A705a2DC65e5b1E1205896BaA2be8A07c6e0
88+
AccountId::from_str("5GeqNhKWj1KG78VHzbmo3ZjZgUTrCuWeamdgiA114XHGdaEr").unwrap(),
89+
2000000000000u128,
90+
),
8091
];
8192

8293
// Check if the environment variable is set

runtime/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ pallet-commitments = { default-features = false, path = "../pallets/commitments"
9898
fp-evm = { workspace = true }
9999
fp-rpc = { workspace = true }
100100
fp-self-contained = { workspace = true }
101+
precompile-utils = { workspace = true }
101102

102103
# Frontier FRAME
103104
pallet-base-fee = { workspace = true }
@@ -191,6 +192,7 @@ std = [
191192
"fp-evm/std",
192193
"fp-rpc/std",
193194
"fp-self-contained/std",
195+
"precompile-utils/std",
194196
# Frontier FRAME
195197
"pallet-base-fee/std",
196198
"pallet-dynamic-fee/std",

runtime/src/precompiles/balance_transfer.rs

Lines changed: 47 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,71 +3,75 @@ use pallet_evm::{
33
BalanceConverter, ExitError, ExitSucceed, PrecompileFailure, PrecompileHandle,
44
PrecompileOutput, PrecompileResult,
55
};
6+
use precompile_utils::prelude::RuntimeHelper;
67
use sp_core::U256;
7-
use sp_runtime::traits::{Dispatchable, UniqueSaturatedInto};
8+
use sp_runtime::traits::UniqueSaturatedInto;
89
use sp_std::vec;
910

10-
use crate::{Runtime, RuntimeCall};
11-
1211
use crate::precompiles::{bytes_to_account_id, get_method_id, get_slice};
12+
use crate::{Runtime, RuntimeCall};
1313

1414
pub const BALANCE_TRANSFER_INDEX: u64 = 2048;
1515

16+
// This is a hardcoded hashed address mapping of 0x0000000000000000000000000000000000000800 to an
17+
// ss58 public key i.e., the contract sends funds it received to the destination address from the
18+
// method parameter.
19+
const CONTRACT_ADDRESS_SS58: [u8; 32] = [
20+
0x07, 0xec, 0x71, 0x2a, 0x5d, 0x38, 0x43, 0x4d, 0xdd, 0x03, 0x3f, 0x8f, 0x02, 0x4e, 0xcd, 0xfc,
21+
0x4b, 0xb5, 0x95, 0x1c, 0x13, 0xc3, 0x08, 0x5c, 0x39, 0x9c, 0x8a, 0x5f, 0x62, 0x93, 0x70, 0x5d,
22+
];
23+
1624
pub struct BalanceTransferPrecompile;
1725

1826
impl BalanceTransferPrecompile {
1927
pub fn execute(handle: &mut impl PrecompileHandle) -> PrecompileResult {
2028
let txdata = handle.input();
2129

2230
// Match method ID: keccak256("transfer(bytes32)")
23-
let method: &[u8] = get_slice(txdata, 0, 4)?;
24-
if get_method_id("transfer(bytes32)") == method {
25-
// Forward all received value to the destination address
26-
let amount: U256 = handle.context().apparent_value;
31+
let method = get_slice(txdata, 0, 4)?;
32+
if get_method_id("transfer(bytes32)") != method {
33+
return Ok(PrecompileOutput {
34+
exit_status: ExitSucceed::Returned,
35+
output: vec![],
36+
});
37+
}
2738

28-
// Use BalanceConverter to convert EVM amount to Substrate balance
29-
let amount_sub =
30-
<Runtime as pallet_evm::Config>::BalanceConverter::into_substrate_balance(amount)
31-
.ok_or(ExitError::OutOfFund)?;
39+
// Forward all received value to the destination address
40+
let amount: U256 = handle.context().apparent_value;
3241

33-
if amount_sub.is_zero() {
34-
return Ok(PrecompileOutput {
35-
exit_status: ExitSucceed::Returned,
36-
output: vec![],
37-
});
38-
}
42+
// Use BalanceConverter to convert EVM amount to Substrate balance
43+
let amount_sub =
44+
<Runtime as pallet_evm::Config>::BalanceConverter::into_substrate_balance(amount)
45+
.ok_or(ExitError::OutOfFund)?;
3946

40-
// This is a hardcoded hashed address mapping of
41-
// 0x0000000000000000000000000000000000000800 to an ss58 public key
42-
// i.e., the contract sends funds it received to the destination address
43-
// from the method parameter.
44-
const ADDRESS_BYTES_SRC: [u8; 32] = [
45-
0x07, 0xec, 0x71, 0x2a, 0x5d, 0x38, 0x43, 0x4d, 0xdd, 0x03, 0x3f, 0x8f, 0x02, 0x4e,
46-
0xcd, 0xfc, 0x4b, 0xb5, 0x95, 0x1c, 0x13, 0xc3, 0x08, 0x5c, 0x39, 0x9c, 0x8a, 0x5f,
47-
0x62, 0x93, 0x70, 0x5d,
48-
];
49-
let address_bytes_dst: &[u8] = get_slice(txdata, 4, 36)?;
50-
let account_id_src = bytes_to_account_id(&ADDRESS_BYTES_SRC)?;
51-
let account_id_dst = bytes_to_account_id(address_bytes_dst)?;
47+
if amount_sub.is_zero() {
48+
return Ok(PrecompileOutput {
49+
exit_status: ExitSucceed::Returned,
50+
output: vec![],
51+
});
52+
}
5253

53-
let call =
54-
RuntimeCall::Balances(pallet_balances::Call::<Runtime>::transfer_allow_death {
55-
dest: account_id_dst.into(),
56-
value: amount_sub.unique_saturated_into(),
57-
});
54+
let address_bytes_dst = get_slice(txdata, 4, 36)?;
55+
let account_id_src = bytes_to_account_id(&CONTRACT_ADDRESS_SS58)?;
56+
let account_id_dst = bytes_to_account_id(address_bytes_dst)?;
5857

59-
// Dispatch the call
60-
let result = call.dispatch(RawOrigin::Signed(account_id_src).into());
61-
if result.is_err() {
62-
return Err(PrecompileFailure::Error {
63-
exit_status: ExitError::OutOfFund,
64-
});
65-
}
66-
}
58+
let call = RuntimeCall::Balances(pallet_balances::Call::<Runtime>::transfer_allow_death {
59+
dest: account_id_dst.into(),
60+
value: amount_sub.unique_saturated_into(),
61+
});
6762

68-
Ok(PrecompileOutput {
63+
// Dispatch the call
64+
RuntimeHelper::<Runtime>::try_dispatch(
65+
handle,
66+
RawOrigin::Signed(account_id_src).into(),
67+
call,
68+
)
69+
.map(|_| PrecompileOutput {
6970
exit_status: ExitSucceed::Returned,
7071
output: vec![],
7172
})
73+
.map_err(|_| PrecompileFailure::Error {
74+
exit_status: ExitError::OutOfFund,
75+
})
7276
}
7377
}

runtime/src/precompiles/staking.rs

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,22 @@
2626
//
2727

2828
use frame_system::RawOrigin;
29-
use pallet_evm::{AddressMapping, BalanceConverter, HashedAddressMapping};
3029
use pallet_evm::{
31-
ExitError, ExitSucceed, PrecompileFailure, PrecompileHandle, PrecompileOutput, PrecompileResult,
30+
AddressMapping, BalanceConverter, ExitError, ExitSucceed, HashedAddressMapping,
31+
PrecompileFailure, PrecompileHandle, PrecompileOutput, PrecompileResult,
3232
};
33+
use precompile_utils::prelude::RuntimeHelper;
3334
use sp_core::crypto::Ss58Codec;
3435
use sp_core::U256;
35-
use sp_runtime::traits::Dispatchable;
36-
use sp_runtime::traits::{BlakeTwo256, StaticLookup, UniqueSaturatedInto};
36+
use sp_runtime::traits::{BlakeTwo256, Dispatchable, StaticLookup, UniqueSaturatedInto};
3737
use sp_runtime::AccountId32;
38+
use sp_std::vec;
3839

3940
use crate::{
4041
precompiles::{get_method_id, get_slice},
41-
ProxyType,
42+
ProxyType, Runtime, RuntimeCall,
4243
};
43-
use sp_std::vec;
4444

45-
use crate::{Runtime, RuntimeCall};
4645
pub const STAKING_PRECOMPILE_INDEX: u64 = 2049;
4746

4847
pub struct StakingPrecompile;
@@ -209,24 +208,28 @@ impl StakingPrecompile {
209208
);
210209

211210
// Transfer the amount back to the caller before executing the staking operation
212-
// let caller = handle.context().caller;
213211
let amount = handle.context().apparent_value;
214212

215213
if !amount.is_zero() {
216214
Self::transfer_back_to_caller(&account_id, amount)?;
217215
}
218216

219-
let result = call.dispatch(RawOrigin::Signed(account_id.clone()).into());
220-
match &result {
221-
Ok(post_info) => log::info!("Dispatch succeeded. Post info: {:?}", post_info),
222-
Err(dispatch_error) => log::error!("Dispatch failed. Error: {:?}", dispatch_error),
223-
}
224-
match result {
225-
Ok(_) => Ok(PrecompileOutput {
226-
exit_status: ExitSucceed::Returned,
227-
output: vec![],
228-
}),
229-
Err(_) => {
217+
match RuntimeHelper::<Runtime>::try_dispatch(
218+
handle,
219+
RawOrigin::Signed(account_id.clone()).into(),
220+
call,
221+
) {
222+
Ok(post_info) => {
223+
log::info!("Dispatch succeeded. Post info: {:?}", post_info);
224+
225+
Ok(PrecompileOutput {
226+
exit_status: ExitSucceed::Returned,
227+
output: vec![],
228+
})
229+
}
230+
231+
Err(dispatch_error) => {
232+
log::error!("Dispatch failed. Error: {:?}", dispatch_error);
230233
log::warn!("Returning error PrecompileFailure::Error");
231234
Err(PrecompileFailure::Error {
232235
exit_status: ExitError::Other("Subtensor call failed".into()),

0 commit comments

Comments
 (0)