Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configurable stratum diff, block refresh, and extranonce #59

Merged
merged 10 commits into from
Dec 13, 2022
Prev Previous commit
Next Next commit
Updated flags to use proper types
  • Loading branch information
rdugan committed Dec 13, 2022
commit 17a95cc70756ccf9620762784507769a8456d13c
6 changes: 3 additions & 3 deletions cmd/kaspabridge/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ kaspad_address: localhost:16110
# accurate hashrate measurements
# min_share_diff: 4

# block_wait_time: time in ms to wait since last new block message from kaspad
# before manually requesting a new block
# block_wait_time: 500
# block_wait_time: time to wait since last new block message from kaspad before
# manually requesting a new block
# block_wait_time: 500ms

# extranonce_size: size in bytes of extranonce, from 0 (no extranonce) to 3.
# With no extranonce (0), all clients will search through the same nonce-space,
Expand Down
10 changes: 5 additions & 5 deletions cmd/kaspabridge/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ func main() {
flag.StringVar(&cfg.StratumPort, "stratum", cfg.StratumPort, "stratum port to listen on, default `:5555`")
flag.BoolVar(&cfg.PrintStats, "stats", cfg.PrintStats, "true to show periodic stats to console, default `true`")
flag.StringVar(&cfg.RPCServer, "kaspa", cfg.RPCServer, "address of the kaspad node, default `localhost:16110`")
flag.StringVar(&cfg.BlockWaitTime, "blockwait", cfg.BlockWaitTime, "time in ms to wait before manually requesting new block, default `500`")
flag.StringVar(&cfg.MinShareDiff, "mindiff", cfg.MinShareDiff, "minimum share difficulty to accept from miner(s), default `4`")
flag.StringVar(&cfg.ExtranonceSize, "extranonce", cfg.ExtranonceSize, "size in bytes of extranonce, default `0`")
flag.DurationVar(&cfg.BlockWaitTime, "blockwait", cfg.BlockWaitTime, "time in ms to wait before manually requesting new block, default `500`")
flag.UintVar(&cfg.MinShareDiff, "mindiff", cfg.MinShareDiff, "minimum share difficulty to accept from miner(s), default `4`")
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`")
flag.StringVar(&cfg.HealthCheckPort, "hcp", cfg.HealthCheckPort, `(rarely used) if defined will expose a health check on /readyz, default ""`)
Expand All @@ -44,9 +44,9 @@ func main() {
log.Printf("\tprom: %s", cfg.PromPort)
log.Printf("\tstats: %t", cfg.PrintStats)
log.Printf("\tlog: %t", cfg.UseLogFile)
log.Printf("\tmin diff: %s", cfg.MinShareDiff)
log.Printf("\tmin diff: %d", cfg.MinShareDiff)
log.Printf("\tblock wait: %s", cfg.BlockWaitTime)
log.Printf("\textranonce size: %s", cfg.ExtranonceSize)
log.Printf("\textranonce size: %d", cfg.ExtranonceSize)
log.Printf("\thealth check: %s", cfg.HealthCheckPort)
log.Println("----------------------------------")

Expand Down
14 changes: 4 additions & 10 deletions src/kaspastratum/kaspaapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package kaspastratum
import (
"context"
"fmt"
"strconv"
"time"

"github.com/kaspanet/kaspad/app/appmessage"
Expand All @@ -15,13 +14,13 @@ import (

type KaspaApi struct {
address string
blockWaitTime string
blockWaitTime time.Duration
logger *zap.SugaredLogger
kaspad *rpcclient.RPCClient
connected bool
}

func NewKaspaAPI(address string, blockWaitTime string, logger *zap.SugaredLogger) (*KaspaApi, error) {
func NewKaspaAPI(address string, blockWaitTime time.Duration, logger *zap.SugaredLogger) (*KaspaApi, error) {
client, err := rpcclient.NewRPCClient(address)
if err != nil {
return nil, err
Expand Down Expand Up @@ -108,12 +107,7 @@ func (s *KaspaApi) startBlockTemplateListener(ctx context.Context, blockReadyCb
s.logger.Error("fatal: failed to register for block notifications from kaspa")
}

i, err := strconv.ParseUint(s.blockWaitTime, 10, 64)
if err != nil {
i = 500
}
tickerTime := time.Duration(i) * time.Millisecond
ticker := time.NewTicker(tickerTime)
ticker := time.NewTicker(s.blockWaitTime)
for {
if err := s.waitForSync(false); err != nil {
s.logger.Error("error checking kaspad sync state, attempting reconnect: ", err)
Expand All @@ -128,7 +122,7 @@ func (s *KaspaApi) startBlockTemplateListener(ctx context.Context, blockReadyCb
return
case <-blockReadyChan:
blockReadyCb()
ticker.Reset(tickerTime)
ticker.Reset(s.blockWaitTime)
case <-ticker.C: // timeout, manually check for new blocks
blockReadyCb()
}
Expand Down
41 changes: 23 additions & 18 deletions src/kaspastratum/stratum_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"net/http"
_ "net/http/pprof"
"os"
"strconv"
"time"

"github.com/mattn/go-colorable"
"github.com/onemorebsmith/kaspastratum/src/gostratum"
Expand All @@ -14,17 +14,18 @@ import (
)

const version = "v1.1.6"
const minBlockWaitTime = time.Duration(500) * time.Millisecond

type BridgeConfig struct {
StratumPort string `yaml:"stratum_port"`
RPCServer string `yaml:"kaspad_address"`
PromPort string `yaml:"prom_port"`
PrintStats bool `yaml:"print_stats"`
UseLogFile bool `yaml:"log_to_file"`
HealthCheckPort string `yaml:"health_check_port"`
BlockWaitTime string `yaml:"block_wait_time"`
MinShareDiff string `yaml:"min_share_diff"`
ExtranonceSize string `yaml:"extranonce_size"`
StratumPort string `yaml:"stratum_port"`
RPCServer string `yaml:"kaspad_address"`
PromPort string `yaml:"prom_port"`
PrintStats bool `yaml:"print_stats"`
UseLogFile bool `yaml:"log_to_file"`
HealthCheckPort string `yaml:"health_check_port"`
BlockWaitTime time.Duration `yaml:"block_wait_time"`
MinShareDiff uint `yaml:"min_share_diff"`
ExtranonceSize uint `yaml:"extranonce_size"`
}

func configureZap(cfg BridgeConfig) (*zap.SugaredLogger, func()) {
Expand Down Expand Up @@ -58,7 +59,11 @@ func ListenAndServe(cfg BridgeConfig) error {
StartPromServer(logger, cfg.PromPort)
}

ksApi, err := NewKaspaAPI(cfg.RPCServer, cfg.BlockWaitTime, logger)
blockWaitTime := cfg.BlockWaitTime
if blockWaitTime < minBlockWaitTime {
blockWaitTime = minBlockWaitTime
}
ksApi, err := NewKaspaAPI(cfg.RPCServer, blockWaitTime, logger)
if err != nil {
return err
}
Expand All @@ -72,15 +77,15 @@ func ListenAndServe(cfg BridgeConfig) error {
}

shareHandler := newShareHandler(ksApi.kaspad)
minDiff, err := strconv.ParseFloat(cfg.MinShareDiff, 64)
if err != nil {
minDiff = 4
minDiff := cfg.MinShareDiff
if minDiff < 1 {
minDiff = 1
}
extranonceSize, err := strconv.ParseInt(cfg.ExtranonceSize, 10, 8)
if err != nil {
extranonceSize = 0
extranonceSize := cfg.ExtranonceSize
if extranonceSize > 3 {
extranonceSize = 3
}
clientHandler := newClientListener(logger, shareHandler, minDiff, int8(extranonceSize))
clientHandler := newClientListener(logger, shareHandler, float64(minDiff), int8(extranonceSize))
handlers := gostratum.DefaultHandlers()
// override the submit handler with an actual useful handler
handlers[string(gostratum.StratumMethodSubmit)] =
Expand Down