diff --git a/go-pool/stratum/miner.go b/go-pool/stratum/miner.go index b160fca..0dcede9 100644 --- a/go-pool/stratum/miner.go +++ b/go-pool/stratum/miner.go @@ -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 } diff --git a/go-pool/stratum/stratum.go b/go-pool/stratum/stratum.go index 4f97b7f..df01ed2 100644 --- a/go-pool/stratum/stratum.go +++ b/go-pool/stratum/stratum.go @@ -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) diff --git a/go-pool/util/util.go b/go-pool/util/util.go index 0308fac..9a890e9 100644 --- a/go-pool/util/util.go +++ b/go-pool/util/util.go @@ -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 {