keva-stratum/go-pool/stratum/miner.go

204 lines
4.9 KiB
Go
Raw Normal View History

2015-07-05 09:49:07 +00:00
package stratum
import (
"bytes"
"encoding/binary"
"encoding/hex"
"log"
2016-12-07 08:42:41 +00:00
"strconv"
2015-07-05 09:49:07 +00:00
"sync"
"sync/atomic"
2016-12-06 14:25:07 +00:00
"time"
2015-07-05 09:49:07 +00:00
"../../cnutil"
"../../hashing"
"../util"
)
type Job struct {
sync.RWMutex
2016-12-06 20:54:26 +00:00
id string
extraNonce uint32
height int64
submissions map[string]struct{}
2015-07-05 09:49:07 +00:00
}
type Miner struct {
sync.RWMutex
2016-12-07 06:55:29 +00:00
id string
ip string
lastBeat int64
2016-12-06 14:25:07 +00:00
startedAt int64
validShares uint64
invalidShares uint64
staleShares uint64
accepts uint64
rejects uint64
shares map[int64]int64
2015-07-05 09:49:07 +00:00
}
func (job *Job) submit(nonce string) bool {
job.Lock()
defer job.Unlock()
2016-12-07 07:02:38 +00:00
if _, exist := job.submissions[nonce]; exist {
2015-07-05 09:49:07 +00:00
return true
}
2016-12-06 20:54:26 +00:00
job.submissions[nonce] = struct{}{}
2015-07-05 09:49:07 +00:00
return false
}
2016-12-06 20:29:59 +00:00
func NewMiner(id string, ip string) *Miner {
2016-12-06 15:03:42 +00:00
shares := make(map[int64]int64)
2016-12-07 06:55:29 +00:00
return &Miner{id: id, ip: ip, shares: shares}
2015-07-05 09:49:07 +00:00
}
2016-12-06 20:29:59 +00:00
func (cs *Session) getJob(t *BlockTemplate) *JobReplyData {
2016-12-07 07:00:18 +00:00
height := atomic.SwapInt64(&cs.lastBlockHeight, t.height)
2015-07-05 09:49:07 +00:00
2016-12-07 07:00:18 +00:00
if height == t.height {
2016-12-06 20:29:59 +00:00
return &JobReplyData{}
2015-07-05 09:49:07 +00:00
}
2016-12-06 20:29:59 +00:00
extraNonce := atomic.AddUint32(&cs.endpoint.extraNonce, 1)
blob := t.nextBlob(extraNonce, cs.endpoint.instanceId)
2016-12-07 08:42:41 +00:00
id := atomic.AddUint64(&cs.endpoint.jobSequence, 1)
2016-12-07 11:49:50 +00:00
job := &Job{
id: strconv.FormatUint(id, 10),
extraNonce: extraNonce,
height: t.height,
}
2016-12-06 20:54:26 +00:00
job.submissions = make(map[string]struct{})
2016-12-06 20:29:59 +00:00
cs.pushJob(job)
2016-12-07 08:57:02 +00:00
reply := &JobReplyData{JobId: job.id, Blob: blob, Target: cs.endpoint.targetHex}
2016-12-06 20:29:59 +00:00
return reply
2015-07-05 09:49:07 +00:00
}
2016-12-06 20:29:59 +00:00
func (cs *Session) pushJob(job *Job) {
cs.Lock()
defer cs.Unlock()
cs.validJobs = append(cs.validJobs, job)
2015-07-05 09:49:07 +00:00
2016-12-06 20:29:59 +00:00
if len(cs.validJobs) > 4 {
cs.validJobs = cs.validJobs[1:]
2015-07-05 09:49:07 +00:00
}
2016-12-06 20:29:59 +00:00
}
2015-07-05 09:49:07 +00:00
2016-12-06 20:29:59 +00:00
func (cs *Session) findJob(id string) *Job {
cs.Lock()
defer cs.Unlock()
for _, job := range cs.validJobs {
2016-12-06 20:54:26 +00:00
if job.id == id {
2016-12-06 20:29:59 +00:00
return job
}
}
return nil
2015-07-05 09:49:07 +00:00
}
func (m *Miner) heartbeat() {
now := util.MakeTimestamp()
2016-12-07 06:55:29 +00:00
atomic.StoreInt64(&m.lastBeat, now)
2015-07-05 09:49:07 +00:00
}
2016-12-06 14:25:07 +00:00
func (m *Miner) getLastBeat() int64 {
2016-12-07 06:55:29 +00:00
return atomic.LoadInt64(&m.lastBeat)
2016-12-06 14:25:07 +00:00
}
func (m *Miner) storeShare(diff int64) {
2016-12-06 15:03:42 +00:00
now := util.MakeTimestamp() / 1000
2016-12-06 14:25:07 +00:00
m.Lock()
m.shares[now] += diff
m.Unlock()
}
2016-12-06 15:03:42 +00:00
func (m *Miner) hashrate(estimationWindow time.Duration) float64 {
now := util.MakeTimestamp() / 1000
2016-12-06 14:25:07 +00:00
totalShares := int64(0)
2016-12-06 15:03:42 +00:00
window := int64(estimationWindow / time.Second)
2016-12-06 14:25:07 +00:00
boundary := now - m.startedAt
if boundary > window {
boundary = window
}
m.Lock()
for k, v := range m.shares {
2016-12-06 15:03:42 +00:00
if k < now-86400 {
2016-12-06 14:25:07 +00:00
delete(m.shares, k)
} else if k >= now-window {
totalShares += v
}
}
m.Unlock()
return float64(totalShares) / float64(boundary)
}
2016-12-06 12:07:45 +00:00
func (m *Miner) processShare(s *StratumServer, e *Endpoint, job *Job, t *BlockTemplate, nonce string, result string) bool {
2016-12-06 17:16:57 +00:00
r := s.rpc()
2016-12-07 07:00:18 +00:00
shareBuff := make([]byte, len(t.buffer))
copy(shareBuff, t.buffer)
copy(shareBuff[t.reservedOffset+4:t.reservedOffset+7], e.instanceId)
2015-07-05 09:49:07 +00:00
extraBuff := new(bytes.Buffer)
2016-12-06 20:54:26 +00:00
binary.Write(extraBuff, binary.BigEndian, job.extraNonce)
2016-12-07 07:00:18 +00:00
copy(shareBuff[t.reservedOffset:], extraBuff.Bytes())
2015-07-05 09:49:07 +00:00
nonceBuff, _ := hex.DecodeString(nonce)
copy(shareBuff[39:], nonceBuff)
2016-12-05 16:28:58 +00:00
var hashBytes, convertedBlob []byte
2015-07-05 09:49:07 +00:00
2016-12-05 16:28:58 +00:00
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 {
2016-12-07 06:55:29 +00:00
log.Printf("Bad hash from miner %v@%v", m.id, m.ip)
2016-12-06 14:25:07 +00:00
atomic.AddUint64(&m.invalidShares, 1)
2015-07-05 09:49:07 +00:00
return false
}
hashDiff := util.GetHashDifficulty(hashBytes).Int64() // FIXME: Will return max int64 value if overflows
2016-12-06 15:03:42 +00:00
atomic.AddInt64(&s.roundShares, e.config.Difficulty)
atomic.AddUint64(&m.validShares, 1)
m.storeShare(e.config.Difficulty)
2016-12-07 07:00:18 +00:00
block := hashDiff >= t.difficulty
2016-12-06 15:03:42 +00:00
2015-07-05 09:49:07 +00:00
if block {
2016-12-06 17:16:57 +00:00
_, err := r.SubmitBlock(hex.EncodeToString(shareBuff))
2015-07-05 09:49:07 +00:00
if err != nil {
2016-12-06 14:25:07 +00:00
atomic.AddUint64(&m.rejects, 1)
2016-12-06 17:16:57 +00:00
atomic.AddUint64(&r.Rejects, 1)
2016-12-07 07:00:18 +00:00
log.Printf("Block submission failure at height %v: %v", t.height, err)
2015-07-05 09:49:07 +00:00
} else {
2016-12-05 16:28:58 +00:00
if len(convertedBlob) == 0 {
convertedBlob = cnutil.ConvertBlob(shareBuff)
}
2015-07-05 09:49:07 +00:00
blockFastHash := hex.EncodeToString(hashing.FastHash(convertedBlob))
2016-12-07 08:02:35 +00:00
now := util.MakeTimestamp()
roundShares := atomic.SwapInt64(&s.roundShares, 0)
ratio := float64(roundShares) / float64(t.difficulty)
s.blocksMu.Lock()
s.blockStats[now] = ratio
s.blocksMu.Unlock()
2016-12-06 14:25:07 +00:00
atomic.AddUint64(&m.accepts, 1)
2016-12-06 17:16:57 +00:00
atomic.AddUint64(&r.Accepts, 1)
2016-12-07 08:02:35 +00:00
atomic.StoreInt64(&r.LastSubmissionAt, now)
log.Printf("Block %s found at height %d by miner %v@%v with ratio %.4f", blockFastHash[0:6], t.height, m.id, m.ip, ratio)
// Immediately refresh current BT and send new jobs
s.refreshBlockTemplate(true)
2015-07-05 09:49:07 +00:00
}
2016-12-07 11:49:50 +00:00
} else if hashDiff < e.config.Difficulty {
2016-12-07 06:55:29 +00:00
log.Printf("Rejected low difficulty share of %v from %v@%v", hashDiff, m.id, m.ip)
2016-12-06 14:25:07 +00:00
atomic.AddUint64(&m.invalidShares, 1)
2015-07-05 09:49:07 +00:00
return false
}
2016-12-06 12:07:45 +00:00
log.Printf("Valid share at difficulty %v/%v", e.config.Difficulty, hashDiff)
2015-07-05 09:49:07 +00:00
return true
}