Skip to content

Commit

Permalink
wip:2
Browse files Browse the repository at this point in the history
Signed-off-by: Nikolay Nedkov <[email protected]>
  • Loading branch information
Psykepro committed Dec 23, 2022
1 parent b19bf97 commit ada278d
Show file tree
Hide file tree
Showing 20 changed files with 394 additions and 299 deletions.
11 changes: 5 additions & 6 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"github.com/0xPolygonHermez/zkevm-node/metrics"
"github.com/0xPolygonHermez/zkevm-node/pool"
"github.com/0xPolygonHermez/zkevm-node/pool/pgpoolstorage"
"github.com/0xPolygonHermez/zkevm-node/pricegetter"
"github.com/0xPolygonHermez/zkevm-node/sequencer"
"github.com/0xPolygonHermez/zkevm-node/sequencer/broadcast"
"github.com/0xPolygonHermez/zkevm-node/sequencer/broadcast/pb"
Expand Down Expand Up @@ -171,12 +170,12 @@ func runJSONRPCServer(c config.Config, pool *pool.Pool, st *state.State, gpe gas

func createSequencer(c config.Config, pool *pool.Pool, state *state.State, etherman *etherman.Client,
ethTxManager *ethtxmanager.Client, gpe gasPriceEstimator) *sequencer.Sequencer {
pg, err := pricegetter.NewClient(c.PriceGetter)
if err != nil {
log.Fatal(err)
}
//pg, err := pricegetter.NewClient(c.PriceGetter)
//if err != nil {
// log.Fatal(err)
//}

seq, err := sequencer.New(c.Sequencer, pool, state, etherman, pg, ethTxManager, gpe)
seq, err := sequencer.New(c.Sequencer, pool, state, etherman, ethTxManager)
if err != nil {
log.Fatal(err)
}
Expand Down
6 changes: 3 additions & 3 deletions pool/pgpoolstorage/pgpoolstorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,10 @@ func (p *PostgresPoolStorage) GetTxs(ctx context.Context, filterStatus pool.TxSt
var (
encoded, status string
receivedAt time.Time
cumulativeGasUsed int64
cumulativeGasUsed uint64

usedKeccakHashes, usedPoseidonHashes, usedPoseidonPaddings,
usedMemAligns, usedArithmetics, usedBinaries, usedSteps int32
usedMemAligns, usedArithmetics, usedBinaries, usedSteps uint32
nonce, failedCounter uint64
)

Expand Down Expand Up @@ -276,7 +276,7 @@ func (p *PostgresPoolStorage) GetTxs(ctx context.Context, filterStatus pool.TxSt

tx.Status = pool.TxStatus(status)
tx.ReceivedAt = receivedAt
tx.ZkCounters = pool.ZkCounters{
tx.ZKCounters = state.ZKCounters{
CumulativeGasUsed: cumulativeGasUsed,
UsedKeccakHashes: usedKeccakHashes,
UsedPoseidonHashes: usedPoseidonHashes,
Expand Down
52 changes: 3 additions & 49 deletions pool/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"strings"
"time"

"github.com/0xPolygonHermez/zkevm-node/state"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
)
Expand Down Expand Up @@ -32,59 +34,11 @@ type Transaction struct {
types.Transaction
Status TxStatus
IsClaims bool
ZkCounters
state.ZKCounters
FailedCounter uint64
ReceivedAt time.Time
}

// ZkCounters counters for the tx
type ZkCounters struct {
CumulativeGasUsed uint64
UsedKeccakHashes uint32
UsedPoseidonHashes uint32
UsedPoseidonPaddings uint32
UsedMemAligns uint32
UsedArithmetics uint32
UsedBinaries uint32
UsedSteps uint32
}

// IsZkCountersBelowZero checks if any of the counters are below zero
func (zkc *ZkCounters) IsZkCountersBelowZero() bool {
return zkc.CumulativeGasUsed < 0 ||
zkc.UsedArithmetics < 0 ||
zkc.UsedSteps < 0 ||
zkc.UsedBinaries < 0 ||
zkc.UsedMemAligns < 0 ||
zkc.UsedPoseidonPaddings < 0 ||
zkc.UsedPoseidonHashes < 0 ||
zkc.UsedKeccakHashes < 0
}

// SumUpZkCounters sum ups zk counters with passed tx zk counters
func (zkc *ZkCounters) SumUpZkCounters(txZkCounters ZkCounters) {
zkc.CumulativeGasUsed += txZkCounters.CumulativeGasUsed
zkc.UsedKeccakHashes += txZkCounters.UsedKeccakHashes
zkc.UsedPoseidonHashes += txZkCounters.UsedPoseidonHashes
zkc.UsedPoseidonPaddings += txZkCounters.UsedPoseidonPaddings
zkc.UsedMemAligns += txZkCounters.UsedMemAligns
zkc.UsedArithmetics += txZkCounters.UsedArithmetics
zkc.UsedBinaries += txZkCounters.UsedBinaries
zkc.UsedSteps += txZkCounters.UsedSteps
}

// SubZkCounters subtract zk counters with passed zk counters (not safe)
func (zkc *ZkCounters) SubZkCounters(txZkCounters ZkCounters) {
zkc.CumulativeGasUsed -= txZkCounters.CumulativeGasUsed
zkc.UsedKeccakHashes -= txZkCounters.UsedKeccakHashes
zkc.UsedPoseidonHashes -= txZkCounters.UsedPoseidonHashes
zkc.UsedPoseidonPaddings -= txZkCounters.UsedPoseidonPaddings
zkc.UsedMemAligns -= txZkCounters.UsedMemAligns
zkc.UsedArithmetics -= txZkCounters.UsedArithmetics
zkc.UsedBinaries -= txZkCounters.UsedBinaries
zkc.UsedSteps -= txZkCounters.UsedSteps
}

// IsClaimTx checks, if tx is a claim tx
func (tx *Transaction) IsClaimTx(l2BridgeAddr common.Address) bool {
if tx.To() == nil {
Expand Down
4 changes: 2 additions & 2 deletions sequencer/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ type Config struct {
// WaitBlocksToUpdateGER is number of blocks for sequencer to wait
WaitBlocksToUpdateGER uint64 `mapstructure:"WaitBlocksToUpdateGER"`

// WaitBlocksToConsiderGerFinal is number of blocks for sequencer to consider GER final
// WaitBlocksToConsiderGerFinal is number of blocks for sequencer to consider globalExitRoot final
WaitBlocksToConsiderGerFinal uint64 `mapstructure:"WaitBlocksToConsiderGerFinal"`

// ElapsedTimeToCloseBatchWithoutTxsDueToNewGER it's time to close a batch bcs new GER appeared
// ElapsedTimeToCloseBatchWithoutTxsDueToNewGER it's time to close a batch bcs new globalExitRoot appeared
ElapsedTimeToCloseBatchWithoutTxsDueToNewGER types.Duration `mapstructure:"ElapsedTimeToCloseBatchWithoutTxsDueToNewGER"`

// MaxTimeForBatchToBeOpen is time after which new batch should be closed
Expand Down
80 changes: 76 additions & 4 deletions sequencer/dbmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package sequencer
import (
"context"
"errors"
"time"

"github.com/0xPolygonHermez/zkevm-node/log"
"github.com/0xPolygonHermez/zkevm-node/state"
"github.com/ethereum/go-ethereum/common"
"github.com/jackc/pgx/v4"
Expand All @@ -24,6 +26,38 @@ func (d *dbManager) Start() {
go d.loadFromPool()
}

func (d *dbManager) GetLastBatchNumber(ctx context.Context) (uint64, error) {
// TODO: Fetch last BatchNumber from database
return 0, errors.New("")
}

func (d *dbManager) CreateFirstBatch(ctx context.Context, sequencerAddress common.Address) state.ProcessingContext {
processingCtx := state.ProcessingContext{
BatchNumber: 1,
Coinbase: sequencerAddress,
Timestamp: time.Now(),
GlobalExitRoot: state.ZeroHash,
}
dbTx, err := d.state.BeginStateTransaction(ctx)
if err != nil {
log.Fatalf("failed to begin state transaction for opening a batch, err: %v", err)
}
err = d.state.OpenBatch(ctx, processingCtx, dbTx)
if err != nil {
if rollbackErr := dbTx.Rollback(ctx); rollbackErr != nil {
log.Fatalf(
"failed to rollback dbTx when opening batch that gave err: %v. Rollback err: %v",
rollbackErr, err,
)
}
log.Fatalf("failed to open a batch, err: %v", err)
}
if err := dbTx.Commit(ctx); err != nil {
log.Fatalf("failed to commit dbTx when opening batch, err: %v", err)
}
return processingCtx
}

func (d *dbManager) loadFromPool() {
// TODO: Endless loop that keeps loading tx from the DB into the worker
}
Expand Down Expand Up @@ -69,11 +103,49 @@ func (d *dbManager) StoreProcessedTxAndDeleteFromPool(ctx context.Context, batch
}
}

func (d *dbManager) CloseBatch(ctx context.Context, receipt state.ProcessingReceipt) {
func (d *dbManager) GetWIPBatch(ctx context.Context) (wipBatch, error) {
// TODO: Make this method to return ready WIP batch it has following cases:
// if lastBatch IS OPEN - load data from it but set wipBatch.initialStateRoot to Last Closed Batch
// if lastBatch IS CLOSED - open new batch in the database and load all data from the closed one without the txs and increase batch number
return wipBatch{}, errors.New("")
}

func (d *dbManager) GetLastClosedBatch(ctx context.Context) (state.Batch, error) {
// TODO: Returns last closed batch
return state.Batch{}, errors.New("")
}

func (d *dbManager) GetLastBatch(ctx context.Context) (state.Batch, error) {
// TODO: Returns last batch
return state.Batch{}, errors.New("")

}

func (d *dbManager) IsBatchClosed(ctx context.Context, batchNum uint64) (bool, error) {
// TODO: Returns if the batch with passed batchNum is closed
return false, errors.New("")
}

func (d *dbManager) GetLastNBatches(ctx context.Context, numBatches uint) ([]*state.Batch, error) {
// TODO: Returns last N batches
return []*state.Batch{}, errors.New("")

}

// ClosingBatchParameters contains the necessary parameters to close a batch
type ClosingBatchParameters struct {
BatchNumber uint64
StateRoot common.Hash
LocalExitRoot common.Hash
AccInputHash common.Hash
Txs []TxTracker
}

func (d *dbManager) CloseBatch(ctx context.Context, params ClosingBatchParameters) {
// TODO: Close current open batch
}

func (d *dbManager) GetLastBatch(ctx context.Context) (*state.Batch, error) {
// TODO: Get last batch
return nil, errors.New("")
func (d *dbManager) MarkReorgedTxsAsPending(ctx context.Context) error {
// TODO: call pool.MarkReorgedTxsAsPending and return result
return errors.New("")
}
4 changes: 2 additions & 2 deletions sequencer/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ package sequencer
import "errors"

var (
// ErrBatchRemainingResourcesOverflow error returned when the resources for the current batch are overflown
ErrBatchRemainingResourcesOverflow = errors.New("overflow of remaining resources for current batch %s")
// ErrBatchRemainingResourcesUnderflow error returned when there is underflow of the resources for the current batch
ErrBatchRemainingResourcesUnderflow = errors.New("underflow of remaining resources for current batch %s")
)
Loading

0 comments on commit ada278d

Please sign in to comment.