Skip to content

Commit

Permalink
Add evm config
Browse files Browse the repository at this point in the history
  • Loading branch information
mpetrun5 committed Feb 8, 2024
1 parent b8c9b71 commit fc7af4c
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 0 deletions.
33 changes: 33 additions & 0 deletions chains/evm/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// The Licensed Work is (c) 2023 Sygma
// SPDX-License-Identifier: LGPL-3.0-only

package config

import (
"fmt"

"github.com/kelseyhightower/envconfig"
"github.com/sygmaprotocol/sygma-inclusion-prover/config"
)

type EVMConfig struct {
config.BaseNetworkConfig
BeaconEndpoint string `required:"true" split_words:"true"`
Router string `required:"true"`
Executor string `required:"true"`
MaxGasPrice int64 `default:"500000000000" split_words:"true"`
GasMultiplier float64 `default:"1" split_words:"true"`
GasIncreasePercentage int64 `default:"15" split_words:"true"`
StateRootAddresses []string `required:"true" split_words:"true"`
}

// LoadEVMConfig loads EVM config from the environment and validates the fields
func LoadEVMConfig(domainID uint8) (*EVMConfig, error) {
var c EVMConfig
err := envconfig.Process(fmt.Sprintf("%s_DOMAINS_%d", config.PREFIX, domainID), &c)
if err != nil {
return nil, err
}

return &c, nil
}
96 changes: 96 additions & 0 deletions chains/evm/config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// The Licensed Work is (c) 2023 Sygma
// SPDX-License-Identifier: LGPL-3.0-only

package config_test

import (
"os"
"testing"

"github.com/stretchr/testify/suite"
"github.com/sygmaprotocol/sygma-inclusion-prover/chains/evm/config"
baseConfig "github.com/sygmaprotocol/sygma-inclusion-prover/config"
)

type EVMConfigTestSuite struct {
suite.Suite
}

func TestRunEVMConfigTestSuite(t *testing.T) {
suite.Run(t, new(EVMConfigTestSuite))
}

func (c *EVMConfigTestSuite) TearDownTest() {
os.Clearenv()
}

func (s *EVMConfigTestSuite) Test_LoadEVMConfig_MissingField() {
os.Setenv("INCLUSION_PROVER_DOMAINS_1_ENDPOINT", "http://endpoint.com")
os.Setenv("INCLUSION_PROVER_DOMAINS_1_KEY", "key")
os.Setenv("INCLUSION_PROVER_DOMAINS_1_SPECTRE", "spectre")
os.Setenv("INCLUSION_PROVER_DOMAINS_2_ROUTER", "invalid")

_, err := config.LoadEVMConfig(1)

s.NotNil(err)
}

func (s *EVMConfigTestSuite) Test_LoadEVMConfig_SuccessfulLoad_DefaultValues() {
os.Setenv("INCLUSION_PROVER_DOMAINS_1_ENDPOINT", "http://endpoint.com")
os.Setenv("INCLUSION_PROVER_DOMAINS_1_KEY", "key")
os.Setenv("INCLUSION_PROVER_DOMAINS_1_SPECTRE", "spectre")
os.Setenv("INCLUSION_PROVER_DOMAINS_1_ROUTER", "router")
os.Setenv("INCLUSION_PROVER_DOMAINS_1_EXECUTOR", "executor")
os.Setenv("INCLUSION_PROVER_DOMAINS_1_STATE_ROOT_ADDRESSES", "0x1,0x2")
os.Setenv("INCLUSION_PROVER_DOMAINS_1_BEACON_ENDPOINT", "endpoint")
os.Setenv("INCLUSION_PROVER_DOMAINS_2_ROUTER", "invalid")
os.Setenv("INCLUSION_PROVER_DOMAINS_1_STATE_ROOT_ADDRESSES", "0x1,0x2")

c, err := config.LoadEVMConfig(1)

s.Nil(err)
s.Equal(c, &config.EVMConfig{
BaseNetworkConfig: baseConfig.BaseNetworkConfig{
Key: "key",
Endpoint: "http://endpoint.com",
},
Router: "router",
Executor: "executor",
GasMultiplier: 1,
GasIncreasePercentage: 15,
MaxGasPrice: 500000000000,
BeaconEndpoint: "endpoint",
StateRootAddresses: []string{"0x1", "0x2"},
})
}

func (s *EVMConfigTestSuite) Test_LoadEVMConfig_SuccessfulLoad() {
os.Setenv("INCLUSION_PROVER_DOMAINS_1_ENDPOINT", "http://endpoint.com")
os.Setenv("INCLUSION_PROVER_DOMAINS_1_KEY", "key")
os.Setenv("INCLUSION_PROVER_DOMAINS_1_ROUTER", "router")
os.Setenv("INCLUSION_PROVER_DOMAINS_1_EXECUTOR", "executor")
os.Setenv("INCLUSION_PROVER_DOMAINS_1_BEACON_ENDPOINT", "endpoint")
os.Setenv("INCLUSION_PROVER_DOMAINS_1_MAX_GAS_PRICE", "1000")
os.Setenv("INCLUSION_PROVER_DOMAINS_1_BLOCK_INTERVAL", "10")
os.Setenv("INCLUSION_PROVER_DOMAINS_1_GAS_MULTIPLIER", "1")
os.Setenv("INCLUSION_PROVER_DOMAINS_1_GAS_INCREASE_PERCENTAGE", "20")
os.Setenv("INCLUSION_PROVER_DOMAINS_2_ROUTER", "invalid")
os.Setenv("INCLUSION_PROVER_DOMAINS_1_STATE_ROOT_ADDRESSES", "0x1,0x2")

c, err := config.LoadEVMConfig(1)

s.Nil(err)
s.Equal(c, &config.EVMConfig{
BaseNetworkConfig: baseConfig.BaseNetworkConfig{
Key: "key",
Endpoint: "http://endpoint.com",
},
Router: "router",
Executor: "executor",
GasMultiplier: 1,
GasIncreasePercentage: 20,
MaxGasPrice: 1000,
BeaconEndpoint: "endpoint",
StateRootAddresses: []string{"0x1", "0x2"},
})
}

0 comments on commit fc7af4c

Please sign in to comment.