This commit is contained in:
klim0v 2025-09-15 01:42:22 +08:00
parent 1ff3345d13
commit 0e0ddea458
3 changed files with 59 additions and 24 deletions

View file

@ -20,10 +20,10 @@ import (
"crypto/sha256" "crypto/sha256"
"fmt" "fmt"
"math" "math"
"math/big"
"os" "os"
"slices" "slices"
"sort" "sort"
"strconv"
"strings" "strings"
"github.com/ethereum/go-ethereum/beacon/merkle" "github.com/ethereum/go-ethereum/beacon/merkle"
@ -90,11 +90,7 @@ func (c *ChainConfig) AddFork(name string, epoch uint64, version []byte) *ChainC
// LoadForks parses the beacon chain configuration file (config.yaml) and extracts // LoadForks parses the beacon chain configuration file (config.yaml) and extracts
// the list of forks. // the list of forks.
func (c *ChainConfig) LoadForks(path string) error { func (c *ChainConfig) LoadForks(file []byte) error {
file, err := os.ReadFile(path)
if err != nil {
return fmt.Errorf("failed to read beacon chain config file: %v", err)
}
config := make(map[string]any) config := make(map[string]any)
if err := yaml.Unmarshal(file, &config); err != nil { if err := yaml.Unmarshal(file, &config); err != nil {
return fmt.Errorf("failed to parse beacon chain config file: %v", err) return fmt.Errorf("failed to parse beacon chain config file: %v", err)
@ -108,26 +104,29 @@ func (c *ChainConfig) LoadForks(path string) error {
for key, value := range config { for key, value := range config {
if strings.HasSuffix(key, "_FORK_VERSION") { if strings.HasSuffix(key, "_FORK_VERSION") {
name := key[:len(key)-len("_FORK_VERSION")] name := key[:len(key)-len("_FORK_VERSION")]
version, ok := value.(string) switch version := value.(type) {
if !ok { case int:
version = fmt.Sprintf("0x%x", value) versions[name] = make([]byte, 4)
} big.NewInt(int64(version)).FillBytes(versions[name])
if v, err := hexutil.Decode(version); err == nil { case string:
v, err := hexutil.Decode(version)
if err != nil {
return fmt.Errorf("failed to decode hex fork id %q in beacon chain config file: %v", version, err)
}
versions[name] = v versions[name] = v
} else {
return fmt.Errorf("failed to decode hex fork id %q in beacon chain config file: %v", version, err)
} }
} }
if strings.HasSuffix(key, "_FORK_EPOCH") { if strings.HasSuffix(key, "_FORK_EPOCH") {
name := key[:len(key)-len("_FORK_EPOCH")] name := key[:len(key)-len("_FORK_EPOCH")]
version, ok := value.(string) switch epoch := value.(type) {
if !ok { case int:
version = fmt.Sprintf("0x%x", value) epochs[name] = uint64(epoch)
} case string:
if v, err := hexutil.DecodeUint64(version); err == nil { v, err := hexutil.DecodeUint64(epoch)
if err != nil {
return fmt.Errorf("failed to parse epoch number %q in beacon chain config file: %v", epoch, err)
}
epochs[name] = v epochs[name] = v
} else {
return fmt.Errorf("failed to parse epoch number %q in beacon chain config file: %v", version, err)
} }
} }
} }

View file

@ -0,0 +1,32 @@
package params
import (
"bytes"
"testing"
)
func TestChainConfig_LoadForksName(t *testing.T) {
const config = `
GENESIS_FORK_VERSION: 0x00000000
ALTAIR_FORK_VERSION: 0x00000001
ALTAIR_FORK_EPOCH: 1
BLOB_SCHEDULE: []
`
c := &ChainConfig{}
err := c.LoadForks([]byte(config))
if err != nil {
t.Fatal(err)
}
for _, fork := range c.Forks {
if fork.Name == "GENESIS" && (fork.Epoch != 0) {
t.Errorf("unexpected genesis fork epoch %d", fork.Epoch)
}
if fork.Name == "ALTAIR" && (fork.Epoch != 1 || !bytes.Equal(fork.Version, []byte{0, 0, 0, 1})) {
t.Errorf("unexpected altair fork epoch %d version %x", fork.Epoch, fork.Version)
}
}
}

View file

@ -1902,11 +1902,15 @@ func MakeBeaconLightConfig(ctx *cli.Context) bparams.ClientConfig {
} else { } else {
Fatalf("Could not parse --%s: %v", BeaconGenesisRootFlag.Name, err) Fatalf("Could not parse --%s: %v", BeaconGenesisRootFlag.Name, err)
} }
configFile := ctx.String(BeaconConfigFlag.Name) configPath := ctx.String(BeaconConfigFlag.Name)
if err := config.ChainConfig.LoadForks(configFile); err != nil { file, err := os.ReadFile(configPath)
Fatalf("Could not load beacon chain config '%s': %v", configFile, err) if err != nil {
Fatalf("failed to read beacon chain config file '%s': %v", configPath, err)
} }
log.Info("Using custom beacon chain config", "file", configFile) if err := config.ChainConfig.LoadForks(file); err != nil {
Fatalf("Could not load beacon chain config '%s': %v", configPath, err)
}
log.Info("Using custom beacon chain config", "file", configPath)
} else { } else {
if ctx.IsSet(BeaconGenesisRootFlag.Name) { if ctx.IsSet(BeaconGenesisRootFlag.Name) {
Fatalf("Genesis root is specified but custom beacon chain config is missing") Fatalf("Genesis root is specified but custom beacon chain config is missing")