Skip to content
This repository has been archived by the owner on Jun 9, 2024. It is now read-only.

Commit

Permalink
Merge branch 'zaki-hack' into zaki-with-logs
Browse files Browse the repository at this point in the history
  • Loading branch information
calbera committed Feb 1, 2024
2 parents 06bc7ed + 2100f45 commit 2b50bce
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 42 deletions.
64 changes: 26 additions & 38 deletions cosmos/runtime/txpool/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,12 @@ import (
ethtypes "github.com/ethereum/go-ethereum/core/types"
)

// AnteHandle implements sdk.AnteHandler.
// It is used to determine whether transactions should be ejected
// from the comet mempool.
// AnteHandle implements sdk.AnteHandler. It is used to determine whether transactions should be
// ejected from the comet mempool.
func (m *Mempool) AnteHandle(
ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler,
) (sdk.Context, error) {
// The transaction put into this function by CheckTx
// is a transaction from CometBFT mempool.
// The transaction put into this function by CheckTx is a transaction from CometBFT mempool.
telemetry.IncrCounter(float32(1), MetricKeyCometPoolTxs)
msgs := tx.GetMsgs()

Expand Down Expand Up @@ -83,55 +81,45 @@ func (m *Mempool) shouldEjectFromCometMempool(
return false
}

// First check things that are stateless.
if m.validateStateless(ctx, tx) {
ctx.Logger().Info("shouldEjectFromCometMempool: stateless failed", "tx", tx.Hash())
return true
}

// Then check for things that are stateful.
return m.validateStateful(ctx, tx)
return m.isInvalidLocal(ctx, tx) || m.includedCanonicalChain(ctx, tx)
}

// validateStateless returns whether the tx of the given hash is stateless.
func (m *Mempool) validateStateless(ctx sdk.Context, tx *ethtypes.Transaction) bool {
txHash := tx.Hash()
currentTime := ctx.BlockTime()

// 1. If the transaction has been in the mempool for longer than the configured timeout.
expired := currentTime.Sub(m.crc.TimeFirstSeen(txHash)) > m.lifetime
if expired {
telemetry.IncrCounter(float32(1), MetricKeyAnteShouldEjectExpiredTx)
}

// 2. If the transaction's gas params are less than or equal to the configured limit.
// isInvalidLocal returns whether the tx is invalid for the local node's mempool settings.
func (m *Mempool) isInvalidLocal(ctx sdk.Context, tx *ethtypes.Transaction) bool {
// 1. If the transaction's gas params are less than or equal to the configured limit.
priceLeLimit := tx.GasPrice().Cmp(m.priceLimit) <= 0
if priceLeLimit {
telemetry.IncrCounter(float32(1), MetricKeyAnteShouldEjectPriceLimit)
}

// If the transaction was not seen before, it is valid for now.
timeFirstSeen, notSeen := m.crc.TimeFirstSeen(tx.Hash())
if !notSeen {
ctx.Logger().Info("validateStateless notSeenBefore", "txHash", tx.Hash())
return false
}

// 2. If the transaction has been in the mempool for longer than the configured lifetime.
expired := time.Since(timeFirstSeen) > m.lifetime
if expired {
telemetry.IncrCounter(float32(1), MetricKeyAnteShouldEjectExpiredTx)
}

ctx.Logger().Info(
"validateStateless",
"timeFirstSeen", m.crc.TimeFirstSeen(txHash),
"timeInMempool", currentTime.Sub(m.crc.TimeFirstSeen(txHash)).Seconds(),
"txHash", txHash, "currentTime", currentTime,
"validateStateless", "timeFirstSeen", timeFirstSeen, "currentTime", time.Now(),
"timeInMempool", time.Since(timeFirstSeen), "txHash", tx.Hash(),
"expired", expired, "priceLeLimit", priceLeLimit,
)

return expired || priceLeLimit
}

// includedCanonicalChain returns whether the tx of the given hash is included in the canonical
// Eth chain.
func (m *Mempool) validateStateful(ctx sdk.Context, tx *ethtypes.Transaction) bool {
// // 1. If the transaction has been included in a block.
// signer := ethtypes.LatestSignerForChainID(m.chainConfig.ChainID)
// if _, err := ethtypes.Sender(signer, tx); err != nil {
// return true
// }

func (m *Mempool) includedCanonicalChain(ctx sdk.Context, tx *ethtypes.Transaction) bool {
included := m.chain.GetTransactionLookup(tx.Hash()) != nil
telemetry.IncrCounter(float32(1), MetricKeyAnteShouldEjectInclusion)
if included {
telemetry.IncrCounter(float32(1), MetricKeyAnteShouldEjectInclusion)
}
ctx.Logger().Info("validateStateful", "included", included, "txHash", tx.Hash())
return included
}
7 changes: 3 additions & 4 deletions cosmos/runtime/txpool/comet.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const (
type CometRemoteCache interface {
IsRemoteTx(txHash common.Hash) bool
MarkRemoteSeen(txHash common.Hash) bool
TimeFirstSeen(txHash common.Hash) time.Time
TimeFirstSeen(txHash common.Hash) (time.Time, bool)
}

// Thread-safe implementation of CometRemoteCache.
Expand All @@ -63,7 +63,6 @@ func (crc *cometRemoteCache) MarkRemoteSeen(txHash common.Hash) bool {
return false
}

func (crc *cometRemoteCache) TimeFirstSeen(txHash common.Hash) time.Time {
i, _ := crc.timeInserted.Get(txHash)
return i
func (crc *cometRemoteCache) TimeFirstSeen(txHash common.Hash) (time.Time, bool) {
return crc.timeInserted.Get(txHash)
}

0 comments on commit 2b50bce

Please sign in to comment.