Skip to content

Commit

Permalink
[pallet-revive] Use address20 for contract's address (#5548)
Browse files Browse the repository at this point in the history
Co-authored-by: Alexander Theißen <[email protected]>
Co-authored-by: command-bot <>
  • Loading branch information
2 people authored and x3c41a committed Sep 4, 2024
1 parent 153d03a commit 63a1e49
Show file tree
Hide file tree
Showing 36 changed files with 1,515 additions and 1,328 deletions.
11 changes: 7 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions prdoc/pr_5548.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
title: Use H160 when interfacing with contracts

doc:
- audience: Runtime Dev
description: |
When interfacing with a contract we now use the native ethereum address
type and map it to AccountId32 when interfacing with the rest
of substrate.

crates:
- name: pallet-revive
bump: major
- name: pallet-revive-fixtures
bump: major
- name: pallet-revive-mock-network
bump: major
20 changes: 10 additions & 10 deletions substrate/bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ use sp_consensus_beefy::{
mmr::MmrLeafVersion,
};
use sp_consensus_grandpa::AuthorityId as GrandpaId;
use sp_core::{crypto::KeyTypeId, OpaqueMetadata};
use sp_core::{crypto::KeyTypeId, OpaqueMetadata, H160};
use sp_inherents::{CheckInherentsResult, InherentData};
use sp_runtime::{
create_runtime_str,
Expand Down Expand Up @@ -1392,7 +1392,7 @@ impl pallet_revive::Config for Runtime {
type WeightPrice = pallet_transaction_payment::Pallet<Self>;
type WeightInfo = pallet_revive::weights::SubstrateWeight<Self>;
type ChainExtension = ();
type AddressGenerator = pallet_revive::DefaultAddressGenerator;
type AddressMapper = pallet_revive::DefaultAddressMapper;
type MaxCodeLen = ConstU32<{ 123 * 1024 }>;
type RuntimeMemory = ConstU32<{ 128 * 1024 * 1024 }>;
type PVFMemory = ConstU32<{ 512 * 1024 * 1024 }>;
Expand Down Expand Up @@ -2988,11 +2988,11 @@ impl_runtime_apis! {
}
}

impl pallet_revive::ReviveApi<Block, AccountId, Balance, BlockNumber, Hash, EventRecord> for Runtime
impl pallet_revive::ReviveApi<Block, AccountId, Balance, BlockNumber, EventRecord> for Runtime
{
fn call(
origin: AccountId,
dest: AccountId,
dest: H160,
value: Balance,
gas_limit: Option<Weight>,
storage_deposit_limit: Option<Balance>,
Expand All @@ -3015,10 +3015,10 @@ impl_runtime_apis! {
value: Balance,
gas_limit: Option<Weight>,
storage_deposit_limit: Option<Balance>,
code: pallet_revive::Code<Hash>,
code: pallet_revive::Code,
data: Vec<u8>,
salt: Vec<u8>,
) -> pallet_revive::ContractInstantiateResult<AccountId, Balance, EventRecord>
salt: [u8; 32],
) -> pallet_revive::ContractInstantiateResult<Balance, EventRecord>
{
Revive::bare_instantiate(
RuntimeOrigin::signed(origin),
Expand All @@ -3037,7 +3037,7 @@ impl_runtime_apis! {
origin: AccountId,
code: Vec<u8>,
storage_deposit_limit: Option<Balance>,
) -> pallet_revive::CodeUploadResult<Hash, Balance>
) -> pallet_revive::CodeUploadResult<Balance>
{
Revive::bare_upload_code(
RuntimeOrigin::signed(origin),
Expand All @@ -3047,8 +3047,8 @@ impl_runtime_apis! {
}

fn get_storage(
address: AccountId,
key: Vec<u8>,
address: H160,
key: [u8; 32],
) -> pallet_revive::GetStorageResult {
Revive::get_storage(
address,
Expand Down
12 changes: 6 additions & 6 deletions substrate/frame/revive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ workspace = true
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
environmental = { workspace = true }
paste = { workspace = true }
polkavm = { version = "0.10.0", default-features = false }
bitflags = { workspace = true }
Expand All @@ -29,9 +30,9 @@ scale-info = { features = ["derive"], workspace = true }
log = { workspace = true }
serde = { optional = true, features = ["derive"], workspace = true, default-features = true }
impl-trait-for-tuples = { workspace = true }
rlp = { workspace = true }

# Substrate Dependencies
environmental = { workspace = true }
# Polkadot SDK Dependencies
frame-benchmarking = { optional = true, workspace = true }
frame-support = { workspace = true }
frame-system = { workspace = true }
Expand All @@ -54,10 +55,7 @@ pretty_assertions = { workspace = true }
wat = { workspace = true }
pallet-revive-fixtures = { workspace = true, default-features = true }

# Polkadot Dependencies
xcm-builder = { workspace = true, default-features = true }

# Substrate Dependencies
# Polkadot SDK Dependencies
pallet-balances = { workspace = true, default-features = true }
pallet-timestamp = { workspace = true, default-features = true }
pallet-message-queue = { workspace = true, default-features = true }
Expand All @@ -66,6 +64,7 @@ pallet-assets = { workspace = true, default-features = true }
pallet-proxy = { workspace = true, default-features = true }
sp-keystore = { workspace = true, default-features = true }
sp-tracing = { workspace = true, default-features = true }
xcm-builder = { workspace = true, default-features = true }

[features]
default = ["std"]
Expand All @@ -86,6 +85,7 @@ std = [
"pallet-timestamp/std",
"pallet-utility/std",
"polkavm/std",
"rlp/std",
"scale-info/std",
"serde",
"sp-api/std",
Expand Down
2 changes: 1 addition & 1 deletion substrate/frame/revive/fixtures/contracts/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub extern "C" fn deploy() {}
pub extern "C" fn call() {
input!(
callee_input: [u8; 4],
callee_addr: [u8; 32],
callee_addr: [u8; 20],
);

// Call the callee
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub extern "C" fn deploy() {}
pub extern "C" fn call() {
input!(
100,
callee_addr: [u8; 32],
callee_addr: [u8; 20],
input: [u8],
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub extern "C" fn call() {
input!(
512,
callee_input: [u8; 4],
callee_addr: [u8; 32],
callee_addr: [u8; 20],
call: [u8],
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub extern "C" fn deploy() {}
pub extern "C" fn call() {
input!(
256,
callee_addr: [u8; 32],
callee_addr: [u8; 20],
flags: u32,
value: u64,
forwarded_input: [u8],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub extern "C" fn deploy() {}
pub extern "C" fn call() {
input!(
256,
callee_addr: [u8; 32],
callee_addr: [u8; 20],
ref_time: u64,
proof_size: u64,
forwarded_input: [u8],
Expand Down
6 changes: 3 additions & 3 deletions substrate/frame/revive/fixtures/contracts/caller_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub extern "C" fn call() {
// The value to transfer on instantiation and calls. Chosen to be greater than existential
// deposit.
let value = 32768u64.to_le_bytes();
let salt = [0u8; 0];
let salt = [0u8; 32];

// Callee will use the first 4 bytes of the input to return an exit status.
let input = [0u8, 1, 34, 51, 68, 85, 102, 119];
Expand Down Expand Up @@ -72,7 +72,7 @@ pub extern "C" fn call() {
assert!(matches!(res, Err(ReturnErrorCode::CalleeTrapped)));

// Deploy the contract successfully.
let mut callee = [0u8; 32];
let mut callee = [0u8; 20];
let callee = &mut &mut callee[..];

api::instantiate(
Expand All @@ -87,7 +87,7 @@ pub extern "C" fn call() {
&salt,
)
.unwrap();
assert_eq!(callee.len(), 32);
assert_eq!(callee.len(), 20);

// Call the new contract and expect it to return failing exit code.
let res = api::call(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub extern "C" fn call() {
input!(
buffer,
input: [u8; 4],
callee: [u8; 32],
callee: [u8; 20],
deposit_limit: [u8; 8],
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ pub extern "C" fn call() {
);

let value = 10_000u64.to_le_bytes();
let salt = [0u8; 0];
let mut address = [0u8; 32];
let salt = [0u8; 32];
let mut address = [0u8; 20];
let address = &mut &mut address[..];

api::instantiate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub extern "C" fn call() {
buffer,
len: u32,
input: [u8; 4],
callee: [u8; 32],
callee: [u8; 20],
);

let rounds = len as usize / BUFFER.len();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ pub extern "C" fn call() {
assert_eq!(value_transferred, 1337);

// Assert that ALICE is the caller of the contract.
output!(caller, [0u8; 32], api::caller,);
assert_eq!(&caller[..], &[1u8; 32]);
output!(caller, [0u8; 20], api::caller,);
assert_eq!(&caller[..], &[1u8; 20]);
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ pub extern "C" fn deploy() {
input!(code_hash: [u8; 32],);

let input = [0u8; 0];
let mut address = [0u8; 32];
let mut address = [0u8; 20];
let address = &mut &mut address[..];
let salt = [71u8, 17u8];
let salt = [47u8; 32];

api::instantiate(
code_hash,
Expand All @@ -54,7 +54,7 @@ pub extern "C" fn deploy() {
#[no_mangle]
#[polkavm_derive::polkavm_export]
pub extern "C" fn call() {
let mut callee_addr = [0u8; 32];
let mut callee_addr = [0u8; 20];
let callee_addr = &mut &mut callee_addr[..];
api::get_storage(StorageFlags::empty(), &ADDRESS_KEY, callee_addr).unwrap();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
use common::input;
use uapi::{HostFn, HostFnImpl as api};

const ALICE: [u8; 32] = [1u8; 32];
const ETH_ALICE: [u8; 20] = [1u8; 20];

/// Load input data and perform the action specified by the input.
/// If `delegate_call` is true, then delegate call into the contract.
Expand All @@ -44,7 +44,7 @@ fn load_input(delegate_call: bool) {
},
// 3 = Terminate
3 => {
api::terminate(&ALICE);
api::terminate(&ETH_ALICE);
},
// Everything else is a noop
_ => {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub extern "C" fn deploy() {}
pub extern "C" fn call() {
input!(
256,
callee_addr: [u8; 32],
callee_addr: [u8; 20],
callee_input: [u8],
);

Expand Down
6 changes: 3 additions & 3 deletions substrate/frame/revive/fixtures/contracts/self_destruct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
use common::{input, output};
use uapi::{HostFn, HostFnImpl as api};

const DJANGO: [u8; 32] = [4u8; 32];
const ETH_DJANGO: [u8; 20] = [4u8; 20];

#[no_mangle]
#[polkavm_derive::polkavm_export]
Expand All @@ -36,7 +36,7 @@ pub extern "C" fn call() {
input!(input, 4,);

if !input.is_empty() {
output!(addr, [0u8; 32], api::address,);
output!(addr, [0u8; 20], api::address,);
api::call(
uapi::CallFlags::ALLOW_REENTRY,
addr,
Expand All @@ -50,6 +50,6 @@ pub extern "C" fn call() {
.unwrap();
} else {
// Try to terminate and give balance to django.
api::terminate(&DJANGO);
api::terminate(&ETH_DJANGO);
}
}
12 changes: 3 additions & 9 deletions substrate/frame/revive/fixtures/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,12 @@ extern crate alloc;

/// Load a given wasm module and returns a wasm binary contents along with it's hash.
#[cfg(feature = "std")]
pub fn compile_module<T>(
fixture_name: &str,
) -> anyhow::Result<(Vec<u8>, <T::Hashing as sp_runtime::traits::Hash>::Output)>
where
T: frame_system::Config,
{
use sp_runtime::traits::Hash;
pub fn compile_module(fixture_name: &str) -> anyhow::Result<(Vec<u8>, sp_core::H256)> {
let out_dir: std::path::PathBuf = env!("OUT_DIR").into();
let fixture_path = out_dir.join(format!("{fixture_name}.polkavm"));
let binary = std::fs::read(fixture_path)?;
let code_hash = T::Hashing::hash(&binary);
Ok((binary, code_hash))
let code_hash = sp_io::hashing::keccak_256(&binary);
Ok((binary, sp_core::H256(code_hash)))
}

/// Fixtures used in runtime benchmarks.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use frame_support::derive_impl;

#[derive_impl(pallet_revive::config_preludes::TestDefaultConfig)]
impl pallet_revive::Config for Runtime {
type AddressGenerator = pallet_revive::DefaultAddressGenerator;
type AddressMapper = pallet_revive::DefaultAddressMapper;
type Currency = Balances;
type Time = super::Timestamp;
type Xcm = pallet_xcm::Pallet<Self>;
Expand Down
Loading

0 comments on commit 63a1e49

Please sign in to comment.