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.

68 lines
1.8 KiB

10 years ago
package stratum
import (
"bytes"
"encoding/binary"
"encoding/hex"
"log"
"math/big"
"../cnutil"
10 years ago
)
type BlockTemplate struct {
diffInt64 int64
height int64
difficulty *big.Int
reservedOffset int
prevHash string
buffer []byte
seedHash string
nextSeedHash string
10 years ago
}
func (b *BlockTemplate) nextBlob(extraNonce uint32, instanceId []byte) string {
10 years ago
extraBuff := new(bytes.Buffer)
binary.Write(extraBuff, binary.BigEndian, extraNonce)
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())
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 on %s at height %v, diff: %v", r.Name, reply.Height, reply.Difficulty)
10 years ago
} 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{
diffInt64: reply.Difficulty,
difficulty: big.NewInt(reply.Difficulty),
height: reply.Height,
prevHash: reply.PrevHash,
reservedOffset: reply.ReservedOffset,
seedHash: reply.SeedHash,
nextSeedHash: reply.NextSeedHash,
10 years ago
}
newTemplate.buffer, _ = hex.DecodeString(reply.Blob)
10 years ago
s.blockTemplate.Store(&newTemplate)
return true
}