forked from rdugan/kaspa-stratum-bridge
/
stratum_server.go
138 lines (120 loc) · 4.02 KB
/
stratum_server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package kaspastratum
import (
"context"
"math"
"net/http"
_ "net/http/pprof"
"os"
"time"
"github.com/mattn/go-colorable"
"github.com/GImDX/pyrin-stratum-bridge/src/gostratum"
"github.com/GImDX/pyrin-stratum-bridge/src/utils"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
const version = "v1.0.0"
const minBlockWaitTime = 3 * time.Second
type BridgeConfig struct {
StratumPort string `yaml:"stratum_port"`
RPCServer string `yaml:"pyipad_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"`
VarDiff bool `yaml:"var_diff"`
SharesPerMin uint `yaml:"shares_per_min"`
VarDiffStats bool `yaml:"var_diff_stats"`
ExtranonceSize uint `yaml:"extranonce_size"`
ClampPow2 bool `yaml:"pow2_clamp"`
}
func configureZap(cfg BridgeConfig) (*zap.SugaredLogger, func()) {
pe := zap.NewProductionEncoderConfig()
pe.EncodeTime = zapcore.RFC3339TimeEncoder
fileEncoder := zapcore.NewJSONEncoder(pe)
consoleEncoder := zapcore.NewConsoleEncoder(pe)
bws := &utils.BufferedWriteSyncer{WS: zapcore.AddSync(colorable.NewColorableStdout()), FlushInterval: 5 * time.Second}
if !cfg.UseLogFile {
return zap.New(zapcore.NewCore(consoleEncoder,
bws, zap.InfoLevel)).Sugar(), func() { bws.Stop() }
}
// log file fun
logFile, err := os.OpenFile("bridge.log", os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0666)
if err != nil {
panic(err)
}
blws := &utils.BufferedWriteSyncer{WS: zapcore.AddSync(logFile), FlushInterval: 5 * time.Second}
core := zapcore.NewTee(
zapcore.NewCore(fileEncoder, blws, zap.InfoLevel),
zapcore.NewCore(consoleEncoder, bws, zap.InfoLevel),
)
return zap.New(core).Sugar(), func() { bws.Stop(); blws.Stop(); logFile.Close() }
}
func ListenAndServe(cfg BridgeConfig) error {
logger, logCleanup := configureZap(cfg)
defer logCleanup()
if cfg.PromPort != "" {
StartPromServer(logger, cfg.PromPort)
}
blockWaitTime := cfg.BlockWaitTime
if blockWaitTime == 0 {
blockWaitTime = minBlockWaitTime
}
ksApi, err := NewKaspaAPI(cfg.RPCServer, blockWaitTime, logger)
if err != nil {
return err
}
if cfg.HealthCheckPort != "" {
logger.Info("enabling health check on port " + cfg.HealthCheckPort)
http.HandleFunc("/readyz", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
go http.ListenAndServe(cfg.HealthCheckPort, nil)
}
shareHandler := newShareHandler(ksApi.kaspad)
minDiff := float64(cfg.MinShareDiff)
if minDiff == 0 {
minDiff = 4
}
if cfg.ClampPow2 {
minDiff = math.Pow(2, math.Floor(math.Log2(minDiff)))
}
extranonceSize := cfg.ExtranonceSize
if extranonceSize > 3 {
extranonceSize = 3
}
clientHandler := newClientListener(logger, shareHandler, minDiff, int8(extranonceSize))
handlers := gostratum.DefaultHandlers()
// 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 {
ctx.Logger.Sugar().Error(err) // sink error
}
return nil
}
stratumConfig := gostratum.StratumListenerConfig{
Port: cfg.StratumPort,
HandlerMap: handlers,
StateGenerator: MiningStateGenerator,
ClientListener: clientHandler,
Logger: logger.Desugar(),
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ksApi.Start(ctx, func() {
clientHandler.NewBlockAvailable(ksApi)
})
if cfg.VarDiff {
sharesPerMin := cfg.SharesPerMin
if sharesPerMin <= 0 {
sharesPerMin = 20
}
go shareHandler.startVardiffThread(sharesPerMin, cfg.VarDiffStats, cfg.ClampPow2)
}
if cfg.PrintStats {
go shareHandler.startStatsThread()
}
return gostratum.NewListener(stratumConfig).Listen(context.Background())
}