Skip to content

Conversation

@iulianbarbu
Copy link

@iulianbarbu iulianbarbu commented Sep 30, 2025

Motivation

Closes #242 .

Solution

  1. Copy the cheat manager from anvil under substrate_node/impersonation.rs.
  2. Override secp256k1_ecdsa_recover versions of sp_io crypto host functions and use them for the wasm executor of the substrate runtime.
  3. Override kecckak_256 sp_io's hashing host functions and use it for the wasm executor of the substrate runtime.
  4. Integration tests that go through: random account impersonation, stopping account impersonation, auto account impersonation.

...account RPC

Signed-off-by: Iulian Barbu <iulian.barbu@parity.io>
Signed-off-by: Iulian Barbu <iulian.barbu@parity.io>
Signed-off-by: Iulian Barbu <iulian.barbu@parity.io>
Signed-off-by: Iulian Barbu <iulian.barbu@parity.io>
...account test

Signed-off-by: Iulian Barbu <iulian.barbu@parity.io>
Signed-off-by: Iulian Barbu <iulian.barbu@parity.io>
Signed-off-by: Iulian Barbu <iulian.barbu@parity.io>
Signed-off-by: Iulian Barbu <iulian.barbu@parity.io>
Signed-off-by: Iulian Barbu <iulian.barbu@parity.io>
Signed-off-by: Iulian Barbu <iulian.barbu@parity.io>
@iulianbarbu iulianbarbu self-assigned this Sep 30, 2025
@iulianbarbu iulianbarbu added the enhancement New feature or request label Sep 30, 2025
redundant-lifetimes = "warn"
rust-2018-idioms = "warn"
unused-must-use = "warn"
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(substrate_runtime)'] }
Copy link
Author

Choose a reason for hiding this comment

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

needed to supress some warnings after overriding the host functions

@iulianbarbu
Copy link
Author

iulianbarbu commented Sep 30, 2025

From @alindima offline:

  • try to reduce Cargo.lock size if possible
  • remove lock from cheats manager (it is accessed in RPC calls which are serialized)
  • rename cheats logic specifically to impersonation (we don't cheat other than impersonation)

Copy link

@alindima alindima left a comment

Choose a reason for hiding this comment

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

LGTM overall

@alindima
Copy link

IIRC this could also be configured via CLI

@iulianbarbu
Copy link
Author

IIRC this could also be configured via CLI

Good catch, the auto impersonation enabling can be done via CLI too. Will add support for this, thanks!

Signed-off-by: Iulian Barbu <iulian.barbu@parity.io>
Signed-off-by: Iulian Barbu <iulian.barbu@parity.io>
Signed-off-by: Iulian Barbu <iulian.barbu@parity.io>
Signed-off-by: Iulian Barbu <iulian.barbu@parity.io>
Signed-off-by: Iulian Barbu <iulian.barbu@parity.io>
Signed-off-by: Iulian Barbu <iulian.barbu@parity.io>
…alse

Signed-off-by: Iulian Barbu <iulian.barbu@parity.io>
Also created the impersonation manager accounting for the
`enable_auto_impersonate` anvil config flag.

Signed-off-by: Iulian Barbu <iulian.barbu@parity.io>
Signed-off-by: Iulian Barbu <iulian.barbu@parity.io>
Signed-off-by: Iulian Barbu <iulian.barbu@parity.io>
...touched Cargo.tomls

Signed-off-by: Iulian Barbu <iulian.barbu@parity.io>
@iulianbarbu
Copy link
Author

From @alindima offline:

* try to reduce Cargo.lock size if possible

* remove lock from cheats manager (it is accessed in RPC calls which are serialized)

* rename cheats logic specifically to impersonation (we don't cheat other than impersonation)

IIRC this could also be configured via CLI

cc @alindima

  • used impersonation naming instead of cheats everywhere (simplifying the manager to be dedicated to impersonation as well by removing the cheats state, which planned to be more generic and used for other cheats too), removed the inner lock and added support for auto impersonation setting via CLI: 61c25f0
  • reduced cargo.lock size: 31dcfd0 + 4e60394

@AlexandruCihodaru given our discussion offline about the toml formatters, I added a .taplo.toml config file, same as the one used by taplo itself. I don't think it is a bad idea doing it at this point, since editors differ in how they treat identation so using same formatting will simplify reviews. Alternatively we can reuse same config as for polkadot-sdk, but I don't think it is important. I might be able to do it in a separate PR but I think it is a bit pointless to revert the changes (they aren't that many and also they are simple to review). Either way please lmk your thoughts.

@iulianbarbu iulianbarbu requested a review from re-gius October 1, 2025 10:48
@iulianbarbu iulianbarbu marked this pull request as ready for review October 1, 2025 10:48
Signed-off-by: Iulian Barbu <iulian.barbu@parity.io>
@iulianbarbu iulianbarbu requested a review from skunert October 1, 2025 12:35
@iulianbarbu iulianbarbu mentioned this pull request Oct 2, 2025
Copy link

@AlexandruCihodaru AlexandruCihodaru left a comment

Choose a reason for hiding this comment

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

Amazing job! ❤️

// Assert on balances after first transfer.
let alith_final_balance = node.get_balance(alith_account.address(), latest_block).await;
let dest_final_balance = node.get_balance(dest_h160, latest_block).await;
let existential_deposit = U256::from_str_radix("100000000000000000000", 10).unwrap();

Choose a reason for hiding this comment

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

nit: make this a constant in utils.rs because it could be useful in other tests

Copy link

Choose a reason for hiding this comment

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

and make it use the constant in substrate_runtime::currency::DOLLARS under the hood

Copy link
Author

Choose a reason for hiding this comment

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

Created a u128 cost, and whenever needed as u256 I am converting it. Did it this way to avoid creating a static/lazylock (since a cost u256 creation requires calling non-const fn's like U256::from(..).unwrap(), which can not be done with consts): bcab3f9

Copy link

@alindima alindima left a comment

Choose a reason for hiding this comment

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

Good job!

"target",
]

[formatting]
Copy link

Choose a reason for hiding this comment

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

do we have a CI step for checking this format? Because if we don't, it's not that useful. Not everyone uses a code editor that automatically uses taplo for formatting.

Also, where did we get this taplo format from? I am not a big fan of the rule which adds whitespace in order to align the = signs in dependency lists.

Overall I'm not really sure we need it at this point. Anyway, let's not bundle this in this PR, as it's unrelated and would be best to also ask the embedded EVM guys for feedback

Copy link
Author

Choose a reason for hiding this comment

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

do we have a CI step for checking this format?

We should add one. I can work on this in some other PR, once a taplo config makes its way into the repo.

Because if we don't, it's not that useful. Not everyone uses a code editor that automatically uses taplo for formatting.

It is not a matter of automatically using taplo with the code editor, but to configure it (assuming our code editors allows it). I thought that our team should have taplo configured already given we also opened in the past PRs against polkadot-sdk.

Also, where did we get this taplo format from? I am not a big fan of the rule which adds whitespace in order to align the = signs in dependency lists.

It is the taplo config used by the taplo-cli repo. Changing a rule or the entire taplo config is straight forward (with the caveat that all tomls from anvil-polkadot should be formatted afterward). I am fine with most of the rules, only to have all of us use the same ones.

Overall I'm not really sure we need it at this point. Anyway, let's not bundle this in this PR, as it's unrelated and would be best to also ask the embedded EVM guys for feedback

Sure, we can ask. By then I'll revert my toml changes. I hoped we can align quickly behind some "battle tested" formatting, whichever that is and assumed people can comply easily with taplo formatting in their editors (given our previous polkadot-sdk practice), and preferences to be factored in incrementally. Will open a new PR on this subject, maybe with the formatting rules of polkadot-sdk (since we know them already).

Copy link
Author

Choose a reason for hiding this comment

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

Revert here: 5d489e0

Copy link
Author

Choose a reason for hiding this comment

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

Removed taplo config: 6162666.

Copy link

@alindima alindima Oct 6, 2025

Choose a reason for hiding this comment

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

This could be useful, but we should request feedback on the other devs working on the repo (since it affects all of us) and make sure that it's enforced by CI (otherwise it's not that useful)

// Assert on balances after first transfer.
let alith_final_balance = node.get_balance(alith_account.address(), latest_block).await;
let dest_final_balance = node.get_balance(dest_h160, latest_block).await;
let existential_deposit = U256::from_str_radix("100000000000000000000", 10).unwrap();
Copy link

Choose a reason for hiding this comment

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

and make it use the constant in substrate_runtime::currency::DOLLARS under the hood

Signed-off-by: Iulian Barbu <iulian.barbu@parity.io>
Signed-off-by: Iulian Barbu <iulian.barbu@parity.io>
Signed-off-by: Iulian Barbu <iulian.barbu@parity.io>
@iulianbarbu
Copy link
Author

Some unrelated tests are failing, will have to look into it too before merging.

@alindima
Copy link

alindima commented Oct 7, 2025

Some unrelated tests are failing, will have to look into it too before merging.

I think this will fix them: #338. I think we can merge it since it's unrelated and the fix is in review

iulianbarbu and others added 3 commits October 7, 2025 15:14
@iulianbarbu iulianbarbu enabled auto-merge (squash) October 7, 2025 13:26
@iulianbarbu iulianbarbu merged commit 26eda0d into master Oct 7, 2025
30 checks passed
@iulianbarbu iulianbarbu deleted the ib-add-impersonation branch October 7, 2025 15:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Anvil] Impersonation

4 participants