Skip to content

Commit

Permalink
Feature/0xPolygonHermez#1145 genesis refactor (0xPolygonHermez#1211)
Browse files Browse the repository at this point in the history
* Wip

* chainID from smc

* smc addresses

* linter

* docker compose

* chainID

* fix merge

* minor refactor

* e2e group 2 fixed
  • Loading branch information
ARR552 authored Oct 18, 2022
1 parent 8104066 commit db001a9
Show file tree
Hide file tree
Showing 22 changed files with 206 additions and 1,114 deletions.
6 changes: 3 additions & 3 deletions cmd/approve.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func approveTokens(ctx *cli.Context) error {

if !ctx.Bool(config.FlagYes) {
fmt.Print("*WARNING* Are you sure you want to approve ", amount,
" tokens to be spent by the smc <Name: PoE. Address: "+c.NetworkConfig.PoEAddr.String()+">? [y/N]: ")
" tokens to be spent by the smc <Name: PoE. Address: "+c.Etherman.PoEAddr.String()+">? [y/N]: ")
var input string
if _, err := fmt.Scanln(&input); err != nil {
return err
Expand Down Expand Up @@ -52,7 +52,7 @@ func approveTokens(ctx *cli.Context) error {
amountInWei := new(big.Float).Mul(amount, big.NewFloat(decimals))
amountB := new(big.Int)
amountInWei.Int(amountB)
tx, err := etherman.ApproveMatic(amountB, c.NetworkConfig.PoEAddr)
tx, err := etherman.ApproveMatic(amountB, c.Etherman.PoEAddr)
if err != nil {
return err
}
Expand All @@ -61,7 +61,7 @@ func approveTokens(ctx *cli.Context) error {
rinkeby = 4
goerli = 5
)
switch c.NetworkConfig.L1ChainID {
switch c.Etherman.L1ChainID {
case mainnet:
fmt.Println("Check tx status: https://etherscan.io/tx/" + tx.Hash().String())
case rinkeby:
Expand Down
3 changes: 0 additions & 3 deletions cmd/dumpstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ var dumpStateFlags = []cli.Flag{
Required: true,
},
&configFileFlag,
&networkFlag,
&customNetworkFlag,
&baseNetworkFlag,
}

type dumpedState struct {
Expand Down
27 changes: 6 additions & 21 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,11 @@ var (
Usage: "Configuration `FILE`",
Required: false,
}
networkFlag = cli.StringFlag{
Name: config.FlagNetwork,
Aliases: []string{"n"},
Usage: "Network: mainnet, testnet, internaltestnet, local, custom, merge. By default it uses mainnet",
Required: false,
}
customNetworkFlag = cli.StringFlag{
Name: config.FlagNetworkCfg,
Aliases: []string{"nc"},
Usage: "Custom network configuration `FILE` when using --network custom parameter",
}
baseNetworkFlag = cli.StringFlag{
Name: config.FlagNetworkBase,
Aliases: []string{"nb"},
Usage: "Base existing network configuration to be merged with the custom configuration passed with --network-cfg, by default it uses internaltestnet",
Value: "internaltestnet",
Required: false,
genesisFlag = cli.StringFlag{
Name: config.FlagGenesisFile,
Aliases: []string{"gen"},
Usage: "Loads the genesis `FILE`",
Required: true,
}
yesFlag = cli.BoolFlag{
Name: config.FlagYes,
Expand Down Expand Up @@ -87,9 +75,6 @@ func main() {
app.Version = version
flags := []cli.Flag{
&configFileFlag,
&networkFlag,
&customNetworkFlag,
&baseNetworkFlag,
&yesFlag,
&componentsFlag,
&httpAPIFlag,
Expand All @@ -106,7 +91,7 @@ func main() {
Aliases: []string{},
Usage: "Run the zkevm-node",
Action: start,
Flags: flags,
Flags: append(flags, &genesisFlag),
},
{
Name: "approve",
Expand Down
52 changes: 22 additions & 30 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"os"
"os/signal"
"path/filepath"
"strings"

"github.com/0xPolygonHermez/zkevm-node/aggregator"
"github.com/0xPolygonHermez/zkevm-node/config"
Expand All @@ -29,6 +28,7 @@ import (
"github.com/0xPolygonHermez/zkevm-node/synchronizer"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/common"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/urfave/cli/v2"
"google.golang.org/grpc"
Expand All @@ -52,48 +52,41 @@ func start(cliCtx *cli.Context) error {
etherman *etherman.Client
)

if strings.Contains(cliCtx.String(config.FlagComponents), AGGREGATOR) ||
strings.Contains(cliCtx.String(config.FlagComponents), SEQUENCER) ||
strings.Contains(cliCtx.String(config.FlagComponents), SYNCHRONIZER) ||
strings.Contains(cliCtx.String(config.FlagComponents), RPC) {
var err error
etherman, err = newEtherman(*c)
if err != nil {
log.Fatal(err)
}

// READ CHAIN ID FROM POE SC
chainID, err := etherman.GetL2ChainID()
if err != nil {
log.Fatal(err)
}
etherman, err = newEtherman(*c)
if err != nil {
log.Fatal(err)
}

c.NetworkConfig.L2ChainID = chainID
log.Infof("Chain ID read from POE SC = %v", c.NetworkConfig.L2ChainID)
// READ CHAIN ID FROM POE SC
l2ChainID, err := etherman.GetL2ChainID()
if err != nil {
log.Fatal(err)
}
c.Aggregator.ChainID = l2ChainID
c.RPC.ChainID = l2ChainID
log.Infof("Chain ID read from POE SC = %v", l2ChainID)

ctx := context.Background()
st := newState(ctx, c, stateSqlDB)
st := newState(ctx, c, l2ChainID, stateSqlDB)

ethTxManager := ethtxmanager.New(c.EthTxManager, etherman)

for _, item := range cliCtx.StringSlice(config.FlagComponents) {
switch item {
case AGGREGATOR:
log.Info("Running aggregator")
c.Aggregator.ChainID = c.NetworkConfig.L2ChainID
c.Aggregator.ProverURIs = c.Provers.ProverURIs
go runAggregator(ctx, c.Aggregator, etherman, ethTxManager, st, grpcClientConns)
case SEQUENCER:
log.Info("Running sequencer")
poolInstance := createPool(c.PoolDB, c.NetworkConfig, st)
poolInstance := createPool(c.PoolDB, c.NetworkConfig.L2BridgeAddr, l2ChainID, st)
gpe := createGasPriceEstimator(c.GasPriceEstimator, st, poolInstance)
seq := createSequencer(*c, poolInstance, st, etherman, ethTxManager, gpe)
go seq.Start(ctx)
case RPC:
log.Info("Running JSON-RPC server")
runRPCMigrations(c.RPC.DB)
poolInstance := createPool(c.PoolDB, c.NetworkConfig, st)
poolInstance := createPool(c.PoolDB, c.NetworkConfig.L2BridgeAddr, l2ChainID, st)
gpe := createGasPriceEstimator(c.GasPriceEstimator, st, poolInstance)
apis := map[string]bool{}
for _, a := range cliCtx.StringSlice(config.FlagHTTPAPI) {
Expand Down Expand Up @@ -138,19 +131,19 @@ func runMigrations(c db.Config, name string) {
}

func newEtherman(c config.Config) (*etherman.Client, error) {
auth, err := newAuthFromKeystore(c.Etherman.PrivateKeyPath, c.Etherman.PrivateKeyPassword, c.NetworkConfig.L1ChainID)
auth, err := newAuthFromKeystore(c.Etherman.PrivateKeyPath, c.Etherman.PrivateKeyPassword, c.Etherman.L1ChainID)
if err != nil {
return nil, err
}
etherman, err := etherman.NewClient(c.Etherman, auth, c.NetworkConfig.PoEAddr, c.NetworkConfig.MaticAddr, c.NetworkConfig.GlobalExitRootManagerAddr)
etherman, err := etherman.NewClient(c.Etherman, auth)
if err != nil {
return nil, err
}
return etherman, nil
}

func runSynchronizer(cfg config.Config, etherman *etherman.Client, st *state.State) {
sy, err := synchronizer.NewSynchronizer(cfg.IsTrustedSequencer, etherman, st, cfg.NetworkConfig.GenBlockNumber, cfg.NetworkConfig.Genesis, cfg.Synchronizer)
sy, err := synchronizer.NewSynchronizer(cfg.IsTrustedSequencer, etherman, st, cfg.NetworkConfig.Genesis, cfg.Synchronizer)
if err != nil {
log.Fatal(err)
}
Expand All @@ -166,7 +159,6 @@ func runJSONRPCServer(c config.Config, pool *pool.Pool, st *state.State, gpe gas
}

c.RPC.MaxCumulativeGasUsed = c.Sequencer.MaxCumulativeGasUsed
c.RPC.ChainID = c.NetworkConfig.L2ChainID

if err := jsonrpc.NewServer(c.RPC, pool, st, gpe, storage, apis).Start(); err != nil {
log.Fatal(err)
Expand Down Expand Up @@ -275,27 +267,27 @@ func newAuthFromKeystore(path, password string, chainID uint64) (*bind.TransactO
return auth, nil
}

func newState(ctx context.Context, c *config.Config, sqlDB *pgxpool.Pool) *state.State {
func newState(ctx context.Context, c *config.Config, l2ChainID uint64, sqlDB *pgxpool.Pool) *state.State {
stateDb := state.NewPostgresStorage(sqlDB)
executorClient, _, _ := executor.NewExecutorClient(ctx, c.Executor)
stateDBClient, _, _ := merkletree.NewMTDBServiceClient(ctx, c.MTClient)
stateTree := merkletree.NewStateTree(stateDBClient)

stateCfg := state.Config{
MaxCumulativeGasUsed: c.Sequencer.MaxCumulativeGasUsed,
ChainID: c.NetworkConfig.L2ChainID,
ChainID: l2ChainID,
}

st := state.NewState(stateCfg, stateDb, executorClient, stateTree)
return st
}

func createPool(poolDBConfig db.Config, networkConfig config.NetworkConfig, st *state.State) *pool.Pool {
func createPool(poolDBConfig db.Config, l2BridgeAddr common.Address, l2ChainID uint64, st *state.State) *pool.Pool {
runPoolMigrations(poolDBConfig)
poolStorage, err := pgpoolstorage.NewPostgresPoolStorage(poolDBConfig)
if err != nil {
log.Fatal(err)
}
poolInstance := pool.NewPool(poolStorage, st, networkConfig.L2BridgeAddr, networkConfig.L2ChainID)
poolInstance := pool.NewPool(poolStorage, st, l2BridgeAddr, l2ChainID)
return poolInstance
}
5 changes: 5 additions & 0 deletions config/config.debug.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ MaxConns = 10

[Etherman]
URL = "http://localhost:8545"
L1ChainID = 1337
PrivateKeyPath = "../test/test.keystore"
PrivateKeyPassword = "testonly"
PoEAddr = "0x2279B7A0a67DB372996a5FaB50D91eAA73d2eBe6"
MaticAddr = "0x5FbDB2315678afecb367f032d93F642f64180aa3"
GlobalExitRootManagerAddr = "0xDc64a140Aa3E981100a9becA4E685f962f0cF6C9"

[EthTxManager]
MaxSendBatchTxRetries = 10
Expand Down Expand Up @@ -56,6 +60,7 @@ DefaultSenderAddress = "0x1111111111111111111111111111111111111111"
SyncInterval = "5s"
SyncChunkSize = 100
TrustedSequencerURI = ""
GenBlockNumber = 1

[Sequencer]
MaxSequenceSize = "2000000"
Expand Down
8 changes: 2 additions & 6 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,8 @@ const (
FlagYes = "yes"
// FlagCfg is the flag for cfg.
FlagCfg = "cfg"
// FlagNetwork is the flag for network.
FlagNetwork = "network"
// FlagNetworkCfg is the flag for network-cfg.
FlagNetworkCfg = "network-cfg"
// FlagNetworkBase is the flag for netwotk-base.
FlagNetworkBase = "network-base"
// FlagGenesisFile is the flag for genesis file.
FlagGenesisFile = "genesis"
// FlagAmount is the flag for amount.
FlagAmount = "amount"
// FlagRemoteMT is the flag for remote-merkletree.
Expand Down
5 changes: 5 additions & 0 deletions config/config.local.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ MaxConns = 200

[Etherman]
URL = "http://zkevm-mock-l1-network:8545"
L1ChainID = 1337
PrivateKeyPath = "./test/test.keystore"
PrivateKeyPassword = "testonly"
PoEAddr = "0x2279B7A0a67DB372996a5FaB50D91eAA73d2eBe6"
MaticAddr = "0x5FbDB2315678afecb367f032d93F642f64180aa3"
GlobalExitRootManagerAddr = "0xDc64a140Aa3E981100a9becA4E685f962f0cF6C9"

[EthTxManager]
MaxSendBatchTxRetries = 10
Expand Down Expand Up @@ -56,6 +60,7 @@ DefaultSenderAddress = "0x1111111111111111111111111111111111111111"
SyncInterval = "1s"
SyncChunkSize = 100
TrustedSequencerURI = ""
GenBlockNumber = 1

[Sequencer]
MaxSequenceSize = "2000000"
Expand Down
12 changes: 11 additions & 1 deletion config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package config_test

import (
"flag"
"io/ioutil"
"math/big"
"os"
"reflect"
"strings"
"testing"
Expand Down Expand Up @@ -262,8 +264,16 @@ func Test_Defaults(t *testing.T) {
expectedValue: 61090,
},
}
file, err := ioutil.TempFile("", "genesisConfig")
require.NoError(t, err)
defer func() {
require.NoError(t, os.Remove(file.Name()))
}()
require.NoError(t, os.WriteFile(file.Name(), []byte("{}"), 0600))

ctx := cli.NewContext(cli.NewApp(), flag.NewFlagSet("", flag.PanicOnError), nil)
flagSet := flag.NewFlagSet("", flag.PanicOnError)
flagSet.String(config.FlagGenesisFile, file.Name(), "")
ctx := cli.NewContext(cli.NewApp(), flagSet, nil)
cfg, err := config.Load(ctx)
if err != nil {
t.Fatalf("Unexpected error loading default config: %v", err)
Expand Down
5 changes: 5 additions & 0 deletions config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ MaxConns = 200
[Etherman]
URL = "http://localhost:8545"
L1ChainID = 1337
PrivateKeyPath = "./test/test.keystore"
PrivateKeyPassword = "testonly"
PoEAddr = "0x2279B7A0a67DB372996a5FaB50D91eAA73d2eBe6"
MaticAddr = "0x5FbDB2315678afecb367f032d93F642f64180aa3"
GlobalExitRootManagerAddr = "0xDc64a140Aa3E981100a9becA4E685f962f0cF6C9"
[EthTxManager]
MaxSendBatchTxRetries = 10
Expand Down Expand Up @@ -60,6 +64,7 @@ DefaultSenderAddress = "0x1111111111111111111111111111111111111111"
SyncInterval = "0s"
SyncChunkSize = 100
TrustedSequencerURI = ""
GenBlockNumber = 1
[Sequencer]
MaxSequenceSize = "2000000"
Expand Down
423 changes: 0 additions & 423 deletions config/genesis.go

This file was deleted.

84 changes: 84 additions & 0 deletions config/genesis.local.json

Large diffs are not rendered by default.

Loading

0 comments on commit db001a9

Please sign in to comment.