github.com/0xPolygon/supernets2-node@v0.0.0-20230711153321-2fe574524eaa/sequencer/interfaces.go (about)

     1  package sequencer
     2  
     3  import (
     4  	"context"
     5  	"math/big"
     6  	"time"
     7  
     8  	"github.com/0xPolygon/supernets2-node/ethtxmanager"
     9  	"github.com/0xPolygon/supernets2-node/pool"
    10  	"github.com/0xPolygon/supernets2-node/state"
    11  	"github.com/0xPolygon/supernets2-node/state/metrics"
    12  	pb "github.com/0xPolygon/supernets2-node/state/runtime/executor/pb"
    13  	"github.com/ethereum/go-ethereum/common"
    14  	"github.com/ethereum/go-ethereum/core/types"
    15  	"github.com/jackc/pgx/v4"
    16  )
    17  
    18  // Consumer interfaces required by the package.
    19  
    20  // txPool contains the methods required to interact with the tx pool.
    21  type txPool interface {
    22  	DeleteTransactionsByHashes(ctx context.Context, hashes []common.Hash) error
    23  	DeleteTransactionByHash(ctx context.Context, hash common.Hash) error
    24  	MarkWIPTxsAsPending(ctx context.Context) error
    25  	GetNonWIPPendingTxs(ctx context.Context, limit uint64) ([]pool.Transaction, error)
    26  	UpdateTxStatus(ctx context.Context, hash common.Hash, newStatus pool.TxStatus, isWIP bool, failedReason *string) error
    27  	GetTxZkCountersByHash(ctx context.Context, hash common.Hash) (*state.ZKCounters, error)
    28  	UpdateTxWIPStatus(ctx context.Context, hash common.Hash, isWIP bool) error
    29  }
    30  
    31  // etherman contains the methods required to interact with ethereum.
    32  type etherman interface {
    33  	GetSendSequenceFee(numBatches uint64) (*big.Int, error)
    34  	TrustedSequencer() (common.Address, error)
    35  	GetLatestBatchNumber() (uint64, error)
    36  	GetLastBatchTimestamp() (uint64, error)
    37  	GetLatestBlockTimestamp(ctx context.Context) (uint64, error)
    38  	GetLatestBlockNumber(ctx context.Context) (uint64, error)
    39  }
    40  
    41  // stateInterface gathers the methods required to interact with the state.
    42  type stateInterface interface {
    43  	GetTimeForLatestBatchVirtualization(ctx context.Context, dbTx pgx.Tx) (time.Time, error)
    44  	GetTxsOlderThanNL1Blocks(ctx context.Context, nL1Blocks uint64, dbTx pgx.Tx) ([]common.Hash, error)
    45  	GetBatchByNumber(ctx context.Context, batchNumber uint64, dbTx pgx.Tx) (*state.Batch, error)
    46  	GetTransactionsByBatchNumber(ctx context.Context, batchNumber uint64, dbTx pgx.Tx) (txs []types.Transaction, err error)
    47  	BeginStateTransaction(ctx context.Context) (pgx.Tx, error)
    48  	GetLastVirtualBatchNum(ctx context.Context, dbTx pgx.Tx) (uint64, error)
    49  	IsBatchClosed(ctx context.Context, batchNum uint64, dbTx pgx.Tx) (bool, error)
    50  	Begin(ctx context.Context) (pgx.Tx, error)
    51  	GetBalanceByStateRoot(ctx context.Context, address common.Address, root common.Hash) (*big.Int, error)
    52  	GetNonceByStateRoot(ctx context.Context, address common.Address, root common.Hash) (*big.Int, error)
    53  	GetLastStateRoot(ctx context.Context, dbTx pgx.Tx) (common.Hash, error)
    54  	ProcessBatch(ctx context.Context, request state.ProcessRequest, updateMerkleTree bool) (*state.ProcessBatchResponse, error)
    55  	CloseBatch(ctx context.Context, receipt state.ProcessingReceipt, dbTx pgx.Tx) error
    56  	ExecuteBatch(ctx context.Context, batch state.Batch, updateMerkleTree bool, dbTx pgx.Tx) (*pb.ProcessBatchResponse, error)
    57  	GetForcedBatch(ctx context.Context, forcedBatchNumber uint64, dbTx pgx.Tx) (*state.ForcedBatch, error)
    58  	GetLastBatch(ctx context.Context, dbTx pgx.Tx) (*state.Batch, error)
    59  	GetLastBatchNumber(ctx context.Context, dbTx pgx.Tx) (uint64, error)
    60  	OpenBatch(ctx context.Context, processingContext state.ProcessingContext, dbTx pgx.Tx) error
    61  	GetLastNBatches(ctx context.Context, numBatches uint, dbTx pgx.Tx) ([]*state.Batch, error)
    62  	StoreTransaction(ctx context.Context, batchNumber uint64, processedTx *state.ProcessTransactionResponse, coinbase common.Address, timestamp uint64, dbTx pgx.Tx) error
    63  	GetLastClosedBatch(ctx context.Context, dbTx pgx.Tx) (*state.Batch, error)
    64  	GetLastL2Block(ctx context.Context, dbTx pgx.Tx) (*types.Block, error)
    65  	GetLastBlock(ctx context.Context, dbTx pgx.Tx) (*state.Block, error)
    66  	GetLatestGlobalExitRoot(ctx context.Context, maxBlockNumber uint64, dbTx pgx.Tx) (state.GlobalExitRoot, time.Time, error)
    67  	GetLastL2BlockHeader(ctx context.Context, dbTx pgx.Tx) (*types.Header, error)
    68  	UpdateBatchL2Data(ctx context.Context, batchNumber uint64, batchL2Data []byte, dbTx pgx.Tx) error
    69  	ProcessSequencerBatch(ctx context.Context, batchNumber uint64, batchL2Data []byte, caller metrics.CallerLabel, dbTx pgx.Tx) (*state.ProcessBatchResponse, error)
    70  	GetForcedBatchesSince(ctx context.Context, forcedBatchNumber, maxBlockNumber uint64, dbTx pgx.Tx) ([]*state.ForcedBatch, error)
    71  	GetLastTrustedForcedBatchNumber(ctx context.Context, dbTx pgx.Tx) (uint64, error)
    72  	GetLatestVirtualBatchTimestamp(ctx context.Context, dbTx pgx.Tx) (time.Time, error)
    73  	CountReorgs(ctx context.Context, dbTx pgx.Tx) (uint64, error)
    74  	GetLatestGer(ctx context.Context, maxBlockNumber uint64) (state.GlobalExitRoot, time.Time, error)
    75  	FlushMerkleTree(ctx context.Context) error
    76  }
    77  
    78  type workerInterface interface {
    79  	GetBestFittingTx(resources state.BatchResources) *TxTracker
    80  	UpdateAfterSingleSuccessfulTxExecution(from common.Address, touchedAddresses map[common.Address]*state.InfoReadWrite) []*TxTracker
    81  	UpdateTx(txHash common.Hash, from common.Address, ZKCounters state.ZKCounters)
    82  	AddTxTracker(ctx context.Context, txTracker *TxTracker) (dropReason error, isWIP bool)
    83  	MoveTxToNotReady(txHash common.Hash, from common.Address, actualNonce *uint64, actualBalance *big.Int) []*TxTracker
    84  	DeleteTx(txHash common.Hash, from common.Address)
    85  	HandleL2Reorg(txHashes []common.Hash)
    86  	NewTxTracker(tx types.Transaction, counters state.ZKCounters, ip string) (*TxTracker, error)
    87  }
    88  
    89  // The dbManager will need to handle the errors inside the functions which don't return error as they will be used async in the other abstractions.
    90  // Also if dbTx is missing this needs also to be handled in the dbManager
    91  type dbManagerInterface interface {
    92  	OpenBatch(ctx context.Context, processingContext state.ProcessingContext, dbTx pgx.Tx) error
    93  	BeginStateTransaction(ctx context.Context) (pgx.Tx, error)
    94  	CreateFirstBatch(ctx context.Context, sequencerAddress common.Address) state.ProcessingContext
    95  	GetLastBatchNumber(ctx context.Context) (uint64, error)
    96  	StoreProcessedTransaction(ctx context.Context, batchNumber uint64, processedTx *state.ProcessTransactionResponse, coinbase common.Address, timestamp uint64, dbTx pgx.Tx) error
    97  	DeleteTransactionFromPool(ctx context.Context, txHash common.Hash) error
    98  	CloseBatch(ctx context.Context, params ClosingBatchParameters) error
    99  	GetWIPBatch(ctx context.Context) (*WipBatch, error)
   100  	GetTransactionsByBatchNumber(ctx context.Context, batchNumber uint64) (txs []types.Transaction, err error)
   101  	GetLastBatch(ctx context.Context) (*state.Batch, error)
   102  	GetLastNBatches(ctx context.Context, numBatches uint) ([]*state.Batch, error)
   103  	GetLastClosedBatch(ctx context.Context) (*state.Batch, error)
   104  	GetBatchByNumber(ctx context.Context, batchNumber uint64, dbTx pgx.Tx) (*state.Batch, error)
   105  	IsBatchClosed(ctx context.Context, batchNum uint64) (bool, error)
   106  	GetLatestGer(ctx context.Context, maxBlockNumber uint64) (state.GlobalExitRoot, time.Time, error)
   107  	ProcessForcedBatch(ForcedBatchNumber uint64, request state.ProcessRequest) (*state.ProcessBatchResponse, error)
   108  	GetForcedBatchesSince(ctx context.Context, forcedBatchNumber, maxBlockNumber uint64, dbTx pgx.Tx) ([]*state.ForcedBatch, error)
   109  	GetLastL2BlockHeader(ctx context.Context, dbTx pgx.Tx) (*types.Header, error)
   110  	GetLastBlock(ctx context.Context, dbTx pgx.Tx) (*state.Block, error)
   111  	GetLastTrustedForcedBatchNumber(ctx context.Context, dbTx pgx.Tx) (uint64, error)
   112  	GetBalanceByStateRoot(ctx context.Context, address common.Address, root common.Hash) (*big.Int, error)
   113  	UpdateTxStatus(ctx context.Context, hash common.Hash, newStatus pool.TxStatus, isWIP bool, reason *string) error
   114  	GetLatestVirtualBatchTimestamp(ctx context.Context, dbTx pgx.Tx) (time.Time, error)
   115  	CountReorgs(ctx context.Context, dbTx pgx.Tx) (uint64, error)
   116  	FlushMerkleTree(ctx context.Context) error
   117  }
   118  
   119  type ethTxManager interface {
   120  	Add(ctx context.Context, owner, id string, from common.Address, to *common.Address, value *big.Int, data []byte, dbTx pgx.Tx) error
   121  	Result(ctx context.Context, owner, id string, dbTx pgx.Tx) (ethtxmanager.MonitoredTxResult, error)
   122  	ResultsByStatus(ctx context.Context, owner string, statuses []ethtxmanager.MonitoredTxStatus, dbTx pgx.Tx) ([]ethtxmanager.MonitoredTxResult, error)
   123  	ProcessPendingMonitoredTxs(ctx context.Context, owner string, failedResultHandler ethtxmanager.ResultHandler, dbTx pgx.Tx)
   124  }