Skip to content

Commit

Permalink
Merge pull request #54 from DarkLord017/Move_utils_from_common_to_uti…
Browse files Browse the repository at this point in the history
…ls_directory

Move utils from common to utils directory
  • Loading branch information
star-gazer111 authored Sep 21, 2024
2 parents 8fa50ce + b89ac06 commit 49fefa8
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 26 deletions.
8 changes: 5 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package config

import (
"fmt"

"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"`
Expand Down Expand Up @@ -76,7 +78,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 +140,4 @@ func (c Config) to_base_config() BaseConfig {
LoadExternalFallback: c.LoadExternalFallback,
StrictCheckpointAge: c.StrictCheckpointAge,
}
}
}
39 changes: 22 additions & 17 deletions config/networks.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
package config

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

"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 +57,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 +68,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 Down Expand Up @@ -111,15 +116,15 @@ func Mainnet() (BaseConfig, error) {
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 Down Expand Up @@ -158,11 +163,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 Down Expand Up @@ -199,4 +204,4 @@ func Sepolia() (BaseConfig, error) {
MaxCheckpointAge: 1_209_600, // 14 days
DataDir: &dataDir,
}, nil
}
}
4 changes: 2 additions & 2 deletions config/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"encoding/hex"
"encoding/json"

"github.com/BlocSoc-iitr/selene/common"
"github.com/BlocSoc-iitr/selene/utils"
)

func BytesSerialise(bytes []byte) ([]byte, error) {
Expand All @@ -30,7 +30,7 @@ func BytesDeserialise(data []byte) ([]byte, error) {
if bytesOpt == nil {
return nil, nil
} else {
bytes, err := common.Hex_str_to_bytes(*bytesOpt)
bytes, err := utils.Hex_str_to_bytes(*bytesOpt)
if err != nil {
return nil, err
}
Expand Down
6 changes: 2 additions & 4 deletions common/utils.go → utils/utils.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package common
package utils

import (
"encoding/hex"
"strings"
"encoding/json"
"fmt"
"strings"

"github.com/ethereum/go-ethereum/common"
)

// if we need to export the functions , just make their first letter capitalised
func Hex_str_to_bytes(s string) ([]byte, error) {
s = strings.TrimPrefix(s, "0x")

Expand Down

0 comments on commit 49fefa8

Please sign in to comment.