Skip to content

Commit

Permalink
Merge pull request #2 from Tritonn204/main
Browse files Browse the repository at this point in the history
QOL Updates + Solo Mining Stats
  • Loading branch information
0xA001113 authored Sep 9, 2024
2 parents a438667 + 8843c1e commit 2e6ce8a
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 12 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
# Log files
*.log

# executables
*.exe

# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
Expand Down
4 changes: 4 additions & 0 deletions cmd/spectrebridge/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions cmd/spectrebridge/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion misc/grafana-templates/spr_dashboard.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
17 changes: 13 additions & 4 deletions src/spectrestratum/client_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions src/spectrestratum/hasher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 15 additions & 3 deletions src/spectrestratum/share_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type WorkStats struct {

type shareHandler struct {
spectre *rpcclient.RPCClient
soloDiff float64
stats map[string]*WorkStats
statsLock sync.Mutex
overall WorkStats
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()))
Expand Down Expand Up @@ -313,6 +322,7 @@ func (sh *shareHandler) startStatsThread() error {
str += "-------------------------------------------------------------------------------\n"
var lines []string
totalRate := float64(0)

for _, v := range sh.stats {
// print stats
rate := GetAverageHashrateKHs(v)
Expand All @@ -327,8 +337,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)
Expand Down
9 changes: 5 additions & 4 deletions src/spectrestratum/stratum_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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"`
Expand Down Expand Up @@ -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
Expand All @@ -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)
}

Expand Down

0 comments on commit 2e6ce8a

Please sign in to comment.