Просмотр исходного кода

validator/consensus: define max block gas limit based on actual upper limits

skoupidi 1 год назад
Родитель
Сommit
5c3e9d65a6

+ 11 - 15
bin/darkfid/src/tests/unproposed_txs.rs

@@ -19,20 +19,16 @@
 //! Test cases for unproprosed transactions.
 //!
 //! The following are supported test cases:
-//! - Verifying the processing of unproposed transactions that are within the unproposed transactions gas limit.
-//! - Verifying the processing of unproposed transactions that exceed the unproposed transactions gas limit.
+//! - Verifying the processing of unproposed transactions that are within the block transactions gas limit.
+//! - Verifying the processing of unproposed transactions that exceed the block transactions gas limit.
 //!
-//! The tests were written with a 'GAS_LIMIT_UNPROPOSED_TXS' set to `23_822_290 * 50`. The number `23_822_290` is derived
-//! from the average gas used per transaction, yielding an overall limit of 1_191_114_500 for the pool
-//! of unproposed transactions.
-//!
-//! Please update the test to reflect any changes to the unproposed transactions gas limit value.
+//! Please update the test to reflect any changes to the block transactions gas limit value.
 
 use darkfi::Result;
 use std::sync::Arc;
 
 use crate::tests::{Harness, HarnessConfig};
-use darkfi::validator::{consensus::GAS_LIMIT_UNPROPOSED_TXS, utils::best_fork_index};
+use darkfi::validator::{consensus::BLOCK_GAS_LIMIT, utils::best_fork_index};
 use darkfi_contract_test_harness::{init_logger, Holder, TestHarness};
 use darkfi_sdk::{crypto::BaseBlind, num_traits::One};
 use num_bigint::BigUint;
@@ -107,10 +103,10 @@ async fn simulate_unproposed_txs(
     Ok((tx.len() as u64, total_gas_used))
 }
 
-/// Tests the processing of unproposed transactions within `GAS_LIMIT_UNPROPOSED_TXS`.
+/// Tests the processing of unproposed transactions within `BLOCK_GAS_LIMIT`.
 ///
 /// Note: In this test scenario, the mempool is populated with 5 pending transactions that each use roughly 9_851_908 gas,
-/// falling within `GAS_LIMIT_UNPROPOSED_TXS`.
+/// falling within `BLOCK_GAS_LIMIT`.
 #[test]
 fn test_unproposed_txs_within_gas_limit() -> Result<()> {
     let ex = Arc::new(Executor::new());
@@ -141,14 +137,14 @@ fn test_unproposed_txs_within_gas_limit() -> Result<()> {
     Ok(())
 }
 
-/// Tests the processing of unproposed transactions with a mempool of transactions that collectively exceed `GAS_LIMIT_UNPROPOSED_TXS`.
+/// Tests the processing of unproposed transactions with a mempool of transactions that collectively exceed `BLOCK_GAS_LIMIT`.
 ///
-/// Note: In this test scenario, the mempool is populated with 135 pending transactions, with an average gas usage of 9_851_647 gas.
-/// The total estimated gas usage of these transactions exceeds `GAS_LIMIT_UNPROPOSED_TXS`.
+/// Note: In this test scenario, the mempool is populated by pending transactions with an average gas usage of 9_851_647 gas.
+/// The total estimated gas usage of these transactions exceeds `BLOCK_GAS_LIMIT`.
 #[test]
 fn test_unproposed_txs_exceeding_gas_limit() -> Result<()> {
     let avg_gas_usage = 9_851_647;
-    let min_expected = GAS_LIMIT_UNPROPOSED_TXS / avg_gas_usage;
+    let min_expected = BLOCK_GAS_LIMIT / avg_gas_usage;
     let ex = Arc::new(Executor::new());
     let (signal, shutdown) = smol::channel::unbounded::<()>();
 
@@ -172,7 +168,7 @@ fn test_unproposed_txs_exceeding_gas_limit() -> Result<()> {
                 assert!(num_unproposed_txs >= min_expected);
 
                 // Verify test result falls within gas limit
-                assert!(total_gas_used <= GAS_LIMIT_UNPROPOSED_TXS);
+                assert!(total_gas_used <= BLOCK_GAS_LIMIT);
             });
         },
     );

+ 1 - 1
src/runtime/vm_runtime.rs

@@ -45,7 +45,7 @@ use crate::{blockchain::BlockchainOverlayPtr, Error, Result};
 const MEMORY: &str = "memory";
 
 /// Gas limit for a single contract call (Single WASM instance)
-const GAS_LIMIT: u64 = 400_000_000;
+pub const GAS_LIMIT: u64 = 400_000_000;
 
 // ANCHOR: contract-section
 #[derive(Clone, Copy, PartialEq)]

+ 6 - 12
src/validator/consensus.rs

@@ -30,7 +30,8 @@ use crate::{
         block_store::{BlockDifficulty, BlockRanks},
         BlockInfo, Blockchain, BlockchainOverlay, BlockchainOverlayPtr, Header, HeaderHash,
     },
-    tx::Transaction,
+    runtime::vm_runtime::GAS_LIMIT,
+    tx::{Transaction, MAX_TX_CALLS},
     validator::{
         pow::PoWModule,
         utils::{best_fork_index, block_rank, find_extended_fork_index},
@@ -40,15 +41,8 @@ use crate::{
     Error, Result,
 };
 
-// Consensus configuration
-/// Average amount of gas consumed during transaction execution, derived by the Gas Analyzer
-const GAS_TX_AVG: u64 = 23_822_290;
-
-/// Multiplier used to calculate the gas limit for unproposed transactions
-const GAS_LIMIT_MULTIPLIER_UNPROPOSED_TXS: u64 = 50;
-
-/// Gas limit for unproposed transactions
-pub const GAS_LIMIT_UNPROPOSED_TXS: u64 = GAS_TX_AVG * GAS_LIMIT_MULTIPLIER_UNPROPOSED_TXS;
+/// Gas limit for total block transactions(50 full transactions).
+pub const BLOCK_GAS_LIMIT: u64 = GAS_LIMIT * MAX_TX_CALLS as u64 * 50;
 
 /// This struct represents the information required by the consensus algorithm
 pub struct Consensus {
@@ -878,11 +872,11 @@ impl Fork {
             let accumulated_gas_usage = total_gas_used + tx_gas_used;
 
             // Check gas limit - if accumulated gas used exceeds it, break out of loop
-            if accumulated_gas_usage > GAS_LIMIT_UNPROPOSED_TXS {
+            if accumulated_gas_usage > BLOCK_GAS_LIMIT {
                 warn!(
                     target: "validator::consensus::unproposed_txs",
                     "Retrieving transaction {} would exceed configured unproposed transaction gas limit: {} - {}",
-                    tx, accumulated_gas_usage, GAS_LIMIT_UNPROPOSED_TXS,
+                    tx, accumulated_gas_usage, BLOCK_GAS_LIMIT,
                 );
                 overlay.lock().unwrap().revert_to_checkpoint()?;
                 break

+ 7 - 3
src/validator/verification.rs

@@ -43,7 +43,7 @@ use crate::{
     runtime::vm_runtime::Runtime,
     tx::{Transaction, MAX_TX_CALLS, MIN_TX_CALLS},
     validator::{
-        consensus::{Consensus, Fork, Proposal, GAS_LIMIT_UNPROPOSED_TXS},
+        consensus::{Consensus, Fork, Proposal, BLOCK_GAS_LIMIT},
         fees::{circuit_gas_use, compute_fee, GasData, PALLAS_SCHNORR_SIGNATURE_FEE},
         pow::PoWModule,
     },
@@ -997,8 +997,12 @@ pub async fn verify_transactions(
         let accumulated_gas_usage = total_gas_used + tx_gas_used;
 
         // Check gas limit - if accumulated gas used exceeds it, break out of loop
-        if accumulated_gas_usage > GAS_LIMIT_UNPROPOSED_TXS {
-            warn!(target: "validator::verification::verify_transactions", "Transaction {} exceeds configured transaction gas limit: {} - {}", tx.hash(), accumulated_gas_usage, GAS_LIMIT_UNPROPOSED_TXS);
+        if accumulated_gas_usage > BLOCK_GAS_LIMIT {
+            warn!(
+                target: "validator::verification::verify_transactions",
+                "Transaction {} exceeds configured transaction gas limit: {} - {}",
+                tx.hash(), accumulated_gas_usage, BLOCK_GAS_LIMIT,
+            );
             erroneous_txs.push(tx.clone());
             overlay.lock().unwrap().revert_to_checkpoint()?;
             break