Kevacoin stratum server for solo-mining
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

364 lines
8.2 KiB

9 years ago
package stratum
import (
"bufio"
"crypto/rand"
"encoding/json"
"fmt"
"io"
"log"
"net"
"sync"
"sync/atomic"
"time"
"../pool"
"../rpc"
8 years ago
"../util"
9 years ago
)
type StratumServer struct {
config *pool.Config
miners MinersMap
blockTemplate atomic.Value
upstream int32
upstreams []*rpc.RPCClient
timeout time.Duration
estimationWindow time.Duration
blocksMu sync.RWMutex
blockStats map[int64]float64
luckWindow int64
luckLargeWindow int64
roundShares int64
8 years ago
sessionsMu sync.RWMutex
sessions map[*Session]struct{}
}
type Endpoint struct {
config *pool.Port
instanceId []byte
extraNonce uint32
9 years ago
}
type Session struct {
sync.Mutex
8 years ago
conn *net.TCPConn
enc *json.Encoder
ip string
endpoint *Endpoint
difficulty int64
validJobs []*Job
lastBlockHeight int64
target uint32
targetHex string
9 years ago
}
const (
MaxReqSize = 10 * 1024
)
func NewStratum(cfg *pool.Config) *StratumServer {
stratum := &StratumServer{config: cfg}
stratum.upstreams = make([]*rpc.RPCClient, len(cfg.Upstream))
for i, v := range cfg.Upstream {
client, err := rpc.NewRPCClient(&v)
if err != nil {
log.Fatal(err)
} else {
stratum.upstreams[i] = client
log.Printf("Upstream: %s => %s", client.Name, client.Url)
}
}
log.Printf("Default upstream: %s => %s", stratum.rpc().Name, stratum.rpc().Url)
9 years ago
stratum.miners = NewMinersMap()
8 years ago
stratum.sessions = make(map[*Session]struct{})
9 years ago
timeout, _ := time.ParseDuration(cfg.Stratum.Timeout)
stratum.timeout = timeout
8 years ago
estimationWindow, _ := time.ParseDuration(cfg.EstimationWindow)
stratum.estimationWindow = estimationWindow
luckWindow, _ := time.ParseDuration(cfg.LuckWindow)
stratum.luckWindow = int64(luckWindow / time.Millisecond)
luckLargeWindow, _ := time.ParseDuration(cfg.LargeLuckWindow)
stratum.luckLargeWindow = int64(luckLargeWindow / time.Millisecond)
refreshIntv, _ := time.ParseDuration(cfg.BlockRefreshInterval)
9 years ago
refreshTimer := time.NewTimer(refreshIntv)
log.Printf("Set block refresh every %v", refreshIntv)
checkIntv, _ := time.ParseDuration(cfg.UpstreamCheckInterval)
checkTimer := time.NewTimer(checkIntv)
// Init block template
stratum.refreshBlockTemplate(false)
9 years ago
go func() {
for {
select {
case <-refreshTimer.C:
stratum.refreshBlockTemplate(true)
refreshTimer.Reset(refreshIntv)
}
}
}()
go func() {
for {
select {
case <-checkTimer.C:
stratum.checkUpstreams()
checkTimer.Reset(checkIntv)
}
}
}()
9 years ago
return stratum
}
func NewEndpoint(cfg *pool.Port) *Endpoint {
e := &Endpoint{config: cfg}
e.instanceId = make([]byte, 4)
_, err := rand.Read(e.instanceId)
if err != nil {
log.Fatalf("Can't seed with random bytes: %v", err)
}
return e
}
9 years ago
func (s *StratumServer) Listen() {
quit := make(chan bool)
for _, port := range s.config.Stratum.Ports {
go func(cfg pool.Port) {
e := NewEndpoint(&cfg)
e.Listen(s)
}(port)
}
<-quit
}
func (e *Endpoint) Listen(s *StratumServer) {
bindAddr := fmt.Sprintf("%s:%d", e.config.Host, e.config.Port)
9 years ago
addr, err := net.ResolveTCPAddr("tcp", bindAddr)
if err != nil {
log.Fatalf("Error: %v", err)
}
9 years ago
server, err := net.ListenTCP("tcp", addr)
if err != nil {
log.Fatalf("Error: %v", err)
}
9 years ago
defer server.Close()
log.Printf("Stratum listening on %s", bindAddr)
accept := make(chan int, e.config.MaxConn)
9 years ago
n := 0
for {
conn, err := server.AcceptTCP()
if err != nil {
continue
}
9 years ago
conn.SetKeepAlive(true)
ip, _, _ := net.SplitHostPort(conn.RemoteAddr().String())
8 years ago
cs := &Session{conn: conn, ip: ip, enc: json.NewEncoder(conn), endpoint: e}
9 years ago
n += 1
accept <- n
go func() {
s.handleClient(cs, e)
9 years ago
<-accept
}()
}
}
func (s *StratumServer) handleClient(cs *Session, e *Endpoint) {
8 years ago
_, targetHex := util.GetTargetHex(e.config.Difficulty)
cs.targetHex = targetHex
connbuff := bufio.NewReaderSize(cs.conn, MaxReqSize)
s.setDeadline(cs.conn)
9 years ago
for {
data, isPrefix, err := connbuff.ReadLine()
if isPrefix {
log.Println("Socket flood detected from", cs.ip)
break
9 years ago
} else if err == io.EOF {
log.Println("Client disconnected", cs.ip)
9 years ago
break
} else if err != nil {
log.Println("Error reading:", err)
break
9 years ago
}
// NOTICE: cpuminer-multi sends junk newlines, so we demand at least 1 byte for decode
// NOTICE: Ns*CNMiner.exe will send malformed JSON on very low diff, not sure we should handle this
if len(data) > 1 {
var req JSONRpcReq
err = json.Unmarshal(data, &req)
if err != nil {
log.Printf("Malformed request from: %v", cs.ip, err)
break
9 years ago
}
8 years ago
s.setDeadline(cs.conn)
err = cs.handleMessage(s, e, &req)
if err != nil {
break
}
9 years ago
}
}
s.removeSession(cs)
cs.conn.Close()
9 years ago
}
func (cs *Session) handleMessage(s *StratumServer, e *Endpoint, req *JSONRpcReq) error {
9 years ago
if req.Id == nil {
err := fmt.Errorf("Server disconnect request")
log.Println(err)
return err
9 years ago
} else if req.Params == nil {
err := fmt.Errorf("Server RPC request params")
log.Println(err)
return err
9 years ago
}
// Handle RPC methods
switch req.Method {
9 years ago
case "login":
var params LoginParams
err := json.Unmarshal(*req.Params, &params)
9 years ago
if err != nil {
log.Println("Unable to parse params")
return err
9 years ago
}
reply, errReply := s.handleLoginRPC(cs, e, &params)
9 years ago
if errReply != nil {
return cs.sendError(req.Id, errReply, true)
9 years ago
}
return cs.sendResult(req.Id, &reply)
9 years ago
case "getjob":
var params GetJobParams
err := json.Unmarshal(*req.Params, &params)
9 years ago
if err != nil {
log.Println("Unable to parse params")
return err
9 years ago
}
reply, errReply := s.handleGetJobRPC(cs, e, &params)
9 years ago
if errReply != nil {
return cs.sendError(req.Id, errReply, true)
9 years ago
}
return cs.sendResult(req.Id, &reply)
9 years ago
case "submit":
var params SubmitParams
err := json.Unmarshal(*req.Params, &params)
if err != nil {
log.Println("Unable to parse params")
return err
9 years ago
}
reply, errReply := s.handleSubmitRPC(cs, e, &params)
9 years ago
if errReply != nil {
return cs.sendError(req.Id, errReply, false)
9 years ago
}
return cs.sendResult(req.Id, &reply)
9 years ago
default:
errReply := s.handleUnknownRPC(cs, req)
return cs.sendError(req.Id, errReply, true)
9 years ago
}
}
func (cs *Session) sendResult(id *json.RawMessage, result interface{}) error {
cs.Lock()
defer cs.Unlock()
message := JSONRpcResp{Id: id, Version: "2.0", Error: nil, Result: result}
return cs.enc.Encode(&message)
}
func (cs *Session) pushMessage(method string, params interface{}) error {
cs.Lock()
defer cs.Unlock()
message := JSONPushMessage{Version: "2.0", Method: method, Params: params}
return cs.enc.Encode(&message)
}
func (cs *Session) sendError(id *json.RawMessage, reply *ErrorReply, drop bool) error {
9 years ago
cs.Lock()
defer cs.Unlock()
message := JSONRpcResp{Id: id, Version: "2.0", Error: reply}
err := cs.enc.Encode(&message)
if err != nil {
return err
}
if drop {
return fmt.Errorf("Server disconnect request")
9 years ago
}
return nil
9 years ago
}
func (s *StratumServer) setDeadline(conn *net.TCPConn) {
conn.SetDeadline(time.Now().Add(s.timeout))
}
8 years ago
func (s *StratumServer) registerSession(cs *Session) {
s.sessionsMu.Lock()
defer s.sessionsMu.Unlock()
s.sessions[cs] = struct{}{}
}
func (s *StratumServer) removeSession(cs *Session) {
s.sessionsMu.Lock()
defer s.sessionsMu.Unlock()
delete(s.sessions, cs)
}
func (s *StratumServer) isActive(cs *Session) bool {
s.sessionsMu.RLock()
defer s.sessionsMu.RUnlock()
_, exist := s.sessions[cs]
return exist
}
9 years ago
func (s *StratumServer) registerMiner(miner *Miner) {
s.miners.Set(miner.id, miner)
9 years ago
}
func (s *StratumServer) removeMiner(id string) {
s.miners.Remove(id)
}
func (s *StratumServer) currentBlockTemplate() *BlockTemplate {
if t := s.blockTemplate.Load(); t != nil {
return t.(*BlockTemplate)
}
return nil
9 years ago
}
func (s *StratumServer) checkUpstreams() {
candidate := int32(0)
backup := false
for i, v := range s.upstreams {
ok, err := v.Check(8, s.config.Address)
if err != nil {
log.Printf("Upstream %v didn't pass check: %v", v.Name, err)
}
if ok && !backup {
candidate = int32(i)
backup = true
}
}
if s.upstream != candidate {
log.Printf("Switching to %v upstream", s.upstreams[candidate].Name)
atomic.StoreInt32(&s.upstream, candidate)
}
}
func (s *StratumServer) rpc() *rpc.RPCClient {
i := atomic.LoadInt32(&s.upstream)
return s.upstreams[i]
}