Skip to content

Commit

Permalink
fix(mempool)!: check gas limit range (#1634)
Browse files Browse the repository at this point in the history
* fix(mempool): check gas limit range

* fix typo
  • Loading branch information
Eason Gao authored Dec 13, 2023
1 parent b540ce5 commit 68a7b4a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 17 deletions.
35 changes: 25 additions & 10 deletions core/mempool/src/adapter/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub mod message;

use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::{collections::HashMap, error::Error, marker::PhantomData, sync::Arc, time::Duration};

use dashmap::DashMap;
Expand All @@ -11,6 +11,7 @@ use futures::{
use log::{debug, error};
use parking_lot::Mutex;

use protocol::constants::{MAX_GAS_LIMIT, MIN_TRANSACTION_GAS_LIMIT};
use protocol::traits::{
Context, Gossip, Interoperation, MemPoolAdapter, PeerTrust, Priority, ReadOnlyStorage, Rpc,
TrustFeedback,
Expand Down Expand Up @@ -122,7 +123,6 @@ pub struct DefaultMemPoolAdapter<C, N, S, DB, I> {
trie_db: Arc<DB>,

addr_nonce: DashMap<H160, (U64, U256)>,
gas_limit: AtomicU64,
max_tx_size: AtomicUsize,
chain_id: u64,

Expand All @@ -146,7 +146,6 @@ where
storage: Arc<S>,
trie_db: Arc<DB>,
chain_id: u64,
gas_limit: u64,
max_tx_size: usize,
broadcast_txs_size: usize,
broadcast_txs_interval: u64,
Expand All @@ -168,7 +167,6 @@ where
trie_db,

addr_nonce: DashMap::new(),
gas_limit: AtomicU64::new(gas_limit),
max_tx_size: AtomicUsize::new(max_tx_size),
chain_id,

Expand Down Expand Up @@ -253,7 +251,25 @@ where

fn verify_gas_limit(&self, ctx: Context, stx: &SignedTransaction) -> ProtocolResult<()> {
let gas_limit_tx = stx.transaction.unsigned.gas_limit();
if gas_limit_tx > &U64::from(self.gas_limit.load(Ordering::Acquire)) {
if gas_limit_tx < &(MIN_TRANSACTION_GAS_LIMIT.into()) {
if ctx.is_network_origin_txs() {
self.network.report(
ctx,
TrustFeedback::Bad(format!(
"Mempool under gas limit of tx {:#x}",
stx.transaction.hash
)),
);
}

return Err(MemPoolError::UnderGasLimit {
tx_hash: stx.transaction.hash,
gas_limit_tx: gas_limit_tx.low_u64(),
}
.into());
}

if gas_limit_tx > &(MAX_GAS_LIMIT.into()) {
if ctx.is_network_origin_txs() {
self.network.report(
ctx,
Expand All @@ -263,10 +279,10 @@ where
)),
);
}

return Err(MemPoolError::ExceedGasLimit {
tx_hash: stx.transaction.hash,
gas_limit_tx: gas_limit_tx.low_u64(),
gas_limit_config: self.gas_limit.load(Ordering::Acquire),
tx_hash: stx.transaction.hash,
gas_limit_tx: gas_limit_tx.low_u64(),
}
.into());
}
Expand Down Expand Up @@ -474,10 +490,9 @@ where
&self,
_context: Context,
_state_root: MerkleRoot,
cycles_limit: u64,
_cycles_limit: u64,
max_tx_size: u64,
) {
self.gas_limit.store(cycles_limit, Ordering::Release);
self.max_tx_size
.store(max_tx_size as usize, Ordering::Release);
self.addr_nonce.clear();
Expand Down
20 changes: 14 additions & 6 deletions core/mempool/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,15 +391,23 @@ pub enum MemPoolError {
},

#[display(
fmt = "Tx: {:?} exceeds cycle limit, tx: {}, config: {}",
fmt = "Tx: {:?} exceeds 30000000, tx gas limit {}",
tx_hash,
gas_limit_tx,
gas_limit_config
gas_limit_tx
)]
ExceedGasLimit {
tx_hash: Hash,
gas_limit_config: u64,
gas_limit_tx: u64,
tx_hash: Hash,
gas_limit_tx: u64,
},

#[display(
fmt = "Tx: {:?} gas price is less than 21000, tx gas limit {}",
tx_hash,
gas_limit_tx
)]
UnderGasLimit {
tx_hash: Hash,
gas_limit_tx: u64,
},

#[display(fmt = "Tx nonce {} is invalid current nonce {}", tx_nonce, current)]
Expand Down
1 change: 0 additions & 1 deletion core/run/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,6 @@ where
Arc::clone(storage),
Arc::clone(trie_db),
current_header.chain_id,
current_header.gas_limit.low_u64(),
config.pool_size as usize,
config.broadcast_txs_size,
config.broadcast_txs_interval,
Expand Down

0 comments on commit 68a7b4a

Please sign in to comment.