Skip to content

Commit

Permalink
Updated
Browse files Browse the repository at this point in the history
  • Loading branch information
18aaddy committed Sep 24, 2024
1 parent 505590b commit 32f7977
Show file tree
Hide file tree
Showing 5 changed files with 360 additions and 11 deletions.
221 changes: 221 additions & 0 deletions cli/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
package cli

import (
"fmt"
// "net"
"os"
"os/signal"
"path/filepath"
"strings"
"sync"
"syscall"
"encoding/hex"

"github.com/spf13/pflag"
"github.com/spf13/viper"
"go.uber.org/zap"

"github.com/BlocSoc-iitr/selene/config"
)

// type Config struct {
// Network string
// RpcBindIP net.IP
// RpcPort uint16
// Checkpoint string
// ExecutionRPC string
// ConsensusRPC string
// DataDir string
// Fallback string
// LoadExternalFallback bool
// StrictCheckpointAge bool
// }

type Client struct {
// Add necessary fields
}

func main() {
logger, _ := zap.NewProduction()
defer logger.Sync()

sugar := logger.Sugar()

config, err := getConfig()
if err != nil {
sugar.Errorf("Failed to get config: %v", err)
os.Exit(1)
}

client, err := newClient(config)
if err != nil {
sugar.Errorf("Failed to create client: %v", err)
os.Exit(1)
}

if err := client.start(); err != nil {
sugar.Errorf("Failed to start client: %v", err)
os.Exit(1)
}

registerShutdownHandler(client, sugar)

// Wait indefinitely
select {}
}

func registerShutdownHandler(client *Client, logger *zap.SugaredLogger) {
var shutdownCounter int
var mu sync.Mutex

c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)

go func() {
for sig := range c {
_ = sig
mu.Lock()
shutdownCounter++
counter := shutdownCounter
mu.Unlock()

if counter == 3 {
logger.Info("Forced shutdown")
os.Exit(0)
}

logger.Infof("Shutting down... press ctrl-c %d more times to force quit", 3-counter)

if counter == 1 {
go func() {
client.shutdown()
os.Exit(0)
}()
}
}
}()
}

func getConfig() (*config.Config, error) {
cli := &Cli{}

// Set up command-line flags
pflag.StringVar(cli.ConsensusRpc, "consensus-rpc", "", "Consensus RPC URL")
pflag.StringVar(cli.ExecutionRpc, "execution-rpc", "", "Execution RPC URL")

var rpcBindIp string
pflag.StringVar(&rpcBindIp, "rpc-bind-ip", "", "RPC bind IP")

var rpcPort uint16
pflag.Uint16Var(&rpcPort, "rpc-port", 0, "RPC port")

var checkpointStr string
pflag.StringVar(&checkpointStr, "checkpoint", "", "Checkpoint (32 byte hex)")

var dataDir string
pflag.StringVar(&dataDir, "data-dir", "", "Data directory")

var fallback string
pflag.StringVar(&fallback, "fallback", "", "Fallback URL")

pflag.BoolVar(&cli.LoadExternalFallback, "load-external-fallback", false, "Load external fallback")
pflag.BoolVar(&cli.StrictCheckpointAge, "strict-checkpoint-age", false, "Strict checkpoint age")

var databaseType string
pflag.StringVar(&databaseType, "database-type", "", "Database type")

pflag.Parse()

// Bind flags to viper
viper.BindPFlags(pflag.CommandLine)

// Set up environment variables
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
viper.AutomaticEnv()

// Set default values
if dataDir == "" {
home, err := os.UserHomeDir()
if err != nil {
return nil, fmt.Errorf("failed to get user home directory: %v", err)
}
dataDir = filepath.Join(home, ".selene")
}
cli.DataDir = &dataDir

// Process the checkpoint
if checkpointStr != "" {
checkpointBytes, err := hex.DecodeString(checkpointStr)
if err != nil {
return nil, fmt.Errorf("invalid checkpoint hex string: %v", err)
}
if len(checkpointBytes) != 32 {
return nil, fmt.Errorf("checkpoint must be exactly 32 bytes")
}
var checkpoint [32]byte
copy(checkpoint[:], checkpointBytes)
cli.Checkpoint = &checkpoint
}

// Set pointers for optional fields
if rpcBindIp != "" {
cli.RpcBindIp = &rpcBindIp
}
if rpcPort != 0 {
cli.RpcPort = &rpcPort
}
if fallback != "" {
cli.Fallback = &fallback
}

cliConfig := cli.asCliConfig()
homeDir, _ := os.UserHomeDir()
configPath := filepath.Join(homeDir, ".selene", "selene.toml")
var finalConfig config.Config

finalConfig.FromFile(&configPath, &cli.Network, &cliConfig)

return &finalConfig, nil
}

type Cli struct {
Network string
RpcBindIp *string
RpcPort *uint16
Checkpoint *[32]byte
ExecutionRpc *string
ConsensusRpc *string
DataDir *string
Fallback *string
LoadExternalFallback bool
StrictCheckpointAge bool
}

func (c *Cli) asCliConfig() config.CliConfig {
checkpoint := c.Checkpoint[:]
return config.CliConfig{
Checkpoint: &checkpoint,
ExecutionRpc: c.ExecutionRpc,
ConsensusRpc: c.ConsensusRpc,
DataDir: c.DataDir,
RpcBindIp: c.RpcBindIp,
RpcPort: c.RpcPort,
Fallback: c.Fallback,
LoadExternalFallback: &c.LoadExternalFallback,
StrictCheckpointAge: &c.StrictCheckpointAge,
}
}

func newClient(config *config.Config) (*Client, error) {
// Implement client creation logic
return &Client{}, nil
}

func (c *Client) start() error {
// Implement client start logic
return nil
}

func (c *Client) shutdown() {
// Implement client shutdown logic
}
2 changes: 2 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package client

4 changes: 2 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type Config struct {
}

// only if we are using CLI
func (c Config) from_file(configPath *string, network *string, cliConfig *CliConfig) Config {
func (c Config) FromFile(configPath *string, network *string, cliConfig *CliConfig) Config {
n := Network(*network)
baseConfig, err := n.BaseConfig(*network)
if err != nil {
Expand Down Expand Up @@ -118,7 +118,7 @@ func (c Config) from_file(configPath *string, network *string, cliConfig *CliCon
}
return finalConfig
}
func (c Config) to_base_config() BaseConfig {
func (c Config) ToBaseConfig() BaseConfig {
return BaseConfig{
RpcBindIp: func() string {
if c.RpcBindIp != nil {
Expand Down
11 changes: 8 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,22 @@ module github.com/BlocSoc-iitr/selene
go 1.22.3

require (
github.com/avast/retry-go v3.0.0+incompatible
github.com/consensys/gnark-crypto v0.12.1
github.com/ethereum/go-ethereum v1.14.8
github.com/holiman/uint256 v1.3.1
github.com/pkg/errors v0.9.1
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.19.0
github.com/wealdtech/go-merkletree v1.0.0
go.uber.org/zap v1.21.0
gopkg.in/yaml.v2 v2.4.0
)

require (
github.com/avast/retry-go v3.0.0+incompatible // indirect
github.com/bits-and-blooms/bitset v1.10.0 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.3.4 // indirect
github.com/consensys/bavard v0.1.13 // indirect
github.com/consensys/gnark-crypto v0.12.1 // indirect
github.com/crate-crypto/go-kzg-4844 v1.0.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect
Expand All @@ -37,6 +41,8 @@ require (
github.com/supranational/blst v0.3.11 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
github.com/wealdtech/go-merkletree v1.0.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/supranational/blst v0.3.11 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/crypto v0.22.0 // indirect
Expand All @@ -45,7 +51,6 @@ require (
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.14.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
rsc.io/tmplfunc v0.0.3 // indirect
)
Loading

0 comments on commit 32f7977

Please sign in to comment.