-
Notifications
You must be signed in to change notification settings - Fork 1.1k
pallet-revive: add interface to implement mocks and pranks #9909
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 7 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
a57b755
add mock handlers
alexggh 9d13964
minor fixes
alexggh 2c25da9
make no_std build work
alexggh ec1c7c2
review feedback
alexggh 1c24619
pallet-revive: make a try_upload_pvm_code/code_hash public
alexggh 5ab45ad
use references
alexggh 42e82dd
address revivew feedback
alexggh b6d975c
Merge remote-tracking branch 'origin/master' into alexggh/inject_stat…
alexggh 5f4e784
add mock hooks tests
alexggh 98634c8
Merge remote-tracking branch 'origin/master' into alexggh/inject_stat…
alexggh 47418ca
make sure mock calls don't consume gas, storage, transfer value
alexggh 47f5cf8
remove unneeded empty
alexggh 4b2835f
Merge remote-tracking branch 'origin/master' into alexggh/inject_stat…
alexggh ebff833
Update from github-actions[bot] running command 'prdoc --audience nod…
github-actions[bot] 077ef11
Update pr_9909.prdoc
alexggh c63805e
address review feedback
alexggh fc05c46
make semver happy
alexggh 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
alexggh marked this conversation as resolved.
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 |
|---|---|---|
|
|
@@ -38,6 +38,7 @@ mod vm; | |
|
|
||
| pub mod evm; | ||
| pub mod migrations; | ||
| pub mod mock; | ||
| pub mod precompiles; | ||
| pub mod test_utils; | ||
| pub mod tracing; | ||
|
|
@@ -48,7 +49,7 @@ use crate::{ | |
| create_call, fees::InfoT as FeeInfo, runtime::SetWeightLimit, CallTracer, | ||
| GenericTransaction, PrestateTracer, Trace, Tracer, TracerType, TYPE_EIP1559, | ||
| }, | ||
| exec::{AccountIdOf, ExecError, Executable, Stack as ExecStack}, | ||
| exec::{AccountIdOf, ExecError, Stack as ExecStack}, | ||
| gas::GasMeter, | ||
| storage::{meter::Meter as StorageMeter, AccountType, DeletionQueueManager}, | ||
| tracing::if_tracing, | ||
|
|
@@ -87,7 +88,7 @@ pub use crate::{ | |
| address::{ | ||
| create1, create2, is_eth_derived, AccountId32Mapper, AddressMapper, TestAccountMapper, | ||
| }, | ||
| exec::{Key, MomentOf, Origin as ExecOrigin}, | ||
| exec::{DelegateInfo, Executable, Key, MomentOf, Origin as ExecOrigin}, | ||
| pallet::{genesis, *}, | ||
| storage::{AccountInfo, ContractInfo}, | ||
| }; | ||
|
|
@@ -1275,7 +1276,7 @@ impl<T: Config> Pallet<T> { | |
| gas_limit: Weight, | ||
| storage_deposit_limit: BalanceOf<T>, | ||
| data: Vec<u8>, | ||
| exec_config: ExecConfig, | ||
| exec_config: ExecConfig<T>, | ||
| ) -> ContractResult<ExecReturnValue, BalanceOf<T>> { | ||
| if let Err(contract_result) = Self::ensure_non_contract_if_signed(&origin) { | ||
| return contract_result; | ||
|
|
@@ -1334,7 +1335,7 @@ impl<T: Config> Pallet<T> { | |
| code: Code, | ||
| data: Vec<u8>, | ||
| salt: Option<[u8; 32]>, | ||
| exec_config: ExecConfig, | ||
| exec_config: ExecConfig<T>, | ||
| ) -> ContractResult<InstantiateReturnValue, BalanceOf<T>> { | ||
| // Enforce EIP-3607 for top-level signed origins: deny signed contract addresses. | ||
| if let Err(contract_result) = Self::ensure_non_contract_if_signed(&origin) { | ||
|
|
@@ -1849,11 +1850,11 @@ impl<T: Config> Pallet<T> { | |
| } | ||
|
|
||
| /// Uploads new code and returns the Vm binary contract blob and deposit amount collected. | ||
| fn try_upload_pvm_code( | ||
| pub fn try_upload_pvm_code( | ||
| origin: T::AccountId, | ||
| code: Vec<u8>, | ||
| storage_deposit_limit: BalanceOf<T>, | ||
| exec_config: &ExecConfig, | ||
| exec_config: &ExecConfig<T>, | ||
| ) -> Result<(ContractBlob<T>, BalanceOf<T>), DispatchError> { | ||
| let mut module = ContractBlob::from_pvm_code(code, origin)?; | ||
| let deposit = module.store_code(exec_config, None)?; | ||
|
|
@@ -1881,7 +1882,7 @@ impl<T: Config> Pallet<T> { | |
| } | ||
|
|
||
| /// Convert a weight to a gas value. | ||
| fn evm_gas_from_weight(weight: Weight) -> U256 { | ||
| pub fn evm_gas_from_weight(weight: Weight) -> U256 { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where is that used from outside the crate?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| T::FeeInfo::weight_to_fee(weight).into() | ||
| } | ||
|
|
||
|
|
@@ -1967,7 +1968,7 @@ impl<T: Config> Pallet<T> { | |
| from: &T::AccountId, | ||
| to: &T::AccountId, | ||
| amount: BalanceOf<T>, | ||
| exec_config: &ExecConfig, | ||
| exec_config: &ExecConfig<T>, | ||
| ) -> DispatchResult { | ||
| use frame_support::traits::tokens::{Fortitude, Precision, Preservation}; | ||
| match (exec_config.collect_deposit_from_hold, hold_reason) { | ||
|
|
@@ -2012,7 +2013,7 @@ impl<T: Config> Pallet<T> { | |
| from: &T::AccountId, | ||
| to: &T::AccountId, | ||
| amount: BalanceOf<T>, | ||
| exec_config: &ExecConfig, | ||
| exec_config: &ExecConfig<T>, | ||
| ) -> Result<BalanceOf<T>, DispatchError> { | ||
| use frame_support::traits::{ | ||
| tokens::{Fortitude, Precision, Preservation, Restriction}, | ||
|
|
||
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,55 @@ | ||
| // This file is part of Substrate. | ||
|
|
||
| // Copyright (C) Parity Technologies (UK) Ltd. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| //! Helper interfaces and functions, that help with controlling the execution of EVM contracts. | ||
| //! It is mostly used to help with the implementation of foundry cheatscodes for forge test | ||
| //! integration. | ||
| use frame_system::pallet_prelude::OriginFor; | ||
| use sp_core::{H160, U256}; | ||
|
|
||
| use crate::{pallet, DelegateInfo, ExecReturnValue}; | ||
|
|
||
| /// A trait that provides hooks for mocking EVM contract calls and callers. | ||
| /// This is useful for testing and simulating contract interactions within foundry forge tests. | ||
| pub trait MockHandler<T: pallet::Config> { | ||
pgherveou marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /// Mock an EVM contract call. | ||
| /// | ||
| /// Returns `Some(ExecReturnValue)` if the call is mocked, otherwise `None`. | ||
| fn mock_call( | ||
| &self, | ||
| _callee: H160, | ||
| _call_data: &[u8], | ||
| _value_transferred: U256, | ||
| ) -> Option<ExecReturnValue> { | ||
| None | ||
| } | ||
|
|
||
| /// Mock the caller of a contract. | ||
| /// | ||
| /// Returns `Some(OriginFor<T>)` if the caller is mocked, otherwise `None`. | ||
| fn mock_caller(&self, _frames_len: usize) -> Option<OriginFor<T>> { | ||
alexggh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| None | ||
| } | ||
|
|
||
| /// Mock a delegated caller for a contract call. | ||
| /// | ||
| /// Returns `Some(DelegateInfo<T>)` if the delegated caller is mocked, otherwise `None`. | ||
| fn mock_delegated_caller(&self, _dest: H160, _input_data: &[u8]) -> Option<DelegateInfo<T>> { | ||
| None | ||
| } | ||
| } | ||
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.