-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Fix generated address returned by Substrate RPC runtime call #8504
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
75cffc3
Fix generated address returned by Substrate RPC runtime call
41fa5d3
Update from github-actions[bot] running command 'prdoc --audience run…
github-actions[bot] e2171b5
Update from github-actions[bot] running command 'fmt'
github-actions[bot] a1cb3e0
Update exec contexts in some places
5397556
Merge branch 'master' into castillax-dryrun-api
b4047ff
Update tests-evm
pgherveou b686c94
Use IncrementOnce enum and disassociate skip_transfer
b1e357a
Merge branch 'master' into castillax-dryrun-api
8c2d486
fix tests
a69a1f2
Change enum to NonceAlreadyIncremented
4cc01dd
Merge branch 'master' into castillax-dryrun-api
4d21dc3
Runtime bare_instantiates are dry runs
89b0c05
Merge branch 'master' into castillax-dryrun-api
5dc7092
Update .github/workflows/tests-evm.yml
b215946
remove unused arg
683c84c
Merge branch 'master' into castillax-dryrun-api
be30983
Add test for nonce_already_incremented
f8c3187
Merge branch 'master' into castillax-dryrun-api
6c30760
Fix comments on tests
998f58e
remove the other test
2517693
Merge branch 'master' into castillax-dryrun-api
846b885
Use builder pattern
e350976
Merge branch 'master' into castillax-dryrun-api
f929174
salt defaults to None, no need to set it
e5e683b
Merge branch 'master' into castillax-dryrun-api
0fd29ad
Merge branch 'master' into castillax-dryrun-api
7f124d2
setting salt to Some() will lead to create2 being used
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| title: Fix generated address returned by Substrate RPC runtime call | ||
| doc: | ||
| - audience: Runtime Dev | ||
| description: |- | ||
| ## Description | ||
|
|
||
| When dry-running a contract deployment through the runtime API, the returned address does not match the actual address that will be used when the transaction is submitted. This inconsistency occurs because the address derivation logic doesn't properly account for the difference between transaction execution and dry-run execution contexts. | ||
|
|
||
| The issue stems from the `create1` address derivation logic in `exec.rs`: | ||
|
|
||
| ```rust | ||
| address::create1( | ||
| &deployer, | ||
| // the Nonce from the origin has been incremented pre-dispatch, so we | ||
| // need to subtract 1 to get the nonce at the time of the call. | ||
| if origin_is_caller { | ||
| account_nonce.saturating_sub(1u32.into()).saturated_into() | ||
| } else { | ||
| account_nonce.saturated_into() | ||
| }, | ||
| ) | ||
| ``` | ||
|
|
||
| The code correctly subtracts 1 from the account nonce during a transaction execution (because the nonce is incremented pre-dispatch), but doesn't account for execution context - whether it's a real transaction or a dry run through the RPC. | ||
|
|
||
| ## Review Notes | ||
|
|
||
| This PR adds a new condition to check for the `ExecContext` when calculating the nonce for address derivation: | ||
|
|
||
| ```rust | ||
| address::create1( | ||
| &deployer, | ||
| // the Nonce from the origin has been incremented pre-dispatch, so we | ||
| // need to subtract 1 to get the nonce at the time of the call. | ||
| if origin_is_caller && matches!(exec_context, ExecContext::Transaction) { | ||
| account_nonce.saturating_sub(1u32.into()).saturated_into() | ||
| } else { | ||
| account_nonce.saturated_into() | ||
| }, | ||
| ) | ||
| ``` | ||
|
|
||
| A new test `nonce_not_incremented_in_dry_run()` has been added to verify the behavior. | ||
|
|
||
| ## Before Fix | ||
|
|
||
| - Dry-run contract deployment returns address derived with nonce N | ||
| - Actual transaction deployment creates contract at address derived with nonce N-1 | ||
| - Result: Inconsistent addresses between simulation and actual execution | ||
|
|
||
| ## After Fix | ||
|
|
||
| - Dry-run and actual transaction deployments both create contracts at the same address | ||
| - Result: Consistent contract addresses regardless of execution context | ||
| - Added test case to verify nonce handling in different execution contexts | ||
|
|
||
| This fix ensures that users can rely on the address returned by a dry run to match the actual address that will be used when the transaction is submitted. | ||
|
|
||
| Fixes https://github.com/paritytech/contract-issues/issues/37 | ||
|
|
||
| # Checklist | ||
|
|
||
| * [x] My PR includes a detailed description as outlined in the "Description" and its two subsections above. | ||
| * [x] My PR follows the [labeling requirements]( | ||
| https://github.com/paritytech/polkadot-sdk/blob/master/docs/contributor/CONTRIBUTING.md#Process | ||
| ) of this project (at minimum one label for `T` required) | ||
| * External contributors: ask maintainers to put the right label on your PR. | ||
| * [x] I have made corresponding changes to the documentation (if applicable) | ||
| * [x] I have added tests that prove my fix is effective or that my feature works (if applicable) | ||
| crates: | ||
| - name: asset-hub-westend-runtime | ||
| bump: patch | ||
| - name: pallet-revive | ||
| bump: patch | ||
castillax marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3432,6 +3432,7 @@ impl_runtime_apis! { | |
| gas_limit.unwrap_or(RuntimeBlockWeights::get().max_block), | ||
| pallet_revive::DepositLimit::Balance(storage_deposit_limit.unwrap_or(u128::MAX)), | ||
| input_data, | ||
| pallet_revive::ExecContext::Transaction, | ||
|
||
| ) | ||
| } | ||
|
|
||
|
|
@@ -3453,6 +3454,7 @@ impl_runtime_apis! { | |
| code, | ||
| data, | ||
| salt, | ||
| pallet_revive::ExecContext::Transaction, | ||
| ) | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.