-
Notifications
You must be signed in to change notification settings - Fork 994
[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
[pallet-revive] Move to_account_id
host function to System
pre-compile
#9455
Conversation
/// # Note | ||
/// | ||
/// If no mapping exists for `addr`, the fallback account id will be returned. | ||
function toAccountId(address input) external pure returns (bytes32 account_id); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the assumption of AccountId32
correct or should we rather use bytes memory
here? I'm not sure if pallet-revive
supports generic AccountId
s?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in theory it could be anything,
but most likely if this is not an accountId32 it's an H160 and you don't need this precompile at all.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't revert though. We are using T::AccountId.encode()
here. In case that is everything else than AccountId32
we just return undecodeable garbage. I guess Solidity will just panic in this case. But it should be documented to not call this function on a native H160 chain.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, so my understanding from what you both wrote:
- Since you are using
T::AccountId.encode()
, which could be anything, the function must returnbytes memory
. That's also why the function name stays withtoAccountId()
. - Calling this function on a chain with
type AccountId = H160
does not make sense, as it would just return the address that it was called with. This is what we want to document.
Correct?
@athei What do you mean with this?
In case that is everything else than
AccountId32
we just return undecodeable garbage.
Why would it be undecodable garbage if the chain uses anything else than type AccountId = AccountId32
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've updated the PR with the assumptions made above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't revert though. We are using T::AccountId.encode()
I am fine switching it to the current returns (bytes memory account_id)
I was thinking that it should revert if we do a try_into and return a 32 byte array.
keeping a static return type with the original returns (bytes32 account_id)
would be more efficient thus my suggestion above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why would it be undecodable garbage if the chain uses anything else than
type AccountId = AccountId32
?
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 comment
The 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.
FYI, for Solidity's abi.decode
, this is only true if you return less bytes than the target type (i.e. less than 32 in this example), the more bytes than expected case will decode fine (the excess bytes are essentially ignored)
Co-authored-by: xermicus <[email protected]>
/// # Note | ||
/// | ||
/// If no mapping exists for `addr`, the fallback account id will be returned. | ||
function toAccountId(address input) external pure returns (bytes32 account_id); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't revert though. We are using T::AccountId.encode()
here. In case that is everything else than AccountId32
we just return undecodeable garbage. I guess Solidity will just panic in this case. But it should be documented to not call this function on a native H160 chain.
Co-authored-by: Alexander Theißen <[email protected]>
Looks good. But let's hold off merging until #9487 is in. |
8744f5e
…mpile (#9455) Part of closing #8572. cc @athei @pgherveou --------- Co-authored-by: xermicus <[email protected]> Co-authored-by: Alexander Theißen <[email protected]> (cherry picked from commit 8744f5e)
Successfully created backport PR for |
Part of closing #8572.
cc @athei @pgherveou