Browse Source

Tidy up struct exports

pool
Sammy Libre 8 years ago
parent
commit
423cf03e1a
  1. 8
      go-pool/stratum/api.go
  2. 32
      go-pool/stratum/blocks.go
  3. 2
      go-pool/stratum/handlers.go
  4. 20
      go-pool/stratum/miner.go

8
go-pool/stratum/api.go

@ -38,11 +38,11 @@ func (s *StratumServer) StatsIndex(w http.ResponseWriter, r *http.Request) {
stats["luck"] = s.getLuckStats() stats["luck"] = s.getLuckStats()
if t := s.currentBlockTemplate(); t != nil { if t := s.currentBlockTemplate(); t != nil {
stats["height"] = t.Height stats["height"] = t.height
stats["diff"] = t.Difficulty stats["diff"] = t.difficulty
roundShares := atomic.LoadInt64(&s.roundShares) roundShares := atomic.LoadInt64(&s.roundShares)
stats["variance"] = float64(roundShares) / float64(t.Difficulty) stats["variance"] = float64(roundShares) / float64(t.difficulty)
stats["prevHash"] = t.PrevHash[0:8] stats["prevHash"] = t.prevHash[0:8]
stats["template"] = true stats["template"] = true
} }
json.NewEncoder(w).Encode(stats) json.NewEncoder(w).Encode(stats)

32
go-pool/stratum/blocks.go

@ -10,21 +10,21 @@ import (
) )
type BlockTemplate struct { type BlockTemplate struct {
Difficulty int64 difficulty int64
Height int64 height int64
ReservedOffset int reservedOffset int
PrevHash string prevHash string
Buffer []byte buffer []byte
} }
func (b *BlockTemplate) nextBlob(extraNonce uint32, instanceId []byte) string { func (b *BlockTemplate) nextBlob(extraNonce uint32, instanceId []byte) string {
extraBuff := new(bytes.Buffer) extraBuff := new(bytes.Buffer)
binary.Write(extraBuff, binary.BigEndian, extraNonce) binary.Write(extraBuff, binary.BigEndian, extraNonce)
blobBuff := make([]byte, len(b.Buffer)) blobBuff := make([]byte, len(b.buffer))
copy(blobBuff, b.Buffer) copy(blobBuff, b.buffer)
copy(blobBuff[b.ReservedOffset+4:b.ReservedOffset+7], instanceId) copy(blobBuff[b.reservedOffset+4:b.reservedOffset+7], instanceId)
copy(blobBuff[b.ReservedOffset:], extraBuff.Bytes()) copy(blobBuff[b.reservedOffset:], extraBuff.Bytes())
blob := cnutil.ConvertBlob(blobBuff) blob := cnutil.ConvertBlob(blobBuff)
return hex.EncodeToString(blob) return hex.EncodeToString(blob)
} }
@ -38,9 +38,9 @@ func (s *StratumServer) fetchBlockTemplate() bool {
} }
t := s.currentBlockTemplate() t := s.currentBlockTemplate()
if t != nil && t.PrevHash == reply.PrevHash { if t != nil && t.prevHash == reply.PrevHash {
// Fallback to height comparison // Fallback to height comparison
if len(reply.PrevHash) == 0 && reply.Height > t.Height { if len(reply.PrevHash) == 0 && reply.Height > t.height {
log.Printf("New block to mine at height %v, diff: %v", reply.Height, reply.Difficulty) log.Printf("New block to mine at height %v, diff: %v", reply.Height, reply.Difficulty)
} else { } else {
return false return false
@ -49,12 +49,12 @@ func (s *StratumServer) fetchBlockTemplate() bool {
log.Printf("New block to mine on %s at height %v, diff: %v, prev_hash: %s", r.Name, reply.Height, reply.Difficulty, reply.PrevHash) log.Printf("New block to mine on %s at height %v, diff: %v, prev_hash: %s", r.Name, reply.Height, reply.Difficulty, reply.PrevHash)
} }
newTemplate := BlockTemplate{ newTemplate := BlockTemplate{
Difficulty: reply.Difficulty, difficulty: reply.Difficulty,
Height: reply.Height, height: reply.Height,
PrevHash: reply.PrevHash, prevHash: reply.PrevHash,
ReservedOffset: reply.ReservedOffset, reservedOffset: reply.ReservedOffset,
} }
newTemplate.Buffer, _ = hex.DecodeString(reply.Blob) newTemplate.buffer, _ = hex.DecodeString(reply.Blob)
s.blockTemplate.Store(&newTemplate) s.blockTemplate.Store(&newTemplate)
return true return true
} }

2
go-pool/stratum/handlers.go

@ -78,7 +78,7 @@ func (s *StratumServer) handleSubmitRPC(cs *Session, e *Endpoint, params *Submit
} }
t := s.currentBlockTemplate() t := s.currentBlockTemplate()
if job.height != t.Height { if job.height != t.height {
log.Printf("Stale share for height %d from %s@%s", job.height, miner.id, miner.ip) log.Printf("Stale share for height %d from %s@%s", job.height, miner.id, miner.ip)
atomic.AddUint64(&miner.staleShares, 1) atomic.AddUint64(&miner.staleShares, 1)
return nil, &ErrorReply{Code: -1, Message: "Block expired"} return nil, &ErrorReply{Code: -1, Message: "Block expired"}

20
go-pool/stratum/miner.go

@ -55,15 +55,15 @@ func NewMiner(id string, ip string) *Miner {
} }
func (cs *Session) getJob(t *BlockTemplate) *JobReplyData { func (cs *Session) getJob(t *BlockTemplate) *JobReplyData {
height := atomic.SwapInt64(&cs.lastBlockHeight, t.Height) height := atomic.SwapInt64(&cs.lastBlockHeight, t.height)
if height == t.Height { if height == t.height {
return &JobReplyData{} return &JobReplyData{}
} }
extraNonce := atomic.AddUint32(&cs.endpoint.extraNonce, 1) extraNonce := atomic.AddUint32(&cs.endpoint.extraNonce, 1)
blob := t.nextBlob(extraNonce, cs.endpoint.instanceId) blob := t.nextBlob(extraNonce, cs.endpoint.instanceId)
job := &Job{id: util.Random(), extraNonce: extraNonce, height: t.Height, difficulty: cs.difficulty} job := &Job{id: util.Random(), extraNonce: extraNonce, height: t.height, difficulty: cs.difficulty}
job.submissions = make(map[string]struct{}) job.submissions = make(map[string]struct{})
cs.pushJob(job) cs.pushJob(job)
reply := &JobReplyData{JobId: job.id, Blob: blob, Target: cs.targetHex} reply := &JobReplyData{JobId: job.id, Blob: blob, Target: cs.targetHex}
@ -132,13 +132,13 @@ func (m *Miner) hashrate(estimationWindow time.Duration) float64 {
func (m *Miner) processShare(s *StratumServer, e *Endpoint, job *Job, t *BlockTemplate, nonce string, result string) bool { func (m *Miner) processShare(s *StratumServer, e *Endpoint, job *Job, t *BlockTemplate, nonce string, result string) bool {
r := s.rpc() r := s.rpc()
shareBuff := make([]byte, len(t.Buffer)) shareBuff := make([]byte, len(t.buffer))
copy(shareBuff, t.Buffer) copy(shareBuff, t.buffer)
copy(shareBuff[t.ReservedOffset+4:t.ReservedOffset+7], e.instanceId) copy(shareBuff[t.reservedOffset+4:t.reservedOffset+7], e.instanceId)
extraBuff := new(bytes.Buffer) extraBuff := new(bytes.Buffer)
binary.Write(extraBuff, binary.BigEndian, job.extraNonce) binary.Write(extraBuff, binary.BigEndian, job.extraNonce)
copy(shareBuff[t.ReservedOffset:], extraBuff.Bytes()) copy(shareBuff[t.reservedOffset:], extraBuff.Bytes())
nonceBuff, _ := hex.DecodeString(nonce) nonceBuff, _ := hex.DecodeString(nonce)
copy(shareBuff[39:], nonceBuff) copy(shareBuff[39:], nonceBuff)
@ -163,14 +163,14 @@ func (m *Miner) processShare(s *StratumServer, e *Endpoint, job *Job, t *BlockTe
atomic.AddUint64(&m.validShares, 1) atomic.AddUint64(&m.validShares, 1)
m.storeShare(e.config.Difficulty) m.storeShare(e.config.Difficulty)
block := hashDiff >= t.Difficulty block := hashDiff >= t.difficulty
if block { if block {
_, err := r.SubmitBlock(hex.EncodeToString(shareBuff)) _, err := r.SubmitBlock(hex.EncodeToString(shareBuff))
if err != nil { if err != nil {
atomic.AddUint64(&m.rejects, 1) atomic.AddUint64(&m.rejects, 1)
atomic.AddUint64(&r.Rejects, 1) atomic.AddUint64(&r.Rejects, 1)
log.Printf("Block submission failure at height %v: %v", t.Height, err) log.Printf("Block submission failure at height %v: %v", t.height, err)
} else { } else {
if len(convertedBlob) == 0 { if len(convertedBlob) == 0 {
convertedBlob = cnutil.ConvertBlob(shareBuff) convertedBlob = cnutil.ConvertBlob(shareBuff)
@ -181,7 +181,7 @@ func (m *Miner) processShare(s *StratumServer, e *Endpoint, job *Job, t *BlockTe
atomic.AddUint64(&m.accepts, 1) atomic.AddUint64(&m.accepts, 1)
atomic.AddUint64(&r.Accepts, 1) atomic.AddUint64(&r.Accepts, 1)
atomic.StoreInt64(&r.LastSubmissionAt, util.MakeTimestamp()) 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) log.Printf("Block %v found at height %v by miner %v@%v", blockFastHash[0:6], t.height, m.id, m.ip)
} }
} else if hashDiff < job.difficulty { } else if hashDiff < job.difficulty {
log.Printf("Rejected low difficulty share of %v from %v@%v", hashDiff, m.id, m.ip) log.Printf("Rejected low difficulty share of %v from %v@%v", hashDiff, m.id, m.ip)

Loading…
Cancel
Save