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.

155 lines
3.8 KiB

9 years ago
package stratum
import (
"log"
"regexp"
"strings"
"sync/atomic"
9 years ago
"../util"
)
var noncePattern *regexp.Regexp
const defaultWorkerId = "0"
9 years ago
func init() {
noncePattern, _ = regexp.Compile("^[0-9a-f]{8}$")
}
func (s *StratumServer) handleLoginRPC(cs *Session, e *Endpoint, params *LoginParams) (*JobReply, *ErrorReply) {
if !s.config.BypassAddressValidation && !util.ValidateAddress(params.Login, s.config.Address) {
return nil, &ErrorReply{Code: -1, Message: "Invalid address used for login", Close: true}
9 years ago
}
t := s.currentBlockTemplate()
if t == nil {
return nil, &ErrorReply{Code: -1, Message: "Job not ready", Close: true}
}
id := extractWorkerId(params.Login)
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)
9 years ago
miner.heartbeat()
8 years ago
return &JobReply{Id: id, Job: cs.getJob(t), Status: "OK"}, nil
9 years ago
}
func (s *StratumServer) handleGetJobRPC(cs *Session, e *Endpoint, params *GetJobParams) (reply *JobReplyData, errorReply *ErrorReply) {
9 years ago
miner, ok := s.miners.Get(params.Id)
if !ok {
errorReply = &ErrorReply{Code: -1, Message: "Unauthenticated", Close: true}
return
}
t := s.currentBlockTemplate()
if t == nil {
errorReply = &ErrorReply{Code: -1, Message: "Job not ready", Close: true}
return
}
9 years ago
miner.heartbeat()
8 years ago
reply = cs.getJob(t)
9 years ago
return
}
func (s *StratumServer) handleSubmitRPC(cs *Session, e *Endpoint, params *SubmitParams) (reply *SubmitReply, errorReply *ErrorReply) {
9 years ago
miner, ok := s.miners.Get(params.Id)
if !ok {
errorReply = &ErrorReply{Code: -1, Message: "Unauthenticated", Close: true}
return
}
miner.heartbeat()
8 years ago
job := cs.findJob(params.JobId)
9 years ago
if job == nil {
errorReply = &ErrorReply{Code: -1, Message: "Invalid job id", Close: true}
atomic.AddUint64(&miner.invalidShares, 1)
9 years ago
return
}
if !noncePattern.MatchString(params.Nonce) {
errorReply = &ErrorReply{Code: -1, Message: "Malformed nonce", Close: true}
atomic.AddUint64(&miner.invalidShares, 1)
9 years ago
return
}
nonce := strings.ToLower(params.Nonce)
exist := job.submit(nonce)
if exist {
errorReply = &ErrorReply{Code: -1, Message: "Duplicate share", Close: true}
atomic.AddUint64(&miner.invalidShares, 1)
9 years ago
return
}
t := s.currentBlockTemplate()
if job.height != t.Height {
log.Printf("Block expired for height %v %s@%s", job.height, miner.Id, miner.IP)
9 years ago
errorReply = &ErrorReply{Code: -1, Message: "Block expired", Close: false}
atomic.AddUint64(&miner.staleShares, 1)
9 years ago
return
}
validShare := miner.processShare(s, e, job, t, nonce, params.Result)
9 years ago
if !validShare {
errorReply = &ErrorReply{Code: -1, Message: "Low difficulty share", Close: !ok}
return
}
reply = &SubmitReply{Status: "OK"}
return
}
func (s *StratumServer) handleUnknownRPC(cs *Session, req *JSONRpcReq) *ErrorReply {
log.Printf("Unknown RPC method: %v", req)
return &ErrorReply{Code: -1, Message: "Invalid method", Close: true}
}
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)
9 years ago
n := 0
8 years ago
for m := range s.sessions {
9 years ago
n++
bcast <- n
8 years ago
go func(cs *Session) {
reply := cs.getJob(t)
err := cs.pushMessage("job", &reply)
9 years ago
<-bcast
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)
9 years ago
}
8 years ago
}(m)
9 years ago
}
}
func (s *StratumServer) refreshBlockTemplate(bcast bool) {
newBlock := s.fetchBlockTemplate()
if newBlock && bcast {
s.broadcastNewJobs()
}
}
func extractWorkerId(loginWorkerPair string) string {
parts := strings.SplitN(loginWorkerPair, ".", 2)
if len(parts) > 1 {
return parts[1]
}
return defaultWorkerId
}