Skip to content

Commit

Permalink
Track & enforce data gas usage in transaction pool YieldBest (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
roberto-bayardo committed Jan 21, 2023
1 parent 77db2ff commit b032215
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
19 changes: 14 additions & 5 deletions txpool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ const (

BaseFeePoolBits = EnoughFeeCapProtocol + NoNonceGaps + EnoughBalance + NotTooMuchGas
QueuedPoolBits = EnoughFeeCapProtocol

DataGasPerBlob = 1 << 17
)

type DiscardReason uint8
Expand Down Expand Up @@ -612,7 +614,7 @@ func (p *TxPool) IsLocal(idHash []byte) bool {
func (p *TxPool) AddNewGoodPeer(peerID types.PeerID) { p.recentlyConnectedPeers.AddPeer(peerID) }
func (p *TxPool) Started() bool { return p.started.Load() }

func (p *TxPool) best(n uint16, txs *types.TxsRlp, tx kv.Tx, onTopOf, availableGas uint64, toSkip mapset.Set[[32]byte]) (bool, int, error) {
func (p *TxPool) best(n uint16, txs *types.TxsRlp, tx kv.Tx, onTopOf, availableGas uint64, availableDataGas uint64, toSkip mapset.Set[[32]byte]) (bool, int, error) {
// First wait for the corresponding block to arrive
if p.lastSeenBlock.Load() < onTopOf {
return false, 0, nil // Too early
Expand Down Expand Up @@ -641,6 +643,13 @@ func (p *TxPool) best(n uint16, txs *types.TxsRlp, tx kv.Tx, onTopOf, availableG
continue
}

// Skip transactions that require more datagas than is available
if uint64(mt.Tx.Blobs*DataGasPerBlob) > availableDataGas {
continue
} else {
availableDataGas -= uint64(mt.Tx.Blobs) * DataGasPerBlob
}

if mt.Tx.Gas >= p.blockGasLimit.Load() {
// Skip transactions with very large gas limit
continue
Expand Down Expand Up @@ -695,13 +704,13 @@ func (p *TxPool) ResetYieldedStatus() {
}
}

func (p *TxPool) YieldBest(n uint16, txs *types.TxsRlp, tx kv.Tx, onTopOf, availableGas uint64, toSkip mapset.Set[[32]byte]) (bool, int, error) {
return p.best(n, txs, tx, onTopOf, availableGas, toSkip)
func (p *TxPool) YieldBest(n uint16, txs *types.TxsRlp, tx kv.Tx, onTopOf, availableGas, availableDataGas uint64, toSkip mapset.Set[[32]byte]) (bool, int, error) {
return p.best(n, txs, tx, onTopOf, availableGas, availableDataGas, toSkip)
}

func (p *TxPool) PeekBest(n uint16, txs *types.TxsRlp, tx kv.Tx, onTopOf, availableGas uint64) (bool, error) {
func (p *TxPool) PeekBest(n uint16, txs *types.TxsRlp, tx kv.Tx, onTopOf, availableGas, availableDataGas uint64) (bool, error) {
set := mapset.NewSet[[32]byte]()
onTime, _, err := p.best(n, txs, tx, onTopOf, availableGas, set)
onTime, _, err := p.best(n, txs, tx, onTopOf, availableGas, availableDataGas, set)
return onTime, err
}

Expand Down
4 changes: 2 additions & 2 deletions txpool/txpool_grpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ var TxPoolAPIVersion = &types2.VersionReply{Major: 1, Minor: 0, Patch: 0}
type txPool interface {
ValidateSerializedTxn(serializedTxn []byte) error

PeekBest(n uint16, txs *types.TxsRlp, tx kv.Tx, onTopOf, availableGas uint64) (bool, error)
PeekBest(n uint16, txs *types.TxsRlp, tx kv.Tx, onTopOf, availableGas, availableDataGas uint64) (bool, error)
GetRlp(tx kv.Tx, hash []byte) ([]byte, error)
AddLocalTxs(ctx context.Context, newTxs types.TxSlots, tx kv.Tx) ([]DiscardReason, error)
deprecatedForEach(_ context.Context, f func(rlp, sender []byte, t SubPoolType), tx kv.Tx)
Expand Down Expand Up @@ -155,7 +155,7 @@ func (s *GrpcServer) Pending(ctx context.Context, _ *emptypb.Empty) (*txpool_pro
reply := &txpool_proto.PendingReply{}
reply.Txs = make([]*txpool_proto.PendingReply_Tx, 0, 32)
txSlots := types.TxsRlp{}
if _, err := s.txPool.PeekBest(math.MaxInt16, &txSlots, tx, 0 /* onTopOf */, math.MaxUint64 /* available gas */); err != nil {
if _, err := s.txPool.PeekBest(math.MaxInt16, &txSlots, tx, 0 /* onTopOf */, math.MaxUint64 /* available gas */, math.MaxUint64 /* available data gas*/); err != nil {
return nil, err
}
var senderArr [20]byte
Expand Down
2 changes: 2 additions & 0 deletions types/txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ type TxSlot struct {
IDHash [32]byte // Transaction hash for the purposes of using it as a transaction Id
Traced bool // Whether transaction needs to be traced throughout transaction pool code and generate debug printing
Creation bool // Set to true if "To" field of the transaction is not set
Blobs int // Set to the # of blobs contained by the transaction
}

const (
Expand Down Expand Up @@ -505,6 +506,7 @@ func (ctx *TxParseContext) ParseBlobTransaction(payload []byte, slot *TxSlot, se
slot.DataNonZeroLen = tx.dataNonZeroLen
slot.AlAddrCount = tx.accessListAddressCount
slot.AlStorCount = tx.accessListKeyCount
slot.Blobs = tx.numBlobHashes

err := tx.VerifyBlobs(payload)
if err != nil {
Expand Down

0 comments on commit b032215

Please sign in to comment.