Skip to content

Commit ac37832

Browse files
galt-trCopilot
andauthored
Fix proof of work for genesis block (#22)
* Fix proof of work for genesis block * Add POW tests * Update genesis.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Fix lint --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent de06c35 commit ac37832

File tree

2 files changed

+60
-42
lines changed

2 files changed

+60
-42
lines changed

genesis.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,19 +147,19 @@ var teraTestNetGenesisBlock = wire.MsgBlock{
147147
PrevBlock: chainhash.Hash{}, // 0000000000000000000000000000000000000000000000000000000000000000
148148
MerkleRoot: testNetGenesisMerkleRoot, // 4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b
149149
Timestamp: time.Unix(1755606836, 0), // 2025-08-19T08:33:56 +0000 UTC
150-
Bits: 0x1d00ffff, // 486604799 [00000000ffff0000000000000000000000000000000000000000000000000000]
151-
Nonce: 0x18aea41a, // 414098458
150+
Bits: 0x207fffff, // Easy difficulty for test network
151+
Nonce: 0x00000002, // Nonce value that produces a block hash meeting the proof-of-work requirement for 0x207fffff difficulty
152152
},
153153
Transactions: []*wire.MsgTx{&genesisCoinbaseTx},
154154
}
155155

156156
// teraTestNetGenesisHash is the hash of the first block in the blockchain for the
157157
// tera test network.
158158
var teraTestNetGenesisHash = chainhash.Hash([chainhash.HashSize]byte{ // Make go vet happy.
159-
0x12, 0x32, 0x0e, 0x07, 0x48, 0xfd, 0x4c, 0x44,
160-
0x1d, 0x2d, 0x97, 0xba, 0x98, 0x56, 0xcd, 0xdd,
161-
0x27, 0x13, 0x42, 0xe1, 0xb2, 0xc5, 0x17, 0x30,
162-
0x97, 0x64, 0x58, 0x89, 0x33, 0xe1, 0x27, 0x1a,
159+
0x5c, 0xce, 0x10, 0x0b, 0x1e, 0x5e, 0xdf, 0x9a,
160+
0x24, 0x22, 0x6f, 0x6a, 0x75, 0x50, 0x18, 0x2c,
161+
0xd3, 0xe8, 0xb9, 0x58, 0x65, 0x98, 0xcc, 0xa2,
162+
0xf9, 0x43, 0xa4, 0x09, 0x32, 0x4b, 0xa0, 0xef,
163163
})
164164

165165
// stnGenesisHash is the hash of the first block in the blockchain for the

genesis_test.go

Lines changed: 54 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package chaincfg
77
import (
88
"bytes"
99
"encoding/hex"
10+
"math/big"
1011
"testing"
1112

1213
"github.com/bsv-blockchain/go-bt/v2"
@@ -313,40 +314,57 @@ func TestTeraTestNetGenesisBlock(t *testing.T) {
313314
// teraTestNetGenesisBlockBytes are the wire-encoded bytes for the genesis block of
314315
// the tera test network as of protocol version 60002.
315316
var teraTestNetGenesisBlockBytes = []byte{
316-
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* |........| */
317-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* |........| */
318-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* |........| */
319-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* |........| */
320-
0x00, 0x00, 0x00, 0x00, 0x3b, 0xa3, 0xed, 0xfd, /* |....;...| */
321-
0x7a, 0x7b, 0x12, 0xb2, 0x7a, 0xc7, 0x2c, 0x3e, /* |z{..z.,>| */
322-
0x67, 0x76, 0x8f, 0x61, 0x7f, 0xc8, 0x1b, 0xc3, /* |gv.a....| */
323-
0x88, 0x8a, 0x51, 0x32, 0x3a, 0x9f, 0xb8, 0xaa, /* |..Q2:...| */
324-
0x4b, 0x1e, 0x5e, 0x4a, 0x34, 0x6f, 0xa4, 0x68, /* |K.^J4o.h| */
325-
0xff, 0xff, 0x00, 0x1d, 0x1a, 0xa4, 0xae, 0x18, /* |........| */
326-
0x01, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, /* |........| */
327-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* |........| */
328-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* |........| */
329-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* |........| */
330-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, /* |........| */
331-
0xff, 0xff, 0x4d, 0x04, 0xff, 0xff, 0x00, 0x1d, /* |..M.....| */
332-
0x01, 0x04, 0x45, 0x54, 0x68, 0x65, 0x20, 0x54, /* |..EThe T| */
333-
0x69, 0x6d, 0x65, 0x73, 0x20, 0x30, 0x33, 0x2f, /* |imes 03/| */
334-
0x4a, 0x61, 0x6e, 0x2f, 0x32, 0x30, 0x30, 0x39, /* |Jan/2009| */
335-
0x20, 0x43, 0x68, 0x61, 0x6e, 0x63, 0x65, 0x6c, /* | Chancel| */
336-
0x6c, 0x6f, 0x72, 0x20, 0x6f, 0x6e, 0x20, 0x62, /* |lor on b| */
337-
0x72, 0x69, 0x6e, 0x6b, 0x20, 0x6f, 0x66, 0x20, /* |rink of | */
338-
0x73, 0x65, 0x63, 0x6f, 0x6e, 0x64, 0x20, 0x62, /* |second b| */
339-
0x61, 0x69, 0x6c, 0x6f, 0x75, 0x74, 0x20, 0x66, /* |ailout f| */
340-
0x6f, 0x72, 0x20, 0x62, 0x61, 0x6e, 0x6b, 0x73, /* |or banks| */
341-
0xff, 0xff, 0xff, 0xff, 0x01, 0x00, 0xf2, 0x05, /* |........| */
342-
0x2a, 0x01, 0x00, 0x00, 0x00, 0x43, 0x41, 0x04, /* |*....CA.| */
343-
0x67, 0x8a, 0xfd, 0xb0, 0xfe, 0x55, 0x48, 0x27, /* |g....UH'| */
344-
0x19, 0x67, 0xf1, 0xa6, 0x71, 0x30, 0xb7, 0x10, /* |.g..q0..| */
345-
0x5c, 0xd6, 0xa8, 0x28, 0xe0, 0x39, 0x09, 0xa6, /* |\..(.9..| */
346-
0x79, 0x62, 0xe0, 0xea, 0x1f, 0x61, 0xde, 0xb6, /* |yb...a..| */
347-
0x49, 0xf6, 0xbc, 0x3f, 0x4c, 0xef, 0x38, 0xc4, /* |I..?L.8.| */
348-
0xf3, 0x55, 0x04, 0xe5, 0x1e, 0xc1, 0x12, 0xde, /* |.U......| */
349-
0x5c, 0x38, 0x4d, 0xf7, 0xba, 0x0b, 0x8d, 0x57, /* |\8M....W| */
350-
0x8a, 0x4c, 0x70, 0x2b, 0x6b, 0xf1, 0x1d, 0x5f, /* |.Lp+k.._| */
351-
0xac, 0x00, 0x00, 0x00, 0x00, /* |.....| */
317+
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* |................| */
318+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* |................| */
319+
0x00, 0x00, 0x00, 0x00, 0x3b, 0xa3, 0xed, 0xfd, 0x7a, 0x7b, 0x12, 0xb2, 0x7a, 0xc7, 0x2c, 0x3e, /* |....;...z{..z.,>| */
320+
0x67, 0x76, 0x8f, 0x61, 0x7f, 0xc8, 0x1b, 0xc3, 0x88, 0x8a, 0x51, 0x32, 0x3a, 0x9f, 0xb8, 0xaa, /* |gv.a......Q2:...| */
321+
0x4b, 0x1e, 0x5e, 0x4a, 0x34, 0x6f, 0xa4, 0x68, 0xff, 0xff, 0x7f, 0x20, 0x02, 0x00, 0x00, 0x00, /* |K.^J4o.h... ....| */
322+
0x01, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* |................| */
323+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* |................| */
324+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x4d, 0x04, 0xff, 0xff, 0x00, 0x1d, /* |..........M.....| */
325+
0x01, 0x04, 0x45, 0x54, 0x68, 0x65, 0x20, 0x54, 0x69, 0x6d, 0x65, 0x73, 0x20, 0x30, 0x33, 0x2f, /* |..EThe Times 03/| */
326+
0x4a, 0x61, 0x6e, 0x2f, 0x32, 0x30, 0x30, 0x39, 0x20, 0x43, 0x68, 0x61, 0x6e, 0x63, 0x65, 0x6c, /* |Jan/2009 Chancel| */
327+
0x6c, 0x6f, 0x72, 0x20, 0x6f, 0x6e, 0x20, 0x62, 0x72, 0x69, 0x6e, 0x6b, 0x20, 0x6f, 0x66, 0x20, /* |lor on brink of | */
328+
0x73, 0x65, 0x63, 0x6f, 0x6e, 0x64, 0x20, 0x62, 0x61, 0x69, 0x6c, 0x6f, 0x75, 0x74, 0x20, 0x66, /* |second bailout f| */
329+
0x6f, 0x72, 0x20, 0x62, 0x61, 0x6e, 0x6b, 0x73, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00, 0xf2, 0x05, /* |or banks........| */
330+
0x2a, 0x01, 0x00, 0x00, 0x00, 0x43, 0x41, 0x04, 0x67, 0x8a, 0xfd, 0xb0, 0xfe, 0x55, 0x48, 0x27, /* |*....CA.g....UH'| */
331+
0x19, 0x67, 0xf1, 0xa6, 0x71, 0x30, 0xb7, 0x10, 0x5c, 0xd6, 0xa8, 0x28, 0xe0, 0x39, 0x09, 0xa6, /* |.g..q0..\..(.9..| */
332+
0x79, 0x62, 0xe0, 0xea, 0x1f, 0x61, 0xde, 0xb6, 0x49, 0xf6, 0xbc, 0x3f, 0x4c, 0xef, 0x38, 0xc4, /* |yb...a..I..?L.8.| */
333+
0xf3, 0x55, 0x04, 0xe5, 0x1e, 0xc1, 0x12, 0xde, 0x5c, 0x38, 0x4d, 0xf7, 0xba, 0x0b, 0x8d, 0x57, /* |.U......\8M....W| */
334+
0x8a, 0x4c, 0x70, 0x2b, 0x6b, 0xf1, 0x1d, 0x5f, 0xac, 0x00, 0x00, 0x00, 0x00, /* |.Lp+k.._..... | */
335+
}
336+
337+
// TestTeraTestNetGenesisProofOfWork tests that the tera test network genesis
338+
// block has valid proof of work.
339+
func TestTeraTestNetGenesisProofOfWork(t *testing.T) {
340+
// Get the genesis block
341+
genesisBlock := TeraTestNetParams.GenesisBlock
342+
343+
// Calculate the hash
344+
hash := genesisBlock.BlockHash()
345+
346+
// Get the target from bits
347+
bits := genesisBlock.Header.Bits
348+
mantissa := bits & 0x00ffffff
349+
exponent := bits >> 24
350+
351+
target := new(big.Int).SetInt64(int64(mantissa))
352+
if exponent > 3 {
353+
target.Lsh(target, 8*(uint(exponent)-3))
354+
}
355+
356+
// Check if hash meets difficulty
357+
hashInt := new(big.Int).SetBytes(hash.CloneBytes())
358+
359+
if hashInt.Cmp(target) > 0 {
360+
t.Fatalf("TestTeraTestNetGenesisProofOfWork: Genesis block does not meet difficulty requirement. "+
361+
"Hash: %s, Target: %064x", hash, target.Bytes())
362+
}
363+
364+
// Verify the nonce is the expected value
365+
expectedNonce := uint32(0x00000002)
366+
if genesisBlock.Header.Nonce != expectedNonce {
367+
t.Fatalf("TestTeraTestNetGenesisProofOfWork: Unexpected nonce - got %d, want %d",
368+
genesisBlock.Header.Nonce, expectedNonce)
369+
}
352370
}

0 commit comments

Comments
 (0)