Skip to content

Commit

Permalink
CRU
Browse files Browse the repository at this point in the history
  • Loading branch information
hbandura committed Nov 14, 2023
1 parent 4696960 commit da9ab8f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 47 deletions.
14 changes: 10 additions & 4 deletions core/txpool/blobpool/blobpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -1031,7 +1031,7 @@ func (p *BlobPool) validateTx(tx *types.Transaction) error {
// consensus rules
baseOpts := &txpool.CeloValidationOptions{
Config: p.chain.Config(),
AcceptMap: txpool.NewAcceptMap(types.BlobTxType),
AcceptSet: txpool.NewAcceptSet(types.BlobTxType),
MaxSize: txMaxSize,
MinTip: p.gasTip.ToBig(),
}
Expand All @@ -1040,7 +1040,7 @@ func (p *BlobPool) validateTx(tx *types.Transaction) error {
return err
}
// Ensure the transaction adheres to the stateful pool filters (nonce, balance)
stateOpts := &txpool.CeloValidationOptionsWithState{
stateOpts := &txpool.ValidationOptionsWithState{
State: p.state,

FirstNonceGap: func(addr common.Address) uint64 {
Expand Down Expand Up @@ -1069,9 +1069,15 @@ func (p *BlobPool) validateTx(tx *types.Transaction) error {
}
return nil
},
FeeCurrencyValidator: p.feeCurrencyValidator,
}
if err := txpool.ValidateTransactionWithState(tx, p.signer, stateOpts); err != nil {

// Adapt to celo validation options
celoOpts := &txpool.CeloValidationOptionsWithState{
ValidationOptionsWithState: *stateOpts,
FeeCurrencyValidator: p.feeCurrencyValidator,
}

if err := txpool.ValidateTransactionWithState(tx, p.signer, celoOpts); err != nil {
return err
}
// If the transaction replaces an existing one, ensure that price bumps are
Expand Down
52 changes: 14 additions & 38 deletions core/txpool/celo_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ func (f *feeval) Balance(st *state.StateDB, address common.Address, feeCurrency
return st.GetBalance(address)
}

// AcceptMap is a set of accepted transaction types for a transaction subpool.
type AcceptMap = map[uint8]struct{}
// AcceptSet is a set of accepted transaction types for a transaction subpool.
type AcceptSet = map[uint8]struct{}

// CeloValidationOptions define certain differences between transaction validation
// across the different pools without having to duplicate those checks.
Expand All @@ -48,14 +48,23 @@ type AcceptMap = map[uint8]struct{}
type CeloValidationOptions struct {
Config *params.ChainConfig // Chain configuration to selectively validate based on current fork rules

AcceptMap AcceptMap // Map of transaction types that should be accepted for the calling pool
AcceptSet AcceptSet // Set of transaction types that should be accepted for the calling pool
MaxSize uint64 // Maximum size of a transaction that the caller can meaningfully handle
MinTip *big.Int // Minimum gas tip needed to allow a transaction into the caller pool
}

// NewAcceptSet creates a new AcceptSet with the types provided.
func NewAcceptSet(types ...uint8) AcceptSet {
m := make(AcceptSet, len(types))
for _, t := range types {
m[t] = struct{}{}
}
return m
}

// Accepts returns true iff txType is accepted by this CeloValidationOptions.
func (cvo *CeloValidationOptions) Accepts(txType uint8) bool {
_, ok := cvo.AcceptMap[txType]
_, ok := cvo.AcceptSet[txType]
return ok
}

Expand All @@ -79,15 +88,6 @@ func CeloValidateTransaction(tx *types.Transaction, head *types.Header,
return nil
}

// NewAcceptMap creates a new AcceptMap with the types provided as keys.
func NewAcceptMap(types ...uint8) AcceptMap {
m := make(AcceptMap, len(types))
for _, t := range types {
m[t] = struct{}{}
}
return m
}

// FeeCurrencyTxType returns true if and only if the transaction type
// given can handle custom gas fee currencies.
func FeeCurrencyTxType(t uint8) bool {
Expand All @@ -102,31 +102,7 @@ func FeeCurrencyTx(tx *types.Transaction) bool {

// See: txpool.ValidationOptionsWithState
type CeloValidationOptionsWithState struct {
State *state.StateDB // State database to check nonces and balances against

// FirstNonceGap is an optional callback to retrieve the first nonce gap in
// the list of pooled transactions of a specific account. If this method is
// set, nonce gaps will be checked and forbidden. If this method is not set,
// nonce gaps will be ignored and permitted.
FirstNonceGap func(addr common.Address) uint64

// UsedAndLeftSlots is a mandatory callback to retrieve the number of tx slots
// used and the number still permitted for an account. New transactions will
// be rejected once the number of remaining slots reaches zero.
UsedAndLeftSlots func(addr common.Address) (int, int)

// ExistingExpenditure is a mandatory callback to retrieve the cummulative
// cost of the already pooled transactions to check for overdrafts.
ExistingExpenditure func(addr common.Address) *big.Int

// ExistingCost is a mandatory callback to retrieve an already pooled
// transaction's cost with the given nonce to check for overdrafts.
ExistingCost func(addr common.Address, nonce uint64) *big.Int

// L1CostFn is an optional extension, to validate L1 rollup costs of a tx
L1CostFn L1CostFunc

// Celo
ValidationOptionsWithState

// FeeCurrencyValidator allows for balance check of non native fee currencies.
FeeCurrencyValidator FeeCurrencyValidator
Expand Down
16 changes: 11 additions & 5 deletions core/txpool/legacypool/legacypool.go
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ func (pool *LegacyPool) local() map[common.Address]types.Transactions {
func (pool *LegacyPool) validateTxBasics(tx *types.Transaction, local bool) error {
opts := &txpool.CeloValidationOptions{
Config: pool.chainconfig,
AcceptMap: txpool.NewAcceptMap(
AcceptSet: txpool.NewAcceptSet(
types.LegacyTxType,
types.AccessListTxType,
types.DynamicFeeTxType,
Expand All @@ -631,7 +631,7 @@ func (pool *LegacyPool) validateTxBasics(tx *types.Transaction, local bool) erro
// validateTx checks whether a transaction is valid according to the consensus
// rules and adheres to some heuristic limits of the local node (price and size).
func (pool *LegacyPool) validateTx(tx *types.Transaction, local bool) error {
opts := &txpool.CeloValidationOptionsWithState{
opts := &txpool.ValidationOptionsWithState{
State: pool.currentState,

FirstNonceGap: nil, // Pool allows arbitrary arrival order, don't invalidate nonce gaps
Expand Down Expand Up @@ -665,10 +665,16 @@ func (pool *LegacyPool) validateTx(tx *types.Transaction, local bool) error {
}
return nil
},
L1CostFn: pool.l1CostFn,
FeeCurrencyValidator: pool.feeCurrencyValidator,
L1CostFn: pool.l1CostFn,
}
if err := txpool.ValidateTransactionWithState(tx, pool.signer, opts); err != nil {

// Adapt to celo validation options
celoOpts := &txpool.CeloValidationOptionsWithState{
ValidationOptionsWithState: *opts,
FeeCurrencyValidator: pool.feeCurrencyValidator,
}

if err := txpool.ValidateTransactionWithState(tx, pool.signer, celoOpts); err != nil {
return err
}
return nil
Expand Down

0 comments on commit da9ab8f

Please sign in to comment.