197 lines
4.7 KiB
Go
Raw Normal View History

2015-07-05 14:49:07 +05:00
package stratum
import (
"bytes"
"encoding/binary"
"encoding/hex"
"log"
"sync"
"sync/atomic"
2016-12-06 19:25:07 +05:00
"time"
2015-07-05 14:49:07 +05:00
"../../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
2016-12-07 01:29:59 +05:00
Id string
IP string
LastBeat int64
Endpoint *Endpoint
2016-12-06 19:25:07 +05:00
startedAt int64
validShares uint64
invalidShares uint64
staleShares uint64
accepts uint64
rejects uint64
shares map[int64]int64
2015-07-05 14:49:07 +05:00
}
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
}
2016-12-07 01:29:59 +05:00
func NewMiner(id string, ip string) *Miner {
2016-12-06 20:03:42 +05:00
shares := make(map[int64]int64)
2016-12-07 01:29:59 +05:00
return &Miner{Id: id, IP: ip, shares: shares}
2015-07-05 14:49:07 +05:00
}
2016-12-07 01:29:59 +05:00
func (cs *Session) getJob(t *BlockTemplate) *JobReplyData {
height := atomic.SwapInt64(&cs.lastBlockHeight, t.Height)
2015-07-05 14:49:07 +05:00
2016-12-07 01:29:59 +05:00
if height == t.Height {
return &JobReplyData{}
2015-07-05 14:49:07 +05:00
}
2016-12-07 01:29:59 +05:00
extraNonce := atomic.AddUint32(&cs.endpoint.extraNonce, 1)
blob := t.nextBlob(extraNonce, cs.endpoint.instanceId)
job := &Job{Id: util.Random(), ExtraNonce: extraNonce, Height: t.Height, Difficulty: cs.difficulty}
job.Submissions = make(map[string]bool)
cs.pushJob(job)
reply := &JobReplyData{JobId: job.Id, Blob: blob, Target: cs.targetHex}
return reply
2015-07-05 14:49:07 +05:00
}
2016-12-07 01:29:59 +05:00
func (cs *Session) pushJob(job *Job) {
cs.Lock()
defer cs.Unlock()
cs.validJobs = append(cs.validJobs, job)
2015-07-05 14:49:07 +05:00
2016-12-07 01:29:59 +05:00
if len(cs.validJobs) > 4 {
cs.validJobs = cs.validJobs[1:]
2015-07-05 14:49:07 +05:00
}
2016-12-07 01:29:59 +05:00
}
2015-07-05 14:49:07 +05:00
2016-12-07 01:29:59 +05:00
func (cs *Session) findJob(id string) *Job {
cs.Lock()
defer cs.Unlock()
for _, job := range cs.validJobs {
if job.Id == id {
return job
}
}
return nil
2015-07-05 14:49:07 +05:00
}
func (m *Miner) heartbeat() {
now := util.MakeTimestamp()
atomic.StoreInt64(&m.LastBeat, now)
}
2016-12-06 19:25:07 +05:00
func (m *Miner) getLastBeat() int64 {
return atomic.LoadInt64(&m.LastBeat)
}
func (m *Miner) storeShare(diff int64) {
2016-12-06 20:03:42 +05:00
now := util.MakeTimestamp() / 1000
2016-12-06 19:25:07 +05:00
m.Lock()
m.shares[now] += diff
m.Unlock()
}
2016-12-06 20:03:42 +05:00
func (m *Miner) hashrate(estimationWindow time.Duration) float64 {
now := util.MakeTimestamp() / 1000
2016-12-06 19:25:07 +05:00
totalShares := int64(0)
2016-12-06 20:03:42 +05:00
window := int64(estimationWindow / time.Second)
2016-12-06 19:25:07 +05:00
boundary := now - m.startedAt
if boundary > window {
boundary = window
}
m.Lock()
for k, v := range m.shares {
2016-12-06 20:03:42 +05:00
if k < now-86400 {
2016-12-06 19:25:07 +05:00
delete(m.shares, k)
} else if k >= now-window {
totalShares += v
}
}
m.Unlock()
return float64(totalShares) / float64(boundary)
}
2016-12-06 17:07:45 +05:00
func (m *Miner) processShare(s *StratumServer, e *Endpoint, job *Job, t *BlockTemplate, nonce string, result string) bool {
2016-12-06 22:16:57 +05:00
r := s.rpc()
2015-07-05 14:49:07 +05:00
shareBuff := make([]byte, len(t.Buffer))
copy(shareBuff, t.Buffer)
2016-12-06 17:07:45 +05:00
copy(shareBuff[t.ReservedOffset+4:t.ReservedOffset+7], e.instanceId)
2015-07-05 14:49:07 +05:00
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)
2016-12-05 21:28:58 +05:00
var hashBytes, convertedBlob []byte
2015-07-05 14:49:07 +05:00
2016-12-05 21:28:58 +05: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 00:55:16 +05:00
log.Printf("Bad hash from miner %v@%v", m.Id, m.IP)
2016-12-06 19:25:07 +05:00
atomic.AddUint64(&m.invalidShares, 1)
2015-07-05 14:49:07 +05:00
return false
}
hashDiff := util.GetHashDifficulty(hashBytes).Int64() // FIXME: Will return max int64 value if overflows
2016-12-06 20:03:42 +05:00
atomic.AddInt64(&s.roundShares, e.config.Difficulty)
atomic.AddUint64(&m.validShares, 1)
m.storeShare(e.config.Difficulty)
2015-07-05 14:49:07 +05:00
block := hashDiff >= t.Difficulty
2016-12-06 20:03:42 +05:00
2015-07-05 14:49:07 +05:00
if block {
2016-12-06 22:16:57 +05:00
_, err := r.SubmitBlock(hex.EncodeToString(shareBuff))
2015-07-05 14:49:07 +05:00
if err != nil {
2016-12-06 19:25:07 +05:00
atomic.AddUint64(&m.rejects, 1)
2016-12-06 22:16:57 +05:00
atomic.AddUint64(&r.Rejects, 1)
2015-07-05 14:49:07 +05:00
log.Printf("Block submission failure at height %v: %v", t.Height, err)
} else {
2016-12-05 21:28:58 +05:00
if len(convertedBlob) == 0 {
convertedBlob = cnutil.ConvertBlob(shareBuff)
}
2015-07-05 14:49:07 +05:00
blockFastHash := hex.EncodeToString(hashing.FastHash(convertedBlob))
// Immediately refresh current BT and send new jobs
s.refreshBlockTemplate(true)
2016-12-06 19:25:07 +05:00
atomic.AddUint64(&m.accepts, 1)
2016-12-06 22:16:57 +05:00
atomic.AddUint64(&r.Accepts, 1)
2016-12-06 23:37:29 +05:00
atomic.StoreInt64(&r.LastSubmissionAt, util.MakeTimestamp())
2016-12-07 00:55:16 +05:00
log.Printf("Block %v found at height %v by miner %v@%v", blockFastHash[0:6], t.Height, m.Id, m.IP)
2015-07-05 14:49:07 +05:00
}
} else if hashDiff < job.Difficulty {
2016-12-07 00:55:16 +05:00
log.Printf("Rejected low difficulty share of %v from %v@%v", hashDiff, m.Id, m.IP)
2016-12-06 19:25:07 +05:00
atomic.AddUint64(&m.invalidShares, 1)
2015-07-05 14:49:07 +05:00
return false
}
2016-12-06 17:07:45 +05:00
log.Printf("Valid share at difficulty %v/%v", e.config.Difficulty, hashDiff)
2016-12-06 19:25:07 +05:00
atomic.AddUint64(&m.validShares, 1)
2015-07-05 14:49:07 +05:00
return true
}