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

Consensus utils also transferred #56

Merged
49 changes: 0 additions & 49 deletions common/utils.go

This file was deleted.

26 changes: 14 additions & 12 deletions config/base.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package config

import "github.com/BlocSoc-iitr/selene/consensus/consensus_core"

// base config for a network

type BaseConfig struct {
RpcBindIp string `json:"rpc_bind_ip"`
RpcPort uint16 `json:"rpc_port"`
ConsensusRpc *string `json:"consensus_rpc"`
DefaultCheckpoint [32]byte `json:"default_checkpoint"` // In cli.go, checkpoint is currently taken as []byte{}
Chain ChainConfig `json:"chain"` // but it should be [32]byte as it is a hash
Forks Forks `json:"forks"`
MaxCheckpointAge uint64 `json:"max_checkpoint_age"`
DataDir *string `json:"data_dir"`
LoadExternalFallback bool `json:"load_external_fallback"`
StrictCheckpointAge bool `json:"strict_checkpoint_age"`
RpcBindIp string `json:"rpc_bind_ip"`
RpcPort uint16 `json:"rpc_port"`
ConsensusRpc *string `json:"consensus_rpc"`
DefaultCheckpoint [32]byte `json:"default_checkpoint"` // In cli.go, checkpoint is currently taken as []byte{}
Chain ChainConfig `json:"chain"` // but it should be [32]byte as it is a hash
Forks consensus_core.Forks `json:"forks"`
MaxCheckpointAge uint64 `json:"max_checkpoint_age"`
DataDir *string `json:"data_dir"`
LoadExternalFallback bool `json:"load_external_fallback"`
StrictCheckpointAge bool `json:"strict_checkpoint_age"`
}

// implement a default method for the above struct
Expand All @@ -24,10 +26,10 @@ func (b BaseConfig) Default() BaseConfig {
ConsensusRpc: nil,
DefaultCheckpoint: [32]byte{},
Chain: ChainConfig{},
Forks: Forks{},
Forks: consensus_core.Forks{},
MaxCheckpointAge: 0,
DataDir: nil,
LoadExternalFallback: false,
StrictCheckpointAge: false,
}
}
}
37 changes: 20 additions & 17 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,27 @@ package config

import (
"fmt"

"github.com/BlocSoc-iitr/selene/consensus/consensus_core"
"github.com/BlocSoc-iitr/selene/utils"
"github.com/spf13/viper"
"github.com/BlocSoc-iitr/selene/common"
)

type Config struct {
ConsensusRpc string `json:"consensus_rpc"`
ExecutionRpc string `json:"execution_rpc"`
RpcBindIp *string `json:"rpc_bind_ip"`
RpcPort *uint16 `json:"rpc_port"`
DefaultCheckpoint [32]byte `json:"default_checkpoint"` // In cli.go, checkpoint is currently taken as []byte{}
Checkpoint *[32]byte `json:"checkpoint"` // but it should be of 32 bytes or [32]byte{}
DataDir *string `json:"data_dir"`
Chain ChainConfig `json:"chain"`
Forks Forks `json:"forks"`
MaxCheckpointAge uint64 `json:"max_checkpoint_age"`
Fallback *string `json:"fallback"`
LoadExternalFallback bool `json:"load_external_fallback"`
StrictCheckpointAge bool `json:"strict_checkpoint_age"`
DatabaseType *string `json:"database_type"`
ConsensusRpc string `json:"consensus_rpc"`
ExecutionRpc string `json:"execution_rpc"`
RpcBindIp *string `json:"rpc_bind_ip"`
RpcPort *uint16 `json:"rpc_port"`
DefaultCheckpoint [32]byte `json:"default_checkpoint"` // In cli.go, checkpoint is currently taken as []byte{}
Checkpoint *[32]byte `json:"checkpoint"` // but it should be of 32 bytes or [32]byte{}
DataDir *string `json:"data_dir"`
Chain ChainConfig `json:"chain"`
Forks consensus_core.Forks `json:"forks"`
MaxCheckpointAge uint64 `json:"max_checkpoint_age"`
Fallback *string `json:"fallback"`
LoadExternalFallback bool `json:"load_external_fallback"`
StrictCheckpointAge bool `json:"strict_checkpoint_age"`
DatabaseType *string `json:"database_type"`
}

// only if we are using CLI
Expand Down Expand Up @@ -76,7 +79,7 @@ func (c Config) from_file(configPath *string, network *string, cliConfig *CliCon
finalConfig.Checkpoint = (*[32]byte)(*cliConfig.Checkpoint)
} else if tomlProvider["checkpoint"] != nil && tomlProvider["checkpoint"].(string) != "" {
checkpoint, _ := tomlProvider["checkpoint"].(string)
checkpointBytes, err := common.Hex_str_to_bytes(checkpoint)
checkpointBytes, err := utils.Hex_str_to_bytes(checkpoint)
if err != nil {
fmt.Printf("Failed to convert checkpoint value to byte slice, %v", err)
}
Expand Down Expand Up @@ -138,4 +141,4 @@ func (c Config) to_base_config() BaseConfig {
LoadExternalFallback: c.LoadExternalFallback,
StrictCheckpointAge: c.StrictCheckpointAge,
}
}
}
22 changes: 12 additions & 10 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package config
import (
"encoding/hex"
"fmt"
"github.com/spf13/viper"
"os"
"reflect"
"testing"

"github.com/BlocSoc-iitr/selene/consensus/consensus_core"
"github.com/spf13/viper"
)

var (
Expand All @@ -23,9 +25,9 @@ var (
defaultCheckpoint = [32]byte{}
)

/////////////////////////////
///// from_file() tests /////
/////////////////////////////
// ///////////////////////////
// /// from_file() tests /////
// ///////////////////////////
func TestMainnetBaseConfig(t *testing.T) {
network := "MAINNET"
path := "./config.toml"
Expand Down Expand Up @@ -179,17 +181,17 @@ func createConfigFile(v *viper.Viper) {
}
}

//////////////////////////////////
///// to_base_config() tests /////
//////////////////////////////////
// ////////////////////////////////
// /// to_base_config() tests /////
// ////////////////////////////////
func TestReturnsCorrectBaseConfig(t *testing.T) {
config := Config{
ConsensusRpc: consensusRpc,
RpcBindIp: &rpcBindIp,
RpcPort: &rpcPort,
DefaultCheckpoint: defaultCheckpoint,
Chain: ChainConfig{},
Forks: Forks{},
Forks: consensus_core.Forks{},
MaxCheckpointAge: uint64(maxCheckpointAge),
DataDir: &dataDirectory,
LoadExternalFallback: loadExternalFallback,
Expand All @@ -213,7 +215,7 @@ func TestReturnsCorrectDefaultValues(t *testing.T) {
ConsensusRpc: consensusRpc,
DefaultCheckpoint: defaultCheckpoint,
Chain: ChainConfig{},
Forks: Forks{},
Forks: consensus_core.Forks{},
MaxCheckpointAge: uint64(maxCheckpointAge),
DataDir: &dataDirectory,
LoadExternalFallback: loadExternalFallback,
Expand All @@ -230,4 +232,4 @@ func TestReturnsCorrectDefaultValues(t *testing.T) {
if baseConfig.RpcBindIp != "127.0.0.1" {
t.Errorf("Expected Max Checkpoint age to be %v, got %v", "127.0.0.1", baseConfig.RpcBindIp)
}
}
}
76 changes: 41 additions & 35 deletions config/networks.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
package config

import (
"fmt"
"github.com/BlocSoc-iitr/selene/common"
"github.com/pkg/errors"
"os"
"path/filepath"
"strings"

"github.com/BlocSoc-iitr/selene/consensus/consensus_core"
"github.com/BlocSoc-iitr/selene/utils"
"github.com/pkg/errors"
)

type Network string

const (
MAINNET Network = "MAINNET"
GOERLI Network = "GOERLI"
SEPOLIA Network = "SEPOLIA"
)

func (n Network) BaseConfig(s string) (BaseConfig, error) {
switch strings.ToUpper(s) {
case "MAINNET":
Expand Down Expand Up @@ -52,7 +58,7 @@ func (n Network) ChainID(id uint64) (BaseConfig, error) {
}
return config, nil
case 11155111:

config, err := Sepolia()
if err != nil {
return BaseConfig{}, err
Expand All @@ -63,19 +69,19 @@ func (n Network) ChainID(id uint64) (BaseConfig, error) {
}
}
func dataDir(network Network) (string, error) {
homeDir, err := os.UserHomeDir()
if err != nil {
return "", fmt.Errorf("failed to get user home directory: %w", err)
}
path := filepath.Join(homeDir, fmt.Sprintf("selene/data/%s", strings.ToLower(string(network))))
return path, nil
homeDir, err := os.UserHomeDir()
if err != nil {
return "", fmt.Errorf("failed to get user home directory: %w", err)
}
path := filepath.Join(homeDir, fmt.Sprintf("selene/data/%s", strings.ToLower(string(network))))
return path, nil
}
func Mainnet() (BaseConfig, error) {
defaultCheckpoint, err := common.Hex_str_to_bytes("c7fc7b2f4b548bfc9305fa80bc1865ddc6eea4557f0a80507af5dc34db7bd9ce")
defaultCheckpoint, err := utils.Hex_str_to_bytes("c7fc7b2f4b548bfc9305fa80bc1865ddc6eea4557f0a80507af5dc34db7bd9ce")
if err != nil {
return BaseConfig{}, fmt.Errorf("failed to parse default checkpoint: %w", err)
}
genesisRoot, err := common.Hex_str_to_bytes("4b363db94e286120d76eb905340fdd4e54bfe9f06bf33ff6cf5ad27f511bfe95")
genesisRoot, err := utils.Hex_str_to_bytes("4b363db94e286120d76eb905340fdd4e54bfe9f06bf33ff6cf5ad27f511bfe95")
if err != nil {
return BaseConfig{}, fmt.Errorf("failed to parse genesis root: %w", err)
}
Expand All @@ -93,33 +99,33 @@ func Mainnet() (BaseConfig, error) {
GenesisTime: 1606824023,
GenesisRoot: genesisRoot,
},
Forks: Forks{
Genesis: Fork{
Forks: consensus_core.Forks{
Genesis: consensus_core.Fork{
Epoch: 0,
ForkVersion: []byte{0x00, 0x00, 0x00, 0x00}},
Altair: Fork{
Altair: consensus_core.Fork{
Epoch: 74240,
ForkVersion: []byte{0x01, 0x00, 0x00, 0x00}},
Bellatrix: Fork{
Bellatrix: consensus_core.Fork{
Epoch: 144896,
ForkVersion: []byte{0x02, 0x00, 0x00, 0x00}},
Capella: Fork{
Capella: consensus_core.Fork{
Epoch: 194048,
ForkVersion: []byte{0x03, 0x00, 0x00, 0x00}},
Deneb: Fork{
Deneb: consensus_core.Fork{
Epoch: 269568,
ForkVersion: []byte{0x04, 0x00, 0x00, 0x00}},
},
MaxCheckpointAge: 1_209_600, // 14 days
DataDir: &dataDir,
DataDir: &dataDir,
}, nil
}
func Goerli() (BaseConfig, error) {
defaultCheckpoint, err := common.Hex_str_to_bytes("f6e9d5fdd7c406834e888961beab07b2443b64703c36acc1274ae1ce8bb48839")
defaultCheckpoint, err := utils.Hex_str_to_bytes("f6e9d5fdd7c406834e888961beab07b2443b64703c36acc1274ae1ce8bb48839")
if err != nil {
return BaseConfig{}, fmt.Errorf("failed to parse default checkpoint: %w", err)
}
genesisRoot, err := common.Hex_str_to_bytes("043db0d9a83813551ee2f33450d23797757d430911a9320530ad8a0eabc43efb")
genesisRoot, err := utils.Hex_str_to_bytes("043db0d9a83813551ee2f33450d23797757d430911a9320530ad8a0eabc43efb")
if err != nil {
return BaseConfig{}, fmt.Errorf("failed to parse genesis root: %w", err)
}
Expand All @@ -136,20 +142,20 @@ func Goerli() (BaseConfig, error) {
GenesisTime: 1616508000,
GenesisRoot: genesisRoot,
},
Forks: Forks{
Genesis: Fork{
Forks: consensus_core.Forks{
Genesis: consensus_core.Fork{
Epoch: 0,
ForkVersion: []byte{0x00, 0x10, 0x20, 0x00}},
Altair: Fork{
Altair: consensus_core.Fork{
Epoch: 36660,
ForkVersion: []byte{0x01, 0x10, 0x20, 0x00}},
Bellatrix: Fork{
Bellatrix: consensus_core.Fork{
Epoch: 112260,
ForkVersion: []byte{0x02, 0x10, 0x20, 0x00}},
Capella: Fork{
Capella: consensus_core.Fork{
Epoch: 162304,
ForkVersion: []byte{0x03, 0x10, 0x20, 0x00}},
Deneb: Fork{
Deneb: consensus_core.Fork{
Epoch: 231680,
ForkVersion: []byte{0x04, 0x10, 0x20, 0x00}},
},
Expand All @@ -158,11 +164,11 @@ func Goerli() (BaseConfig, error) {
}, nil
}
func Sepolia() (BaseConfig, error) {
defaultCheckpoint, err := common.Hex_str_to_bytes("4135bf01bddcfadac11143ba911f1c7f0772fdd6b87742b0bc229887bbf62b48")
defaultCheckpoint, err := utils.Hex_str_to_bytes("4135bf01bddcfadac11143ba911f1c7f0772fdd6b87742b0bc229887bbf62b48")
if err != nil {
return BaseConfig{}, fmt.Errorf("failed to parse default checkpoint: %w", err)
}
genesisRoot, err := common.Hex_str_to_bytes("d8ea171f3c94aea21ebc42a1ed61052acf3f9209c00e4efbaaddac09ed9b8078")
genesisRoot, err := utils.Hex_str_to_bytes("d8ea171f3c94aea21ebc42a1ed61052acf3f9209c00e4efbaaddac09ed9b8078")
if err != nil {
return BaseConfig{}, fmt.Errorf("failed to parse genesis root: %w", err)
}
Expand All @@ -179,24 +185,24 @@ func Sepolia() (BaseConfig, error) {
GenesisTime: 1655733600,
GenesisRoot: genesisRoot,
},
Forks: Forks{
Genesis: Fork{
Forks: consensus_core.Forks{
Genesis: consensus_core.Fork{
Epoch: 0,
ForkVersion: []byte{0x90, 0x00, 0x00, 0x69}},
Altair: Fork{
Altair: consensus_core.Fork{
Epoch: 50,
ForkVersion: []byte{0x90, 0x00, 0x00, 0x70}},
Bellatrix: Fork{
Bellatrix: consensus_core.Fork{
Epoch: 100,
ForkVersion: []byte{0x90, 0x00, 0x00, 0x71}},
Capella: Fork{
Capella: consensus_core.Fork{
Epoch: 56832,
ForkVersion: []byte{0x90, 0x00, 0x00, 0x72}},
Deneb: Fork{
Deneb: consensus_core.Fork{
Epoch: 132608,
ForkVersion: []byte{0x90, 0x00, 0x00, 0x73}},
},
MaxCheckpointAge: 1_209_600, // 14 days
DataDir: &dataDir,
}, nil
}
}
Loading
Loading