Skip to content

Commit

Permalink
Merge pull request 0xPolygonHermez#2298 from 0xPolygonHermez/bugfix/i…
Browse files Browse the repository at this point in the history
…p-validation

Add IP and ZKCounter Validation, Refactor Batch Configs
  • Loading branch information
Psykepro authored Aug 18, 2023
2 parents 181d16f + a6a1243 commit b8f23d2
Show file tree
Hide file tree
Showing 44 changed files with 1,467 additions and 758 deletions.
2 changes: 1 addition & 1 deletion cmd/dumpstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func dumpState(ctx *cli.Context) error {
}

// Connect to SQL
stateSqlDB, err := db.NewSQLDB(c.StateDB)
stateSqlDB, err := db.NewSQLDB(c.State.DB)
if err != nil {
return err
}
Expand Down
14 changes: 7 additions & 7 deletions cmd/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,25 @@ func restore(ctx *cli.Context) error {
}

// Run migrations to create schemas and tables
runStateMigrations(c.StateDB)
runStateMigrations(c.State.DB)

port, err := strconv.Atoi(c.StateDB.Port)
port, err := strconv.Atoi(c.State.DB.Port)
if err != nil {
log.Error("error converting port to int. Error: ", err)
return err
}
restore, err := pg.NewRestore(&pg.Postgres{
Host: c.StateDB.Host,
Host: c.State.DB.Host,
Port: port,
DB: c.StateDB.Name,
Username: c.StateDB.User,
Password: c.StateDB.Password,
DB: c.State.DB.Name,
Username: c.State.DB.User,
Password: c.State.DB.Password,
})
if err != nil {
log.Error("error: ", err)
return err
}
restore.Role = c.StateDB.User
restore.Role = c.State.DB.User
restore.Schemas = append(restore.Schemas, "state")
log.Info("Restore stateDB snapshot started, please wait...")
restoreExec := restore.Exec(inputFileStateDB, pg.ExecOptions{StreamPrint: false})
Expand Down
28 changes: 14 additions & 14 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ func start(cliCtx *cli.Context) error {
if !cliCtx.Bool(config.FlagMigrations) {
for _, comp := range components {
if comp == SYNCHRONIZER {
runStateMigrations(c.StateDB)
runStateMigrations(c.State.DB)
}
}
}
checkStateMigrations(c.StateDB)
checkStateMigrations(c.State.DB)

// Decide if this node instance needs an executor and/or a state tree
var needsExecutor, needsStateTree bool
Expand Down Expand Up @@ -96,7 +96,7 @@ func start(cliCtx *cli.Context) error {
eventLog = event.NewEventLog(c.EventLog, eventStorage)

// Core State DB
stateSqlDB, err := db.NewSQLDB(c.StateDB)
stateSqlDB, err := db.NewSQLDB(c.State.DB)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -133,7 +133,7 @@ func start(cliCtx *cli.Context) error {
ctx := context.Background()
st := newState(ctx, c, l2ChainID, forkIDIntervals, stateSqlDB, eventLog, needsExecutor, needsStateTree)

ethTxManagerStorage, err := ethtxmanager.NewPostgresStorage(c.StateDB)
ethTxManagerStorage, err := ethtxmanager.NewPostgresStorage(c.State.DB)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -170,7 +170,7 @@ func start(cliCtx *cli.Context) error {
log.Fatal(err)
}
if poolInstance == nil {
poolInstance = createPool(c.Pool, l2ChainID, st, eventLog)
poolInstance = createPool(c.Pool, c.State.Batch.Constraints, l2ChainID, st, eventLog)
}
seq := createSequencer(*c, poolInstance, ethTxManagerStorage, st, eventLog)
go seq.Start(ctx)
Expand All @@ -182,7 +182,7 @@ func start(cliCtx *cli.Context) error {
log.Fatal(err)
}
if poolInstance == nil {
poolInstance = createPool(c.Pool, l2ChainID, st, eventLog)
poolInstance = createPool(c.Pool, c.State.Batch.Constraints, l2ChainID, st, eventLog)
}
seqSender := createSequenceSender(*c, poolInstance, ethTxManagerStorage, st, eventLog)
go seqSender.Start(ctx)
Expand All @@ -194,7 +194,7 @@ func start(cliCtx *cli.Context) error {
log.Fatal(err)
}
if poolInstance == nil {
poolInstance = createPool(c.Pool, l2ChainID, st, eventLog)
poolInstance = createPool(c.Pool, c.State.Batch.Constraints, l2ChainID, st, eventLog)
}
if c.RPC.EnableL2SuggestedGasPricePolling {
// Needed for rejecting transactions with too low gas price
Expand All @@ -213,7 +213,7 @@ func start(cliCtx *cli.Context) error {
log.Fatal(err)
}
if poolInstance == nil {
poolInstance = createPool(c.Pool, l2ChainID, st, eventLog)
poolInstance = createPool(c.Pool, c.State.Batch.Constraints, l2ChainID, st, eventLog)
}
go runSynchronizer(*c, etherman, etm, st, poolInstance, eventLog)
case ETHTXMANAGER:
Expand All @@ -233,7 +233,7 @@ func start(cliCtx *cli.Context) error {
log.Fatal(err)
}
if poolInstance == nil {
poolInstance = createPool(c.Pool, l2ChainID, st, eventLog)
poolInstance = createPool(c.Pool, c.State.Batch.Constraints, l2ChainID, st, eventLog)
}
go runL2GasPriceSuggester(c.L2GasPriceSuggester, st, poolInstance, etherman)
}
Expand Down Expand Up @@ -315,7 +315,7 @@ func runSynchronizer(cfg config.Config, etherman *etherman.Client, ethTxManager
func runJSONRPCServer(c config.Config, etherman *etherman.Client, chainID uint64, pool *pool.Pool, st *state.State, apis map[string]bool) {
var err error
storage := jsonrpc.NewStorage()
c.RPC.MaxCumulativeGasUsed = c.Sequencer.MaxCumulativeGasUsed
c.RPC.MaxCumulativeGasUsed = c.State.Batch.Constraints.MaxCumulativeGasUsed
if !c.IsTrustedSequencer {
if c.RPC.SequencerNodeURI == "" {
log.Debug("getting trusted sequencer URL from smc")
Expand Down Expand Up @@ -383,7 +383,7 @@ func createSequencer(cfg config.Config, pool *pool.Pool, etmStorage *ethtxmanage

ethTxManager := ethtxmanager.New(cfg.EthTxManager, etherman, etmStorage, st)

seq, err := sequencer.New(cfg.Sequencer, pool, st, etherman, ethTxManager, eventLog)
seq, err := sequencer.New(cfg.Sequencer, cfg.State.Batch, pool, st, etherman, ethTxManager, eventLog)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -466,7 +466,7 @@ func newState(ctx context.Context, c *config.Config, l2ChainID uint64, forkIDInt
}

stateCfg := state.Config{
MaxCumulativeGasUsed: c.Sequencer.MaxCumulativeGasUsed,
MaxCumulativeGasUsed: c.State.Batch.Constraints.MaxCumulativeGasUsed,
ChainID: l2ChainID,
ForkIDIntervals: forkIDIntervals,
MaxResourceExhaustedAttempts: c.Executor.MaxResourceExhaustedAttempts,
Expand All @@ -479,13 +479,13 @@ func newState(ctx context.Context, c *config.Config, l2ChainID uint64, forkIDInt
return st
}

func createPool(cfgPool pool.Config, l2ChainID uint64, st *state.State, eventLog *event.EventLog) *pool.Pool {
func createPool(cfgPool pool.Config, constraintsCfg state.BatchConstraintsCfg, l2ChainID uint64, st *state.State, eventLog *event.EventLog) *pool.Pool {
runPoolMigrations(cfgPool.DB)
poolStorage, err := pgpoolstorage.NewPostgresPoolStorage(cfgPool.DB)
if err != nil {
log.Fatal(err)
}
poolInstance := pool.NewPool(cfgPool, poolStorage, st, l2ChainID, eventLog)
poolInstance := pool.NewPool(cfgPool, constraintsCfg, poolStorage, st, l2ChainID, eventLog)
return poolInstance
}

Expand Down
10 changes: 5 additions & 5 deletions cmd/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ func snapshot(ctx *cli.Context) error {
}
setupLog(c.Log)

port, err := strconv.Atoi(c.StateDB.Port)
port, err := strconv.Atoi(c.State.DB.Port)
if err != nil {
log.Error("error converting port to int. Error: ", err)
return err
}
dump, err := pg.NewDump(&pg.Postgres{
Host: c.StateDB.Host,
Host: c.State.DB.Host,
Port: port,
DB: c.StateDB.Name,
Username: c.StateDB.User,
Password: c.StateDB.Password,
DB: c.State.DB.Name,
Username: c.State.DB.User,
Password: c.State.DB.Password,
})
if err != nil {
log.Error("error: ", err)
Expand Down
5 changes: 3 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/0xPolygonHermez/zkevm-node/pool"
"github.com/0xPolygonHermez/zkevm-node/sequencer"
"github.com/0xPolygonHermez/zkevm-node/sequencesender"
"github.com/0xPolygonHermez/zkevm-node/state"
"github.com/0xPolygonHermez/zkevm-node/state/runtime/executor"
"github.com/0xPolygonHermez/zkevm-node/synchronizer"
"github.com/mitchellh/mapstructure"
Expand Down Expand Up @@ -108,14 +109,14 @@ type Config struct {
Executor executor.Config
// Configuration of the merkle tree client service. Not use in the node, only for testing
MTClient merkletree.Config
// Configuration of the state database connection
StateDB db.Config
// Configuration of the metrics service, basically is where is going to publish the metrics
Metrics metrics.Config
// Configuration of the event database connection
EventLog event.Config
// Configuration of the hash database connection
HashDB db.Config
// State service configuration
State state.Config
}

// Default parses the default configuration values.
Expand Down
131 changes: 84 additions & 47 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,6 @@ func Test_Defaults(t *testing.T) {
path: "Sequencer.WaitPeriodPoolIsEmpty",
expectedValue: types.NewDuration(1 * time.Second),
},
{
path: "Sequencer.MaxTxsPerBatch",
expectedValue: uint64(300),
},
{
path: "Sequencer.MaxBatchBytesSize",
expectedValue: uint64(120000),
},
{
path: "Sequencer.BlocksAmountForTxsToBeDeleted",
expectedValue: uint64(100),
Expand All @@ -60,38 +52,6 @@ func Test_Defaults(t *testing.T) {
path: "Sequencer.FrequencyToCheckTxsForDelete",
expectedValue: types.NewDuration(12 * time.Hour),
},
{
path: "Sequencer.MaxCumulativeGasUsed",
expectedValue: uint64(30000000),
},
{
path: "Sequencer.MaxKeccakHashes",
expectedValue: uint32(2145),
},
{
path: "Sequencer.MaxPoseidonHashes",
expectedValue: uint32(252357),
},
{
path: "Sequencer.MaxPoseidonPaddings",
expectedValue: uint32(135191),
},
{
path: "Sequencer.MaxMemAligns",
expectedValue: uint32(236585),
},
{
path: "Sequencer.MaxArithmetics",
expectedValue: uint32(236585),
},
{
path: "Sequencer.MaxBinaries",
expectedValue: uint32(473170),
},
{
path: "Sequencer.MaxSteps",
expectedValue: uint32(7570538),
},
{
path: "Sequencer.TxLifetimeCheckTimeout",
expectedValue: types.NewDuration(10 * time.Minute),
Expand Down Expand Up @@ -245,31 +205,31 @@ func Test_Defaults(t *testing.T) {
expectedValue: "zkevm-prover:50061",
},
{
path: "StateDB.User",
path: "State.DB.User",
expectedValue: "state_user",
},
{
path: "StateDB.Password",
path: "State.DB.Password",
expectedValue: "state_password",
},
{
path: "StateDB.Name",
path: "State.DB.Name",
expectedValue: "state_db",
},
{
path: "StateDB.Host",
path: "State.DB.Host",
expectedValue: "zkevm-state-db",
},
{
path: "StateDB.Port",
path: "State.DB.Port",
expectedValue: "5432",
},
{
path: "StateDB.EnableLog",
path: "State.DB.EnableLog",
expectedValue: false,
},
{
path: "StateDB.MaxConns",
path: "State.DB.MaxConns",
expectedValue: 200,
},
{
Expand Down Expand Up @@ -437,6 +397,83 @@ func Test_Defaults(t *testing.T) {
path: "Aggregator.GeneratingProofCleanupThreshold",
expectedValue: "10m",
},

{
path: "State.Batch.Constraints.MaxTxsPerBatch",
expectedValue: uint64(300),
},
{
path: "State.Batch.Constraints.MaxBatchBytesSize",
expectedValue: uint64(120000),
},
{
path: "State.Batch.Constraints.MaxCumulativeGasUsed",
expectedValue: uint64(30000000),
},
{
path: "State.Batch.Constraints.MaxKeccakHashes",
expectedValue: uint32(2145),
},
{
path: "State.Batch.Constraints.MaxPoseidonHashes",
expectedValue: uint32(252357),
},
{
path: "State.Batch.Constraints.MaxPoseidonPaddings",
expectedValue: uint32(135191),
},
{
path: "State.Batch.Constraints.MaxMemAligns",
expectedValue: uint32(236585),
},
{
path: "State.Batch.Constraints.MaxArithmetics",
expectedValue: uint32(236585),
},
{
path: "State.Batch.Constraints.MaxBinaries",
expectedValue: uint32(473170),
},
{
path: "State.Batch.Constraints.MaxSteps",
expectedValue: uint32(7570538),
},
{
path: "State.Batch.ResourceWeights.WeightBatchBytesSize",
expectedValue: 1,
},
{
path: "State.Batch.ResourceWeights.WeightCumulativeGasUsed",
expectedValue: 1,
},
{
path: "State.Batch.ResourceWeights.WeightKeccakHashes",
expectedValue: 1,
},
{
path: "State.Batch.ResourceWeights.WeightPoseidonHashes",
expectedValue: 1,
},
{
path: "State.Batch.ResourceWeights.WeightPoseidonPaddings",
expectedValue: 1,
},
{
path: "State.Batch.ResourceWeights.WeightMemAligns",
expectedValue: 1,
},
{
path: "State.Batch.ResourceWeights.WeightArithmetics",
expectedValue: 1,
},
{
path: "State.Batch.ResourceWeights.WeightBinaries",
expectedValue: 1,
},
{
path: "State.Batch.ResourceWeights.WeightSteps",
expectedValue: 1,
},
}
file, err := os.CreateTemp("", "genesisConfig")
require.NoError(t, err)
Expand Down
Loading

0 comments on commit b8f23d2

Please sign in to comment.