Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/73 support validium #75

Merged
merged 22 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
56fcbfc
new contract DA and update etrog to validium one (that extents zkevm,…
joanestebanr Jun 13, 2024
bd9dfdc
Revert "new contract DA and update etrog to validium one (that extent…
joanestebanr Jun 13, 2024
a057f41
add new contracts
joanestebanr Jun 13, 2024
cc86203
add validium contract for etrog
joanestebanr Jun 14, 2024
bd90236
refactor decode batches
joanestebanr Jun 14, 2024
e7ca9a3
add validium for etrog
joanestebanr Jun 17, 2024
eebe6eb
elderberry validium is not implemented, just returns an error
joanestebanr Jun 17, 2024
90bb862
can configure data source priorities, allow to translate URL form dat…
joanestebanr Jun 17, 2024
d9f996b
add validium/elderberry support, check config file unknown values
joanestebanr Jun 19, 2024
c7b5ffc
v0.6.0
joanestebanr Jun 19, 2024
4f83dca
Merge branch 'develop' into feature/73-support_validium
joanestebanr Jun 19, 2024
b58f2e3
fix unittest and lint
joanestebanr Jun 19, 2024
25af054
undo change on main()
joanestebanr Jun 19, 2024
47f73a9
fix Incorrect conversion between integer types
joanestebanr Jun 19, 2024
9b3746f
fix Incorrect conversion between integer types
joanestebanr Jun 19, 2024
88b512b
fix Incorrect conversion between integer types
joanestebanr Jun 19, 2024
6ca088b
reduce duplication
joanestebanr Jun 19, 2024
c508d51
reduce duplication
joanestebanr Jun 19, 2024
73530be
unittest decode batch etrog!
joanestebanr Jun 19, 2024
1a2d6a9
add more unittest
joanestebanr Jun 20, 2024
ade397a
Merge branch 'develop' into feature/73-support_validium
joanestebanr Jun 20, 2024
271ff04
fix unittest
joanestebanr Jun 20, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/run/run_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func RunCmd(cliCtx *cli.Context) error {
log.Error("Error creating synchronizer", err)
return err
}
err = sync.Sync(false)
err = sync.Sync(true)
if err != nil {
log.Error("Error syncing", err)
} else {
Expand Down
90 changes: 64 additions & 26 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package config

import (
"bytes"
"fmt"
"os"
"path/filepath"
"strings"

Expand All @@ -19,6 +21,8 @@ const (
FlagYes = "yes"
// FlagCfg is the flag for cfg.
FlagCfg = "cfg"
// EnvPrefix is the prefix for the environment variables.
EnvVarPrefix = "ZKEVM_SYNCL1"
)

type Config struct {
Expand Down Expand Up @@ -52,47 +56,81 @@ func Load(ctx *cli.Context) (*Config, error) {
}

// Load loads the configuration
func LoadFile(configFilePath string) (*Config, error) {
cfg, err := Default()
func LoadFileFromString(configFileData string, configType string) (*Config, error) {
cfg := &Config{}
err := loadString(cfg, DefaultValues, "toml", true, EnvVarPrefix, nil)
if err != nil {
return nil, err
}
expectedKeys := viper.AllKeys()
err = loadString(cfg, configFileData, configType, true, EnvVarPrefix, &expectedKeys)
if err != nil {
return nil, err
}
return cfg, nil
}

if configFilePath != "" {
dirName, fileName := filepath.Split(configFilePath)

fileExtension := strings.TrimPrefix(filepath.Ext(fileName), ".")
fileNameWithoutExtension := strings.TrimSuffix(fileName, "."+fileExtension)

viper.AddConfigPath(dirName)
viper.SetConfigName(fileNameWithoutExtension)
viper.SetConfigType(fileExtension)
// Load loads the configuration
func LoadFile(configFilePath string) (*Config, error) {
_, fileName := filepath.Split(configFilePath)
fileExtension := strings.TrimPrefix(filepath.Ext(fileName), ".")
configData, err := os.ReadFile(configFilePath)
if err != nil {
return nil, err
}
viper.AutomaticEnv()
replacer := strings.NewReplacer(".", "_")
viper.SetEnvKeyReplacer(replacer)
viper.SetEnvPrefix("ZKEVM_SYNCL1")
err = viper.ReadInConfig()
cfg, err := LoadFileFromString(string(configData), fileExtension)
if err != nil {
_, ok := err.(viper.ConfigFileNotFoundError)
if ok {
log.Infof("config file not found %s", configFilePath)
return nil, err
} else {
log.Infof("error reading config file: ", err)
return nil, err
}
return nil, err
}
return cfg, nil
}

// Load loads the configuration
func loadString(cfg *Config, configData string, configType string, allowEnvVars bool, envPrefix string, expectedKeys *[]string) error {
viper.SetConfigType(configType)
if allowEnvVars {
replacer := strings.NewReplacer(".", "_")
viper.SetEnvKeyReplacer(replacer)
viper.SetEnvPrefix(envPrefix)
viper.AutomaticEnv()
}
err := viper.ReadConfig(bytes.NewBuffer([]byte(configData)))
if err != nil {
return err
}
decodeHooks := []viper.DecoderConfigOption{
// this allows arrays to be decoded from env var separated by ",", example: MY_VAR="value1,value2,value3"
viper.DecodeHook(mapstructure.ComposeDecodeHookFunc(mapstructure.TextUnmarshallerHookFunc(), mapstructure.StringToSliceHookFunc(","))),
}

err = viper.Unmarshal(&cfg, decodeHooks...)
if err != nil {
return nil, err
return err
}
if expectedKeys != nil {
configKeys := viper.AllKeys()
err = checkUnknownFieldsOnFile(configKeys, *expectedKeys)
if err != nil {
return err
}
}
return nil
}

return cfg, nil
func checkUnknownFieldsOnFile(keysOnFile, expectedConfigKeys []string) error {
for _, key := range keysOnFile {
if !contains(expectedConfigKeys, key) {
return fmt.Errorf("unknown field %s on config file", key)
}
}
return nil
}

func contains(keys []string, key string) bool {
for _, k := range keys {
if k == key {
return true
}
}
return false
}
65 changes: 65 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package config_test

import (
"testing"

"github.com/0xPolygonHermez/zkevm-synchronizer-l1/config"
"github.com/stretchr/testify/require"
)

const ConfigFileWithUnknownFieldTest = `
[Log]
Environment = "development" # "production" or "development"
Level = "info"
Outputs = ["stderr"]
ThisIsAnUnknownField = "unknown"
`
const ConfigFileOkTest = `
[Log]
Environment = "development" # "production" or "development"
Level = "info"
Outputs = ["stderr"]
#ThisIsAnUnknownField = "unknown"
`

const ConfigFileValidiumTranslatorTest = `
[Etherman.Validium.Translator]
FullMatchRules = [
{Old="http://dataavailability-003.cdk-validium-cardona-03.zkevm.polygon.private:8444", New="https://dataavailability-003-cdk-validium-cardona-03-zkevm.polygondev.tools"},
{Old="http://dataavailability-002.cdk-validium-cardona-03.zkevm.polygon.private:8444", New="https://dataavailability-002-cdk-validium-cardona-03-zkevm.polygondev.tools"},
{Old="http://dataavailability-001.cdk-validium-cardona-03.zkevm.polygon.private:8444", New="https://dataavailability-001-cdk-validium-cardona-01-zkevm.polygondev.tools"}
]
`

const ConfigFileValidiumTranslatorWrongFieldsInMapTest = `
[Etherman.Validium.Translator]
FullMatchRules = [
{NoExpectedField="http://dataavailability-003.cdk-validium-cardona-03.zkevm.polygon.private:8444", New="https://dataavailability-003-cdk-validium-cardona-03-zkevm.polygondev.tools"},
{Old="http://dataavailability-002.cdk-validium-cardona-03.zkevm.polygon.private:8444", New="https://dataavailability-002-cdk-validium-cardona-03-zkevm.polygondev.tools"},
{Old="http://dataavailability-001.cdk-validium-cardona-03.zkevm.polygon.private:8444", New="https://dataavailability-001-cdk-validium-cardona-01-zkevm.polygondev.tools"}
]
`

func TestLoadConfigUnknownFieldFails(t *testing.T) {
fileExtension := "toml"
_, err := config.LoadFileFromString(string(ConfigFileWithUnknownFieldTest), fileExtension)
require.Error(t, err)
}

func TestLoadConfigUnknownFieldInMapFails(t *testing.T) {
t.Skip("This test is not working as expected, need a deep dive into Viper library")
fileExtension := "toml"
_, err := config.LoadFileFromString(string(ConfigFileValidiumTranslatorWrongFieldsInMapTest), fileExtension)
require.Error(t, err)
}

func TestLoadConfigCommentedUnknownFieldOk(t *testing.T) {
fileExtension := "toml"
_, err := config.LoadFileFromString(string(ConfigFileOkTest), fileExtension)
require.NoError(t, err)
}
func TestLoadConfigValdiumTranslatorOk(t *testing.T) {
fileExtension := "toml"
_, err := config.LoadFileFromString(string(ConfigFileValidiumTranslatorTest), fileExtension)
require.NoError(t, err)
}
16 changes: 13 additions & 3 deletions config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,20 @@ const DefaultValues = `
GenesisBlockNumber = 0
SyncUpToBlock = "latest"
BlockFinality = "finalized"
OverrideStorageCheck = false
[Etherman]
L1URL = "http://localhost:8545"
ForkIDChunkSize = 100
L1ChainID = 0
PararellBlockRequest = false
[Etherman.Contracts]
GlobalExitRootManagerAddr = "0x2968D6d736178f8FE7393CC33C87f29D9C287e78"
RollupManagerAddr = "0xE2EF6215aDc132Df6913C8DD16487aBF118d1764"
ZkEVMAddr = "0x89BA0Ed947a88fe43c22Ae305C0713eC8a7Eb361"
GlobalExitRootManagerAddr = "0x2968D6d736178f8FE7393CC33C87f29D9C287e78"
RollupManagerAddr = "0xE2EF6215aDc132Df6913C8DD16487aBF118d1764"
ZkEVMAddr = "0x89BA0Ed947a88fe43c22Ae305C0713eC8a7Eb361"
[Etherman.Validium]
Enabled = false
TrustedSequencerURL = ""
DataSourcePriority = ["trusted", "external"]
[Etherman.Validium.Translator]
FullMatchRules = []
`
12 changes: 11 additions & 1 deletion config/default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import (

"github.com/0xPolygonHermez/zkevm-synchronizer-l1/config"
"github.com/0xPolygonHermez/zkevm-synchronizer-l1/config/types"
"github.com/0xPolygonHermez/zkevm-synchronizer-l1/dataavailability"
"github.com/0xPolygonHermez/zkevm-synchronizer-l1/etherman"
"github.com/0xPolygonHermez/zkevm-synchronizer-l1/log"
storage "github.com/0xPolygonHermez/zkevm-synchronizer-l1/state/storage"
syncconfig "github.com/0xPolygonHermez/zkevm-synchronizer-l1/synchronizer/config"
"github.com/0xPolygonHermez/zkevm-synchronizer-l1/translator"
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -38,12 +40,20 @@ func TestDefault(t *testing.T) {
OverrideStorageCheck: false,
},
Etherman: etherman.Config{
L1URL: "http://localhost:8545",
L1URL: "http://localhost:8545",
ForkIDChunkSize: 100,
L1ChainID: 0,
Contracts: etherman.ContractConfig{
GlobalExitRootManagerAddr: common.HexToAddress("0x2968D6d736178f8FE7393CC33C87f29D9C287e78"),
RollupManagerAddr: common.HexToAddress("0xE2EF6215aDc132Df6913C8DD16487aBF118d1764"),
ZkEVMAddr: common.HexToAddress("0x89BA0Ed947a88fe43c22Ae305C0713eC8a7Eb361"),
},
Validium: etherman.ValidiumConfig{
Enabled: false,
TrustedSequencerURL: "",
DataSourcePriority: []dataavailability.DataSourcePriority{dataavailability.Trusted, dataavailability.External},
Translator: translator.Config{},
},
},
}
cfg, err := config.Default()
Expand Down
9 changes: 9 additions & 0 deletions dataavailability/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package dataavailability

// DABackendType is the data availability protocol for the CDK
type DABackendType string

const (
// DataAvailabilityCommittee is the DAC protocol backend
DataAvailabilityCommittee DABackendType = "DataAvailabilityCommittee"
)
Loading
Loading