Skip to content

Commit

Permalink
Make stateTree and executor optional in the state. (#1995)
Browse files Browse the repository at this point in the history
* make executor and statedb optional

* make executor and statedb optional

* make executor and statedb optional

* make executor and statedb optional

* make executor and statedb optional

* make executor and statedb optional

* make executor and statedb optional

* make executor and statedb optional

* make executor and statedb optional

* make executor and statedb optional

* make executor and statedb optional

* fix eventlog

* fix test

* fix method name
  • Loading branch information
ToniRamirezM committed Apr 11, 2023
1 parent 9076260 commit c6ad18a
Show file tree
Hide file tree
Showing 21 changed files with 1,719 additions and 1,770 deletions.
45 changes: 32 additions & 13 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,14 @@ import (
"github.com/0xPolygonHermez/zkevm-node/sequencer"
"github.com/0xPolygonHermez/zkevm-node/state"
"github.com/0xPolygonHermez/zkevm-node/state/runtime/executor"
executorpb "github.com/0xPolygonHermez/zkevm-node/state/runtime/executor/pb"
"github.com/0xPolygonHermez/zkevm-node/synchronizer"
"github.com/ethereum/go-ethereum/common"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/urfave/cli/v2"
)

const (
two = 2
ten = 10
)

func start(cliCtx *cli.Context) error {
c, err := config.Load(cliCtx)
if err != nil {
Expand Down Expand Up @@ -72,11 +68,22 @@ func start(cliCtx *cli.Context) error {
}
checkStateMigrations(c.StateDB)

// Prepare event log
// Decide if this node instance needs an executor and/or a state tree
var needsExecutor, needsStateTree bool

for _, component := range components {
switch component {
case SEQUENCER, RPC, SYNCHRONIZER:
needsExecutor = true
needsStateTree = true
}
}

// Event log
var eventLog *event.EventLog
var eventStorage event.Storage

if c.EventLog.DB.Name == "" {
if c.EventLog.DB.Name != "" {
eventStorage, err = pgeventstorage.NewPostgresEventStorage(c.EventLog.DB)
if err != nil {
log.Fatal(err)
Expand All @@ -87,9 +94,9 @@ func start(cliCtx *cli.Context) error {
log.Fatal(err)
}
}

eventLog = event.NewEventLog(c.EventLog, eventStorage)

// Core State DB
stateSqlDB, err := db.NewSQLDB(c.StateDB)
if err != nil {
log.Fatal(err)
Expand Down Expand Up @@ -123,7 +130,7 @@ func start(cliCtx *cli.Context) error {
log.Infof("Chain ID read from POE SC = %v", l2ChainID)

ctx := context.Background()
st := newState(ctx, c, l2ChainID, forkIDIntervals, stateSqlDB, eventLog)
st := newState(ctx, c, l2ChainID, forkIDIntervals, stateSqlDB, eventLog, needsExecutor, needsStateTree)

ethTxManagerStorage, err := ethtxmanager.NewPostgresStorage(c.StateDB)
if err != nil {
Expand Down Expand Up @@ -345,11 +352,21 @@ func waitSignal(cancelFuncs []context.CancelFunc) {
}
}

func newState(ctx context.Context, c *config.Config, l2ChainID uint64, forkIDIntervals []state.ForkIDInterval, sqlDB *pgxpool.Pool, eventLog *event.EventLog) *state.State {
func newState(ctx context.Context, c *config.Config, l2ChainID uint64, forkIDIntervals []state.ForkIDInterval, sqlDB *pgxpool.Pool, eventLog *event.EventLog, needsExecutor, needsStateTree bool) *state.State {
stateDb := state.NewPostgresStorage(sqlDB)
executorClient, _, _ := executor.NewExecutorClient(ctx, c.Executor)
stateDBClient, _, _ := merkletree.NewMTDBServiceClient(ctx, c.MTClient)
stateTree := merkletree.NewStateTree(stateDBClient)

// Executor
var executorClient executorpb.ExecutorServiceClient
if needsExecutor {
executorClient, _, _ = executor.NewExecutorClient(ctx, c.Executor)
}

// State Tree
var stateTree *merkletree.StateTree
if needsStateTree {
stateDBClient, _, _ := merkletree.NewMTDBServiceClient(ctx, c.MTClient)
stateTree = merkletree.NewStateTree(stateDBClient)
}

stateCfg := state.Config{
MaxCumulativeGasUsed: c.Sequencer.MaxCumulativeGasUsed,
Expand Down Expand Up @@ -388,6 +405,7 @@ func createEthTxManager(cfg config.Config, etmStorage *ethtxmanager.PostgresStor
}

func startProfilingHttpServer(c metrics.Config) {
const two = 2
mux := http.NewServeMux()
address := fmt.Sprintf("%s:%d", c.ProfilingHost, c.ProfilingPort)
lis, err := net.Listen("tcp", address)
Expand Down Expand Up @@ -417,6 +435,7 @@ func startProfilingHttpServer(c metrics.Config) {
}

func startMetricsHttpServer(c metrics.Config) {
const ten = 10
mux := http.NewServeMux()
address := fmt.Sprintf("%s:%d", c.Host, c.Port)
lis, err := net.Listen("tcp", address)
Expand Down
7 changes: 4 additions & 3 deletions sequencer/finalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/0xPolygonHermez/zkevm-node/pool"
"github.com/0xPolygonHermez/zkevm-node/sequencer/metrics"
"github.com/0xPolygonHermez/zkevm-node/state"
stateMetrics "github.com/0xPolygonHermez/zkevm-node/state/metrics"
"github.com/0xPolygonHermez/zkevm-node/state/runtime"
"github.com/0xPolygonHermez/zkevm-node/state/runtime/executor"
"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -568,7 +569,7 @@ func (f *finalizer) syncWithState(ctx context.Context, lastBatchNum *uint64) err
Coinbase: f.sequencerAddress,
Timestamp: f.batch.timestamp,
Transactions: make([]byte, 0, 1),
Caller: state.SequencerCallerLabel,
Caller: stateMetrics.SequencerCallerLabel,
}

log.Infof("synced with state, lastBatchNum: %d. State root: %s", *lastBatchNum, f.batch.initialStateRoot.Hex())
Expand Down Expand Up @@ -619,7 +620,7 @@ func (f *finalizer) processForcedBatch(lastBatchNumberInState uint64, stateRoot
Transactions: forcedBatch.RawTxsData,
Coinbase: f.sequencerAddress,
Timestamp: uint64(now().Unix()),
Caller: state.SequencerCallerLabel,
Caller: stateMetrics.SequencerCallerLabel,
}
response, err := f.dbManager.ProcessForcedBatch(forcedBatch.ForcedBatchNumber, processRequest)
if err != nil {
Expand Down Expand Up @@ -722,7 +723,7 @@ func (f *finalizer) reprocessFullBatch(ctx context.Context, batchNum uint64, exp
Transactions: batch.BatchL2Data,
Coinbase: batch.Coinbase,
Timestamp: uint64(batch.Timestamp.Unix()),
Caller: state.DiscardCallerLabel,
Caller: stateMetrics.DiscardCallerLabel,
}
log.Infof("reprocessFullBatch: BatchNumber: %d, OldStateRoot: %s, Ger: %s", batch.BatchNumber, f.batch.initialStateRoot.String(), batch.GlobalExitRoot.String())
txs, _, err := state.DecodeTxs(batch.BatchL2Data)
Expand Down
3 changes: 2 additions & 1 deletion sequencer/finalizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/0xPolygonHermez/zkevm-node/event/nileventstorage"
"github.com/0xPolygonHermez/zkevm-node/pool"
"github.com/0xPolygonHermez/zkevm-node/state"
stateMetrics "github.com/0xPolygonHermez/zkevm-node/state/metrics"
"github.com/0xPolygonHermez/zkevm-node/state/runtime/executor"
"github.com/0xPolygonHermez/zkevm-node/state/runtime/executor/pb"
"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -517,7 +518,7 @@ func TestFinalizer_processForcedBatches(t *testing.T) {
Transactions: forcedBatch.RawTxsData,
Coinbase: f.sequencerAddress,
Timestamp: uint64(now().Unix()),
Caller: state.SequencerCallerLabel,
Caller: stateMetrics.SequencerCallerLabel,
}
dbManagerMock.On("ProcessForcedBatch", forcedBatch.ForcedBatchNumber, processRequest).Return(&state.ProcessBatchResponse{
NewStateRoot: stateRoot,
Expand Down
5 changes: 3 additions & 2 deletions sequencer/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/0xPolygonHermez/zkevm-node/ethtxmanager"
"github.com/0xPolygonHermez/zkevm-node/pool"
"github.com/0xPolygonHermez/zkevm-node/state"
"github.com/0xPolygonHermez/zkevm-node/state/metrics"
pb "github.com/0xPolygonHermez/zkevm-node/state/runtime/executor/pb"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
Expand Down Expand Up @@ -68,7 +69,7 @@ type stateInterface interface {
GetLatestGlobalExitRoot(ctx context.Context, maxBlockNumber uint64, dbTx pgx.Tx) (state.GlobalExitRoot, time.Time, error)
GetLastL2BlockHeader(ctx context.Context, dbTx pgx.Tx) (*types.Header, error)
UpdateBatchL2Data(ctx context.Context, batchNumber uint64, batchL2Data []byte, dbTx pgx.Tx) error
ProcessSequencerBatch(ctx context.Context, batchNumber uint64, batchL2Data []byte, caller state.CallerLabel, dbTx pgx.Tx) (*state.ProcessBatchResponse, error)
ProcessSequencerBatch(ctx context.Context, batchNumber uint64, batchL2Data []byte, caller metrics.CallerLabel, dbTx pgx.Tx) (*state.ProcessBatchResponse, error)
GetForcedBatchesSince(ctx context.Context, forcedBatchNumber, maxBlockNumber uint64, dbTx pgx.Tx) ([]*state.ForcedBatch, error)
GetLastTrustedForcedBatchNumber(ctx context.Context, dbTx pgx.Tx) (uint64, error)
GetLatestVirtualBatchTimestamp(ctx context.Context, dbTx pgx.Tx) (time.Time, error)
Expand Down Expand Up @@ -138,7 +139,7 @@ type dbManagerStateInterface interface {
GetBatchByNumber(ctx context.Context, batchNumber uint64, dbTx pgx.Tx) (*state.Batch, error)
UpdateBatchL2Data(ctx context.Context, batchNumber uint64, batchL2Data []byte, dbTx pgx.Tx) error
GetForcedBatch(ctx context.Context, forcedBatchNumber uint64, dbTx pgx.Tx) (*state.ForcedBatch, error)
ProcessSequencerBatch(ctx context.Context, batchNumber uint64, batchL2Data []byte, caller state.CallerLabel, dbTx pgx.Tx) (*state.ProcessBatchResponse, error)
ProcessSequencerBatch(ctx context.Context, batchNumber uint64, batchL2Data []byte, caller metrics.CallerLabel, dbTx pgx.Tx) (*state.ProcessBatchResponse, error)
GetForcedBatchesSince(ctx context.Context, forcedBatchNumber, maxBlockNumber uint64, dbTx pgx.Tx) ([]*state.ForcedBatch, error)
GetLastTrustedForcedBatchNumber(ctx context.Context, dbTx pgx.Tx) (uint64, error)
GetBalanceByStateRoot(ctx context.Context, address common.Address, root common.Hash) (*big.Int, error)
Expand Down
10 changes: 6 additions & 4 deletions sequencer/mock_state.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions sequencer/sequencer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"github.com/0xPolygonHermez/zkevm-node/event"
"github.com/0xPolygonHermez/zkevm-node/log"
"github.com/0xPolygonHermez/zkevm-node/pool"
"github.com/0xPolygonHermez/zkevm-node/sequencer/metrics"
"github.com/0xPolygonHermez/zkevm-node/state"
"github.com/0xPolygonHermez/zkevm-node/state/metrics"
"github.com/ethereum/go-ethereum/common"
)

Expand Down Expand Up @@ -225,7 +225,7 @@ func (s *Sequencer) bootstrap(ctx context.Context, dbManager *dbManager, finaliz
GlobalExitRoot: processingCtx.GlobalExitRoot,
Coinbase: processingCtx.Coinbase,
Timestamp: timestamp,
Caller: state.SequencerCallerLabel,
Caller: metrics.SequencerCallerLabel,
}
currBatch = &WipBatch{
globalExitRoot: processingCtx.GlobalExitRoot,
Expand Down
Loading

0 comments on commit c6ad18a

Please sign in to comment.