Skip to content

Commit

Permalink
feat: contract deployment callback (#981)
Browse files Browse the repository at this point in the history
Co-authored-by: Grigoriy Simonov <gsimonov@usetech.com>
  • Loading branch information
fairax and Grigoriy Simonov authored Feb 6, 2023
1 parent e52e5bd commit ede6023
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 2 deletions.
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

0 comments on commit ede6023

Please sign in to comment.