Skip to content

Commit 3056caa

Browse files
committed
Merge branch 'devnet-ready' into fix/share-pool-precision
2 parents 41d50fd + 66d8fbd commit 3056caa

File tree

13 files changed

+927
-2398
lines changed

13 files changed

+927
-2398
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.

runtime/Cargo.toml

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

104105
# Frontier FRAME
105106
pallet-base-fee = { workspace = true }
@@ -163,6 +164,7 @@ std = [
163164
"pallet-scheduler/std",
164165
"pallet-preimage/std",
165166
"pallet-commitments/std",
167+
"precompile-utils/std",
166168
"sp-api/std",
167169
"sp-block-builder/std",
168170
"sp-consensus-aura/std",
Lines changed: 22 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,40 @@
1-
use pallet_evm::{
2-
BalanceConverter, ExitError, ExitSucceed, PrecompileHandle, PrecompileOutput, PrecompileResult,
3-
};
1+
use pallet_evm::PrecompileHandle;
2+
use precompile_utils::EvmResult;
3+
use sp_core::H256;
44
use sp_runtime::traits::UniqueSaturatedInto;
5-
use sp_std::vec;
65

7-
use crate::precompiles::{
8-
contract_to_origin, get_method_id, get_pubkey, get_slice, try_dispatch_runtime_call,
9-
};
6+
use crate::precompiles::{contract_to_origin, parse_pubkey, PrecompileExt, PrecompileHandleExt};
107
use crate::Runtime;
118

12-
pub const BALANCE_TRANSFER_INDEX: u64 = 2048;
13-
// ss58 public key i.e., the contract sends funds it received to the destination address from the
14-
// method parameter.
15-
const CONTRACT_ADDRESS_SS58: [u8; 32] = [
16-
0x07, 0xec, 0x71, 0x2a, 0x5d, 0x38, 0x43, 0x4d, 0xdd, 0x03, 0x3f, 0x8f, 0x02, 0x4e, 0xcd, 0xfc,
17-
0x4b, 0xb5, 0x95, 0x1c, 0x13, 0xc3, 0x08, 0x5c, 0x39, 0x9c, 0x8a, 0x5f, 0x62, 0x93, 0x70, 0x5d,
18-
];
19-
209
pub struct BalanceTransferPrecompile;
2110

22-
impl BalanceTransferPrecompile {
23-
pub fn execute(handle: &mut impl PrecompileHandle) -> PrecompileResult {
24-
let txdata = handle.input();
25-
26-
// Match method ID: keccak256("transfer(bytes32)")
27-
let method = get_slice(txdata, 0, 4)?;
28-
if get_method_id("transfer(bytes32)") != method {
29-
return Ok(PrecompileOutput {
30-
exit_status: ExitSucceed::Returned,
31-
output: vec![],
32-
});
33-
}
34-
35-
// Forward all received value to the destination address
36-
let amount = handle.context().apparent_value;
11+
impl PrecompileExt for BalanceTransferPrecompile {
12+
const INDEX: u64 = 2048;
13+
const ADDRESS_SS58: [u8; 32] = [
14+
0x07, 0xec, 0x71, 0x2a, 0x5d, 0x38, 0x43, 0x4d, 0xdd, 0x03, 0x3f, 0x8f, 0x02, 0x4e, 0xcd,
15+
0xfc, 0x4b, 0xb5, 0x95, 0x1c, 0x13, 0xc3, 0x08, 0x5c, 0x39, 0x9c, 0x8a, 0x5f, 0x62, 0x93,
16+
0x70, 0x5d,
17+
];
18+
}
3719

38-
// Use BalanceConverter to convert EVM amount to Substrate balance
39-
let amount_sub =
40-
<Runtime as pallet_evm::Config>::BalanceConverter::into_substrate_balance(amount)
41-
.ok_or(ExitError::OutOfFund)?;
20+
#[precompile_utils::precompile]
21+
impl BalanceTransferPrecompile {
22+
#[precompile::public("transfer(bytes32)")]
23+
#[precompile::payable]
24+
fn transfer(handle: &mut impl PrecompileHandle, address: H256) -> EvmResult<()> {
25+
let amount_sub = handle.try_convert_apparent_value()?;
4226

4327
if amount_sub.is_zero() {
44-
return Ok(PrecompileOutput {
45-
exit_status: ExitSucceed::Returned,
46-
output: vec![],
47-
});
28+
return Ok(());
4829
}
4930

50-
let address_bytes_dst = get_slice(txdata, 4, 36)?;
51-
let (account_id_dst, _) = get_pubkey(address_bytes_dst)?;
31+
let dest = parse_pubkey(address.as_bytes())?.0.into();
5232

5333
let call = pallet_balances::Call::<Runtime>::transfer_allow_death {
54-
dest: account_id_dst.into(),
34+
dest,
5535
value: amount_sub.unique_saturated_into(),
5636
};
5737

58-
try_dispatch_runtime_call(handle, call, contract_to_origin(&CONTRACT_ADDRESS_SS58)?)
38+
handle.try_dispatch_runtime_call(call, contract_to_origin(&Self::ADDRESS_SS58)?)
5939
}
6040
}

runtime/src/precompiles/ed25519.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@ extern crate alloc;
22

33
use alloc::vec::Vec;
44

5-
use crate::precompiles::get_slice;
65
use ed25519_dalek::{Signature, Verifier, VerifyingKey};
76
use fp_evm::{ExitError, ExitSucceed, LinearCostPrecompile, PrecompileFailure};
87

9-
pub const EDVERIFY_PRECOMPILE_INDEX: u64 = 1026;
8+
use crate::precompiles::{parse_slice, PrecompileExt};
109

1110
pub struct Ed25519Verify;
1211

12+
impl PrecompileExt for Ed25519Verify {
13+
const INDEX: u64 = 1026;
14+
const ADDRESS_SS58: [u8; 32] = [0; 32];
15+
}
16+
1317
impl LinearCostPrecompile for Ed25519Verify {
1418
const BASE: u64 = 15;
1519
const WORD: u64 = 3;
@@ -23,13 +27,13 @@ impl LinearCostPrecompile for Ed25519Verify {
2327

2428
let mut buf = [0u8; 32];
2529

26-
let msg = get_slice(input, 4, 36)?;
27-
let pk = VerifyingKey::try_from(get_slice(input, 36, 68)?).map_err(|_| {
30+
let msg = parse_slice(input, 4, 36)?;
31+
let pk = VerifyingKey::try_from(parse_slice(input, 36, 68)?).map_err(|_| {
2832
PrecompileFailure::Error {
2933
exit_status: ExitError::Other("Public key recover failed".into()),
3034
}
3135
})?;
32-
let sig = Signature::try_from(get_slice(input, 68, 132)?).map_err(|_| {
36+
let sig = Signature::try_from(parse_slice(input, 68, 132)?).map_err(|_| {
3337
PrecompileFailure::Error {
3438
exit_status: ExitError::Other("Signature recover failed".into()),
3539
}

0 commit comments

Comments
 (0)