Skip to content

Commit

Permalink
Merge pull request #56 from DarkLord017/Consensus_utils_also_transferred
Browse files Browse the repository at this point in the history
Consensus utils also transferred
  • Loading branch information
star-gazer111 authored Sep 22, 2024
2 parents c481054 + 1ce4468 commit b425ff2
Show file tree
Hide file tree
Showing 9 changed files with 337 additions and 354 deletions.
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,
}
}
}
29 changes: 15 additions & 14 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,26 @@ package config
import (
"fmt"

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

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
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)
}
}
}
37 changes: 19 additions & 18 deletions config/networks.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"path/filepath"
"strings"

"github.com/BlocSoc-iitr/selene/consensus/consensus_core"
"github.com/BlocSoc-iitr/selene/utils"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -98,20 +99,20 @@ 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}},
},
Expand Down Expand Up @@ -141,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 Down Expand Up @@ -184,20 +185,20 @@ 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}},
},
Expand Down
39 changes: 3 additions & 36 deletions config/types.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
package config

import (
"encoding/hex"
"encoding/json"
)

type ChainConfig struct {
ChainID uint64 `json:"chain_id"`
GenesisTime uint64 `json:"genesis_time"`
GenesisRoot []byte `json:"genesis_root"`
}
type Fork struct {
Epoch uint64 `json:"epoch"`
ForkVersion []byte `json:"fork_version"`
}
type Forks struct {
Genesis Fork `json:"genesis"`
Altair Fork `json:"altair"`
Bellatrix Fork `json:"bellatrix"`
Capella Fork `json:"capella"`
Deneb Fork `json:"deneb"`
}

func (c ChainConfig) MarshalJSON() ([]byte, error) {
type Alias ChainConfig
return json.Marshal(&struct {
Expand All @@ -44,28 +36,3 @@ func (c *ChainConfig) UnmarshalJSON(data []byte) error {
c.GenesisRoot, err = hex.DecodeString(aux.GenesisRoot)
return err
}
func (f Fork) MarshalJSON() ([]byte, error) {
type Alias Fork
return json.Marshal(&struct {
Alias
ForkVersion string `json:"fork_version"`
}{
Alias: (Alias)(f),
ForkVersion: hex.EncodeToString(f.ForkVersion),
})
}
func (f *Fork) UnmarshalJSON(data []byte) error {
type Alias Fork
aux := &struct {
*Alias
ForkVersion string `json:"fork_version"`
}{
Alias: (*Alias)(f),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
var err error
f.ForkVersion, err = hex.DecodeString(aux.ForkVersion)
return err
}
Loading

0 comments on commit b425ff2

Please sign in to comment.