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 { @@ -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
}

7
go-pool/stratum/stratum.go

@ -38,6 +38,7 @@ type Endpoint struct { @@ -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 { @@ -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 { @@ -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) { @@ -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)

18
go-pool/util/util.go

@ -1,8 +1,6 @@ @@ -1,8 +1,6 @@
package util
import (
"bytes"
"encoding/binary"
"encoding/hex"
"math/big"
"time"
@ -22,24 +20,14 @@ func MakeTimestamp() int64 { @@ -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…
Cancel
Save