From e20b290ded6fa2562c332a0ff8278347959ca9c8 Mon Sep 17 00:00:00 2001 From: Sambhav Jain <136801346+DarkLord017@users.noreply.github.com> Date: Sat, 21 Sep 2024 22:29:39 +0530 Subject: [PATCH 1/4] Move utils.go functions from common to utils directory --- utils/utils.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 utils/utils.go diff --git a/utils/utils.go b/utils/utils.go new file mode 100644 index 0000000..be170c7 --- /dev/null +++ b/utils/utils.go @@ -0,0 +1,47 @@ +package utils + +import ( + "encoding/hex" + "strings" + "encoding/json" + "fmt" + "github.com/ethereum/go-ethereum/common" +) + +func Hex_str_to_bytes(s string) ([]byte, error) { + s = strings.TrimPrefix(s, "0x") + + bytesArray, err := hex.DecodeString(s) + + if err != nil { + return nil, err + } + + return bytesArray, nil +} + +func Address_to_hex_string(addr common.Address) string { + bytesArray := addr.Bytes() + return fmt.Sprintf("0x%x", hex.EncodeToString(bytesArray)) +} + +func U64_to_hex_string(val uint64) string { + return fmt.Sprintf("0x%x", val) +} + +func Bytes_deserialize(data []byte) ([]byte, error) { + var hexString string + if err := json.Unmarshal(data, &hexString); err != nil { + return nil, err + } + + return Hex_str_to_bytes(hexString) +} + +func Bytes_serialize(bytes []byte) ([]byte, error) { + if bytes == nil { + return json.Marshal(nil) + } + hexString := hex.EncodeToString(bytes) + return json.Marshal(hexString) +} From 368f18c3315cdffac08614caf1712f522921f7f8 Mon Sep 17 00:00:00 2001 From: DarkLord017 Date: Sat, 21 Sep 2024 22:39:33 +0530 Subject: [PATCH 2/4] Deleted utils.go --- common/utils.go | 49 ------------------------------------------------- 1 file changed, 49 deletions(-) delete mode 100644 common/utils.go diff --git a/common/utils.go b/common/utils.go deleted file mode 100644 index deb1423..0000000 --- a/common/utils.go +++ /dev/null @@ -1,49 +0,0 @@ -package common - -import ( - "encoding/hex" - "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") - - bytesArray, err := hex.DecodeString(s) - - if err != nil { - return nil, err - } - - return bytesArray, nil -} - -func Address_to_hex_string(addr common.Address) string { - bytesArray := addr.Bytes() - return fmt.Sprintf("0x%x", hex.EncodeToString(bytesArray)) -} - -func U64_to_hex_string(val uint64) string { - return fmt.Sprintf("0x%x", val) -} - -func Bytes_deserialize(data []byte) ([]byte, error) { - var hexString string - if err := json.Unmarshal(data, &hexString); err != nil { - return nil, err - } - - return Hex_str_to_bytes(hexString) -} - -func Bytes_serialize(bytes []byte) ([]byte, error) { - if bytes == nil { - return json.Marshal(nil) - } - hexString := hex.EncodeToString(bytes) - return json.Marshal(hexString) -} From 45efb384610cd9a7337a15b26b86f5366bd977cb Mon Sep 17 00:00:00 2001 From: DarkLord017 Date: Sat, 21 Sep 2024 22:46:40 +0530 Subject: [PATCH 3/4] Code in config directory uses utils package --- config/config.go | 8 +++++--- config/networks.go | 39 ++++++++++++++++++++++----------------- 2 files changed, 27 insertions(+), 20 deletions(-) diff --git a/config/config.go b/config/config.go index bc184ce..eb55a79 100644 --- a/config/config.go +++ b/config/config.go @@ -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"` @@ -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) } @@ -138,4 +140,4 @@ func (c Config) to_base_config() BaseConfig { LoadExternalFallback: c.LoadExternalFallback, StrictCheckpointAge: c.StrictCheckpointAge, } -} \ No newline at end of file +} diff --git a/config/networks.go b/config/networks.go index a98e176..c98d71a 100644 --- a/config/networks.go +++ b/config/networks.go @@ -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": @@ -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 @@ -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) } @@ -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) } @@ -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) } @@ -199,4 +204,4 @@ func Sepolia() (BaseConfig, error) { MaxCheckpointAge: 1_209_600, // 14 days DataDir: &dataDir, }, nil -} \ No newline at end of file +} From b89ac0648d23ed663247961da16353e22b8b7dc3 Mon Sep 17 00:00:00 2001 From: DarkLord017 Date: Sat, 21 Sep 2024 22:49:22 +0530 Subject: [PATCH 4/4] Code in config directory uses utils package --- config/utils.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config/utils.go b/config/utils.go index 3004d37..0170924 100644 --- a/config/utils.go +++ b/config/utils.go @@ -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) { @@ -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 }