Skip to content

Commit 480763d

Browse files
committed
antimev: add envelopeFee set and check
1 parent e3faec3 commit 480763d

File tree

10 files changed

+56
-9
lines changed

10 files changed

+56
-9
lines changed

antimev/envelope.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,24 @@ var (
4040
// IsEnvelope checks whether a transaction is an Envelope transaction. The criteria
4141
// include receiver's address, data prefix and data length check.
4242
func IsEnvelope(tx *types.Transaction) bool {
43-
if tx.To() == nil || *(tx.To()) != systemcontracts.GovernanceRewardProxyHash {
43+
return IsEnvelopeToAddress(tx.To()) && IsEnvelopeData(tx.Data())
44+
}
45+
46+
// IsEnvelopeToAddress checks whether an address pointer has the expected value for
47+
// Envelope To address.
48+
func IsEnvelopeToAddress(addr *common.Address) bool {
49+
if addr == nil || *addr != systemcontracts.GovernanceRewardProxyHash {
4450
return false
4551
}
52+
return true
53+
}
4654

47-
data := tx.Data()
55+
// IsEnvelopeData checks whether the input data bytes has the expected prefix for
56+
// Envelope specification.
57+
func IsEnvelopeData(data []byte) bool {
4858
if len(data) < minEncryptedDataSize || !bytes.HasPrefix(data, EncryptedDataPrefix) {
4959
return false
5060
}
51-
5261
return true
5362
}
5463

contracts/solidity/Policy.sol

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ contract Policy is IPolicy, GovernanceVote, GovProxyUpgradeable {
1717
uint256 public minGasTipCap;
1818
uint256 public baseFee;
1919
uint256 internal candidateLimit;
20+
uint256 public envelopeFee;
2021

2122
// Only for precompiled uups implementation in genesis file, need to be removed when upgrading the contract.
2223
// This override is added because "immutable __self" in UUPSUpgradeable is not avaliable in precompiled contract.
@@ -130,4 +131,21 @@ contract Policy is IPolicy, GovernanceVote, GovProxyUpgradeable {
130131
if (limit > 0) return limit;
131132
else return DEFAULT_CANDIDATE_LIMIT;
132133
}
134+
135+
function setEnvelopeFee(
136+
uint256 _fee
137+
)
138+
external
139+
needVote(
140+
bytes32(
141+
// keccak256("setEnvelopeFee")
142+
0xab26bca1cb5d7b0a97ee434cabffdf1efbc3073c1645a8ed4d1732335c51df49
143+
),
144+
keccak256(abi.encode(_fee))
145+
)
146+
{
147+
if (_fee <= 0) revert Errors.InvalidEnvelopeFee();
148+
envelopeFee = _fee;
149+
emit SetEnvelopeFee(_fee);
150+
}
133151
}

contracts/solidity/interfaces/IPolicy.sol

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ interface IPolicy {
77
event SetMinGasTipCap(uint256 gasTipCap);
88
event SetBaseFee(uint256 baseFee);
99
event SetCandidateLimit(uint256 candidateLimit);
10+
event SetEnvelopeFee(uint256 envelopeFee);
1011

1112
// add an address to blacklist policy
1213
function addBlackList(address _addr) external;

contracts/solidity/libraries/Errors.sol

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ library Errors {
2020
error InvalidMinGasTipCap();
2121
error InvalidBaseFee();
2222
error InvalidCandidateLimit();
23+
error InvalidEnvelopeFee();
2324

2425
// Governance Errors
2526
error SideCallNotAllowed();

core/state_transition.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"math"
2222
"math/big"
2323

24+
"github.com/ethereum/go-ethereum/antimev"
2425
"github.com/ethereum/go-ethereum/common"
2526
cmath "github.com/ethereum/go-ethereum/common/math"
2627
"github.com/ethereum/go-ethereum/core/systemcontracts"
@@ -328,9 +329,13 @@ func (st *StateTransition) preCheck() error {
328329
// For LegacyTx, GasFeeCap and GasPrice are equal, so checking GasTipCap and GasFeeCap is enough.
329330
if st.evm.ChainConfig().DBFT != nil {
330331
var minGasTipCap = st.state.GetState(systemcontracts.PolicyProxyHash, systemcontracts.GetMinGasTipCapStateHash()).Big()
332+
if antimev.IsEnvelopeToAddress(msg.To) && antimev.IsEnvelopeData(msg.Data) {
333+
var envelopeFee = st.state.GetState(systemcontracts.PolicyProxyHash, systemcontracts.GetEnvelopeFeeStateHash()).Big()
334+
minGasTipCap.Add(minGasTipCap, envelopeFee)
335+
}
331336
if cmath.BigMin(msg.GasTipCap, new(big.Int).Sub(msg.GasFeeCap, st.evm.Context.BaseFee)).Cmp(minGasTipCap) < 0 {
332-
return fmt.Errorf("%w: address %v, gasTipCap %v, gasFeeCap %v, policy minGasTipCap %v, baseFee %v ", ErrUnderpriced,
333-
msg.From.Hex(), msg.GasTipCap, msg.GasFeeCap, minGasTipCap, st.evm.Context.BaseFee)
337+
return fmt.Errorf("%w: address %v, gasTipCap %v, gasFeeCap %v, policy minGasTipCap (including Envelope fee for Envelopes) %v, baseFee %v ",
338+
ErrUnderpriced, msg.From.Hex(), msg.GasTipCap, msg.GasFeeCap, minGasTipCap, st.evm.Context.BaseFee)
334339
}
335340
}
336341
}

core/systemcontracts/contracts.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const (
2424
const blackListSlotIndex = 1
2525
const minGasTipCapSlotIndex = 2
2626
const baseFeeSlotIndex = 3
27+
const envelopeFeeSlotIndex = 5
2728

2829
// A set of genesis contract hashes.
2930
var (
@@ -95,3 +96,9 @@ func GetBaseFeeStateHash() common.Hash {
9596
func GetBlackListStateHash(addr common.Address) common.Hash {
9697
return crypto.Keccak256Hash(common.LeftPadBytes(addr.Bytes(), 32), common.LeftPadBytes([]byte{blackListSlotIndex}, 32))
9798
}
99+
100+
// GetEnvelopeFeeStateHash computes and returns the storage key of envelopeFee
101+
// in policy contract, for reading corresponding values from statedb.
102+
func GetEnvelopeFeeStateHash() common.Hash {
103+
return common.BytesToHash([]byte{envelopeFeeSlotIndex})
104+
}

core/txpool/validation.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"fmt"
2222
"math/big"
2323

24+
"github.com/ethereum/go-ethereum/antimev"
2425
"github.com/ethereum/go-ethereum/common"
2526
"github.com/ethereum/go-ethereum/common/math"
2627
"github.com/ethereum/go-ethereum/core"
@@ -215,8 +216,13 @@ func ValidateTransactionWithState(tx *types.Transaction, signer types.Signer, op
215216
// For LegacyTx, GasFeeCap and GasPrice are equal, so checking GasTipCap and GasFeeCap is enough
216217
var minGasTipCap = opts.State.GetState(systemcontracts.PolicyProxyHash, systemcontracts.GetMinGasTipCapStateHash()).Big()
217218
var baseFee = opts.State.GetState(systemcontracts.PolicyProxyHash, systemcontracts.GetBaseFeeStateHash()).Big()
219+
// Apply policy envelope fee check
220+
if antimev.IsEnvelope(tx) {
221+
var envelopeFee = opts.State.GetState(systemcontracts.PolicyProxyHash, systemcontracts.GetEnvelopeFeeStateHash()).Big()
222+
minGasTipCap.Add(minGasTipCap, envelopeFee)
223+
}
218224
if math.BigMin(tx.GasTipCap(), new(big.Int).Sub(tx.GasFeeCap(), baseFee)).Cmp(minGasTipCap) < 0 {
219-
return fmt.Errorf("%w: policy minGasTipCap needed %v, baseFee needed %v, gasTipCap %v, gasFeeCap %v ",
225+
return fmt.Errorf("%w: policy minGasTipCap (including Envelope fee for Envelopes) needed %v, baseFee needed %v, gasTipCap %v, gasFeeCap %v ",
220226
ErrUnderpriced, minGasTipCap, baseFee, tx.GasTipCap(), tx.GasFeeCap())
221227
}
222228
// Apply policy blacklist

privnet/four/genesis_privnet.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

privnet/seven/genesis_privnet.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

privnet/single/genesis_privnet.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)