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

fix(mempool)!: check gas limit range #1634

Merged
merged 2 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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
Loading