revm_handler/
validation.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
use context_interface::{
    journaled_state::JournaledState,
    result::{InvalidHeader, InvalidTransaction},
    transaction::{
        eip7702::Authorization, Eip1559CommonTxFields, Eip2930Tx, Eip4844Tx, Eip7702Tx, LegacyTx,
        Transaction, TransactionType,
    },
    Block, BlockGetter, Cfg, CfgGetter, JournalStateGetter, JournalStateGetterDBError,
    TransactionGetter,
};
use core::cmp::{self, Ordering};
use handler_interface::ValidationHandler;
use interpreter::gas;
use primitives::{B256, U256};
use specification::{eip4844, hardfork::SpecId};
use state::Account;
use std::boxed::Box;

pub struct EthValidation<CTX, ERROR> {
    pub _phantom: core::marker::PhantomData<fn() -> (CTX, ERROR)>,
}

impl<CTX, ERROR> Default for EthValidation<CTX, ERROR> {
    fn default() -> Self {
        Self {
            _phantom: core::marker::PhantomData,
        }
    }
}

impl<CTX, ERROR> EthValidation<CTX, ERROR> {
    pub fn new() -> Self {
        Self {
            _phantom: core::marker::PhantomData,
        }
    }

    pub fn new_boxed() -> Box<Self> {
        Box::new(Self::new())
    }
}

impl<CTX, ERROR> ValidationHandler for EthValidation<CTX, ERROR>
where
    CTX: EthValidationContext,
    ERROR: From<InvalidTransaction> + From<InvalidHeader> + From<JournalStateGetterDBError<CTX>>,
{
    type Context = CTX;
    type Error = ERROR;

    fn validate_env(&self, context: &Self::Context) -> Result<(), Self::Error> {
        let spec = context.cfg().spec().into();
        // `prevrandao` is required for the merge
        if spec.is_enabled_in(SpecId::MERGE) && context.block().prevrandao().is_none() {
            return Err(InvalidHeader::PrevrandaoNotSet.into());
        }
        // `excess_blob_gas` is required for Cancun
        if spec.is_enabled_in(SpecId::CANCUN)
            && context.block().blob_excess_gas_and_price().is_none()
        {
            return Err(InvalidHeader::ExcessBlobGasNotSet.into());
        }
        validate_tx_env::<&Self::Context, InvalidTransaction>(context, spec).map_err(Into::into)
    }

    fn validate_tx_against_state(&self, context: &mut Self::Context) -> Result<(), Self::Error> {
        let tx_caller = context.tx().common_fields().caller();

        // load acc
        let account = &mut context.journal().load_account_code(tx_caller)?;
        let account = account.data.clone();

        validate_tx_against_account::<CTX, ERROR>(&account, context)
    }

    fn validate_initial_tx_gas(&self, context: &Self::Context) -> Result<u64, Self::Error> {
        let spec = context.cfg().spec().into();
        validate_initial_tx_gas::<&Self::Context, InvalidTransaction>(context, spec)
            .map_err(Into::into)
    }
}

/// Validate transaction that has EIP-1559 priority fee
pub fn validate_priority_fee_tx(
    max_fee: u128,
    max_priority_fee: u128,
    base_fee: Option<U256>,
) -> Result<(), InvalidTransaction> {
    if max_priority_fee > max_fee {
        // or gas_max_fee for eip1559
        return Err(InvalidTransaction::PriorityFeeGreaterThanMaxFee);
    }

    // check minimal cost against basefee
    if let Some(base_fee) = base_fee {
        let effective_gas_price = cmp::min(
            U256::from(max_fee),
            base_fee.saturating_add(U256::from(max_priority_fee)),
        );
        if effective_gas_price < base_fee {
            return Err(InvalidTransaction::GasPriceLessThanBasefee);
        }
    }

    Ok(())
}

/// Validate EIP-4844 transaction.
pub fn validate_eip4844_tx(
    blobs: &[B256],
    max_blob_fee: u128,
    block_blob_gas_price: u128,
) -> Result<(), InvalidTransaction> {
    // ensure that the user was willing to at least pay the current blob gasprice
    if block_blob_gas_price > max_blob_fee {
        return Err(InvalidTransaction::BlobGasPriceGreaterThanMax);
    }

    // there must be at least one blob
    if blobs.is_empty() {
        return Err(InvalidTransaction::EmptyBlobs);
    }

    // all versioned blob hashes must start with VERSIONED_HASH_VERSION_KZG
    for blob in blobs {
        if blob[0] != eip4844::VERSIONED_HASH_VERSION_KZG {
            return Err(InvalidTransaction::BlobVersionNotSupported);
        }
    }

    // ensure the total blob gas spent is at most equal to the limit
    // assert blob_gas_used <= MAX_BLOB_GAS_PER_BLOCK
    if blobs.len() > eip4844::MAX_BLOB_NUMBER_PER_BLOCK as usize {
        return Err(InvalidTransaction::TooManyBlobs {
            have: blobs.len(),
            max: eip4844::MAX_BLOB_NUMBER_PER_BLOCK as usize,
        });
    }
    Ok(())
}

/// Validate transaction against block and configuration for mainnet.
pub fn validate_tx_env<CTX: TransactionGetter + BlockGetter + CfgGetter, Error>(
    context: CTX,
    spec_id: SpecId,
) -> Result<(), Error>
where
    Error: From<InvalidTransaction>,
{
    // Check if the transaction's chain id is correct
    let common_field = context.tx().common_fields();
    let tx_type = context.tx().tx_type().into();

    let base_fee = if context.cfg().is_base_fee_check_disabled() {
        None
    } else {
        Some(*context.block().basefee())
    };

    match tx_type {
        TransactionType::Legacy => {
            let tx = context.tx().legacy();
            // check chain_id only if it is present in the legacy transaction.
            // EIP-155: Simple replay attack protection
            if let Some(chain_id) = tx.chain_id() {
                if chain_id != context.cfg().chain_id() {
                    return Err(InvalidTransaction::InvalidChainId.into());
                }
            }
            // gas price must be at least the basefee.
            if let Some(base_fee) = base_fee {
                if U256::from(tx.gas_price()) < base_fee {
                    return Err(InvalidTransaction::GasPriceLessThanBasefee.into());
                }
            }
        }
        TransactionType::Eip2930 => {
            // enabled in BERLIN hardfork
            if !spec_id.is_enabled_in(SpecId::BERLIN) {
                return Err(InvalidTransaction::Eip2930NotSupported.into());
            }
            let tx = context.tx().eip2930();

            if context.cfg().chain_id() != tx.chain_id() {
                return Err(InvalidTransaction::InvalidChainId.into());
            }

            // gas price must be at least the basefee.
            if let Some(base_fee) = base_fee {
                if U256::from(tx.gas_price()) < base_fee {
                    return Err(InvalidTransaction::GasPriceLessThanBasefee.into());
                }
            }
        }
        TransactionType::Eip1559 => {
            if !spec_id.is_enabled_in(SpecId::LONDON) {
                return Err(InvalidTransaction::Eip1559NotSupported.into());
            }
            let tx = context.tx().eip1559();

            if context.cfg().chain_id() != tx.chain_id() {
                return Err(InvalidTransaction::InvalidChainId.into());
            }

            validate_priority_fee_tx(
                tx.max_fee_per_gas(),
                tx.max_priority_fee_per_gas(),
                base_fee,
            )?;
        }
        TransactionType::Eip4844 => {
            if !spec_id.is_enabled_in(SpecId::CANCUN) {
                return Err(InvalidTransaction::Eip4844NotSupported.into());
            }
            let tx = context.tx().eip4844();

            if context.cfg().chain_id() != tx.chain_id() {
                return Err(InvalidTransaction::InvalidChainId.into());
            }

            validate_priority_fee_tx(
                tx.max_fee_per_gas(),
                tx.max_priority_fee_per_gas(),
                base_fee,
            )?;

            validate_eip4844_tx(
                tx.blob_versioned_hashes(),
                tx.max_fee_per_blob_gas(),
                context.block().blob_gasprice().unwrap_or_default(),
            )?;
        }
        TransactionType::Eip7702 => {
            // check if EIP-7702 transaction is enabled.
            if !spec_id.is_enabled_in(SpecId::PRAGUE) {
                return Err(InvalidTransaction::Eip7702NotSupported.into());
            }
            let tx = context.tx().eip7702();

            if context.cfg().chain_id() != tx.chain_id() {
                return Err(InvalidTransaction::InvalidChainId.into());
            }

            validate_priority_fee_tx(
                tx.max_fee_per_gas(),
                tx.max_priority_fee_per_gas(),
                base_fee,
            )?;

            let auth_list_len = tx.authorization_list_len();
            // The transaction is considered invalid if the length of authorization_list is zero.
            if auth_list_len == 0 {
                return Err(InvalidTransaction::EmptyAuthorizationList.into());
            }

            // TODO temporary here as newest EIP have removed this check.
            for auth in tx.authorization_list_iter() {
                if auth.is_invalid() {
                    return Err(InvalidTransaction::Eip7702NotSupported.into());
                }
            }
        }
        TransactionType::Custom => {
            // custom transaction type check is not done here.
        }
    };

    // Check if gas_limit is more than block_gas_limit
    if !context.cfg().is_block_gas_limit_disabled()
        && U256::from(common_field.gas_limit()) > *context.block().gas_limit()
    {
        return Err(InvalidTransaction::CallerGasLimitMoreThanBlock.into());
    }

    // EIP-3860: Limit and meter initcode
    if spec_id.is_enabled_in(SpecId::SHANGHAI) && context.tx().kind().is_create() {
        let max_initcode_size = context.cfg().max_code_size().saturating_mul(2);
        if context.tx().common_fields().input().len() > max_initcode_size {
            return Err(InvalidTransaction::CreateInitCodeSizeLimit.into());
        }
    }

    Ok(())
}

/// Validate account against the transaction.
#[inline]
pub fn validate_tx_against_account<CTX: TransactionGetter + CfgGetter, ERROR>(
    account: &Account,
    context: &CTX,
) -> Result<(), ERROR>
where
    ERROR: From<InvalidTransaction>,
{
    let tx_type = context.tx().tx_type().into();
    // EIP-3607: Reject transactions from senders with deployed code
    // This EIP is introduced after london but there was no collision in past
    // so we can leave it enabled always
    if !context.cfg().is_eip3607_disabled() {
        let bytecode = &account.info.code.as_ref().unwrap();
        // allow EOAs whose code is a valid delegation designation,
        // i.e. 0xef0100 || address, to continue to originate transactions.
        if !bytecode.is_empty() && !bytecode.is_eip7702() {
            return Err(InvalidTransaction::RejectCallerWithCode.into());
        }
    }

    // Check that the transaction's nonce is correct
    if !context.cfg().is_nonce_check_disabled() {
        let tx = context.tx().common_fields().nonce();
        let state = account.info.nonce;
        match tx.cmp(&state) {
            Ordering::Greater => {
                return Err(InvalidTransaction::NonceTooHigh { tx, state }.into());
            }
            Ordering::Less => {
                return Err(InvalidTransaction::NonceTooLow { tx, state }.into());
            }
            _ => {}
        }
    }

    // gas_limit * max_fee + value
    let mut balance_check = U256::from(context.tx().common_fields().gas_limit())
        .checked_mul(U256::from(context.tx().max_fee()))
        .and_then(|gas_cost| gas_cost.checked_add(context.tx().common_fields().value()))
        .ok_or(InvalidTransaction::OverflowPaymentInTransaction)?;

    if tx_type == TransactionType::Eip4844 {
        let tx = context.tx().eip4844();
        let data_fee = tx.calc_max_data_fee();
        balance_check = balance_check
            .checked_add(data_fee)
            .ok_or(InvalidTransaction::OverflowPaymentInTransaction)?;
    }

    // Check if account has enough balance for `gas_limit * max_fee`` and value transfer.
    // Transfer will be done inside `*_inner` functions.
    if balance_check > account.info.balance && !context.cfg().is_balance_check_disabled() {
        return Err(InvalidTransaction::LackOfFundForMaxFee {
            fee: Box::new(balance_check),
            balance: Box::new(account.info.balance),
        }
        .into());
    }

    Ok(())
}

/// Validate initial transaction gas.
pub fn validate_initial_tx_gas<TxGetter: TransactionGetter, Error>(
    env: TxGetter,
    spec_id: SpecId,
) -> Result<u64, Error>
where
    Error: From<InvalidTransaction>,
{
    let tx_type = env.tx().tx_type().into();

    let authorization_list_num = if tx_type == TransactionType::Eip7702 {
        env.tx().eip7702().authorization_list_len() as u64
    } else {
        0
    };

    let common_fields = env.tx().common_fields();
    let is_create = env.tx().kind().is_create();
    let input = common_fields.input();
    let access_list = env.tx().access_list();

    let initial_gas_spend = gas::validate_initial_tx_gas(
        spec_id,
        input,
        is_create,
        access_list,
        authorization_list_num,
    );

    // Additional check to see if limit is big enough to cover initial gas.
    if initial_gas_spend > common_fields.gas_limit() {
        return Err(InvalidTransaction::CallGasCostMoreThanGasLimit.into());
    }
    Ok(initial_gas_spend)
}

/// Helper trait that summarizes ValidationHandler requirements from Context.
pub trait EthValidationContext:
    TransactionGetter + BlockGetter + JournalStateGetter + CfgGetter
{
}

impl<T: TransactionGetter + BlockGetter + JournalStateGetter + CfgGetter> EthValidationContext
    for T
{
}

/// Helper trait that summarizes all possible requirements by EthValidation.
pub trait EthValidationError<CTX: JournalStateGetter>:
    From<InvalidTransaction> + From<InvalidHeader> + From<JournalStateGetterDBError<CTX>>
{
}

impl<
        CTX: JournalStateGetter,
        T: From<InvalidTransaction> + From<InvalidHeader> + From<JournalStateGetterDBError<CTX>>,
    > EthValidationError<CTX> for T
{
}