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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
// Copyright 2015-2020 Parity Technologies (UK) Ltd.
// This file is part of OpenEthereum.

// OpenEthereum is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// OpenEthereum is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with OpenEthereum.  If not, see <http://www.gnu.org/licenses/>.

//! Transaction Verifier
//!
//! Responsible for verifying a transaction before importing to the pool.
//! Should make sure that the transaction is structuraly valid.
//!
//! May have some overlap with `Readiness` since we don't want to keep around
//! stalled transactions.

use std::{
    cmp,
    sync::{
        atomic::{self, AtomicUsize},
        Arc,
    },
};

use ethereum_types::{H256, U256};
use hash::KECCAK_EMPTY;
use txpool;
use types::transaction;

use super::{
    client::{Client, TransactionType},
    VerifiedTransaction,
};

/// Verification options.
#[derive(Debug, Clone, PartialEq)]
pub struct Options {
    /// Minimal allowed gas price (actually minimal block producer reward = effective_priority_fee).
    pub minimal_gas_price: U256,
    /// Current block gas limit.
    pub block_gas_limit: U256,
    /// Block base fee. Exists if the EIP 1559 is activated.
    pub block_base_fee: Option<U256>,
    /// Maximal gas limit for a single transaction.
    pub tx_gas_limit: U256,
    /// Skip checks for early rejection, to make sure that local transactions are always imported.
    pub no_early_reject: bool,
    /// Accept transactions from non EOAs (see EIP-3607)
    pub allow_non_eoa_sender: bool,
}

#[cfg(test)]
impl Default for Options {
    fn default() -> Self {
        Options {
            minimal_gas_price: 0.into(),
            block_gas_limit: U256::max_value(),
            block_base_fee: None,
            tx_gas_limit: U256::max_value(),
            no_early_reject: false,
            allow_non_eoa_sender: false,
        }
    }
}

/// Transaction to verify.
#[cfg_attr(test, derive(Clone))]
pub enum Transaction {
    /// Fresh, never verified transaction.
    ///
    /// We need to do full verification of such transactions
    Unverified(transaction::UnverifiedTransaction),

    /// Transaction from retracted block.
    ///
    /// We could skip some parts of verification of such transactions
    Retracted(transaction::UnverifiedTransaction),

    /// Locally signed or retracted transaction.
    ///
    /// We can skip consistency verifications and just verify readiness.
    Local(transaction::PendingTransaction),
}

impl Transaction {
    /// Return transaction hash
    pub fn hash(&self) -> H256 {
        match *self {
            Transaction::Unverified(ref tx) => tx.hash(),
            Transaction::Retracted(ref tx) => tx.hash(),
            Transaction::Local(ref tx) => tx.hash(),
        }
    }

    /// Return transaction gas price for non 1559 transactions or maxFeePerGas for 1559 transactions.
    pub fn gas_price(&self) -> &U256 {
        match *self {
            Transaction::Unverified(ref tx) => &tx.tx().gas_price,
            Transaction::Retracted(ref tx) => &tx.tx().gas_price,
            Transaction::Local(ref tx) => &tx.tx().gas_price,
        }
    }

    fn gas(&self) -> &U256 {
        match *self {
            Transaction::Unverified(ref tx) => &tx.tx().gas,
            Transaction::Retracted(ref tx) => &tx.tx().gas,
            Transaction::Local(ref tx) => &tx.tx().gas,
        }
    }

    /// Return actual gas price of the transaction depending on the current block base fee
    pub fn effective_gas_price(&self, block_base_fee: Option<U256>) -> U256 {
        match *self {
            Transaction::Unverified(ref tx) => tx.effective_gas_price(block_base_fee),
            Transaction::Retracted(ref tx) => tx.effective_gas_price(block_base_fee),
            Transaction::Local(ref tx) => tx.effective_gas_price(block_base_fee),
        }
    }

    /// Return effective fee - part of the transaction fee that goes to the miner
    pub fn effective_priority_fee(&self, block_base_fee: Option<U256>) -> U256 {
        match *self {
            Transaction::Unverified(ref tx) => tx.effective_priority_fee(block_base_fee),
            Transaction::Retracted(ref tx) => tx.effective_priority_fee(block_base_fee),
            Transaction::Local(ref tx) => tx.effective_priority_fee(block_base_fee),
        }
    }

    /// Return maximum transaction fee that may go to the miner:
    /// transaction gas price for non 1559 transactions or maxPriorityFeePerGas for 1559 transactions.
    pub fn max_priority_fee(&self) -> U256 {
        match *self {
            Transaction::Unverified(ref tx) => tx.max_priority_fee_per_gas(),
            Transaction::Retracted(ref tx) => tx.max_priority_fee_per_gas(),
            Transaction::Local(ref tx) => tx.max_priority_fee_per_gas(),
        }
    }

    /// Check if transaction has zero gas price
    pub fn has_zero_gas_price(&self) -> bool {
        match *self {
            Transaction::Unverified(ref tx) => tx.has_zero_gas_price(),
            Transaction::Retracted(ref tx) => tx.has_zero_gas_price(),
            Transaction::Local(ref tx) => tx.has_zero_gas_price(),
        }
    }

    fn transaction(&self) -> &transaction::TypedTransaction {
        match *self {
            Transaction::Unverified(ref tx) => &*tx,
            Transaction::Retracted(ref tx) => &*tx,
            Transaction::Local(ref tx) => &*tx,
        }
    }

    fn is_local(&self) -> bool {
        match *self {
            Transaction::Local(..) => true,
            _ => false,
        }
    }

    fn is_retracted(&self) -> bool {
        match *self {
            Transaction::Retracted(..) => true,
            _ => false,
        }
    }
}

/// Transaction verifier.
///
/// Verification can be run in parallel for all incoming transactions.
#[derive(Debug)]
pub struct Verifier<C, S, V> {
    client: C,
    options: Options,
    id: Arc<AtomicUsize>,
    transaction_to_replace: Option<(S, Arc<V>)>,
}

impl<C, S, V> Verifier<C, S, V> {
    /// Creates new transaction verfier with specified options.
    pub fn new(
        client: C,
        options: Options,
        id: Arc<AtomicUsize>,
        transaction_to_replace: Option<(S, Arc<V>)>,
    ) -> Self {
        Verifier {
            client,
            options,
            id,
            transaction_to_replace,
        }
    }
}

impl<C: Client> txpool::Verifier<Transaction>
    for Verifier<C, ::pool::scoring::NonceAndGasPrice, VerifiedTransaction>
{
    type Error = transaction::Error;
    type VerifiedTransaction = VerifiedTransaction;

    fn verify_transaction(
        &self,
        tx: Transaction,
    ) -> Result<Self::VerifiedTransaction, Self::Error> {
        // The checks here should be ordered by cost/complexity.
        // Cheap checks should be done as early as possible to discard unneeded transactions early.

        let hash = tx.hash();

        if self.client.transaction_already_included(&hash) {
            trace!(target: "txqueue", "[{:?}] Rejected tx already in the blockchain", hash);
            bail!(transaction::Error::AlreadyImported)
        }

        let gas_limit = cmp::min(self.options.tx_gas_limit, self.options.block_gas_limit);
        if tx.gas() > &gas_limit {
            debug!(
                target: "txqueue",
                "[{:?}] Rejected transaction above gas limit: {} > min({}, {})",
                hash,
                tx.gas(),
                self.options.block_gas_limit,
                self.options.tx_gas_limit,
            );
            bail!(transaction::Error::GasLimitExceeded {
                limit: gas_limit,
                got: *tx.gas(),
            });
        }

        let minimal_gas = self.client.required_gas(tx.transaction().tx());
        if tx.gas() < &minimal_gas {
            trace!(target: "txqueue",
                "[{:?}] Rejected transaction with insufficient gas: {} < {}",
                hash,
                tx.gas(),
                minimal_gas,
            );

            bail!(transaction::Error::InsufficientGas {
                minimal: minimal_gas,
                got: *tx.gas(),
            })
        }

        let is_own = tx.is_local();
        let has_zero_gas_price = tx.has_zero_gas_price();
        // Quick exit for non-service and non-local transactions
        //
        // We're checking if the transaction is below configured minimal gas price
        // or the effective minimal gas price in case the pool is full.

        if !has_zero_gas_price && !is_own {
            let max_priority_fee = tx.max_priority_fee();

            if max_priority_fee < self.options.minimal_gas_price {
                trace!(
                    target: "txqueue",
                    "[{:?}] Rejected tx below minimal gas price threshold: {} < {}",
                    hash,
                    max_priority_fee,
                    self.options.minimal_gas_price,
                );
                bail!(transaction::Error::InsufficientGasPrice {
                    minimal: self.options.minimal_gas_price,
                    got: max_priority_fee,
                });
            }

            if let Some((ref scoring, ref vtx)) = self.transaction_to_replace {
                if scoring.should_reject_early(vtx, &tx) {
                    trace!(
                        target: "txqueue",
                        "[{:?}] Rejected tx early, cause it doesn't have any chance to get to the pool: (gas price: {} < {})",
                        hash,
                        tx.effective_gas_price(self.options.block_base_fee),
                        vtx.transaction.effective_gas_price(self.options.block_base_fee),
                    );
                    return Err(transaction::Error::TooCheapToReplace {
                        prev: Some(
                            vtx.transaction
                                .effective_gas_price(self.options.block_base_fee),
                        ),
                        new: Some(tx.effective_gas_price(self.options.block_base_fee)),
                    });
                }
            }
        }

        // Some more heavy checks below.
        // Actually recover sender and verify that transaction
        let is_retracted = tx.is_retracted();
        let transaction = match tx {
            Transaction::Retracted(tx) | Transaction::Unverified(tx) => {
                match self.client.verify_transaction(tx) {
                    Ok(signed) => signed.into(),
                    Err(err) => {
                        debug!(target: "txqueue", "[{:?}] Rejected tx {:?}", hash, err);
                        bail!(err)
                    }
                }
            }
            Transaction::Local(tx) => match self.client.verify_transaction_basic(&**tx) {
                Ok(()) => tx,
                Err(err) => {
                    warn!(target: "txqueue", "[{:?}] Rejected local tx {:?}", hash, err);
                    return Err(err);
                }
            },
        };

        // Verify RLP payload
        if let Err(err) = self.client.decode_transaction(&transaction.encode()) {
            debug!(target: "txqueue", "[{:?}] Rejected transaction's rlp payload", err);
            bail!(err)
        }

        let sender = transaction.sender();
        let account_details = self.client.account_details(&sender);

        if !self.options.allow_non_eoa_sender {
            if let Some(code_hash) = account_details.code_hash {
                if code_hash != KECCAK_EMPTY {
                    debug!(
                        target: "txqueue",
                        "[{:?}] Rejected tx, sender is not an EOA: {}",
                        hash,
                        code_hash
                    );
                    bail!(transaction::Error::SenderIsNotEOA);
                }
            }
        }

        let max_priority_fee = transaction.max_priority_fee_per_gas();

        if max_priority_fee < self.options.minimal_gas_price {
            let transaction_type = self.client.transaction_type(&transaction);
            if let TransactionType::Service = transaction_type {
                debug!(target: "txqueue", "Service tx {:?} below minimal gas price accepted", hash);
            } else if is_own || account_details.is_local {
                info!(target: "own_tx", "Local tx {:?} below minimal gas price accepted", hash);
            } else {
                trace!(
                    target: "txqueue",
                    "[{:?}] Rejected tx below minimal gas price threshold: {} < {}",
                    hash,
                    max_priority_fee,
                    self.options.minimal_gas_price,
                );
                bail!(transaction::Error::InsufficientGasPrice {
                    minimal: self.options.minimal_gas_price,
                    got: max_priority_fee,
                });
            }
        }

        let gas_price = transaction.tx().gas_price;

        if gas_price < transaction.max_priority_fee_per_gas() {
            bail!(transaction::Error::InsufficientGasPrice {
                minimal: transaction.max_priority_fee_per_gas(),
                got: gas_price,
            });
        }

        let (full_gas_price, overflow_1) = gas_price.overflowing_mul(transaction.tx().gas);
        let (cost, overflow_2) = transaction.tx().value.overflowing_add(full_gas_price);
        if overflow_1 || overflow_2 {
            trace!(
                target: "txqueue",
                "[{:?}] Rejected tx, price overflow",
                hash
            );
            bail!(transaction::Error::InsufficientBalance {
                cost: U256::max_value(),
                balance: account_details.balance,
            });
        }
        if account_details.balance < cost {
            debug!(
                target: "txqueue",
                "[{:?}] Rejected tx with not enough balance: {} < {}",
                hash,
                account_details.balance,
                cost,
            );
            bail!(transaction::Error::InsufficientBalance {
                cost: cost,
                balance: account_details.balance,
            });
        }

        if transaction.tx().nonce < account_details.nonce {
            debug!(
                target: "txqueue",
                "[{:?}] Rejected tx with old nonce ({} < {})",
                hash,
                transaction.tx().nonce,
                account_details.nonce,
            );
            bail!(transaction::Error::Old);
        }

        let priority = match (is_own || account_details.is_local, is_retracted) {
            (true, _) => super::Priority::Local,
            (false, false) => super::Priority::Regular,
            (false, true) => super::Priority::Retracted,
        };
        Ok(VerifiedTransaction {
            transaction,
            priority,
            hash,
            sender,
            insertion_id: self.id.fetch_add(1, atomic::Ordering::AcqRel),
        })
    }
}