-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathblock_test.go
More file actions
245 lines (224 loc) · 8.19 KB
/
block_test.go
File metadata and controls
245 lines (224 loc) · 8.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package main
import (
"bytes"
"encoding/hex"
"fmt"
"math"
"testing"
"github.com/btcsuite/btcd/wire"
)
// merkleRootFromBtcdTxs mirrors the standard merkle tree computation over a
// slice of btcd MsgTx values, using the same doubleSHA256 used in the pool.
func merkleRootFromBtcdTxs(txs []*wire.MsgTx) []byte {
if len(txs) == 0 {
return nil
}
// Collect txids as big-endian hashes.
layer := make([][]byte, len(txs))
for i, tx := range txs {
h := tx.TxHash()
layer[i] = h.CloneBytes()
}
for len(layer) > 1 {
if len(layer)%2 == 1 {
layer = append(layer, layer[len(layer)-1])
}
next := make([][]byte, 0, len(layer)/2)
for i := 0; i < len(layer); i += 2 {
joined := append(append([]byte{}, layer[i]...), layer[i+1]...)
next = append(next, doubleSHA256(joined))
}
layer = next
}
return layer[0]
}
// TestBuildBlock_ParsesWithBtcdAndHasValidMerkle builds a minimal block using
// buildBlock and then parses it with btcd's wire.MsgBlock, checking header
// fields and merkle root.
func TestBuildBlock_ParsesWithBtcdAndHasValidMerkle(t *testing.T) {
job := &Job{
JobID: "test-job",
Template: GetBlockTemplateResult{
Height: 101,
CurTime: 1700000000,
Mintime: 0,
Bits: "1d00ffff",
Previous: "0000000000000000000000000000000000000000000000000000000000000000",
CoinbaseValue: 50 * 1e8,
},
Extranonce2Size: 4,
TemplateExtraNonce2Size: 8,
PayoutScript: []byte{0x51}, // OP_TRUE for structure test
WitnessCommitment: "",
CoinbaseMsg: "goPool-blocktest",
ScriptTime: 0,
Transactions: nil,
MerkleBranches: nil,
}
ex1 := []byte{0x01, 0x02, 0x03, 0x04}
ex2 := []byte{0xaa, 0xbb, 0xcc, 0xdd}
ntimeHex := fmt.Sprintf("%08x", job.Template.CurTime)
nonceHex := "00000001"
version := int32(1)
blockHex, headerHash, header, merkleRoot, err := buildBlock(job, ex1, ex2, ntimeHex, nonceHex, version)
if err != nil {
t.Fatalf("buildBlock error: %v", err)
}
if len(blockHex) == 0 || len(headerHash) != 32 || len(header) != 80 || len(merkleRoot) != 32 {
t.Fatalf("unexpected block artifacts: len(blockHex)=%d len(headerHash)=%d len(header)=%d len(merkleRoot)=%d",
len(blockHex), len(headerHash), len(header), len(merkleRoot))
}
raw, err := hex.DecodeString(blockHex)
if err != nil {
t.Fatalf("decode blockHex: %v", err)
}
var msgBlock wire.MsgBlock
if err := msgBlock.Deserialize(bytes.NewReader(raw)); err != nil {
t.Fatalf("btcd MsgBlock deserialize error: %v", err)
}
if msgBlock.Header.Version != version {
t.Fatalf("header version mismatch: got %d, want %d", msgBlock.Header.Version, version)
}
if len(msgBlock.Transactions) != 1 {
t.Fatalf("expected 1 transaction (coinbase only), got %d", len(msgBlock.Transactions))
}
// Recompute merkle root from btcd's view of the transactions and ensure
// it matches the header merkle root.
rootFromTxs := merkleRootFromBtcdTxs(msgBlock.Transactions)
if rootFromTxs == nil || !bytes.Equal(rootFromTxs, msgBlock.Header.MerkleRoot.CloneBytes()) {
t.Fatalf("merkle root mismatch: header=%x computed=%x", msgBlock.Header.MerkleRoot.CloneBytes(), rootFromTxs)
}
// Re-serialize the block with btcd and ensure it matches our original
// bytes exactly. This confirms our block layout (header + tx count +
// tx ordering) matches btcd's wire encoding.
var buf bytes.Buffer
if err := msgBlock.Serialize(&buf); err != nil {
t.Fatalf("btcd MsgBlock serialize error: %v", err)
}
roundTrip := buf.Bytes()
if !bytes.Equal(raw, roundTrip) {
t.Fatalf("block serialization mismatch between pool and btcd (len orig=%d len btcd=%d)", len(raw), len(roundTrip))
}
}
// TestDualVsSinglePayoutBlocks simulates block construction for both single-
// payout and dual-payout coinbases and verifies that the resulting blocks are
// structurally valid under btcd and that the coinbase outputs match the
// expected split.
func TestDualVsSinglePayoutBlocks(t *testing.T) {
job := &Job{
JobID: "test-job-dual",
Template: GetBlockTemplateResult{
Height: 150,
CurTime: 1700000100,
Mintime: 0,
Bits: "1d00ffff",
Previous: "0000000000000000000000000000000000000000000000000000000000000000",
CoinbaseValue: 50 * 1e8,
},
Extranonce2Size: 4,
TemplateExtraNonce2Size: 8,
PayoutScript: []byte{0x51}, // OP_TRUE
WitnessCommitment: "",
CoinbaseMsg: "goPool-dualtest",
ScriptTime: 0,
Transactions: nil,
MerkleBranches: nil,
CoinbaseValue: 50 * 1e8,
}
ex1 := []byte{0x0a, 0x0b, 0x0c, 0x0d}
ex2 := []byte{0x01, 0x02, 0x03, 0x04}
ntimeHex := fmt.Sprintf("%08x", job.Template.CurTime)
nonceHex := "00000002"
version := int32(1)
// Single-payout block.
singleBlockHex, _, _, _, err := buildBlock(job, ex1, ex2, ntimeHex, nonceHex, version)
if err != nil {
t.Fatalf("buildBlock (single) error: %v", err)
}
singleRaw, err := hex.DecodeString(singleBlockHex)
if err != nil {
t.Fatalf("decode singleBlockHex: %v", err)
}
var singleBlock wire.MsgBlock
if err := singleBlock.Deserialize(bytes.NewReader(singleRaw)); err != nil {
t.Fatalf("btcd MsgBlock (single) deserialize error: %v", err)
}
if len(singleBlock.Transactions) != 1 {
t.Fatalf("single-payout block: expected 1 tx, got %d", len(singleBlock.Transactions))
}
if len(singleBlock.Transactions[0].TxOut) != 1 {
t.Fatalf("single-payout coinbase: expected 1 output, got %d", len(singleBlock.Transactions[0].TxOut))
}
if singleBlock.Transactions[0].TxOut[0].Value != job.Template.CoinbaseValue {
t.Fatalf("single-payout coinbase value mismatch: got %d, want %d", singleBlock.Transactions[0].TxOut[0].Value, job.Template.CoinbaseValue)
}
// Dual-payout block: build coinbase using serializeDualCoinbaseTx, then
// assemble a full block matching the pool's dual-payout path.
poolScript := job.PayoutScript
workerScript := []byte{0x52} // OP_2
totalValue := job.Template.CoinbaseValue
feePercent := 2.0
cbTx, cbTxid, err := serializeDualCoinbaseTx(job.Template.Height, ex1, ex2, job.TemplateExtraNonce2Size, poolScript, workerScript, totalValue, feePercent, job.WitnessCommitment, job.Template.CoinbaseAux.Flags, job.CoinbaseMsg, job.ScriptTime)
if err != nil {
t.Fatalf("serializeDualCoinbaseTx error: %v", err)
}
if len(cbTxid) != 32 {
t.Fatalf("dual-payout coinbase txid length %d", len(cbTxid))
}
merkleRoot := computeMerkleRootFromBranches(cbTxid, job.MerkleBranches)
header, err := buildBlockHeaderFromHex(version, job.Template.Previous, merkleRoot, ntimeHex, job.Template.Bits, nonceHex)
if err != nil {
t.Fatalf("buildBlockHeader (dual) error: %v", err)
}
var dualBuf bytes.Buffer
dualBuf.Write(header)
writeVarInt(&dualBuf, uint64(1)) // coinbase only
dualBuf.Write(cbTx)
dualRaw := dualBuf.Bytes()
var dualBlock wire.MsgBlock
if err := dualBlock.Deserialize(bytes.NewReader(dualRaw)); err != nil {
t.Fatalf("btcd MsgBlock (dual) deserialize error: %v", err)
}
if len(dualBlock.Transactions) != 1 {
t.Fatalf("dual-payout block: expected 1 tx, got %d", len(dualBlock.Transactions))
}
coinbase := dualBlock.Transactions[0]
if len(coinbase.TxOut) != 2 {
t.Fatalf("dual-payout coinbase: expected 2 outputs, got %d", len(coinbase.TxOut))
}
// Verify split matches the same logic used in serializeDualCoinbaseTx.
poolFee := max(int64(math.Round(float64(totalValue)*feePercent/100.0)), 0)
if poolFee > totalValue {
poolFee = totalValue
}
workerValue := totalValue - poolFee
if workerValue <= 0 {
t.Fatalf("dual-payout worker value <= 0")
}
var (
gotPoolValue *int64
gotWorkerValue *int64
)
for _, o := range coinbase.TxOut {
switch {
case bytes.Equal(o.PkScript, poolScript):
v := o.Value
gotPoolValue = &v
case bytes.Equal(o.PkScript, workerScript):
v := o.Value
gotWorkerValue = &v
}
}
if gotPoolValue == nil {
t.Fatalf("dual-payout pool output not found by script")
}
if gotWorkerValue == nil {
t.Fatalf("dual-payout worker output not found by script")
}
if *gotPoolValue != poolFee {
t.Fatalf("dual-payout pool output value mismatch: got %d, want %d", *gotPoolValue, poolFee)
}
if *gotWorkerValue != workerValue {
t.Fatalf("dual-payout worker output value mismatch: got %d, want %d", *gotWorkerValue, workerValue)
}
}