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.

207 lines
5.0 KiB

9 years ago
package stratum
import (
"bytes"
"encoding/binary"
"encoding/hex"
"log"
"sync"
"sync/atomic"
"time"
9 years ago
"../../cnutil"
"../../hashing"
"../util"
)
type Job struct {
sync.RWMutex
Id string
ExtraNonce uint32
Height int64
Difficulty int64
Submissions map[string]bool
}
type Miner struct {
sync.RWMutex
Id string
IP string
Difficulty int64
ValidJobs []*Job
LastBlockHeight int64
Target uint32
TargetHex string
LastBeat int64
Session *Session
Endpoint *Endpoint
startedAt int64
validShares uint64
invalidShares uint64
staleShares uint64
accepts uint64
rejects uint64
shares map[int64]int64
9 years ago
}
func (job *Job) submit(nonce string) bool {
job.Lock()
defer job.Unlock()
_, exist := job.Submissions[nonce]
if exist {
return true
}
job.Submissions[nonce] = true
return false
}
func NewMiner(id string, diff int64, ip string) *Miner {
8 years ago
shares := make(map[int64]int64)
miner := &Miner{Id: id, Difficulty: diff, IP: ip, shares: shares}
9 years ago
target, targetHex := util.GetTargetHex(diff)
miner.Target = target
miner.TargetHex = targetHex
return miner
}
func (m *Miner) pushJob(job *Job) {
m.Lock()
defer m.Unlock()
m.ValidJobs = append(m.ValidJobs, job)
if len(m.ValidJobs) > 4 {
m.ValidJobs = m.ValidJobs[1:]
}
}
func (m *Miner) getJob(t *BlockTemplate, force bool) *JobReplyData {
9 years ago
height := atomic.SwapInt64(&m.LastBlockHeight, t.Height)
if !force && height == t.Height {
9 years ago
return &JobReplyData{}
}
extraNonce := atomic.AddUint32(&m.Endpoint.extraNonce, 1)
blob := t.nextBlob(extraNonce, m.Endpoint.instanceId)
9 years ago
job := &Job{Id: util.Random(), ExtraNonce: extraNonce, Height: t.Height, Difficulty: m.Difficulty}
job.Submissions = make(map[string]bool)
m.pushJob(job)
reply := &JobReplyData{JobId: job.Id, Blob: blob, Target: m.TargetHex}
return reply
}
func (m *Miner) heartbeat() {
now := util.MakeTimestamp()
atomic.StoreInt64(&m.LastBeat, now)
}
func (m *Miner) getLastBeat() int64 {
return atomic.LoadInt64(&m.LastBeat)
}
func (m *Miner) storeShare(diff int64) {
8 years ago
now := util.MakeTimestamp() / 1000
m.Lock()
m.shares[now] += diff
m.Unlock()
}
8 years ago
func (m *Miner) hashrate(estimationWindow time.Duration) float64 {
now := util.MakeTimestamp() / 1000
totalShares := int64(0)
8 years ago
window := int64(estimationWindow / time.Second)
boundary := now - m.startedAt
if boundary > window {
boundary = window
}
m.Lock()
for k, v := range m.shares {
8 years ago
if k < now-86400 {
delete(m.shares, k)
} else if k >= now-window {
totalShares += v
}
}
m.Unlock()
return float64(totalShares) / float64(boundary)
}
9 years ago
func (m *Miner) findJob(id string) *Job {
m.RLock()
defer m.RUnlock()
for _, job := range m.ValidJobs {
if job.Id == id {
return job
}
}
return nil
}
func (m *Miner) processShare(s *StratumServer, e *Endpoint, job *Job, t *BlockTemplate, nonce string, result string) bool {
r := s.rpc()
9 years ago
shareBuff := make([]byte, len(t.Buffer))
copy(shareBuff, t.Buffer)
copy(shareBuff[t.ReservedOffset+4:t.ReservedOffset+7], e.instanceId)
9 years ago
extraBuff := new(bytes.Buffer)
binary.Write(extraBuff, binary.BigEndian, job.ExtraNonce)
copy(shareBuff[t.ReservedOffset:], extraBuff.Bytes())
nonceBuff, _ := hex.DecodeString(nonce)
copy(shareBuff[39:], nonceBuff)
var hashBytes, convertedBlob []byte
9 years ago
if s.config.BypassShareValidation {
hashBytes, _ = hex.DecodeString(result)
} else {
convertedBlob = cnutil.ConvertBlob(shareBuff)
hashBytes = hashing.Hash(convertedBlob, false)
}
if !s.config.BypassShareValidation && hex.EncodeToString(hashBytes) != result {
log.Printf("Bad hash from miner %v@%v", m.Id, m.IP)
atomic.AddUint64(&m.invalidShares, 1)
9 years ago
return false
}
hashDiff := util.GetHashDifficulty(hashBytes).Int64() // FIXME: Will return max int64 value if overflows
8 years ago
atomic.AddInt64(&s.roundShares, e.config.Difficulty)
atomic.AddUint64(&m.validShares, 1)
m.storeShare(e.config.Difficulty)
9 years ago
block := hashDiff >= t.Difficulty
8 years ago
9 years ago
if block {
_, err := r.SubmitBlock(hex.EncodeToString(shareBuff))
9 years ago
if err != nil {
atomic.AddUint64(&m.rejects, 1)
atomic.AddUint64(&r.Rejects, 1)
9 years ago
log.Printf("Block submission failure at height %v: %v", t.Height, err)
} else {
if len(convertedBlob) == 0 {
convertedBlob = cnutil.ConvertBlob(shareBuff)
}
9 years ago
blockFastHash := hex.EncodeToString(hashing.FastHash(convertedBlob))
// Immediately refresh current BT and send new jobs
s.refreshBlockTemplate(true)
atomic.AddUint64(&m.accepts, 1)
atomic.AddUint64(&r.Accepts, 1)
atomic.StoreInt64(&r.LastSubmissionAt, util.MakeTimestamp())
log.Printf("Block %v found at height %v by miner %v@%v", blockFastHash[0:6], t.Height, m.Id, m.IP)
9 years ago
}
} else if hashDiff < job.Difficulty {
log.Printf("Rejected low difficulty share of %v from %v@%v", hashDiff, m.Id, m.IP)
atomic.AddUint64(&m.invalidShares, 1)
9 years ago
return false
}
log.Printf("Valid share at difficulty %v/%v", e.config.Difficulty, hashDiff)
atomic.AddUint64(&m.validShares, 1)
9 years ago
return true
}