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

EVM + Weight v2 support #1039

Merged
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
70 commits
Select commit Hold shift + click to select a range
4bab80e
wip `record_extarnal_cost`
tgmichel Apr 11, 2023
416d618
Add support for external cost recording from the EVM
tgmichel Apr 13, 2023
24c91b5
Add refund external cost support
tgmichel Apr 14, 2023
3dff83e
Separate dispatchables with weight limit
tgmichel Apr 17, 2023
3ace0af
introduce tbd EthereumWeigher
tgmichel Apr 17, 2023
6deaacb
Validate gas to weight conversion
tgmichel Apr 19, 2023
74708f5
Add static opcode recording with temp costs
tgmichel Apr 20, 2023
8ea977b
Add optional `transaction_len` parameter to `GasWeightMapping`
tgmichel Apr 20, 2023
dc95f98
Account for pallet and call indexes
tgmichel Apr 20, 2023
ac5ed2d
Rollback `transaction_len` param, use MAX_POV_SIZE ratio instead
tgmichel Apr 21, 2023
45c9e25
wip tests
tgmichel Apr 25, 2023
dc33686
Add `uncached_account_code_proof_size_accounting_works` test
tgmichel Apr 26, 2023
312146f
wip tests
tgmichel Apr 26, 2023
88c7c7c
Temp remove static external cost accounting
tgmichel Apr 27, 2023
163b0c5
fix build
tgmichel Apr 27, 2023
a17e331
Temp remove static external cost accounting 2
tgmichel Apr 27, 2023
8cc4568
warning cleanup
tgmichel Apr 27, 2023
3646639
fmt
tgmichel Apr 27, 2023
87e5f6f
clippy
tgmichel Apr 27, 2023
cc4e173
taplo
tgmichel Apr 27, 2023
eb2afed
Add `evm-with-weight-limit` to ci
tgmichel Apr 27, 2023
7814419
Temp set evm fork + update Cargo.lock
tgmichel Apr 27, 2023
255f663
Merge branch 'master' into tgm-record-external-costs
tgmichel Apr 27, 2023
00ffebc
handle `code_hash`
tgmichel Apr 27, 2023
5ab6432
taplo
tgmichel Apr 27, 2023
4bb5e70
fmt
tgmichel Apr 27, 2023
538c072
Handle `transact_with_weight_limit` as self contained
tgmichel Apr 28, 2023
7686543
fix ts tests
tgmichel Apr 28, 2023
e12afd7
suggestion
tgmichel Apr 28, 2023
cd77d2d
remove precompile test
tgmichel Apr 28, 2023
91ce2ed
some suggestions
tgmichel Apr 28, 2023
d6d7a00
remove `transact_with_weight_limit`
tgmichel Apr 28, 2023
83e228a
configurable `MaxPovSize`
tgmichel May 3, 2023
c369476
test pov size constants
tgmichel May 3, 2023
296cb2a
accessed storage overlayed cache
tgmichel May 4, 2023
c8138af
`new_from_weight_limit` suggestion
tgmichel May 4, 2023
34be73b
remove unnecessary check
tgmichel May 4, 2023
44c93c0
warnings cleanup
tgmichel May 5, 2023
17abbd3
set constant gas limit max pov size ratio
tgmichel May 5, 2023
3572aad
check state version for suicide
tgmichel May 5, 2023
4bbbe3d
just completely remove suicide accounting
tgmichel May 5, 2023
bd734b1
- `code` must be able to oog
tgmichel May 11, 2023
0a22130
Merge branch 'master' into tgm-record-external-costs
tgmichel May 12, 2023
1a1a47f
`is_empty` accounting
tgmichel May 12, 2023
a63f1d2
fix build
tgmichel May 12, 2023
e59eb9c
`SSTORE` must record account storage proof size
tgmichel May 12, 2023
91a5259
suggestion: move weight_limit checks
tgmichel May 16, 2023
3915652
editorconfig
tgmichel May 16, 2023
d09fc5b
fmt
tgmichel May 16, 2023
e9df020
rename `transaction_len` to `proof_size_base_cost` in runner
tgmichel May 16, 2023
a8fd45e
move `proof_size_base_cost` to validation primitive
tgmichel May 16, 2023
caefbec
gas limit saturated conversion
tgmichel May 24, 2023
df032b5
remove transaction proof size check outside validation
tgmichel May 24, 2023
e53da69
Merge branch 'master' into tgm-record-external-costs
tgmichel May 25, 2023
76c2f6c
pin evm
tgmichel May 25, 2023
6f6827a
pin evm+
tgmichel May 25, 2023
0154cc9
fix todos
tgmichel May 25, 2023
881f690
fix build
tgmichel May 25, 2023
eddef23
scope of already recorded codes and storages must be per transaction
tgmichel Jun 9, 2023
d6d4765
pin evm + implement new `record_external_operation`
tgmichel Jun 14, 2023
12fbeee
fix runtime api versioning + legacy `ExecutionInfo` handling
tgmichel Jun 19, 2023
8a968e9
Merge branch 'master' into tgm-record-external-costs
tgmichel Jun 19, 2023
da32bba
editorconfig
tgmichel Jun 19, 2023
5ba5fcc
cargo fmt
tgmichel Jun 19, 2023
6d3bfb3
clippy
tgmichel Jun 19, 2023
ebd1486
suggestion remove `evm-with-weight-limit` feature
tgmichel Jun 21, 2023
7198ca4
fmt
tgmichel Jun 21, 2023
d95b7ed
update comment
tgmichel Jun 21, 2023
d9f12de
update tests for additional `AccountBasicRead` in the evm
tgmichel Jun 21, 2023
074711b
update evm pin
tgmichel Jun 21, 2023
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 frame/ethereum/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,4 @@ try-runtime = [
"pallet-evm/try-runtime",
]
forbid-evm-reentrancy = ["pallet-evm/forbid-evm-reentrancy"]
evm-with-weight-limit = ["evm/with-substrate"]
77 changes: 66 additions & 11 deletions frame/ethereum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,28 @@ where
len: usize,
) -> Option<TransactionValidity> {
if let Call::transact { transaction } = self {
// TODO Weight v2 type 16 bytes? check scale compact stuff
let encoded_len = transaction.encode().len() + 16usize;
tgmichel marked this conversation as resolved.
Show resolved Hide resolved

// Validate submitted gas limit to weight conversion
let gas_limit = match transaction {
Transaction::Legacy(t) => t.gas_limit,
Transaction::EIP2930(t) => t.gas_limit,
Transaction::EIP1559(t) => t.gas_limit,
};
let without_base_extrinsic_weight = true;
let submitted_weight = T::GasWeightMapping::gas_to_weight(
tgmichel marked this conversation as resolved.
Show resolved Hide resolved
gas_limit.unique_saturated_into(),
without_base_extrinsic_weight,
Some(encoded_len)
);
if submitted_weight != dispatch_info.weight {
tgmichel marked this conversation as resolved.
Show resolved Hide resolved
return Some(Err(
TransactionValidityError::Invalid(InvalidTransaction::Custom(255))
));
}

// CheckWeight
if let Err(e) = CheckWeight::<T>::do_validate(dispatch_info, len) {
return Some(Err(e));
}
Expand Down Expand Up @@ -240,7 +262,7 @@ pub mod pallet {
Self::validate_transaction_in_block(source, &transaction).expect(
"pre-block transaction verification failed; the block cannot be built",
);
let r = Self::apply_validated_transaction(source, transaction)
let r = Self::apply_validated_transaction(source, transaction, None)
.expect("pre-block apply transaction failed; the block cannot be built");

weight = weight.saturating_add(r.actual_weight.unwrap_or_default());
Expand Down Expand Up @@ -276,7 +298,7 @@ pub mod pallet {
<T as pallet_evm::Config>::GasWeightMapping::gas_to_weight({
let transaction_data: TransactionData = transaction.into();
transaction_data.gas_limit.unique_saturated_into()
}, without_base_extrinsic_weight)
}, without_base_extrinsic_weight, None)
})]
pub fn transact(
origin: OriginFor<T>,
Expand All @@ -289,7 +311,25 @@ pub mod pallet {
"pre log already exists; block is invalid",
);

Self::apply_validated_transaction(source, transaction)
Self::apply_validated_transaction(source, transaction, None)
}

/// Transact an Ethereum transaction with native WeightV2+ limit.
#[pallet::call_index(1)]
#[pallet::weight({ *weight_limit })]
pub fn transact_with_weight_limit(
origin: OriginFor<T>,
transaction: Transaction,
weight_limit: Weight,
) -> DispatchResultWithPostInfo {
let source = ensure_ethereum_transaction(origin)?;
// Disable transact functionality if PreLog exist.
assert!(
fp_consensus::find_pre_log(&frame_system::Pallet::<T>::digest()).is_err(),
"pre log already exists; block is invalid",
);

Self::apply_validated_transaction(source, transaction, Some(weight_limit))
}
}

Expand Down Expand Up @@ -538,14 +578,15 @@ impl<T: Config> Pallet<T> {
fn apply_validated_transaction(
source: H160,
transaction: Transaction,
weight_limit: Option<Weight>,
) -> DispatchResultWithPostInfo {
let (to, _, info) = Self::execute(source, &transaction, None)?;
let (to, _, info) = Self::execute(source, &transaction, None, weight_limit)?;

let pending = Pending::<T>::get();
let transaction_hash = transaction.hash();
let transaction_index = pending.len() as u32;

let (reason, status, used_gas, dest) = match info {
let (reason, status, weight_info, used_gas, dest) = match info {
CallOrCreateInfo::Call(info) => (
info.exit_reason,
TransactionStatus {
Expand All @@ -561,6 +602,7 @@ impl<T: Config> Pallet<T> {
bloom
},
},
info.weight_info,
info.used_gas,
to,
),
Expand All @@ -579,6 +621,7 @@ impl<T: Config> Pallet<T> {
bloom
},
},
info.weight_info,
info.used_gas,
Some(info.value),
),
Expand Down Expand Up @@ -632,10 +675,19 @@ impl<T: Config> Pallet<T> {
});

Ok(PostDispatchInfo {
actual_weight: Some(T::GasWeightMapping::gas_to_weight(
used_gas.unique_saturated_into(),
true,
)),
actual_weight: {
let mut gas_to_weight = T::GasWeightMapping::gas_to_weight(
used_gas.unique_saturated_into(),
true,
None,
);
if let Some(weight_info) = weight_info {
if let Some(proof_size_usage) = weight_info.proof_size_usage {
*gas_to_weight.proof_size_mut() = proof_size_usage;
tgmichel marked this conversation as resolved.
Show resolved Hide resolved
}
}
Some(gas_to_weight)
},
pays_fee: Pays::No,
})
}
Expand All @@ -650,6 +702,7 @@ impl<T: Config> Pallet<T> {
from: H160,
transaction: &Transaction,
config: Option<evm::Config>,
weight_limit: Option<Weight>,
) -> Result<
(Option<H160>, Option<H160>, CallOrCreateInfo),
DispatchErrorWithPostInfo<PostDispatchInfo>,
Expand Down Expand Up @@ -730,6 +783,7 @@ impl<T: Config> Pallet<T> {
access_list,
is_transactional,
validate,
weight_limit,
config.as_ref().unwrap_or_else(|| T::config()),
) {
Ok(res) => res,
Expand Down Expand Up @@ -758,6 +812,7 @@ impl<T: Config> Pallet<T> {
access_list,
is_transactional,
validate,
weight_limit,
config.as_ref().unwrap_or_else(|| T::config()),
) {
Ok(res) => res,
Expand Down Expand Up @@ -876,8 +931,8 @@ impl<T: Config> Pallet<T> {

pub struct ValidatedTransaction<T>(PhantomData<T>);
impl<T: Config> ValidatedTransactionT for ValidatedTransaction<T> {
fn apply(source: H160, transaction: Transaction) -> DispatchResultWithPostInfo {
Pallet::<T>::apply_validated_transaction(source, transaction)
fn apply(source: H160, transaction: Transaction, weight_limit: Option<Weight>) -> DispatchResultWithPostInfo {
Pallet::<T>::apply_validated_transaction(source, transaction, weight_limit)
}
}

Expand Down
3 changes: 3 additions & 0 deletions frame/evm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ 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"] }
hex-literal = { version = "0.3.4" }
impl-trait-for-tuples = "0.2.2"
log = { workspace = true }
rlp = { workspace = true }
scale-codec = { package = "parity-scale-codec", workspace = true }
scale-info = { workspace = true }
sha3 = { version = "0.10", default-features = false }
# Substrate
frame-benchmarking = { workspace = true, optional = true }
frame-support = { workspace = true }
Expand Down Expand Up @@ -74,3 +76,4 @@ try-runtime = [
"pallet-timestamp/try-runtime",
]
forbid-evm-reentrancy = ["dep:environmental"]
evm-with-weight-limit = ["evm/with-substrate"]
1 change: 1 addition & 0 deletions frame/evm/precompile/blake2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ std = [
# Frontier
"fp-evm/std",
]
evm-with-weight-limit = ["pallet-evm-test-vector-support/evm-with-weight-limit"]
1 change: 1 addition & 0 deletions frame/evm/precompile/bn128/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ std = [
# Frontier
"fp-evm/std",
]
evm-with-weight-limit = ["pallet-evm-test-vector-support/evm-with-weight-limit"]
18 changes: 13 additions & 5 deletions frame/evm/precompile/dispatch/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ where

if let Some(gas) = target_gas {
let valid_weight =
info.weight.ref_time() <= T::GasWeightMapping::gas_to_weight(gas, false).ref_time();
info.weight.ref_time() <= T::GasWeightMapping::gas_to_weight(gas, false, None).ref_time();
if !valid_weight {
return Err(PrecompileFailure::Error {
exit_status: ExitError::OutOfGas,
Expand All @@ -83,13 +83,21 @@ where

let origin = T::AddressMapping::into_account_id(context.caller);

#[cfg(feature = "evm-with-weight-limit")]
handle.record_external_cost(
Some(info.weight.ref_time()),
Some(info.weight.proof_size()),
)?;
match call.dispatch(Some(origin).into()) {
Ok(post_info) => {
let cost = T::GasWeightMapping::weight_to_gas(
post_info.actual_weight.unwrap_or(info.weight),
);

let actual_weight = post_info.actual_weight.unwrap_or(info.weight);
let cost = T::GasWeightMapping::weight_to_gas(actual_weight);
handle.record_cost(cost)?;
#[cfg(feature = "evm-with-weight-limit")]
handle.refund_external_cost(
Some(info.weight.ref_time().saturating_sub(actual_weight.ref_time())),
Some(info.weight.proof_size().saturating_sub(actual_weight.proof_size())),
);

Ok(PrecompileOutput {
exit_status: ExitSucceed::Stopped,
Expand Down
7 changes: 7 additions & 0 deletions frame/evm/precompile/dispatch/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ pub(crate) struct MockHandle {
}

impl PrecompileHandle for MockHandle {

type ExternalCost = ();

fn call(
&mut self,
_: H160,
Expand All @@ -183,6 +186,10 @@ impl PrecompileHandle for MockHandle {
Ok(())
}

fn record_external_cost(&mut self, _: Self::ExternalCost) -> Result<(), ExitError> {
Ok(())
}

fn remaining_gas(&self) -> u64 {
unimplemented!()
}
Expand Down
1 change: 1 addition & 0 deletions frame/evm/precompile/modexp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ std = [
# Frontier
"fp-evm/std",
]
evm-with-weight-limit = ["pallet-evm-test-vector-support/evm-with-weight-limit"]
1 change: 1 addition & 0 deletions frame/evm/precompile/simple/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ std = [
# Frontier
"fp-evm/std",
]
evm-with-weight-limit = ["pallet-evm-test-vector-support/evm-with-weight-limit"]
Loading