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

[pallet-revive] Use address20 for contract's address #5548

Merged
merged 23 commits into from
Sep 2, 2024
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
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
10 changes: 7 additions & 3 deletions substrate/frame/revive/fixtures/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@ workspace = true

[dependencies]
frame-system = { workspace = true, default-features = true, optional = true }
sp-core = { workspace = true, default-features = true, optional = true }
sp-io = { workspace = true, default-features = true, optional = true }
sp-runtime = { workspace = true, default-features = true, optional = true }
anyhow = { workspace = true }
anyhow = { workspace = true, default-features = true, optional = true }

[build-dependencies]
parity-wasm = { workspace = true }
tempfile = { workspace = true }
toml = { workspace = true }
polkavm-linker = { version = "0.10.0" }
anyhow = { workspace = true }
anyhow = { workspace = true, default-features = true }

[features]
default = ["std"]
Expand All @@ -30,7 +32,9 @@ default = ["std"]
riscv = []
# only when std is enabled all fixtures are available
std = [
"anyhow/std",
"anyhow",
"frame-system",
"sp-core",
"sp-io",
"sp-runtime",
]
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);
}
}
Loading
Loading