From 5a44940f364373acefe3dee2d3be8a9c52a68bc9 Mon Sep 17 00:00:00 2001 From: Stephen Buttolph Date: Tue, 17 Dec 2024 06:52:47 -0500 Subject: [PATCH 1/2] Verify tx gas isn't too large in VerifyTx --- vms/platformvm/block/executor/manager.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/vms/platformvm/block/executor/manager.go b/vms/platformvm/block/executor/manager.go index 48330dd74788..c1978eef9b96 100644 --- a/vms/platformvm/block/executor/manager.go +++ b/vms/platformvm/block/executor/manager.go @@ -16,6 +16,7 @@ import ( "github.com/ava-labs/avalanchego/vms/platformvm/state" "github.com/ava-labs/avalanchego/vms/platformvm/txs" "github.com/ava-labs/avalanchego/vms/platformvm/txs/executor" + "github.com/ava-labs/avalanchego/vms/platformvm/txs/fee" "github.com/ava-labs/avalanchego/vms/platformvm/txs/mempool" "github.com/ava-labs/avalanchego/vms/platformvm/validators" ) @@ -134,6 +135,18 @@ func (m *manager) VerifyTx(tx *txs.Tx) error { } } + complexity, err := fee.TxComplexity(tx.Unsigned) + if err != nil { + return fmt.Errorf("failed to calculate tx complexity: %w", err) + } + gas, err := complexity.ToGas(m.txExecutorBackend.Config.DynamicFeeConfig.Weights) + if err != nil { + return fmt.Errorf("failed to calculate tx gas: %w", err) + } + if gas > m.txExecutorBackend.Config.DynamicFeeConfig.MaxCapacity { + return fmt.Errorf("tx exceeds maximum gas consumption: %d > %d", gas, m.txExecutorBackend.Config.DynamicFeeConfig.MaxCapacity) + } + recommendedPChainHeight, err := m.ctx.ValidatorState.GetMinimumHeight(context.TODO()) if err != nil { return fmt.Errorf("failed to fetch P-chain height: %w", err) From ac365112053c2b4e8b584a65c1a6886e637351e5 Mon Sep 17 00:00:00 2001 From: Stephen Buttolph Date: Tue, 17 Dec 2024 07:07:44 -0500 Subject: [PATCH 2/2] be more restrictive --- vms/platformvm/block/executor/manager.go | 30 ++++++++++++++---------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/vms/platformvm/block/executor/manager.go b/vms/platformvm/block/executor/manager.go index c1978eef9b96..33b29354a59e 100644 --- a/vms/platformvm/block/executor/manager.go +++ b/vms/platformvm/block/executor/manager.go @@ -135,18 +135,6 @@ func (m *manager) VerifyTx(tx *txs.Tx) error { } } - complexity, err := fee.TxComplexity(tx.Unsigned) - if err != nil { - return fmt.Errorf("failed to calculate tx complexity: %w", err) - } - gas, err := complexity.ToGas(m.txExecutorBackend.Config.DynamicFeeConfig.Weights) - if err != nil { - return fmt.Errorf("failed to calculate tx gas: %w", err) - } - if gas > m.txExecutorBackend.Config.DynamicFeeConfig.MaxCapacity { - return fmt.Errorf("tx exceeds maximum gas consumption: %d > %d", gas, m.txExecutorBackend.Config.DynamicFeeConfig.MaxCapacity) - } - recommendedPChainHeight, err := m.ctx.ValidatorState.GetMinimumHeight(context.TODO()) if err != nil { return fmt.Errorf("failed to fetch P-chain height: %w", err) @@ -181,6 +169,24 @@ func (m *manager) VerifyTx(tx *txs.Tx) error { return fmt.Errorf("failed to advance the chain time: %w", err) } + if timestamp := stateDiff.GetTimestamp(); m.txExecutorBackend.Config.UpgradeConfig.IsEtnaActivated(timestamp) { + complexity, err := fee.TxComplexity(tx.Unsigned) + if err != nil { + return fmt.Errorf("failed to calculate tx complexity: %w", err) + } + gas, err := complexity.ToGas(m.txExecutorBackend.Config.DynamicFeeConfig.Weights) + if err != nil { + return fmt.Errorf("failed to calculate tx gas: %w", err) + } + + // TODO: After the mempool is updated, convert this check to use the + // maximum mempool capacity. + feeState := stateDiff.GetFeeState() + if gas > feeState.Capacity { + return fmt.Errorf("tx exceeds current gas capacity: %d > %d", gas, feeState.Capacity) + } + } + feeCalculator := state.PickFeeCalculator(m.txExecutorBackend.Config, stateDiff) _, _, _, err = executor.StandardTx( m.txExecutorBackend,