Skip to content

Commit

Permalink
Mining a slice now
Browse files Browse the repository at this point in the history
Refactor the Interface and get it to compile
  • Loading branch information
gameofpointers committed Jan 11, 2024
1 parent 8785a70 commit e8ece2b
Show file tree
Hide file tree
Showing 51 changed files with 1,023 additions and 1,606 deletions.
5 changes: 2 additions & 3 deletions cmd/go-quai/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/spf13/viper"

"github.com/dominant-strategies/go-quai/cmd/utils"
"github.com/dominant-strategies/go-quai/common"
"github.com/dominant-strategies/go-quai/common/constants"
"github.com/dominant-strategies/go-quai/log"
)
Expand Down Expand Up @@ -40,7 +39,7 @@ func rootCmdPreRun(cmd *cobra.Command, args []string) error {
viper.SetConfigFile(configDir + constants.CONFIG_FILE_NAME)
viper.SetConfigType("yaml")
// load config from file and environment variables
common.InitConfig()
utils.InitConfig()
// bind cobra flags to viper instance
err := viper.BindPFlags(cmd.Flags())
if err != nil {
Expand All @@ -57,7 +56,7 @@ func rootCmdPreRun(cmd *cobra.Command, args []string) error {
// save config file if SAVE_CONFIG_FILE flag is set to true
saveConfigFile := viper.GetBool(utils.SaveConfigFlag.Name)
if saveConfigFile {
err := common.SaveConfig()
err := utils.SaveConfig()
if err != nil {
log.Errorf("error saving config file: %s . Skipping...", err)
} else {
Expand Down
3 changes: 1 addition & 2 deletions cmd/go-quai/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"github.com/dominant-strategies/go-quai/cmd/utils"
"github.com/dominant-strategies/go-quai/common"
"github.com/dominant-strategies/go-quai/consensus/quai"
"github.com/dominant-strategies/go-quai/log"
"github.com/dominant-strategies/go-quai/p2p/node"
)
Expand Down Expand Up @@ -70,7 +69,7 @@ func runStart(cmd *cobra.Command, args []string) error {
}

// create instance of consensus backend
consensus, err := quai.NewQuaiBackend()
consensus, err := utils.StartQuaiBackend()
if err != nil {
log.Fatalf("error creating consensus backend: %s", err)
}
Expand Down
156 changes: 156 additions & 0 deletions cmd/utils/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
package utils

import (
"fmt"

"github.com/dominant-strategies/go-quai/common"
"github.com/dominant-strategies/go-quai/core/vm"
"github.com/dominant-strategies/go-quai/eth"
quai "github.com/dominant-strategies/go-quai/eth"
"github.com/dominant-strategies/go-quai/eth/ethconfig"
"github.com/dominant-strategies/go-quai/internal/quaiapi"
"github.com/dominant-strategies/go-quai/log"
"github.com/dominant-strategies/go-quai/node"
"github.com/dominant-strategies/go-quai/params"
"github.com/dominant-strategies/go-quai/quaistats"
"github.com/spf13/viper"
"io"
"os"
"runtime"
"time"
)

// Create a new instance of the QuaiBackend consensus service
func StartQuaiBackend() (*quai.QuaiBackend, error) {

// Make full node
go func() {
log.Info("Starting Prime")
stackPrime := makeFullNode(nil)
defer stackPrime.Close()
StartNode(stackPrime)
stackPrime.Wait()
}()

time.Sleep(2 * time.Second)

go func() {
log.Info("Starting Region")
stackRegion := makeFullNode(common.Location{0})
defer stackRegion.Close()
StartNode(stackRegion)
stackRegion.Wait()
}()

time.Sleep(2 * time.Second)

go func() {
log.Info("Starting Zone")
stackZone := makeFullNode(common.Location{0, 0})
defer stackZone.Close()
StartNode(stackZone)
stackZone.Wait()
}()

return &eth.QuaiBackend{}, nil
}

func StartNode(stack *node.Node) {
if err := stack.Start(); err != nil {
Fatalf("Error starting protocol stack: %v", err)
}
// TODO: Stop the node if the memory pressure
}

// makeConfigNode loads quai configuration and creates a blank node instance.
func makeConfigNode(nodeLocation common.Location) (*node.Node, ethconfig.QuaiConfig) {
// Load defaults.
cfg := ethconfig.QuaiConfig{
Quai: ethconfig.Defaults,
Node: defaultNodeConfig(),
}

// Apply flags.
// set the node location
log.Info("Node", "Location", nodeLocation)
cfg.Node.NodeLocation = nodeLocation

SetNodeConfig(&cfg.Node, nodeLocation)
stack, err := node.New(&cfg.Node)
if err != nil {
Fatalf("Failed to create the protocol stack: %v", err)
}
SetQuaiConfig(stack, &cfg.Quai, nodeLocation)

// TODO: Apply stats
if viper.IsSet(QuaiStatsURLFlag.Name) {
cfg.Ethstats.URL = viper.GetString(QuaiStatsURLFlag.Name)
}

nodeCtx := nodeLocation.Context()
// Onlt initialize the precompile for the zone chain
if nodeCtx == common.ZONE_CTX {
vm.InitializePrecompiles(nodeLocation)
}
return stack, cfg
}

func defaultNodeConfig() node.Config {
cfg := node.DefaultConfig
cfg.Name = ""
cfg.Version = params.VersionWithCommit("", "")
cfg.HTTPModules = append(cfg.HTTPModules, "eth")
cfg.WSModules = append(cfg.WSModules, "eth")
return cfg
}

// makeFullNode loads quai configuration and creates the Quai backend.
func makeFullNode(nodeLocation common.Location) *node.Node {
stack, cfg := makeConfigNode(nodeLocation)
backend, _ := RegisterQuaiService(stack, cfg.Quai, cfg.Node.NodeLocation.Context())
sendfullstats := viper.GetBool(SendFullStatsFlag.Name)
// Add the Quai Stats daemon if requested.
if cfg.Ethstats.URL != "" {
RegisterQuaiStatsService(stack, backend, cfg.Ethstats.URL, sendfullstats)
}
return stack
}

// RegisterQuaiService adds a Quai client to the stack.
// The second return value is the full node instance, which may be nil if the
// node is running as a light client.
func RegisterQuaiService(stack *node.Node, cfg ethconfig.Config, nodeCtx int) (quaiapi.Backend, error) {
backend, err := eth.New(stack, &cfg, nodeCtx)
if err != nil {
Fatalf("Failed to register the Quai service: %v", err)
}
return backend.APIBackend, nil
}

// RegisterQuaiStatsService configures the Quai Stats daemon and adds it to
// the given node.
func RegisterQuaiStatsService(stack *node.Node, backend quaiapi.Backend, url string, sendfullstats bool) {
if err := quaistats.New(stack, backend, backend.Engine(), url, sendfullstats); err != nil {
Fatalf("Failed to register the Quai Stats service: %v", err)
}
}

// Fatalf formats a message to standard error and exits the program.
// The message is also printed to standard output if standard error
// is redirected to a different file.
func Fatalf(format string, args ...interface{}) {
w := io.MultiWriter(os.Stdout, os.Stderr)
if runtime.GOOS == "windows" {
// The SameFile check below doesn't work on Windows.
// stdout is unlikely to get redirected though, so just print there.
w = os.Stdout
} else {
outf, _ := os.Stdout.Stat()
errf, _ := os.Stderr.Stat()
if outf != nil && errf != nil && os.SameFile(outf, errf) {
w = os.Stderr
}
}
fmt.Fprintf(w, "Fatal: "+format+"\n", args...)
os.Exit(1)
}
Loading

0 comments on commit e8ece2b

Please sign in to comment.