mirror of
https://github.com/kvazar-network/keva-stratum.git
synced 2025-01-11 15:48:00 +00:00
Tidy up struct exports
This commit is contained in:
parent
f3d5c5c718
commit
423cf03e1a
@ -38,11 +38,11 @@ func (s *StratumServer) StatsIndex(w http.ResponseWriter, r *http.Request) {
|
||||
stats["luck"] = s.getLuckStats()
|
||||
|
||||
if t := s.currentBlockTemplate(); t != nil {
|
||||
stats["height"] = t.Height
|
||||
stats["diff"] = t.Difficulty
|
||||
stats["height"] = t.height
|
||||
stats["diff"] = t.difficulty
|
||||
roundShares := atomic.LoadInt64(&s.roundShares)
|
||||
stats["variance"] = float64(roundShares) / float64(t.Difficulty)
|
||||
stats["prevHash"] = t.PrevHash[0:8]
|
||||
stats["variance"] = float64(roundShares) / float64(t.difficulty)
|
||||
stats["prevHash"] = t.prevHash[0:8]
|
||||
stats["template"] = true
|
||||
}
|
||||
json.NewEncoder(w).Encode(stats)
|
||||
|
@ -10,21 +10,21 @@ import (
|
||||
)
|
||||
|
||||
type BlockTemplate struct {
|
||||
Difficulty int64
|
||||
Height int64
|
||||
ReservedOffset int
|
||||
PrevHash string
|
||||
Buffer []byte
|
||||
difficulty int64
|
||||
height int64
|
||||
reservedOffset int
|
||||
prevHash string
|
||||
buffer []byte
|
||||
}
|
||||
|
||||
func (b *BlockTemplate) nextBlob(extraNonce uint32, instanceId []byte) string {
|
||||
extraBuff := new(bytes.Buffer)
|
||||
binary.Write(extraBuff, binary.BigEndian, extraNonce)
|
||||
|
||||
blobBuff := make([]byte, len(b.Buffer))
|
||||
copy(blobBuff, b.Buffer)
|
||||
copy(blobBuff[b.ReservedOffset+4:b.ReservedOffset+7], instanceId)
|
||||
copy(blobBuff[b.ReservedOffset:], extraBuff.Bytes())
|
||||
blobBuff := make([]byte, len(b.buffer))
|
||||
copy(blobBuff, b.buffer)
|
||||
copy(blobBuff[b.reservedOffset+4:b.reservedOffset+7], instanceId)
|
||||
copy(blobBuff[b.reservedOffset:], extraBuff.Bytes())
|
||||
blob := cnutil.ConvertBlob(blobBuff)
|
||||
return hex.EncodeToString(blob)
|
||||
}
|
||||
@ -38,9 +38,9 @@ func (s *StratumServer) fetchBlockTemplate() bool {
|
||||
}
|
||||
t := s.currentBlockTemplate()
|
||||
|
||||
if t != nil && t.PrevHash == reply.PrevHash {
|
||||
if t != nil && t.prevHash == reply.PrevHash {
|
||||
// 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)
|
||||
} else {
|
||||
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)
|
||||
}
|
||||
newTemplate := BlockTemplate{
|
||||
Difficulty: reply.Difficulty,
|
||||
Height: reply.Height,
|
||||
PrevHash: reply.PrevHash,
|
||||
ReservedOffset: reply.ReservedOffset,
|
||||
difficulty: reply.Difficulty,
|
||||
height: reply.Height,
|
||||
prevHash: reply.PrevHash,
|
||||
reservedOffset: reply.ReservedOffset,
|
||||
}
|
||||
newTemplate.Buffer, _ = hex.DecodeString(reply.Blob)
|
||||
newTemplate.buffer, _ = hex.DecodeString(reply.Blob)
|
||||
s.blockTemplate.Store(&newTemplate)
|
||||
return true
|
||||
}
|
||||
|
@ -78,7 +78,7 @@ func (s *StratumServer) handleSubmitRPC(cs *Session, e *Endpoint, params *Submit
|
||||
}
|
||||
|
||||
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)
|
||||
atomic.AddUint64(&miner.staleShares, 1)
|
||||
return nil, &ErrorReply{Code: -1, Message: "Block expired"}
|
||||
|
@ -55,15 +55,15 @@ func NewMiner(id string, ip string) *Miner {
|
||||
}
|
||||
|
||||
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{}
|
||||
}
|
||||
|
||||
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 := &Job{id: util.Random(), extraNonce: extraNonce, height: t.height, difficulty: cs.difficulty}
|
||||
job.submissions = make(map[string]struct{})
|
||||
cs.pushJob(job)
|
||||
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 {
|
||||
r := s.rpc()
|
||||
|
||||
shareBuff := make([]byte, len(t.Buffer))
|
||||
copy(shareBuff, t.Buffer)
|
||||
copy(shareBuff[t.ReservedOffset+4:t.ReservedOffset+7], e.instanceId)
|
||||
shareBuff := make([]byte, len(t.buffer))
|
||||
copy(shareBuff, t.buffer)
|
||||
copy(shareBuff[t.reservedOffset+4:t.reservedOffset+7], e.instanceId)
|
||||
|
||||
extraBuff := new(bytes.Buffer)
|
||||
binary.Write(extraBuff, binary.BigEndian, job.extraNonce)
|
||||
copy(shareBuff[t.ReservedOffset:], extraBuff.Bytes())
|
||||
copy(shareBuff[t.reservedOffset:], extraBuff.Bytes())
|
||||
|
||||
nonceBuff, _ := hex.DecodeString(nonce)
|
||||
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)
|
||||
m.storeShare(e.config.Difficulty)
|
||||
|
||||
block := hashDiff >= t.Difficulty
|
||||
block := hashDiff >= t.difficulty
|
||||
|
||||
if block {
|
||||
_, err := r.SubmitBlock(hex.EncodeToString(shareBuff))
|
||||
if err != nil {
|
||||
atomic.AddUint64(&m.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 {
|
||||
if len(convertedBlob) == 0 {
|
||||
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(&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)
|
||||
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 {
|
||||
log.Printf("Rejected low difficulty share of %v from %v@%v", hashDiff, m.id, m.ip)
|
||||
|
Loading…
Reference in New Issue
Block a user