Skip to content

Commit 4c52135

Browse files
committed
core: apply envelope gas limit policy to txpool and state transition
1 parent 89a7062 commit 4c52135

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

core/state_transition.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,11 @@ func (st *StateTransition) preCheck() error {
330330
if st.evm.ChainConfig().DBFT != nil {
331331
var minGasTipCap = st.state.GetState(systemcontracts.PolicyProxyHash, systemcontracts.GetMinGasTipCapStateHash()).Big()
332332
if antimev.IsEnvelopeToAddress(msg.To) && antimev.IsEnvelopeData(msg.Data) {
333+
var envelopeGasLimit = st.state.GetState(systemcontracts.PolicyProxyHash, systemcontracts.GetMaxEnvelopeGasLimitStateHash()).Big()
334+
if new(big.Int).SetUint64(msg.GasLimit).Cmp(envelopeGasLimit) > 0 {
335+
return fmt.Errorf("%w: address %v, gasLimit %v, policy maxEnvelopeGasLimit %v, ", ErrGasLimitReached, msg.From.Hex(), msg.GasLimit,
336+
envelopeGasLimit)
337+
}
333338
var envelopeFee = st.state.GetState(systemcontracts.PolicyProxyHash, systemcontracts.GetEnvelopeFeeStateHash()).Big()
334339
minGasTipCap.Add(minGasTipCap, envelopeFee)
335340
}

core/systemcontracts/contracts.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const blackListSlotIndex = 1
2525
const minGasTipCapSlotIndex = 2
2626
const baseFeeSlotIndex = 3
2727
const envelopeFeeSlotIndex = 5
28+
const maxEnvelopeGasLimitSlotIndex = 7
2829

2930
// A set of genesis contract hashes.
3031
var (
@@ -102,3 +103,10 @@ func GetBlackListStateHash(addr common.Address) common.Hash {
102103
func GetEnvelopeFeeStateHash() common.Hash {
103104
return common.BytesToHash([]byte{envelopeFeeSlotIndex})
104105
}
106+
107+
// GetMaxEnvelopeGasLimitStateHash computes and returns the storage key
108+
// of maxEnvelopeGasLimit in policy contract, for reading corresponding
109+
// values from statedb.
110+
func GetMaxEnvelopeGasLimitStateHash() common.Hash {
111+
return common.BytesToHash([]byte{maxEnvelopeGasLimitSlotIndex})
112+
}

core/txpool/validation.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,13 @@ func ValidateTransactionWithState(tx *types.Transaction, signer types.Signer, op
216216
// For LegacyTx, GasFeeCap and GasPrice are equal, so checking GasTipCap and GasFeeCap is enough
217217
var minGasTipCap = opts.State.GetState(systemcontracts.PolicyProxyHash, systemcontracts.GetMinGasTipCapStateHash()).Big()
218218
var baseFee = opts.State.GetState(systemcontracts.PolicyProxyHash, systemcontracts.GetBaseFeeStateHash()).Big()
219-
// Apply policy envelope fee check
219+
// Apply policy envelope check
220220
if antimev.IsEnvelope(tx) {
221+
var envelopeGasLimit = opts.State.GetState(systemcontracts.PolicyProxyHash, systemcontracts.GetMaxEnvelopeGasLimitStateHash()).Big()
222+
if new(big.Int).SetUint64(tx.Gas()).Cmp(envelopeGasLimit) > 0 {
223+
return fmt.Errorf("%w: policy maxEnvelopeGasLimit allowed %v, gas %v", ErrGasLimit, envelopeGasLimit, tx.Gas())
224+
}
225+
// Add envelope fee on top of minGasTipCap check
221226
var envelopeFee = opts.State.GetState(systemcontracts.PolicyProxyHash, systemcontracts.GetEnvelopeFeeStateHash()).Big()
222227
minGasTipCap.Add(minGasTipCap, envelopeFee)
223228
}

0 commit comments

Comments
 (0)