Skip to content

Commit

Permalink
Merge pull request #19 from Leon1777/solo
Browse files Browse the repository at this point in the history
[Feat]: Add solo mining config
  • Loading branch information
lemois-1337 authored Sep 19, 2024
2 parents 778dc66 + 76f7f95 commit 28482e1
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: 1.21
go-version: 1.23.1

- name: Update sources
if: matrix.TARGET == 'linux/aarch64' || matrix.TARGET == 'windows/x64'
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Shares-based work allocation with miner-like periodic stat output:
lemois | 0.13GH/s | 3/0/0 | 0 | 6m48s
-------------------------------------------------------------------------------
| 0.13GH/s | 3/0/0 | 0 | 7m20s
========================================================= kls_bridge_v2.1.0 ===
========================================================= kls_bridge_v2.2.0 ===
```

## Variable difficulty engine (vardiff)
Expand Down
3 changes: 3 additions & 0 deletions cmd/karlsenbridge/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ karlsend_address: localhost:42110
# prefix handling for testnet addresses is applied
testnet_mining: false

# solo_mining: if true, use network difficulty instead of stratum variable difficulty (vardiff)
solo_mining: false

# min_share_diff: only accept shares of the specified difficulty (or higher)
# from the miner(s). Higher values will reduce the number of shares submitted,
# thereby reducing network traffic and server load, while lower values will
Expand Down
2 changes: 2 additions & 0 deletions cmd/karlsenbridge/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func main() {
flag.BoolVar(&cfg.VarDiff, "vardiff", cfg.VarDiff, "true to enable auto-adjusting variable min diff, default `true`")
flag.UintVar(&cfg.SharesPerMin, "sharespermin", cfg.SharesPerMin, "number of shares per minute the vardiff engine should target, default `15`")
flag.BoolVar(&cfg.VarDiffStats, "vardiffstats", cfg.VarDiffStats, "include vardiff stats readout every 10s in log, default `false`")
flag.BoolVar(&cfg.SoloMining, "solo", cfg.SoloMining, "set to true to use network difficulty instead of stratum variable difficulty (vardiff)")
flag.UintVar(&cfg.ExtranonceSize, "extranonce", cfg.ExtranonceSize, "size in bytes of extranonce, default `0`")
flag.StringVar(&cfg.PromPort, "prom", cfg.PromPort, "address to serve prom stats, default `:2112`")
flag.BoolVar(&cfg.UseLogFile, "log", cfg.UseLogFile, "if true will output errors to log file, default `true`")
Expand All @@ -50,6 +51,7 @@ func main() {
log.Printf("\tlog: %t", cfg.UseLogFile)
log.Printf("\tmin diff: %d", cfg.MinShareDiff)
log.Printf("\tvar diff: %t", cfg.VarDiff)
log.Printf("\tsolo mining: %t", cfg.SoloMining)
log.Printf("\tshares per min: %d", cfg.SharesPerMin)
log.Printf("\tvar diff stats: %t", cfg.VarDiffStats)
log.Printf("\tblock wait: %s", cfg.BlockWaitTime)
Expand Down
2 changes: 1 addition & 1 deletion misc/hive-templates/kls_srb_local_adapter.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"url": "YOUR_KARLSEND_NODE_IP_HERE:42110",
"miner": "kls_bridge",
"template": "doesnt.matter",
"install_url": "https://github.com/karlsen-network/karlsen-stratum-bridge/releases/download/v2.1.0/kls_bridge-v2.1.0_hive.tar.gz"
"install_url": "https://github.com/karlsen-network/karlsen-stratum-bridge/releases/download/v2.2.0/kls_bridge-v2.2.0_hive.tar.gz"
},
"pool_geo": []
}
Expand Down
17 changes: 13 additions & 4 deletions src/karlsenstratum/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 *KarlsenApi) {
func (c *clientListener) NewBlockAvailable(kapi *KarlsenApi, 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 *KarlsenApi) {
// first pass through send config/default difficulty
state.stratumDiff = newKarlsenDiff()
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/karlsenstratum/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
17 changes: 14 additions & 3 deletions src/karlsenstratum/share_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type WorkStats struct {

type shareHandler struct {
karlsen *rpcclient.RPCClient
soloDiff float64
stats map[string]*WorkStats
statsLock sync.Mutex
overall WorkStats
Expand Down Expand Up @@ -150,7 +151,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 @@ -213,7 +218,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")
}
RecordWeakShare(ctx)
return ctx.ReplyLowDiffShare(event.Id)
}
Expand Down Expand Up @@ -303,8 +312,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========================================================= kls_bridge_" + version + " ===\n"
// sh.statsLock.Unlock()
log.Println(str)
Expand Down
9 changes: 5 additions & 4 deletions src/karlsenstratum/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 = "v2.1.0"
const version = "v2.2.0"
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 @@ -94,7 +95,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 @@ -111,10 +112,10 @@ func ListenAndServe(cfg BridgeConfig) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ksApi.Start(ctx, func() {
clientHandler.NewBlockAvailable(ksApi)
clientHandler.NewBlockAvailable(ksApi, cfg.SoloMining)
})

if cfg.VarDiff {
if cfg.VarDiff || cfg.SoloMining {
go shareHandler.startVardiffThread(cfg.SharesPerMin, cfg.VarDiffStats)
}

Expand Down

0 comments on commit 28482e1

Please sign in to comment.