Browse Source

Cache targetHex per endpoint

pool
Sammy Libre 8 years ago
parent
commit
dd505ff26c
  1. 2
      go-pool/stratum/miner.go
  2. 7
      go-pool/stratum/stratum.go
  3. 18
      go-pool/util/util.go

2
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 := &Job{id: strconv.FormatUint(id, 10), 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.endpoint.targetHex}
return reply return reply
} }

7
go-pool/stratum/stratum.go

@ -38,6 +38,7 @@ type Endpoint struct {
config *pool.Port config *pool.Port
instanceId []byte instanceId []byte
extraNonce uint32 extraNonce uint32
targetHex string
jobSequence uint64 jobSequence uint64
} }
@ -50,8 +51,6 @@ type Session struct {
difficulty int64 difficulty int64
validJobs []*Job validJobs []*Job
lastBlockHeight int64 lastBlockHeight int64
target uint32
targetHex string
} }
const ( const (
@ -127,6 +126,7 @@ func NewEndpoint(cfg *pool.Port) *Endpoint {
if err != nil { if err != nil {
log.Fatalf("Can't seed with random bytes: %v", err) log.Fatalf("Can't seed with random bytes: %v", err)
} }
e.targetHex = util.GetTargetHex(e.config.Difficulty)
return e return e
} }
@ -176,9 +176,6 @@ func (e *Endpoint) Listen(s *StratumServer) {
} }
func (s *StratumServer) handleClient(cs *Session, e *Endpoint) { func (s *StratumServer) handleClient(cs *Session, e *Endpoint) {
_, targetHex := util.GetTargetHex(e.config.Difficulty)
cs.targetHex = targetHex
connbuff := bufio.NewReaderSize(cs.conn, MaxReqSize) connbuff := bufio.NewReaderSize(cs.conn, MaxReqSize)
s.setDeadline(cs.conn) s.setDeadline(cs.conn)

18
go-pool/util/util.go

@ -1,8 +1,6 @@
package util package util
import ( import (
"bytes"
"encoding/binary"
"encoding/hex" "encoding/hex"
"math/big" "math/big"
"time" "time"
@ -22,24 +20,14 @@ func MakeTimestamp() int64 {
return time.Now().UnixNano() / int64(time.Millisecond) return time.Now().UnixNano() / int64(time.Millisecond)
} }
func GetTargetHex(diff int64) (uint32, string) { func GetTargetHex(diff int64) string {
padded := make([]byte, 32) padded := make([]byte, 32)
diff2 := new(big.Int) diffBuff := new(big.Int).Div(Diff1, big.NewInt(diff)).Bytes()
diff2.SetInt64(int64(diff))
diff3 := new(big.Int)
diff3 = diff3.Div(Diff1, diff2)
diffBuff := diff3.Bytes()
copy(padded[32-len(diffBuff):], diffBuff) copy(padded[32-len(diffBuff):], diffBuff)
buff := padded[0:4] buff := padded[0:4]
var target uint32
targetBuff := bytes.NewReader(buff)
binary.Read(targetBuff, binary.LittleEndian, &target)
targetHex := hex.EncodeToString(reverse(buff)) targetHex := hex.EncodeToString(reverse(buff))
return targetHex
return target, targetHex
} }
func GetHashDifficulty(hashBytes []byte) *big.Int { func GetHashDifficulty(hashBytes []byte) *big.Int {

Loading…
Cancel
Save