mirror of
https://github.com/kvazar-network/keva-stratum.git
synced 2025-01-11 15:48:00 +00:00
Cache targetHex per endpoint
This commit is contained in:
parent
1b6473a52b
commit
dd505ff26c
@ -66,7 +66,7 @@ func (cs *Session) getJob(t *BlockTemplate) *JobReplyData {
|
||||
job := &Job{id: strconv.FormatUint(id, 10), 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}
|
||||
reply := &JobReplyData{JobId: job.id, Blob: blob, Target: cs.endpoint.targetHex}
|
||||
return reply
|
||||
}
|
||||
|
||||
|
@ -38,6 +38,7 @@ type Endpoint struct {
|
||||
config *pool.Port
|
||||
instanceId []byte
|
||||
extraNonce uint32
|
||||
targetHex string
|
||||
jobSequence uint64
|
||||
}
|
||||
|
||||
@ -50,8 +51,6 @@ type Session struct {
|
||||
difficulty int64
|
||||
validJobs []*Job
|
||||
lastBlockHeight int64
|
||||
target uint32
|
||||
targetHex string
|
||||
}
|
||||
|
||||
const (
|
||||
@ -127,6 +126,7 @@ func NewEndpoint(cfg *pool.Port) *Endpoint {
|
||||
if err != nil {
|
||||
log.Fatalf("Can't seed with random bytes: %v", err)
|
||||
}
|
||||
e.targetHex = util.GetTargetHex(e.config.Difficulty)
|
||||
return e
|
||||
}
|
||||
|
||||
@ -176,9 +176,6 @@ func (e *Endpoint) Listen(s *StratumServer) {
|
||||
}
|
||||
|
||||
func (s *StratumServer) handleClient(cs *Session, e *Endpoint) {
|
||||
_, targetHex := util.GetTargetHex(e.config.Difficulty)
|
||||
cs.targetHex = targetHex
|
||||
|
||||
connbuff := bufio.NewReaderSize(cs.conn, MaxReqSize)
|
||||
s.setDeadline(cs.conn)
|
||||
|
||||
|
@ -1,8 +1,6 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"encoding/hex"
|
||||
"math/big"
|
||||
"time"
|
||||
@ -22,24 +20,14 @@ func MakeTimestamp() int64 {
|
||||
return time.Now().UnixNano() / int64(time.Millisecond)
|
||||
}
|
||||
|
||||
func GetTargetHex(diff int64) (uint32, string) {
|
||||
func GetTargetHex(diff int64) string {
|
||||
padded := make([]byte, 32)
|
||||
|
||||
diff2 := new(big.Int)
|
||||
diff2.SetInt64(int64(diff))
|
||||
|
||||
diff3 := new(big.Int)
|
||||
diff3 = diff3.Div(Diff1, diff2)
|
||||
|
||||
diffBuff := diff3.Bytes()
|
||||
diffBuff := new(big.Int).Div(Diff1, big.NewInt(diff)).Bytes()
|
||||
copy(padded[32-len(diffBuff):], diffBuff)
|
||||
buff := padded[0:4]
|
||||
var target uint32
|
||||
targetBuff := bytes.NewReader(buff)
|
||||
binary.Read(targetBuff, binary.LittleEndian, &target)
|
||||
targetHex := hex.EncodeToString(reverse(buff))
|
||||
|
||||
return target, targetHex
|
||||
return targetHex
|
||||
}
|
||||
|
||||
func GetHashDifficulty(hashBytes []byte) *big.Int {
|
||||
|
Loading…
Reference in New Issue
Block a user