Skip to content

Commit

Permalink
feat: use solidity ABI encoding instead of RLP (#2406)
Browse files Browse the repository at this point in the history
* feat: use solidity ABI encoding instead of RLP - works, wip

* test(e2e): add sol -> ink! test with param; failing

* fix: abi_decode failed due to verify set to true

* refactor: remove RLP references to Solidity ABI Encoding

* feat(primitives): impl SolValue for `AccountId` and `Hash`

* fix: return type for RETURN, and warnings in UI tests

* test: Solidity ABI encoding with different inputs -- fails

* Clean up after merge

---------

Co-authored-by: Michael Mueller <[email protected]>
  • Loading branch information
peterwht and cmichi authored Feb 27, 2025
1 parent ec05e8c commit b4563e0
Show file tree
Hide file tree
Showing 32 changed files with 467 additions and 290 deletions.
140 changes: 70 additions & 70 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ proc-macro2 = { version = "1" }
quickcheck = { version = "1" }
quickcheck_macros = { version = "1" }
quote = { version = "1" }
alloy-rlp = { version = "0.3.9", default-features = false }
alloy-sol-types = { version = "0.8.21", default-features = false }
scale = { package = "parity-scale-codec", version = "3.6.12", default-features = false, features = ["derive"] }
scale-decode = { version = "0.14.0", default-features = false }
scale-encode = { version = "0.8.0", default-features = false }
Expand All @@ -79,7 +79,7 @@ tracing-subscriber = { version = "0.3.19" }
trybuild = { version = "1.0.102" }
which = { version = "7.0.0" }
xxhash-rust = { version = "0.8" }
const_env = { version = "0.1"}
const_env = { version = "0.1" }

# Substrate dependencies
frame-metadata = { version = "19.0.0", default-features = false }
Expand Down
48 changes: 24 additions & 24 deletions crates/env/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ ink_primitives = { workspace = true }
ink_macro = { workspace = true }
pallet-revive-uapi = { workspace = true }

alloy-rlp = { workspace = true }
alloy-sol-types = { workspace = true }
scale = { workspace = true, features = ["max-encoded-len"] }
derive_more = { workspace = true, features = ["from", "display"] }
num-traits = { workspace = true, features = ["i128"] }
Expand Down Expand Up @@ -65,40 +65,40 @@ scale-info = { workspace = true, features = ["derive"], optional = true }
ink = { path = "../ink" }

[features]
default = [ "std" ]
default = ["std"]
std = [
"alloy-rlp/std",
"blake2",
"alloy-sol-types/std",
"blake2",
"ink/std",
"ink_allocator/std",
"ink_prelude/std",
"ink_primitives/std",
"ink_storage_traits/std",
"ink_engine/std",
"ink_allocator/std",
"ink_prelude/std",
"ink_primitives/std",
"ink_storage_traits/std",
"ink_engine/std",
"ink_macro/std",
"scale/std",
"scale-decode",
"scale-encode",
"scale-info/std",
"secp256k1",
"schnorrkel",
"scale/std",
"scale-decode",
"scale-encode",
"scale-info/std",
"secp256k1",
"schnorrkel",
"sp-io/std",
"sp-runtime-interface/std",
"num-traits/std",
# Enables hashing crates for off-chain environment.
"sha2",
"sha3",
"scale-decode?/std",
"scale-encode?/std",
"xcm/std",
"derive_more/std"
"num-traits/std",
# Enables hashing crates for off-chain environment.
"sha2",
"sha3",
"scale-decode?/std",
"scale-encode?/std",
"xcm/std",
"derive_more/std"
]

# Enable contract debug messages via `debug_print!` and `debug_println!`.
ink-debug = []

# Disable the ink! provided global memory allocator.
no-allocator = [ "ink_allocator/no-allocator" ]
no-allocator = ["ink_allocator/no-allocator"]

# Disable the ink! provided panic handler.
no-panic-handler = []
Expand Down
8 changes: 4 additions & 4 deletions crates/env/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,17 +449,17 @@ where
})
}

/// Returns the *RLP encoded* value back to the caller of the executed contract.
/// Returns the *Solidity ABI encoded* value back to the caller of the executed contract.
///
/// # Note
///
/// This function stops the execution of the contract immediately.
pub fn return_value_rlp<R>(return_flags: ReturnFlags, return_value: &R) -> !
pub fn return_value_solidity<R>(return_flags: ReturnFlags, return_value: &R) -> !
where
R: alloy_rlp::Encodable,
R: alloy_sol_types::SolValue,
{
<EnvInstance as OnInstance>::on_instance(|instance| {
EnvBackend::return_value_rlp::<R>(instance, return_flags, return_value)
EnvBackend::return_value_solidity::<R>(instance, return_flags, return_value)
})
}

Expand Down
4 changes: 2 additions & 2 deletions crates/env/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ pub trait EnvBackend {
R: scale::Encode;

/// todo: comment
fn return_value_rlp<R>(&mut self, flags: ReturnFlags, return_value: &R) -> !
fn return_value_solidity<R>(&mut self, flags: ReturnFlags, return_value: &R) -> !
where
R: alloy_rlp::Encodable;
R: alloy_sol_types::SolValue;

/// Conducts the crypto hash of the given input and stores the result in `output`.
fn hash_bytes<H>(&mut self, input: &[u8], output: &mut <H as HashOutput>::Type)
Expand Down
6 changes: 3 additions & 3 deletions crates/env/src/engine/off_chain/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,11 +366,11 @@ impl EnvBackend for EnvInstance {
self.engine.set_storage(&[255_u8; 32], &v[..]);
}

fn return_value_rlp<R>(&mut self, _flags: ReturnFlags, _return_value: &R) -> !
fn return_value_solidity<R>(&mut self, _flags: ReturnFlags, _return_value: &R) -> !
where
R: alloy_rlp::Encodable,
R: alloy_sol_types::SolValue,
{
unimplemented!("the off-chain env does not implement `return_value_rlp`")
unimplemented!("the off-chain env does not implement `return_value_solidity`")
}

fn hash_bytes<H>(&mut self, input: &[u8], output: &mut <H as HashOutput>::Type)
Expand Down
19 changes: 0 additions & 19 deletions crates/env/src/engine/on_chain/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,25 +106,6 @@ impl scale::Output for EncodeScope<'_> {
}
}

unsafe impl alloy_rlp::bytes::BufMut for EncodeScope<'_> {
fn remaining_mut(&self) -> usize {
self.capacity() - self.len()
}
unsafe fn advance_mut(&mut self, cnt: usize) {
debug_assert!(
self.len().checked_add(cnt).unwrap() <= self.capacity(),
"encode scope buffer overflowed. capacity is {} but last write index is {}",
self.capacity(),
self.len().checked_add(cnt).unwrap(),
);
self.len = self.len.checked_add(cnt).unwrap()
}

fn chunk_mut(&mut self) -> &mut alloy_rlp::bytes::buf::UninitSlice {
alloy_rlp::bytes::buf::UninitSlice::new(&mut self.buffer[self.len..])
}
}

/// Scoped access to an underlying bytes buffer.
///
/// # Note
Expand Down
10 changes: 4 additions & 6 deletions crates/env/src/engine/on_chain/pallet_revive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,14 +283,12 @@ impl EnvBackend for EnvInstance {
ext::return_value(flags, &self.buffer[..][..len]);
}

fn return_value_rlp<R>(&mut self, flags: ReturnFlags, return_value: &R) -> !
fn return_value_solidity<R>(&mut self, flags: ReturnFlags, return_value: &R) -> !
where
R: alloy_rlp::Encodable,
R: alloy_sol_types::SolValue,
{
let mut scope = EncodeScope::from(&mut self.buffer[..]);
return_value.encode(&mut scope);
let len = scope.len();
ext::return_value(flags, &self.buffer[..][..len]);
let encoded = return_value.abi_encode();
ext::return_value(flags, &encoded[..]);
}

fn hash_bytes<H>(&mut self, input: &[u8], output: &mut <H as HashOutput>::Type)
Expand Down
33 changes: 17 additions & 16 deletions crates/ink/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ ink_metadata = { workspace = true, optional = true }
ink_prelude = { workspace = true }
ink_macro = { workspace = true }
pallet-revive-uapi = { workspace = true }
alloy-rlp = { workspace = true }
alloy-sol-types = { workspace = true }
scale = { workspace = true }
scale-info = { workspace = true, default-features = false, features = ["derive"], optional = true }
derive_more = { workspace = true, features = ["from"] }
Expand All @@ -40,29 +40,30 @@ ink_metadata = { workspace = true }
trybuild = { workspace = true, features = ["diff"] }

[features]
default = [ "std" ]
default = ["std"]
std = [
"alloy-rlp/std",
"ink_env/std",
"ink_macro/std",
"ink_metadata/std",
"ink_prelude/std",
"ink_primitives/std",
"ink_storage/std",
"scale-info/std",
"scale/std",
"xcm/std",
"derive_more/std",
"sp-io/std"
"alloy-sol-types/std",
"ink_env/std",
"ink_macro/std",
"ink_metadata/std",
"ink_prelude/std",
"ink_primitives/std",
"ink_storage/std",
"scale-info/std",
"scale/std",
"xcm/std",
"derive_more/std",
"sp-io/std"
]

# Enable contract debug messages via `debug_print!` and `debug_println!`.
ink-debug = [ "ink_env/ink-debug" ]
ink-debug = ["ink_env/ink-debug"]
ink-as-dependency = []

show-codegen-docs = []

# Disable the ink! provided global memory allocator.
no-allocator = [ "ink_env/no-allocator" ]
no-allocator = ["ink_env/no-allocator"]

# Disable the ink! provided panic handler.
no-panic-handler = ["ink_env/no-panic-handler"]
Expand Down
Loading

0 comments on commit b4563e0

Please sign in to comment.