-
Notifications
You must be signed in to change notification settings - Fork 1.2k
[pallet-revive] Move to_account_id host function to System pre-compile
#9455
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
Changes from 4 commits
b720dc0
d9a1289
729f00b
fc79a65
7b7b659
b4f5804
bdf2cd7
30763c9
c7fb5cf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| title: '[pallet-revive] Migrate `to_account_id` from host function to pre-compile' | ||
| doc: | ||
| - audience: Runtime Dev | ||
| description: Moves the unstable host function `to_account_id` to the | ||
| `System` pre-compile. | ||
| crates: | ||
| - name: pallet-revive | ||
| bump: minor | ||
| - name: pallet-revive-fixtures | ||
| bump: major | ||
|
cmichi marked this conversation as resolved.
Outdated
|
||
| - name: pallet-revive-uapi | ||
| bump: major | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,6 +31,12 @@ sol! { | |
| interface ISystem { | ||
| /// Computes the BLAKE2 256-bit hash on the given input. | ||
| function hashBlake256(bytes memory input) external pure returns (bytes32 digest); | ||
| /// Retrieve the account id for a specified `H160` address. | ||
| /// | ||
| /// # Note | ||
| /// | ||
| /// If no mapping exists for `addr`, the fallback account id will be returned. | ||
| function toAccountId(address input) external pure returns (bytes32 account_id); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the assumption of
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in theory it could be anything, Maybe just call it toAccountId32 to be explicit that this is intended for when the accountId is an accountId32, and mention that this revert if that's not the case
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It doesn't revert though. We are using
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, so my understanding from what you both wrote:
Correct? @athei What do you mean with this?
Why would it be undecodable garbage if the chain uses anything else than
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've updated the PR with the assumptions made above.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I am fine switching it to the current
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Because the data type says 32bytes but you return a different number of bytes. Solidity will fail during decoding. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
FYI, for Solidity's
cmichi marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
|
|
||
|
|
@@ -53,17 +59,96 @@ impl<T: Config> BuiltinPrecompile for System<T> { | |
| let output = sp_io::hashing::blake2_256(input.as_bytes_ref()); | ||
| Ok(output.to_vec()) | ||
| }, | ||
| ISystemCalls::toAccountId(ISystem::toAccountIdCall { input }) => { | ||
| use crate::address::AddressMapper; | ||
| use codec::Encode; | ||
| env.gas_meter_mut().charge(RuntimeCosts::ToAccountId)?; | ||
| let account_id = | ||
| T::AddressMapper::to_account_id(&crate::H160::from_slice(input.as_slice())); | ||
| Ok(account_id.encode()) | ||
| }, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use crate::{precompiles::tests::run_test_vectors, tests::Test}; | ||
| use super::{ISystem, *}; | ||
| use crate::{ | ||
| address::AddressMapper, | ||
| call_builder::{caller_funding, CallSetup}, | ||
| evm::Account, | ||
| pallet, | ||
| precompiles::{tests::run_test_vectors, BuiltinPrecompile}, | ||
| tests::{ExtBuilder, Test}, | ||
| H160, | ||
| }; | ||
| use codec::Decode; | ||
| use frame_support::traits::fungible::Mutate; | ||
|
|
||
| #[test] | ||
| fn test_system_precompile() { | ||
| run_test_vectors::<System<Test>>(include_str!("testdata/900-blake2_256.json")); | ||
| run_test_vectors::<System<Test>>(include_str!("testdata/900-to_account_id.json")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_system_precompile_unmapped_account() { | ||
| ExtBuilder::default().build().execute_with(|| { | ||
| // given | ||
| let mut call_setup = CallSetup::<Test>::default(); | ||
| let (mut ext, _) = call_setup.ext(); | ||
| let unmapped_address = H160::zero(); | ||
|
|
||
| // when | ||
| let input = ISystem::ISystemCalls::toAccountId(ISystem::toAccountIdCall { | ||
| input: unmapped_address.0.into(), | ||
| }); | ||
| let expected_fallback_account_id = | ||
| <System<Test>>::call(&<System<Test>>::MATCHER.base_address(), &input, &mut ext) | ||
| .unwrap(); | ||
|
|
||
| // then | ||
| assert_eq!( | ||
| expected_fallback_account_id[20..32], | ||
| [0xEE; 12], | ||
| "no fallback suffix found where one should be" | ||
| ); | ||
| }) | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_system_precompile_mapped_account() { | ||
| use crate::test_utils::EVE; | ||
| ExtBuilder::default().build().execute_with(|| { | ||
| // given | ||
| let mapped_address = { | ||
| <Test as pallet::Config>::Currency::set_balance(&EVE, caller_funding::<Test>()); | ||
| let _ = <Test as pallet::Config>::AddressMapper::map(&EVE); | ||
| <Test as pallet::Config>::AddressMapper::to_address(&EVE) | ||
| }; | ||
|
|
||
| let mut call_setup = CallSetup::<Test>::default(); | ||
| let (mut ext, _) = call_setup.ext(); | ||
|
|
||
| // when | ||
| let input = ISystem::ISystemCalls::toAccountId(ISystem::toAccountIdCall { | ||
| input: mapped_address.0.into(), | ||
| }); | ||
| let data = | ||
| <System<Test>>::call(&<System<Test>>::MATCHER.base_address(), &input, &mut ext) | ||
| .unwrap(); | ||
|
|
||
| // then | ||
| assert_ne!( | ||
| data.as_slice()[20..32], | ||
| [0xEE; 12], | ||
| "fallback suffix found where none should be" | ||
| ); | ||
| assert_eq!( | ||
| <Test as frame_system::Config>::AccountId::decode(&mut data.as_slice()), | ||
| Ok(EVE), | ||
| ); | ||
| }) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| [ | ||
| { | ||
| "Input": "cf5231cc00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000", | ||
| "Expected": "0000000000000000000000000000000000000020eeeeeeeeeeeeeeeeeeeeeeee", | ||
| "Name": "vector 1", | ||
| "Gas": 8000000, | ||
| "NoBenchmark": false | ||
| } | ||
| ] |
Uh oh!
There was an error while loading. Please reload this page.