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

Precompile for balances ERC-20 #1731

Merged
merged 14 commits into from
Feb 15, 2024
Merged

Precompile for balances ERC-20 #1731

merged 14 commits into from
Feb 15, 2024

Conversation

lemunozm
Copy link
Contributor

@lemunozm lemunozm commented Feb 13, 2024

Description

Fixes #1726

Changes and Descriptions

  • Use last way of defining precompiles from precompile-utils, which is more type safety.
  • Add precompiles for balances-erc20
  • Force forbid-evm-reentrancy feature
  • Protect against DelegateCall security issue

@lemunozm lemunozm added P5-soon Issue should be addressed soon. I8-enhancement An additional feature. labels Feb 13, 2024
@lemunozm lemunozm self-assigned this Feb 13, 2024
Copy link
Contributor Author

@lemunozm lemunozm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor comments

runtime/common/src/evm/precompile.rs Show resolved Hide resolved
runtime/common/src/evm/precompile.rs Outdated Show resolved Hide resolved
runtime/common/src/migrations/precompile_account_codes.rs Outdated Show resolved Hide resolved
@lemunozm lemunozm requested a review from cdamian February 13, 2024 16:12
@lemunozm lemunozm marked this pull request as ready for review February 13, 2024 16:12
Copy link
Collaborator

@mustermeiszer mustermeiszer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Still got questions around the precompiles callable logic.

And we are missing the account code migration for the Erc20 precompile.

node/src/chain_spec.rs Outdated Show resolved Hide resolved
Comment on lines 21 to 28
pub fn to_evm_address(account: &AccountId) -> H160 {
// EVM AccountId20 addresses are right-padded to 32 bytes
let mut bytes20 = [0; 20];
let account_32: [u8; 32] = account.clone().into();
bytes20.copy_from_slice(&account_32);
H160::from(bytes20)
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the new method? Doesn't our current converter already provide means to go back to H160. I am not the biggest fan of having this here if this is a duplicate and creates a second source of truth for conversions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure exactly why, but PrecompileSetBuilder::user_addresses() returns an AccountId instead of H160 (internally, it transforms H160 into AccountId).

So, we need this method to go back from AccountId to H160 in chain_spec.rs.

I really think it is taking more responsibility than it should, but I have no choice right now with the API they offer. Maybe I can open an issue regarding this in Moonbeam.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't our current converter already provide means to go back to H160

Not for this direction, only for H160 -> AccountId

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the router we are doing this conversion:

	/// NOTE - the sender account ID provided here will be converted to an EVM
	/// address via truncating. When the call is processed by the underlying EVM
	/// pallet, this EVM address will be converted back into a substrate account
	/// which will be charged for the transaction. This converted substrate
	/// account is not the same as the original account.
	pub fn do_send(&self, sender: T::AccountId, msg: Vec<u8>) -> DispatchResultWithPostInfo {
		let sender_evm_address = H160::from_slice(&sender.as_ref()[0..20]);

and here we are doing it also. I would like to have the runtime_common::account_conversion::AccountConverter to implement a trait Convert<AccountId32, H160> and then use this converter as an associated type in theses pallets. Otherwise, we have multiple locations that do conversion.

No blocker. Just do not wanna create a foot-gun...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I guess H160::from_slice(&sender.as_ref()[0..20]) should be extracted to a converter.

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 been juggling traits in this commit: 0fb227c, and I've got to remove the conversion, so there is no issue (at least by now).

node/src/chain_spec.rs Outdated Show resolved Hide resolved
runtime/common/src/evm/precompile.rs Show resolved Hide resolved
runtime/common/src/evm/precompile.rs Outdated Show resolved Hide resolved
runtime/common/src/evm/precompile.rs Show resolved Hide resolved
runtime/common/src/evm/precompile.rs Show resolved Hide resolved
runtime/common/src/evm/precompile.rs Show resolved Hide resolved
runtime/common/src/evm/precompile.rs Outdated Show resolved Hide resolved
runtime/integration-tests/src/evm/precompile.rs Outdated Show resolved Hide resolved
@lemunozm
Copy link
Contributor Author

I think I've solved all comments @mustermeiszer

# Recomended command:
# cargo install taplo-cli --locked
cargo install --git=https://github.com/tamasfe/taplo.git taplo-cli
cargo install taplo-cli --locked
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Required to fix taplo CI

Copy link
Collaborator

@mustermeiszer mustermeiszer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If try-runtime does show the migration of the ERC20 precompile then I am happy. Would love to see the logs thought, that we can be sure that T::PrecompilesType::h160_addresses() works correctly.

@lemunozm
Copy link
Contributor Author

lemunozm commented Feb 15, 2024

If try-runtime does show the migration of the ERC20 precompile then I am happy

  • In centrifuge it shows that just 1 new precompile has been added (balances-erc20)
  • In altair it shows that 14 precompiles were added (all precompiles)

I could also log the address in the utility method, but the method is infallible and very simple to work as expected in all cases.

@lemunozm lemunozm merged commit db2209f into main Feb 15, 2024
9 checks passed
@wischli wischli deleted the feat/precompile-balances-erc20 branch February 15, 2024 10:11
@lemunozm
Copy link
Contributor Author

@mustermeiszer, merged but still open to add a log if after what I say you still consider we need it

@mustermeiszer
Copy link
Collaborator

Fine for me if you are confident it works!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
I8-enhancement An additional feature. P5-soon Issue should be addressed soon.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feat: ERC-20 Precompile for Balances
3 participants