You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
1.6 KiB
61 lines
1.6 KiB
10 years ago
|
package stratum
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"encoding/binary"
|
||
|
"encoding/hex"
|
||
|
"log"
|
||
|
|
||
7 years ago
|
"github.com/sammy007/monero-stratum/cnutil"
|
||
10 years ago
|
)
|
||
|
|
||
|
type BlockTemplate struct {
|
||
8 years ago
|
difficulty int64
|
||
|
height int64
|
||
|
reservedOffset int
|
||
|
prevHash string
|
||
|
buffer []byte
|
||
10 years ago
|
}
|
||
|
|
||
8 years ago
|
func (b *BlockTemplate) nextBlob(extraNonce uint32, instanceId []byte) string {
|
||
10 years ago
|
extraBuff := new(bytes.Buffer)
|
||
|
binary.Write(extraBuff, binary.BigEndian, extraNonce)
|
||
8 years ago
|
|
||
8 years ago
|
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())
|
||
10 years ago
|
blob := cnutil.ConvertBlob(blobBuff)
|
||
8 years ago
|
return hex.EncodeToString(blob)
|
||
10 years ago
|
}
|
||
|
|
||
|
func (s *StratumServer) fetchBlockTemplate() bool {
|
||
8 years ago
|
r := s.rpc()
|
||
|
reply, err := r.GetBlockTemplate(8, s.config.Address)
|
||
10 years ago
|
if err != nil {
|
||
|
log.Printf("Error while refreshing block template: %s", err)
|
||
|
return false
|
||
|
}
|
||
|
t := s.currentBlockTemplate()
|
||
|
|
||
8 years ago
|
if t != nil && t.prevHash == reply.PrevHash {
|
||
10 years ago
|
// Fallback to height comparison
|
||
8 years ago
|
if len(reply.PrevHash) == 0 && reply.Height > t.height {
|
||
8 years ago
|
log.Printf("New block to mine on %s at height %v, diff: %v", r.Name, reply.Height, reply.Difficulty)
|
||
10 years ago
|
} else {
|
||
|
return false
|
||
|
}
|
||
|
} else {
|
||
8 years ago
|
log.Printf("New block to mine on %s at height %v, diff: %v, prev_hash: %s", r.Name, reply.Height, reply.Difficulty, reply.PrevHash)
|
||
10 years ago
|
}
|
||
|
newTemplate := BlockTemplate{
|
||
8 years ago
|
difficulty: reply.Difficulty,
|
||
|
height: reply.Height,
|
||
|
prevHash: reply.PrevHash,
|
||
|
reservedOffset: reply.ReservedOffset,
|
||
10 years ago
|
}
|
||
8 years ago
|
newTemplate.buffer, _ = hex.DecodeString(reply.Blob)
|
||
10 years ago
|
s.blockTemplate.Store(&newTemplate)
|
||
|
return true
|
||
|
}
|