Kevacoin stratum server for solo-mining
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

10 years ago
package stratum
import (
"bytes"
"encoding/binary"
"encoding/hex"
"log"
"../../cnutil"
)
type BlockTemplate struct {
Difficulty int64
Height int64
ReservedOffset int
PrevHash string
Buffer []byte
}
func (b *BlockTemplate) nextBlob(extraNonce uint32, instanceId []byte) string {
10 years ago
extraBuff := new(bytes.Buffer)
binary.Write(extraBuff, binary.BigEndian, extraNonce)
10 years ago
blobBuff := make([]byte, len(b.Buffer))
copy(blobBuff, b.Buffer)
copy(blobBuff[b.ReservedOffset+4:b.ReservedOffset+7], instanceId)
10 years ago
copy(blobBuff[b.ReservedOffset:], extraBuff.Bytes())
blob := cnutil.ConvertBlob(blobBuff)
return hex.EncodeToString(blob)
10 years ago
}
func (s *StratumServer) fetchBlockTemplate() bool {
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()
if t != nil && t.PrevHash == reply.PrevHash {
10 years ago
// Fallback to height comparison
if len(reply.PrevHash) == 0 && reply.Height > t.Height {
log.Printf("New block to mine at height %v, diff: %v", reply.Height, reply.Difficulty)
} else {
return false
}
} else {
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{
Difficulty: reply.Difficulty,
Height: reply.Height,
PrevHash: reply.PrevHash,
ReservedOffset: reply.ReservedOffset,
}
newTemplate.Buffer, _ = hex.DecodeString(reply.Blob)
s.blockTemplate.Store(&newTemplate)
return true
}