feat: Lane's with custom tx adapters (#239)

* greedy approach to lane verification

* docs

* base lane testing

* mev lane testing nits

* abci top level testing done

* network spamming in E2E

* string rep of escrow address

* nit

* nit

* nit v1.0.1

* removing logs from testing

* query test

* logging with tx info

* nits

* nit

* nit

* testing nit

---------

Co-authored-by: Alex Johnson <alex@skip.money>
This commit is contained in:
David Terpay
2023-11-28 13:51:42 -05:00
committed by GitHub
co-authored by Alex Johnson
parent 01a84b1cc5
commit be4465ae95
19 changed files with 444 additions and 98 deletions
+36 -2
View File
@@ -42,9 +42,26 @@ func (l *BaseLane) PrepareLane(
)
}
// Get the transaction info for each transaction that was selected.
txsWithInfo := make([]utils.TxWithInfo, len(txsToInclude))
for i, tx := range txsToInclude {
txInfo, err := l.GetTxInfo(ctx, tx)
if err != nil {
l.Logger().Error(
"failed to get tx info",
"lane", l.Name(),
"err", err,
)
return proposal, err
}
txsWithInfo[i] = txInfo
}
// Update the proposal with the selected transactions. This fails if the lane attempted to add
// more transactions than the allocated max block space for the lane.
if err := proposal.UpdateProposal(l, txsToInclude); err != nil {
if err := proposal.UpdateProposal(l, txsWithInfo); err != nil {
l.Logger().Error(
"failed to update proposal",
"lane", l.Name(),
@@ -102,8 +119,25 @@ func (l *BaseLane) ProcessLane(
return proposal, err
}
// Retrieve the transaction info for each transaction that belongs to the lane.
txsWithInfo := make([]utils.TxWithInfo, len(txsFromLane))
for i, tx := range txsFromLane {
txInfo, err := l.GetTxInfo(ctx, tx)
if err != nil {
l.Logger().Error(
"failed to get tx info",
"lane", l.Name(),
"err", err,
)
return proposal, err
}
txsWithInfo[i] = txInfo
}
// Optimistically update the proposal with the partial proposal.
if err := proposal.UpdateProposal(l, txsFromLane); err != nil {
if err := proposal.UpdateProposal(l, txsWithInfo); err != nil {
l.Logger().Error(
"failed to update proposal",
"lane", l.Name(),
+1 -2
View File
@@ -6,7 +6,6 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/skip-mev/block-sdk/block/proposals"
"github.com/skip-mev/block-sdk/block/utils"
)
// DefaultPrepareLaneHandler returns a default implementation of the PrepareLaneHandler. It
@@ -27,7 +26,7 @@ func (l *BaseLane) DefaultPrepareLaneHandler() PrepareLaneHandler {
for iterator := l.Select(ctx, nil); iterator != nil; iterator = iterator.Next() {
tx := iterator.Tx()
txInfo, err := utils.GetTxInfo(l.TxEncoder(), tx)
txInfo, err := l.GetTxInfo(ctx, tx)
if err != nil {
l.Logger().Info("failed to get hash of tx", "err", err)
+11 -6
View File
@@ -102,19 +102,24 @@ func NewMempool[C comparable](txPriority TxPriority[C], txEncoder sdk.TxEncoder,
}
}
// Priority returns the priority of the transaction.
func (cm *Mempool[C]) Priority(ctx sdk.Context, tx sdk.Tx) any {
return cm.txPriority.GetTxPriority(ctx, tx)
}
// Insert inserts a transaction into the mempool.
func (cm *Mempool[C]) Insert(ctx context.Context, tx sdk.Tx) error {
if err := cm.index.Insert(ctx, tx); err != nil {
return fmt.Errorf("failed to insert tx into auction index: %w", err)
}
txInfo, err := utils.GetTxInfo(cm.txEncoder, tx)
hash, err := utils.GetTxHash(cm.txEncoder, tx)
if err != nil {
cm.Remove(tx)
return err
}
cm.txCache[txInfo.Hash] = struct{}{}
cm.txCache[hash] = struct{}{}
return nil
}
@@ -125,12 +130,12 @@ func (cm *Mempool[C]) Remove(tx sdk.Tx) error {
return fmt.Errorf("failed to remove transaction from the mempool: %w", err)
}
txInfo, err := utils.GetTxInfo(cm.txEncoder, tx)
hash, err := utils.GetTxHash(cm.txEncoder, tx)
if err != nil {
return fmt.Errorf("failed to get tx hash string: %w", err)
}
delete(cm.txCache, txInfo.Hash)
delete(cm.txCache, hash)
return nil
}
@@ -150,12 +155,12 @@ func (cm *Mempool[C]) CountTx() int {
// Contains returns true if the transaction is contained in the mempool.
func (cm *Mempool[C]) Contains(tx sdk.Tx) bool {
txInfo, err := utils.GetTxInfo(cm.txEncoder, tx)
hash, err := utils.GetTxHash(cm.txEncoder, tx)
if err != nil {
return false
}
_, ok := cm.txCache[txInfo.Hash]
_, ok := cm.txCache[hash]
return ok
}
+41
View File
@@ -0,0 +1,41 @@
package base
import (
"encoding/hex"
"fmt"
"strings"
comettypes "github.com/cometbft/cometbft/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/skip-mev/block-sdk/block/utils"
)
// GetTxInfo returns various information about the transaction that
// belongs to the lane including its priority, signer's, sequence number,
// size and more.
func (l *BaseLane) GetTxInfo(ctx sdk.Context, tx sdk.Tx) (utils.TxWithInfo, error) {
txBytes, err := l.cfg.TxEncoder(tx)
if err != nil {
return utils.TxWithInfo{}, fmt.Errorf("failed to encode transaction: %w", err)
}
// TODO: Add an adapter to lanes so that this can be flexible to support EVM, etc.
gasTx, ok := tx.(sdk.FeeTx)
if !ok {
return utils.TxWithInfo{}, fmt.Errorf("failed to cast transaction to gas tx")
}
signers, err := l.cfg.SignerExtractor.GetSigners(tx)
if err != nil {
return utils.TxWithInfo{}, err
}
return utils.TxWithInfo{
Hash: strings.ToUpper(hex.EncodeToString(comettypes.Tx(txBytes).Hash())),
Size: int64(len(txBytes)),
GasLimit: gasTx.GetGas(),
TxBytes: txBytes,
Priority: l.LaneMempool.Priority(ctx, tx),
Signers: signers,
}, nil
}