From 986846836881d5854ac7499867eac9a01c701c1d Mon Sep 17 00:00:00 2001 From: Tritonn204 Date: Mon, 2 Sep 2024 13:41:00 -0700 Subject: [PATCH 1/3] Added -solo flag to CLI + solo_mining field to config.yaml. Adjusted terminology for invalid blocks/shares. --- cmd/spectrebridge/config.yaml | 4 ++++ cmd/spectrebridge/main.go | 2 ++ src/spectrestratum/client_handler.go | 17 +++++++++++++---- src/spectrestratum/hasher.go | 8 ++++++++ src/spectrestratum/share_handler.go | 19 ++++++++++++++++--- src/spectrestratum/stratum_server.go | 9 +++++---- 6 files changed, 48 insertions(+), 11 deletions(-) diff --git a/cmd/spectrebridge/config.yaml b/cmd/spectrebridge/config.yaml index d6bec71..2e5e59f 100644 --- a/cmd/spectrebridge/config.yaml +++ b/cmd/spectrebridge/config.yaml @@ -46,6 +46,10 @@ shares_per_min: 4 # var_diff_stats: if true, print vardiff engine stats to the log every 10s var_diff_stats: false +# solo_mining: if true, stratum vardiff calculations will be ignored in favor of +# using network difficulty +solo_mining: false + # block_wait_time: time to wait since last new block message from spectred before # manually requesting a new block. Examples are '500ms', '3s', '1m', etc. block_wait_time: 250ms diff --git a/cmd/spectrebridge/main.go b/cmd/spectrebridge/main.go index 724c52a..9048120 100644 --- a/cmd/spectrebridge/main.go +++ b/cmd/spectrebridge/main.go @@ -43,6 +43,7 @@ func main() { flag.BoolVar(&cfg.VarDiff, "vardiff", cfg.VarDiff, "true to enable auto-adjusting variable min diff") flag.UintVar(&cfg.SharesPerMin, "sharespermin", cfg.SharesPerMin, "number of shares per minute the vardiff engine should target") flag.BoolVar(&cfg.VarDiffStats, "vardiffstats", cfg.VarDiffStats, "include vardiff stats readout every 10s in log") + flag.BoolVar(&cfg.SoloMining, "solo", cfg.SoloMining, "true to use network diff instead of stratum vardiff") flag.UintVar(&cfg.ExtranonceSize, "extranonce", cfg.ExtranonceSize, "size in bytes of extranonce") flag.StringVar(&cfg.PromPort, "prom", cfg.PromPort, "address to serve prom stats") flag.BoolVar(&cfg.UseLogFile, "log", cfg.UseLogFile, "if true will output errors to log file") @@ -60,6 +61,7 @@ func main() { log.Printf("\tvar diff: %t", cfg.VarDiff) log.Printf("\tshares per min: %d", cfg.SharesPerMin) log.Printf("\tvar diff stats: %t", cfg.VarDiffStats) + log.Printf("\tsolo mining: %t", cfg.SoloMining) log.Printf("\tblock wait: %s", cfg.BlockWaitTime) log.Printf("\textranonce size: %d", cfg.ExtranonceSize) log.Printf("\thealth check: %s", cfg.HealthCheckPort) diff --git a/src/spectrestratum/client_handler.go b/src/spectrestratum/client_handler.go index 7596eae..b8fb042 100644 --- a/src/spectrestratum/client_handler.go +++ b/src/spectrestratum/client_handler.go @@ -83,7 +83,7 @@ func (c *clientListener) OnDisconnect(ctx *gostratum.StratumContext) { RecordDisconnect(ctx) } -func (c *clientListener) NewBlockAvailable(kapi *SpectreApi) { +func (c *clientListener) NewBlockAvailable(kapi *SpectreApi, soloMining bool) { c.clientLock.Lock() addresses := make([]string, 0, len(c.clients)) for _, cl := range c.clients { @@ -128,14 +128,23 @@ func (c *clientListener) NewBlockAvailable(kapi *SpectreApi) { // first pass through send config/default difficulty state.stratumDiff = newSpectreDiff() state.stratumDiff.setDiffValue(c.minShareDiff) - sendClientDiff(client, state) + if (!soloMining) { + sendClientDiff(client, state) + } c.shareHandler.setClientVardiff(client, c.minShareDiff) } - varDiff := c.shareHandler.getClientVardiff(client) + varDiff := TargetToDiff(&state.bigDiff) + c.shareHandler.setSoloDiff(varDiff) + if (!soloMining) { + varDiff = c.shareHandler.getClientVardiff(client) + } + if varDiff != state.stratumDiff.diffValue { // send updated vardiff - client.Logger.Info(fmt.Sprintf("changing diff from %f to %f", state.stratumDiff.diffValue, varDiff)) + if (!soloMining) { + client.Logger.Info(fmt.Sprintf("changing diff from %f to %f", state.stratumDiff.diffValue, varDiff)) + } state.stratumDiff.setDiffValue(varDiff) sendClientDiff(client, state) c.shareHandler.startClientVardiff(client) diff --git a/src/spectrestratum/hasher.go b/src/spectrestratum/hasher.go index 6cf7c8c..294a560 100644 --- a/src/spectrestratum/hasher.go +++ b/src/spectrestratum/hasher.go @@ -46,6 +46,14 @@ func DiffToTarget(diff float64) *big.Int { return t } +func TargetToDiff(target *big.Int) float64 { + targetFloat := new(big.Float).SetInt(target) + diff := new(big.Float).Quo(maxTarget, targetFloat) + + diffFloat, _ := diff.Float64() + return diffFloat +} + func DiffToHash(diff float64) float64 { hashVal := new(big.Float).Mul(minHash, big.NewFloat(diff)) hashVal.Quo(hashVal, bigGig) diff --git a/src/spectrestratum/share_handler.go b/src/spectrestratum/share_handler.go index d80a5e0..1507cf2 100644 --- a/src/spectrestratum/share_handler.go +++ b/src/spectrestratum/share_handler.go @@ -42,6 +42,7 @@ type WorkStats struct { type shareHandler struct { spectre *rpcclient.RPCClient + soloDiff float64 stats map[string]*WorkStats statsLock sync.Mutex overall WorkStats @@ -152,7 +153,11 @@ func (sh *shareHandler) checkStales(ctx *gostratum.StratumContext, si *submitInf return nil } -func (sh *shareHandler) HandleSubmit(ctx *gostratum.StratumContext, event gostratum.JsonRpcEvent) error { +func (sh *shareHandler) setSoloDiff(diff float64) { + sh.soloDiff = diff +} + +func (sh *shareHandler) HandleSubmit(ctx *gostratum.StratumContext, event gostratum.JsonRpcEvent, soloMining bool) error { submitInfo, err := validateSubmit(ctx, event) if err != nil { return err @@ -234,7 +239,11 @@ func (sh *shareHandler) HandleSubmit(ctx *gostratum.StratumContext, event gostra return err } } else if powValue.Cmp(state.stratumDiff.targetValue) >= 0 { - ctx.Logger.Warn("weak block") + if (soloMining) { + ctx.Logger.Warn("weak block") + } else { + ctx.Logger.Warn("weak share") + } ctx.Logger.Warn(fmt.Sprintf("Net Target: %s", powState.Target.String())) ctx.Logger.Warn(fmt.Sprintf("Stratum Target: %s", state.stratumDiff.targetValue.String())) ctx.Logger.Warn(fmt.Sprintf("PowValue: %s", powValue.String())) @@ -313,6 +322,8 @@ func (sh *shareHandler) startStatsThread() error { str += "-------------------------------------------------------------------------------\n" var lines []string totalRate := float64(0) + + for _, v := range sh.stats { // print stats rate := GetAverageHashrateKHs(v) @@ -327,8 +338,10 @@ func (sh *shareHandler) startStatsThread() error { rateStr := stringifyHashrate(totalRate) ratioStr := fmt.Sprintf("%d/%d/%d", sh.overall.SharesFound.Load(), sh.overall.StaleShares.Load(), sh.overall.InvalidShares.Load()) str += "\n-------------------------------------------------------------------------------\n" - str += fmt.Sprintf(" | %14.14s | %14.14s | %12d | %11s", + str += fmt.Sprintf(" Total | %14.14s | %14.14s | %12d | %11s", rateStr, ratioStr, sh.overall.BlocksFound.Load(), time.Since(start).Round(time.Second)) + str += "\n-------------------------------------------------------------------------------\n" + str += " Network Hashrate: " + stringifyHashrate(DiffToHash(sh.soloDiff)) str += "\n======================================================== spr_bridge_" + version + " ===\n" // sh.statsLock.Unlock() log.Println(str) diff --git a/src/spectrestratum/stratum_server.go b/src/spectrestratum/stratum_server.go index c826c4e..7b35286 100644 --- a/src/spectrestratum/stratum_server.go +++ b/src/spectrestratum/stratum_server.go @@ -13,7 +13,7 @@ import ( "go.uber.org/zap/zapcore" ) -const version = "v0.3.15" +const version = "v0.3.16" const minBlockWaitTime = 3 * time.Second type BridgeConfig struct { @@ -23,6 +23,7 @@ type BridgeConfig struct { PrintStats bool `yaml:"print_stats"` UseLogFile bool `yaml:"log_to_file"` HealthCheckPort string `yaml:"health_check_port"` + SoloMining bool `yaml:"solo_mining"` BlockWaitTime time.Duration `yaml:"block_wait_time"` MinShareDiff uint `yaml:"min_share_diff"` VarDiff bool `yaml:"var_diff"` @@ -93,7 +94,7 @@ func ListenAndServe(cfg BridgeConfig) error { // override the submit handler with an actual useful handler handlers[string(gostratum.StratumMethodSubmit)] = func(ctx *gostratum.StratumContext, event gostratum.JsonRpcEvent) error { - if err := shareHandler.HandleSubmit(ctx, event); err != nil { + if err := shareHandler.HandleSubmit(ctx, event, cfg.SoloMining); err != nil { ctx.Logger.Sugar().Error(err) // sink error } return nil @@ -110,10 +111,10 @@ func ListenAndServe(cfg BridgeConfig) error { ctx, cancel := context.WithCancel(context.Background()) defer cancel() sprApi.Start(ctx, func() { - clientHandler.NewBlockAvailable(sprApi) + clientHandler.NewBlockAvailable(sprApi, cfg.SoloMining) }) - if cfg.VarDiff { + if cfg.VarDiff || cfg.SoloMining { go shareHandler.startVardiffThread(cfg.SharesPerMin, cfg.VarDiffStats) } From 62c5bbd22d353b67876a535aaefb58fdab048b6d Mon Sep 17 00:00:00 2001 From: Tritonn204 Date: Mon, 2 Sep 2024 13:42:02 -0700 Subject: [PATCH 2/3] Added 8.exe to gitignore, changed remaining 1e12 to 1e9 in spr_dashboard.json --- .gitignore | 3 +++ misc/grafana-templates/spr_dashboard.json | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index ea46c7d..39ac78d 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,9 @@ # Log files *.log +# executables +*.exe + # Compiled Object files, Static and Dynamic libs (Shared Objects) *.o *.a diff --git a/misc/grafana-templates/spr_dashboard.json b/misc/grafana-templates/spr_dashboard.json index 0f43580..080c3a3 100644 --- a/misc/grafana-templates/spr_dashboard.json +++ b/misc/grafana-templates/spr_dashboard.json @@ -1678,7 +1678,7 @@ "uid": "wFbInMnVk" }, "editorMode": "code", - "expr": "spr_network_difficulty_gauge / 1e12", + "expr": "spr_network_difficulty_gauge / 1e9", "hide": false, "legendFormat": "difficulty", "range": true, From 8843c1e5d3d579d5c4765d87042898da5f42d9fd Mon Sep 17 00:00:00 2001 From: Tritonn204 Date: Thu, 5 Sep 2024 11:48:17 -0700 Subject: [PATCH 3/3] gofmt ran --- src/spectrestratum/client_handler.go | 8 ++++---- src/spectrestratum/share_handler.go | 5 ++--- src/spectrestratum/stratum_server.go | 2 +- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/spectrestratum/client_handler.go b/src/spectrestratum/client_handler.go index b8fb042..81ccb2b 100644 --- a/src/spectrestratum/client_handler.go +++ b/src/spectrestratum/client_handler.go @@ -128,7 +128,7 @@ func (c *clientListener) NewBlockAvailable(kapi *SpectreApi, soloMining bool) { // first pass through send config/default difficulty state.stratumDiff = newSpectreDiff() state.stratumDiff.setDiffValue(c.minShareDiff) - if (!soloMining) { + if !soloMining { sendClientDiff(client, state) } c.shareHandler.setClientVardiff(client, c.minShareDiff) @@ -136,13 +136,13 @@ func (c *clientListener) NewBlockAvailable(kapi *SpectreApi, soloMining bool) { varDiff := TargetToDiff(&state.bigDiff) c.shareHandler.setSoloDiff(varDiff) - if (!soloMining) { - varDiff = c.shareHandler.getClientVardiff(client) + if !soloMining { + varDiff = c.shareHandler.getClientVardiff(client) } if varDiff != state.stratumDiff.diffValue { // send updated vardiff - if (!soloMining) { + if !soloMining { client.Logger.Info(fmt.Sprintf("changing diff from %f to %f", state.stratumDiff.diffValue, varDiff)) } state.stratumDiff.setDiffValue(varDiff) diff --git a/src/spectrestratum/share_handler.go b/src/spectrestratum/share_handler.go index 1507cf2..e848cee 100644 --- a/src/spectrestratum/share_handler.go +++ b/src/spectrestratum/share_handler.go @@ -42,7 +42,7 @@ type WorkStats struct { type shareHandler struct { spectre *rpcclient.RPCClient - soloDiff float64 + soloDiff float64 stats map[string]*WorkStats statsLock sync.Mutex overall WorkStats @@ -239,7 +239,7 @@ func (sh *shareHandler) HandleSubmit(ctx *gostratum.StratumContext, event gostra return err } } else if powValue.Cmp(state.stratumDiff.targetValue) >= 0 { - if (soloMining) { + if soloMining { ctx.Logger.Warn("weak block") } else { ctx.Logger.Warn("weak share") @@ -323,7 +323,6 @@ func (sh *shareHandler) startStatsThread() error { var lines []string totalRate := float64(0) - for _, v := range sh.stats { // print stats rate := GetAverageHashrateKHs(v) diff --git a/src/spectrestratum/stratum_server.go b/src/spectrestratum/stratum_server.go index 7b35286..c9396c2 100644 --- a/src/spectrestratum/stratum_server.go +++ b/src/spectrestratum/stratum_server.go @@ -23,7 +23,7 @@ type BridgeConfig struct { PrintStats bool `yaml:"print_stats"` UseLogFile bool `yaml:"log_to_file"` HealthCheckPort string `yaml:"health_check_port"` - SoloMining bool `yaml:"solo_mining"` + SoloMining bool `yaml:"solo_mining"` BlockWaitTime time.Duration `yaml:"block_wait_time"` MinShareDiff uint `yaml:"min_share_diff"` VarDiff bool `yaml:"var_diff"`