Skip to content

Commit

Permalink
feat: configuration setup (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
mpetrun5 committed Oct 24, 2023
1 parent eb0acb4 commit 435cf46
Show file tree
Hide file tree
Showing 9 changed files with 228 additions and 6 deletions.
9 changes: 6 additions & 3 deletions .github/workflows/license.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
# The Licensed Work is (c) 2023 Sygma
# SPDX-License-Identifier: LGPL-3.0-only

on: [pull_request]
name: License check
env:
GO111MODULE: on

jobs:
license-check:
runs-on: ubuntu-latest
container: golang:1.19
steps:
- uses: actions/checkout@v2

- run: make license
- run: go install github.com/google/addlicense@latest

- name: license updated check
run: git diff --exit-code
- run: make license-check
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ all: help
license:
@echo " > \033[32mAdding license headers...\033[0m "
GO111MODULE=off go get -u github.com/google/addlicense
addlicense -v -c "Sygma" -f ./scripts/header.txt -y 2021 -ignore ".idea/**" .
addlicense -v -c "Sygma" -f ./scripts/header.txt -y 2023 -ignore ".idea/**" .

## license-check: Checks for missing license headers
license-check:
@echo " > \033[Checking for license headers...\033[0m "
GO111MODULE=off go get -u github.com/google/addlicense
addlicense -check -c "Sygma" -f ./scripts/header.txt -y 2021 -ignore ".idea/**" .

coverage:
go tool cover -func cover.out | grep total | awk '{print $3}'
Expand Down
29 changes: 29 additions & 0 deletions chains/evm/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// 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/spectre-node/config"
)

type EVMConfig struct {
config.BaseNetworkConfig
Router string `required:"true"`
Executor string `required:"true"`
BlockConfirmations uint8 `default:"5"`
}

// 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
}
56 changes: 56 additions & 0 deletions chains/evm/config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// 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/spectre-node/chains/evm/config"
baseConfig "github.com/sygmaprotocol/spectre-node/config"
)

type EVMConfigTestSuite struct {
suite.Suite
}

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

func (s *EVMConfigTestSuite) Test_LoadEVMConfig_MissingField() {
os.Setenv("SPECTRE_DOMAINS_1_ID", "1")
os.Setenv("SPECTRE_DOMAINS_1_ENDPOINT", "http://endpoint.com")
os.Setenv("SPECTRE_DOMAINS_1_KEY", "key")
os.Setenv("SPECTRE_DOMAINS_1_EXECUTOR", "executor")
os.Setenv("SPECTRE_DOMAINS_2_ROUTER", "invalid")

_, err := config.LoadEVMConfig(1)

s.NotNil(err)
}

func (s *EVMConfigTestSuite) Test_LoadEVMConfig_SuccessfulLoad() {
os.Setenv("SPECTRE_DOMAINS_1_ID", "1")
os.Setenv("SPECTRE_DOMAINS_1_ENDPOINT", "http://endpoint.com")
os.Setenv("SPECTRE_DOMAINS_1_KEY", "key")
os.Setenv("SPECTRE_DOMAINS_1_EXECUTOR", "executor")
os.Setenv("SPECTRE_DOMAINS_1_ROUTER", "router")
os.Setenv("SPECTRE_DOMAINS_2_ROUTER", "invalid")

c, err := config.LoadEVMConfig(1)

s.Nil(err)
s.Equal(c, &config.EVMConfig{
BaseNetworkConfig: baseConfig.BaseNetworkConfig{
ID: 1,
Key: "key",
Endpoint: "http://endpoint.com",
},
Router: "router",
Executor: "executor",
BlockConfirmations: 5,
})
}
33 changes: 33 additions & 0 deletions 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 "github.com/kelseyhightower/envconfig"

const PREFIX = "SPECTRE"

type Config struct {
Observability *Observability `env_config:"observability"`
Prover *Prover `env_config:"prover"`
}

type Observability struct {
LogLevel string `default:"debug" split_words:"true"`
LogFile string `default:"out.log" split_words:"true"`
}

type Prover struct {
URL string `required:"true"`
}

// LoadConfig loads config from the environment and validates the fields
func LoadConfig() (*Config, error) {
var c Config
err := envconfig.Process(PREFIX, &c)
if err != nil {
return nil, err
}

return &c, nil
}
66 changes: 66 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// 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/spectre-node/config"
)

type ConfigTestSuite struct {
suite.Suite
}

func TestRunConfigTestSuite(t *testing.T) {
suite.Run(t, new(ConfigTestSuite))
}

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

func (s *ConfigTestSuite) Test_LoadConfig_MissingField() {
_, err := config.LoadConfig()

s.NotNil(err)
}

func (s *ConfigTestSuite) Test_LoadConfig_DefaultValues() {
os.Setenv("SPECTRE_PROVER_URL", "http://prover.com")

c, err := config.LoadConfig()

s.Nil(err)
s.Equal(c, &config.Config{
Observability: &config.Observability{
LogLevel: "debug",
LogFile: "out.log",
},
Prover: &config.Prover{
URL: "http://prover.com",
},
})
}

func (s *ConfigTestSuite) Test_LoadEVMConfig_SuccessfulLoad() {
os.Setenv("SPECTRE_OBSERVABILITY_LOG_LEVEL", "info")
os.Setenv("SPECTRE_OBSERVABILITY_LOG_FILE", "out2.log")
os.Setenv("SPECTRE_PROVER_URL", "http://prover.com")

c, err := config.LoadConfig()

s.Nil(err)
s.Equal(c, &config.Config{
Observability: &config.Observability{
LogLevel: "info",
LogFile: "out2.log",
},
Prover: &config.Prover{
URL: "http://prover.com",
},
})
}
10 changes: 10 additions & 0 deletions config/network.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// The Licensed Work is (c) 2023 Sygma
// SPDX-License-Identifier: LGPL-3.0-only

package config

type BaseNetworkConfig struct {
ID uint8 `required:"true"`
Endpoint string `required:"true"`
Key string `required:"true"`
}
11 changes: 9 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,12 @@ module github.com/sygmaprotocol/spectre-node
go 1.19

require (

)
github.com/kelseyhightower/envconfig v1.4.0
github.com/stretchr/testify v1.8.4
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8=
github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 comments on commit 435cf46

Please sign in to comment.