Skip to content

Commit

Permalink
support default config file overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickhuie19 committed Aug 9, 2024
1 parent 3015b53 commit d9c6b25
Show file tree
Hide file tree
Showing 3 changed files with 480 additions and 0 deletions.
71 changes: 71 additions & 0 deletions core/chains/evm/config/toml/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package toml
import (
"bytes"
"embed"
"fmt"
"io"
"log"
"os"
"path/filepath"
"slices"
"strings"
Expand All @@ -15,17 +18,23 @@ import (
)

var (
CUSTOM_DEFAULTS_ENV_KEY = "CL_CHAIN_DEFAULTS"

//go:embed defaults/*.toml
defaultsFS embed.FS
fallback Chain
defaults = map[string]Chain{}
defaultNames = map[string]string{}

customDefaults = map[string]Chain{}

// DefaultIDs is the set of chain ids which have defaults.
DefaultIDs []*big.Big
)

func init() {
// read the defaults first

fes, err := defaultsFS.ReadDir("defaults")
if err != nil {
log.Fatalf("failed to read defaults/: %v", err)
Expand Down Expand Up @@ -65,6 +74,65 @@ func init() {
slices.SortFunc(DefaultIDs, func(a, b *big.Big) int {
return a.Cmp(b)
})

// read the custom defaults overrides
dir := os.Getenv(CUSTOM_DEFAULTS_ENV_KEY)
if dir == "" {
// short-circuit; no default overrides provided
return
}

// Read directory contents
entries, err := os.ReadDir(dir)
if err != nil {
fmt.Println("Error reading directory:", err)
return
}

for _, entry := range entries {
if entry.IsDir() {
// Skip directories
continue
}

path := dir + "/" + entry.Name()
file, err := os.Open(path)
if err != nil {
fmt.Println("Error opening file:", err)
continue
}

// Read file contents
b, err := io.ReadAll(file)
if err != nil {
fmt.Println("Error reading file:", err)
file.Close()
continue
}
file.Close()

var config = struct {
ChainID *big.Big
Chain
}{}

if err := cconfig.DecodeTOML(bytes.NewReader(b), &config); err != nil {
log.Fatalf("failed to decode %q: %v", path, err)
}

if config.ChainID == nil {
log.Fatalf("missing ChainID: %s", path)
}

id := config.ChainID.String()
// these custom defaults are only meant to be overrides on existing chains;
// if the chain does not exist already in defaults error out
if _, ok := defaults[id]; !ok {
log.Fatalf("%q does not contain ChainID: %s", path, id)
}
customDefaults[id] = config.Chain
}

}

Check failure on line 136 in core/chains/evm/config/toml/defaults.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary trailing newline (whitespace)

// DefaultsNamed returns the default Chain values, optionally for the given chainID, as well as a name if the chainID is known.
Expand All @@ -78,6 +146,9 @@ func DefaultsNamed(chainID *big.Big) (c Chain, name string) {
c.SetFrom(&d)
name = defaultNames[s]
}
if overrides, ok := customDefaults[s]; ok {
c.SetFrom(&overrides)
}
return
}

Expand Down
4 changes: 4 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ func TestMain(m *testing.M) {
}))
}

// TestScripts walks through the testdata/scripts directory and runs all tests that end in
// .txt or .txtar with the testscripts library. To run an individual test, specify it in the
// -run param of go test without the txtar or txt suffix, like so:
// go test . -run TestScripts/node/validate/default
func TestScripts(t *testing.T) {
if testing.Short() {
t.Skip("skipping testscript")
Expand Down
Loading

0 comments on commit d9c6b25

Please sign in to comment.