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

Add contract deployment callback OnCreate #981

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frame/ethereum/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ impl pallet_evm::Config for Test {
type BlockGasLimit = BlockGasLimit;
type Runner = pallet_evm::runner::stack::Runner<Self>;
type OnChargeTransaction = ();
type OnCreate = ();
type FindAuthor = FindAuthorTruncated;
}

Expand Down
1 change: 1 addition & 0 deletions frame/evm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ targets = ["x86_64-unknown-linux-gnu"]
environmental = { workspace = true, optional = true }
evm = { workspace = true, features = ["with-codec"] }
hex = { version = "0.4.3", default-features = false, features = ["alloc"] }
impl-trait-for-tuples = "0.2.2"
log = { version = "0.4.17", default-features = false }
rlp = { workspace = true }
scale-codec = { package = "parity-scale-codec", workspace = true }
Expand Down
1 change: 1 addition & 0 deletions frame/evm/precompile/dispatch/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ impl pallet_evm::Config for Test {
type BlockGasLimit = BlockGasLimit;
type Runner = pallet_evm::runner::stack::Runner<Self>;
type OnChargeTransaction = ();
type OnCreate = ();
type FindAuthor = FindAuthorTruncated;
}

Expand Down
21 changes: 21 additions & 0 deletions frame/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ use frame_support::{
weights::Weight,
};
use frame_system::RawOrigin;
use impl_trait_for_tuples::impl_for_tuples;
use sp_core::{Hasher, H160, H256, U256};
use sp_runtime::{
traits::{BadOrigin, Saturating, UniqueSaturatedInto, Zero},
Expand Down Expand Up @@ -148,6 +149,9 @@ pub mod pallet {
/// Similar to `OnChargeTransaction` of `pallet_transaction_payment`
type OnChargeTransaction: OnChargeEVMTransaction<Self>;

/// Called on create calls, used to record owner
type OnCreate: OnCreate<Self>;

/// Find author for the current block.
type FindAuthor: FindAuthor<H160>;

Expand Down Expand Up @@ -892,3 +896,20 @@ U256: UniqueSaturatedInto<BalanceOf<T>>,
<EVMCurrencyAdapter::<<T as Config>::Currency, ()> as OnChargeEVMTransaction<T>>::pay_priority_fee(tip);
}
}

pub trait OnCreate<T> {
fn on_create(owner: H160, contract: H160);
}

impl<T> OnCreate<T> for () {
fn on_create(_owner: H160, _contract: H160) {}
}

#[impl_for_tuples(1, 12)]
impl<T> OnCreate<T> for Tuple {
fn on_create(owner: H160, contract: H160) {
for_tuples!(#(
Tuple::on_create(owner, contract);
)*)
}
}
1 change: 1 addition & 0 deletions frame/evm/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ impl crate::Config for Test {
type BlockGasLimit = BlockGasLimit;
type Runner = crate::runner::stack::Runner<Self>;
type OnChargeTransaction = ();
type OnCreate = ();
type FindAuthor = FindAuthorTruncated;
}

Expand Down
6 changes: 4 additions & 2 deletions frame/evm/src/runner/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@

use crate::{
runner::Runner as RunnerT, AccountCodes, AccountStorages, AddressMapping, BalanceOf,
BlockHashMapping, Config, Error, Event, FeeCalculator, OnChargeEVMTransaction, Pallet,
RunnerError,
BlockHashMapping, Config, Error, Event, FeeCalculator, OnChargeEVMTransaction, OnCreate,
Pallet, RunnerError,
};
use evm::{
backend::Backend as BackendT,
Expand Down Expand Up @@ -417,6 +417,7 @@ where
is_transactional,
|executor| {
let address = executor.create_address(evm::CreateScheme::Legacy { caller: source });
T::OnCreate::on_create(source, address);
let (reason, _) =
executor.transact_create(source, value, init, gas_limit, access_list);
(reason, address)
Expand Down Expand Up @@ -470,6 +471,7 @@ where
code_hash,
salt,
});
T::OnCreate::on_create(source, address);
let (reason, _) =
executor.transact_create2(source, value, init, salt, gas_limit, access_list);
(reason, address)
Expand Down
1 change: 1 addition & 0 deletions template/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ impl pallet_evm::Config for Runtime {
type BlockGasLimit = BlockGasLimit;
type Runner = pallet_evm::runner::stack::Runner<Self>;
type OnChargeTransaction = ();
type OnCreate = ();
type FindAuthor = FindAuthorTruncated<Aura>;
}

Expand Down