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

validator: set BLOCK_GAS_LIMIT and enforce TX_GAS_LIMIT

Set BLOCK_GAS_LIMIT to a fixed 16B, replacing the previous
GAS_LIMIT * MAX_TX_CALLS * 50 computation.

Enforce TX_GAS_LIMIT in verify_transaction after computing total gas.
Add TxVerifyFailed::GasLimitExceeded error variant.

Update the signature fee import for the PALLAS_SCHNORR_VERIFY_GAS
rename.
darkfi 1 месяц назад
Родитель
Сommit
53312a5c77
3 измененных файлов с 18 добавлено и 7 удалено
  1. 3 0
      src/error.rs
  2. 3 4
      src/validator/consensus.rs
  3. 12 3
      src/validator/verification.rs

+ 3 - 0
src/error.rs

@@ -632,6 +632,9 @@ pub enum TxVerifyFailed {
     #[error("Insufficient fee paid")]
     InsufficientFee,
 
+    #[error("Transaction exceeds configured gas limit")]
+    GasLimitExceeded,
+
     #[error("Erroneous transactions found")]
     ErroneousTxs(Vec<crate::tx::Transaction>),
 }

+ 3 - 4
src/validator/consensus.rs

@@ -30,8 +30,7 @@ use crate::{
         parse_record, BlockInfo, Blockchain, BlockchainOverlay, BlockchainOverlayPtr, Header,
         HeaderHash,
     },
-    runtime::vm_runtime::GAS_LIMIT,
-    tx::{Transaction, MAX_TX_CALLS},
+    tx::Transaction,
     util::time::Timestamp,
     validator::{
         pow::{PoWModule, RANDOMX_KEY_CHANGE_DELAY, RANDOMX_KEY_CHANGING_HEIGHT},
@@ -42,8 +41,8 @@ use crate::{
     Error, Result,
 };
 
-/// Gas limit for total block transactions(50 full transactions).
-pub const BLOCK_GAS_LIMIT: u64 = GAS_LIMIT * MAX_TX_CALLS as u64 * 50;
+/// Gas limit for a full block.
+pub const BLOCK_GAS_LIMIT: u64 = 16_000_000_000;
 
 /// This struct represents the information required by the consensus
 /// algorithm.

+ 12 - 3
src/validator/verification.rs

@@ -41,12 +41,12 @@ use crate::{
         Blockchain, BlockchainOverlayPtr, HeaderHash,
     },
     error::TxVerifyFailed,
-    runtime::vm_runtime::{Runtime, TxLocalState},
+    runtime::vm_runtime::{Runtime, TxLocalState, TX_GAS_LIMIT},
     tx::{Transaction, MAX_TX_CALLS, MIN_TX_CALLS},
     util::time::Timestamp,
     validator::{
         consensus::{Consensus, Fork, Proposal, BLOCK_GAS_LIMIT},
-        fees::{circuit_gas_use, GasData, PALLAS_SCHNORR_SIGNATURE_FEE},
+        fees::{circuit_gas_use, GasData, PALLAS_SCHNORR_VERIFY_GAS},
         pow::PoWModule,
     },
     zk::VerifyingKey,
@@ -859,7 +859,7 @@ pub async fn verify_transaction(
     }
 
     // The signature fee is tx_size + fixed_sig_fee * n_signatures
-    gas_data.signatures = PALLAS_SCHNORR_SIGNATURE_FEE
+    gas_data.signatures = PALLAS_SCHNORR_VERIFY_GAS
         .saturating_mul(tx.signatures.len() as u64)
         .saturating_add(serialize_async(tx).await.len() as u64);
     debug!(target: "validator::verification::verify_transaction", "The gas used for signature of transaction {tx_hash}: {}", gas_data.signatures);
@@ -878,6 +878,15 @@ pub async fn verify_transaction(
     // for subsequent uses.
     let total_gas_used = gas_data.total_gas_used();
 
+    // Enforce the per-transaction gas limit.
+    if total_gas_used > TX_GAS_LIMIT {
+        error!(
+            target: "validator::verification::verify_transaction",
+            "[VALIDATOR] Transaction {tx_hash} exceeds TX_GAS_LIMIT: {total_gas_used} > {TX_GAS_LIMIT}"
+        );
+        return Err(TxVerifyFailed::GasLimitExceeded.into())
+    }
+
     if verify_fee {
         // Deserialize the fee call to find the paid fee
         let fee: u64 = match deserialize_async(&tx.calls[fee_call_idx].data.data[1..9]).await {