Skip to content

Conversation

cmichi
Copy link
Contributor

@cmichi cmichi commented Aug 8, 2025

Part of closing #8572.

cc @athei @pgherveou

/// # Note
///
/// If no mapping exists for `addr`, the fallback account id will be returned.
function toAccountId(address input) external pure returns (bytes32 account_id);
Copy link
Contributor Author

@cmichi cmichi Aug 8, 2025

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 AccountIds?

Copy link
Contributor

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

Copy link
Member

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.

Copy link
Contributor Author

@cmichi cmichi Aug 14, 2025

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 return bytes memory. That's also why the function name stays with toAccountId().
  • 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?

Copy link
Contributor Author

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.

Copy link
Contributor

@pgherveou pgherveou Aug 15, 2025

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.

Copy link
Member

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.

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)

/// # Note
///
/// If no mapping exists for `addr`, the fallback account id will be returned.
function toAccountId(address input) external pure returns (bytes32 account_id);
Copy link
Member

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.

@athei athei added T7-smart_contracts This PR/Issue is related to smart contracts. A4-backport-unstable2507 Pull request must be backported to the unstable2507 release branch labels Aug 15, 2025
@athei
Copy link
Member

athei commented Aug 15, 2025

Looks good. But let's hold off merging until #9487 is in.

@athei athei added this pull request to the merge queue Aug 18, 2025
Merged via the queue into paritytech:master with commit 8744f5e Aug 18, 2025
272 of 282 checks passed
paritytech-release-backport-bot bot pushed a commit that referenced this pull request Aug 18, 2025
…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)
@paritytech-release-backport-bot

Successfully created backport PR for unstable2507:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A4-backport-unstable2507 Pull request must be backported to the unstable2507 release branch T7-smart_contracts This PR/Issue is related to smart contracts.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants