This commit adds robust input validation to the SetGasPrice method and
implements comprehensive table-driven tests to ensure correct behavior.
Changes in core/txpool/txpool.go:
- Add nil gas price validation with graceful error handling
- Add validation to reject negative gas prices
- Add validation to reject gas prices exceeding 1000 GWei maximum
- Return detailed error messages for each validation failure
- Log warnings when invalid gas prices are rejected
Changes in eth/api_miner.go:
- Update MinerAPI.SetGasPrice to check error return from txPool.SetGasPrice
- Return false when validation fails (when SetGasPrice returns error)
- Return true when validation succeeds (when SetGasPrice returns nil)
New tests in core/txpool/txpool_test.go:
- Implement TestSetGasPrice using table-driven test pattern
- Test 4 invalid cases: nil, negative, exceed max+1, exceed 10000 GWei
- Test 7 valid cases: 0, 1 wei, 1 GWei, 100 GWei, 500 GWei, max-1, max
- Each test case includes expected error value for precise validation
- All 11 test cases verify both error returns and gas price state
- Tests use isolated pool instances to ensure independence
This commit simplifies the transaction pool's gas price validation logic while
maintaining network security through the existing minimum gas price requirement.
Changes:
- Set default PriceLimit to 0 (was 1), allowing transactions with zero tip
- Remove PriceLimit >= 1 validation in config sanitization
- Simplify Pending() method by using EffectiveGasTipIntCmp consistently
- Unify validateTxBasics() logic to use GasTipCapIntCmp for all transactions
Key Points:
1. Economic Protection: All transactions still require gasPrice >= 12.5 Gwei
(enforced by GetMinGasPrice check in validateTx), providing sufficient
protection against DoS attacks even with zero tip.
2. Miner Incentives: Since XDPoSChain includes baseFee in miner rewards
(unlike standard EIP-1559), miners still earn the full gasPrice even when
tip is 0. This maintains miner revenue while allowing greater flexibility.
3. Special Transactions: BlockSigner and Randomize contract transactions
remain exempt from gas price checks, as they are critical for consensus.
4. Code Quality: Reduces complexity by 11 lines and unifies validation logic,
making the codebase more maintainable.
Security Analysis:
- No nil pointer risks: EffectiveGasTipIntCmp has built-in nil handling
- No DoS vulnerability: 12.5 Gwei minimum ensures economic cost per transaction
- EIP-1559 compatible: Existing minGasPrice check covers all validation needs
- Backward compatible: Only relaxes restrictions, doesn't break existing behavior
This change benefits system transactions and special use cases while maintaining
all existing security guarantees through the network's minimum gas price floor.