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.

147 lines
3.5 KiB

10 years ago
package stratum
import (
"log"
"regexp"
"strings"
"sync/atomic"
"../util"
10 years ago
)
var noncePattern *regexp.Regexp
const defaultWorkerId = "0"
10 years ago
func init() {
noncePattern, _ = regexp.Compile("^[0-9a-f]{8}$")
}
8 years ago
func (s *StratumServer) handleLoginRPC(cs *Session, params *LoginParams) (*JobReply, *ErrorReply) {
address, id := extractWorkerId(params.Login)
if !s.config.BypassAddressValidation {
if !s.config.IsKeva && !util.ValidateAddress(address, s.config.Address) {
log.Printf("Invalid address %s used for login by %s", address, cs.ip)
return nil, &ErrorReply{Code: -1, Message: "Invalid address used for login"}
}
}
10 years ago
t := s.currentBlockTemplate()
if t == nil {
return nil, &ErrorReply{Code: -1, Message: "Job not ready"}
}
miner, ok := s.miners.Get(id)
if !ok {
8 years ago
miner = NewMiner(id, cs.ip)
s.registerMiner(miner)
}
log.Printf("Miner connected %s@%s", id, cs.ip)
8 years ago
s.registerSession(cs)
10 years ago
miner.heartbeat()
8 years ago
return &JobReply{Id: id, Job: cs.getJob(t), Status: "OK"}, nil
10 years ago
}
8 years ago
func (s *StratumServer) handleGetJobRPC(cs *Session, params *GetJobParams) (*JobReplyData, *ErrorReply) {
10 years ago
miner, ok := s.miners.Get(params.Id)
if !ok {
return nil, &ErrorReply{Code: -1, Message: "Unauthenticated"}
10 years ago
}
t := s.currentBlockTemplate()
if t == nil {
return nil, &ErrorReply{Code: -1, Message: "Job not ready"}
}
10 years ago
miner.heartbeat()
return cs.getJob(t), nil
10 years ago
}
func (s *StratumServer) handleSubmitRPC(cs *Session, params *SubmitParams) (*StatusReply, *ErrorReply) {
10 years ago
miner, ok := s.miners.Get(params.Id)
if !ok {
return nil, &ErrorReply{Code: -1, Message: "Unauthenticated"}
10 years ago
}
miner.heartbeat()
8 years ago
job := cs.findJob(params.JobId)
10 years ago
if job == nil {
return nil, &ErrorReply{Code: -1, Message: "Invalid job id"}
10 years ago
}
if !noncePattern.MatchString(params.Nonce) {
return nil, &ErrorReply{Code: -1, Message: "Malformed nonce"}
10 years ago
}
nonce := strings.ToLower(params.Nonce)
exist := job.submit(nonce)
if exist {
atomic.AddInt64(&miner.invalidShares, 1)
return nil, &ErrorReply{Code: -1, Message: "Duplicate share"}
10 years ago
}
t := s.currentBlockTemplate()
if job.height != t.height {
log.Printf("Stale share for height %d from %s@%s", job.height, miner.id, cs.ip)
atomic.AddInt64(&miner.staleShares, 1)
return nil, &ErrorReply{Code: -1, Message: "Block expired"}
10 years ago
}
validShare := miner.processShare(s, cs, job, t, nonce, params.Result)
10 years ago
if !validShare {
return nil, &ErrorReply{Code: -1, Message: "Low difficulty share"}
10 years ago
}
return &StatusReply{Status: "OK"}, nil
10 years ago
}
8 years ago
func (s *StratumServer) handleUnknownRPC(req *JSONRpcReq) *ErrorReply {
10 years ago
log.Printf("Unknown RPC method: %v", req)
return &ErrorReply{Code: -1, Message: "Invalid method"}
10 years ago
}
func (s *StratumServer) broadcastNewJobs() {
t := s.currentBlockTemplate()
if t == nil {
return
}
8 years ago
s.sessionsMu.RLock()
defer s.sessionsMu.RUnlock()
count := len(s.sessions)
log.Printf("Broadcasting new jobs to %d miners", count)
bcast := make(chan int, 1024*16)
n := 0
10 years ago
8 years ago
for m := range s.sessions {
n++
bcast <- n
8 years ago
go func(cs *Session) {
reply := cs.getJob(t)
err := cs.pushMessage("job", &reply)
<-bcast
10 years ago
if err != nil {
8 years ago
log.Printf("Job transmit error to %s: %v", cs.ip, err)
s.removeSession(cs)
} else {
8 years ago
s.setDeadline(cs.conn)
10 years ago
}
8 years ago
}(m)
10 years ago
}
}
func (s *StratumServer) refreshBlockTemplate(bcast bool) {
newBlock := s.fetchBlockTemplate()
if newBlock && bcast {
s.broadcastNewJobs()
}
}
func extractWorkerId(loginWorkerPair string) (string, string) {
parts := strings.SplitN(loginWorkerPair, ".", 2)
if len(parts) > 1 {
return parts[0], parts[1]
}
return loginWorkerPair, defaultWorkerId
}