From d90052f28146ba68d2b2f237fbdb51669358b8e1 Mon Sep 17 00:00:00 2001 From: shaorongqiang <26101255+shaorongqiang@users.noreply.github.com> Date: Mon, 29 Apr 2024 14:44:57 +0800 Subject: [PATCH] fix check tx size (#1053) * fix check tx size * fix checkpoint --------- Co-authored-by: shaorongqiang --- .../abciapp/src/abci/server/callback/mod.rs | 36 +++++++++++++------ .../abciapp/src/abci/server/tx_sender.rs | 5 ++- src/components/config/src/abci/mod.rs | 21 +++++++---- 3 files changed, 43 insertions(+), 19 deletions(-) diff --git a/src/components/abciapp/src/abci/server/callback/mod.rs b/src/components/abciapp/src/abci/server/callback/mod.rs index 0945cdfc8..a7b554044 100644 --- a/src/components/abciapp/src/abci/server/callback/mod.rs +++ b/src/components/abciapp/src/abci/server/callback/mod.rs @@ -2,14 +2,14 @@ //! # Impl function of tendermint ABCI //! -use chrono::Local; -use fp_storage::BorrowMut; - mod utils; use { crate::{ - abci::{server::ABCISubmissionServer, staking, IN_SAFE_ITV, IS_EXITING, POOL}, + abci::{ + server::{tx_sender::TX_SIZE, ABCISubmissionServer}, + staking, IN_SAFE_ITV, IS_EXITING, POOL, + }, api::{ query_server::BLOCK_CREATED, submission_server::{convert_tx, try_tx_catalog, TxCatalog}, @@ -21,12 +21,14 @@ use { ResponseBeginBlock, ResponseCheckTx, ResponseCommit, ResponseDeliverTx, ResponseEndBlock, ResponseInfo, ResponseInitChain, ResponseQuery, }, + chrono::Local, config::abci::global_cfg::CFG, enterprise_web3::{ Setter, ALLOWANCES, BALANCE_MAP, BLOCK, CODE_MAP, NONCE_MAP, RECEIPTS, REDIS_CLIENT, STATE_UPDATE_LIST, TOTAL_ISSUANCE, TXS, WEB3_SERVICE_START_HEIGHT, }, fp_storage::hash::{Sha256, StorageHasher}, + fp_storage::BorrowMut, globutils::wallet, lazy_static::lazy_static, ledger::{ @@ -133,15 +135,21 @@ pub fn init_chain( /// any new tx will trigger this callback before it can enter the mem-pool of tendermint pub fn check_tx(s: &mut ABCISubmissionServer, req: &RequestCheckTx) -> ResponseCheckTx { let mut resp = ResponseCheckTx::new(); + let td_height = TENDERMINT_BLOCK_HEIGHT.load(Ordering::Relaxed); - let tx_catalog = try_tx_catalog(req.get_tx(), false); + let tx = req.get_tx(); + if td_height > CFG.checkpoint.check_tx_size_height && tx.len() > TX_SIZE { + resp.log = format!("Transaction too large:{}", tx.len()); + resp.code = 1; + return resp; + } - let td_height = TENDERMINT_BLOCK_HEIGHT.load(Ordering::Relaxed); + let tx_catalog = try_tx_catalog(tx, false); match tx_catalog { TxCatalog::FindoraTx => { if matches!(req.field_type, CheckTxType::New) { - if let Ok(tx) = convert_tx(req.get_tx()) { + if let Ok(tx) = convert_tx(tx) { if td_height > CFG.checkpoint.check_signatures_num { for op in tx.body.operations.iter() { if let Operation::TransferAsset(op) = op { @@ -271,13 +279,21 @@ pub fn deliver_tx( req: &RequestDeliverTx, ) -> ResponseDeliverTx { let mut resp = ResponseDeliverTx::new(); - - let tx_catalog = try_tx_catalog(req.get_tx(), true); let td_height = TENDERMINT_BLOCK_HEIGHT.load(Ordering::Relaxed); + let tx = req.get_tx(); + + if td_height > CFG.checkpoint.check_tx_size_height && tx.len() > TX_SIZE { + resp.log = format!("Transaction too large:{}", tx.len()); + resp.code = 1; + return resp; + } + + let tx_catalog = try_tx_catalog(tx, true); + match tx_catalog { TxCatalog::FindoraTx => { - if let Ok(tx) = convert_tx(req.get_tx()) { + if let Ok(tx) = convert_tx(tx) { if td_height > CFG.checkpoint.check_signatures_num { for op in tx.body.operations.iter() { if let Operation::TransferAsset(op) = op { diff --git a/src/components/abciapp/src/abci/server/tx_sender.rs b/src/components/abciapp/src/abci/server/tx_sender.rs index 4cf747be6..c3051599f 100644 --- a/src/components/abciapp/src/abci/server/tx_sender.rs +++ b/src/components/abciapp/src/abci/server/tx_sender.rs @@ -4,14 +4,13 @@ use { crate::{abci::POOL, api::submission_server::TxnForward}, - config::abci::global_cfg::CFG, ledger::data_model::Transaction, ruc::*, std::sync::atomic::{AtomicU16, Ordering}, }; static TX_PENDING_CNT: AtomicU16 = AtomicU16::new(0); - +pub static TX_SIZE: usize = 8192; pub struct TendermintForward { pub tendermint_reply: String, } @@ -38,7 +37,7 @@ pub fn forward_txn_with_mode( let txn_json = serde_json::to_string(&txn).c(d!())?; let txn_b64 = base64::encode_config(txn_json, base64::URL_SAFE); - if txn_b64.len() > CFG.checkpoint.tx_size as usize { + if txn_b64.len() > TX_SIZE { return Err(eg!("Transaction too large")); } diff --git a/src/components/config/src/abci/mod.rs b/src/components/config/src/abci/mod.rs index 70ad782b4..10e9b9e0c 100644 --- a/src/components/config/src/abci/mod.rs +++ b/src/components/config/src/abci/mod.rs @@ -151,10 +151,11 @@ pub struct CheckPointConfig { #[serde(default = "def_evm_staking_address")] pub evm_staking_address: String, - #[serde(default = "def_evm_staking_inital_height")] + #[serde(default = "def_utxo_fee_height")] pub utxo_fee_height: i64, - pub tx_size: u32, + #[serde(default = "def_check_tx_size_height")] + pub check_tx_size_height: i64, } fn def_fix_check_replay() -> u64 { @@ -232,6 +233,14 @@ fn def_evm_staking_address() -> String { DEFAULT_CHECKPOINT_CONFIG.evm_staking_address.clone() } +fn def_utxo_fee_height() -> i64 { + DEFAULT_CHECKPOINT_CONFIG.utxo_fee_height +} + +fn def_check_tx_size_height() -> i64 { + DEFAULT_CHECKPOINT_CONFIG.check_tx_size_height +} + #[cfg(feature = "debug_env")] lazy_static! { static ref DEFAULT_CHECKPOINT_CONFIG: CheckPointConfig = CheckPointConfig { @@ -276,8 +285,8 @@ lazy_static! { max_gas_price_limit: 0, evm_staking_inital_height: 128, evm_staking_address: "0x321DF28026D01858906D322533900aD3435eE964".to_owned(), - utxo_fee_height: 128, - tx_size: 8192 + utxo_fee_height: 0, + check_tx_size_height: 0 }; } @@ -612,8 +621,8 @@ lazy_static! { max_gas_price_limit: 4636000, evm_staking_inital_height: 4636000, evm_staking_address: "0x38d49e3bd5144059c9f3bA10CF7306E84155B603".to_owned(), - utxo_fee_height: 50000000, - tx_size: 8192 + utxo_fee_height: 5671678, + check_tx_size_height: 5671678 }; }