2022-05-24 18:17:12 +02:00
|
|
|
package proxy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2022-06-19 21:28:53 +02:00
|
|
|
"encoding/binary"
|
2022-05-24 18:17:12 +02:00
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2022-06-19 21:28:53 +02:00
|
|
|
"sync"
|
|
|
|
"time"
|
2022-05-24 18:17:12 +02:00
|
|
|
|
2022-06-19 21:28:53 +02:00
|
|
|
"github.com/bitfield/qrand"
|
2022-05-24 18:17:12 +02:00
|
|
|
"github.com/deroproject/derohe/block"
|
|
|
|
"github.com/deroproject/derohe/rpc"
|
|
|
|
)
|
|
|
|
|
2022-06-19 21:28:53 +02:00
|
|
|
type nonce_store struct {
|
|
|
|
nonce [8]byte
|
|
|
|
timestamp time.Time
|
|
|
|
sync.RWMutex
|
|
|
|
}
|
|
|
|
|
2022-06-08 01:03:38 +02:00
|
|
|
func edit_blob(input []byte, miner [32]byte, nonce bool) (output []byte) {
|
2022-05-24 18:17:12 +02:00
|
|
|
var err error
|
|
|
|
var params rpc.GetBlockTemplate_Result
|
|
|
|
var mbl block.MiniBlock
|
|
|
|
var raw_hex []byte
|
|
|
|
var out bytes.Buffer
|
|
|
|
|
|
|
|
if err = json.Unmarshal(input, ¶ms); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if raw_hex, err = hex.DecodeString(params.Blockhashing_blob); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if mbl.Deserialize(raw_hex); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-27 02:16:25 +02:00
|
|
|
// Insert miner address
|
|
|
|
if !mbl.Final {
|
|
|
|
copy(mbl.KeyHash[:], miner[:])
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert random nonce
|
2022-06-08 01:03:38 +02:00
|
|
|
if nonce {
|
2022-06-19 21:28:53 +02:00
|
|
|
var qnonce [8]byte
|
|
|
|
Found.Lock()
|
|
|
|
// send nonce pattern to all nodes, lasts for 2 hours or until another nonce has been found
|
2022-06-19 21:30:06 +02:00
|
|
|
// TODO: add command argument and switch to random data pool
|
2022-06-19 21:28:53 +02:00
|
|
|
if binary.BigEndian.Uint64(Found.nonce[:]) > 0 && time.Now().Sub(Found.timestamp) < time.Hour*2 {
|
|
|
|
copy(qnonce[:], Found.nonce[:])
|
|
|
|
qrand.Read(qnonce[0:2])
|
|
|
|
mbl.Nonce[1] = binary.BigEndian.Uint32(qnonce[0:4])
|
|
|
|
mbl.Nonce[2] = binary.BigEndian.Uint32(qnonce[4:8])
|
|
|
|
} else {
|
|
|
|
qrand.Read(qnonce[:])
|
|
|
|
mbl.Nonce[1] = binary.BigEndian.Uint32(qnonce[0:4])
|
|
|
|
mbl.Nonce[2] = binary.BigEndian.Uint32(qnonce[4:8])
|
2022-06-08 01:03:38 +02:00
|
|
|
}
|
2022-06-19 21:28:53 +02:00
|
|
|
Found.Unlock()
|
2022-05-24 18:17:12 +02:00
|
|
|
}
|
2022-06-08 01:03:38 +02:00
|
|
|
|
2022-05-24 18:17:12 +02:00
|
|
|
params.Blockhashing_blob = fmt.Sprintf("%x", mbl.Serialize())
|
|
|
|
encoder := json.NewEncoder(&out)
|
|
|
|
|
|
|
|
if err = encoder.Encode(params); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
output = out.Bytes()
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|