decred.org/dcrdex@v1.0.3/dex/networks/zec/block_test.go (about) 1 package zec 2 3 import ( 4 "bytes" 5 _ "embed" 6 "encoding/binary" 7 "encoding/hex" 8 "testing" 9 10 "github.com/btcsuite/btcd/chaincfg/chainhash" 11 "github.com/btcsuite/btcd/wire" 12 ) 13 14 var ( 15 //go:embed test-data/simnet_block_header.dat 16 simnetBlockHeader []byte 17 //go:embed test-data/block_1624455.dat 18 block1624455 []byte 19 //go:embed test-data/header_1624455.dat 20 header1624455 []byte 21 //go:embed test-data/solution_1624455.dat 22 solution1624455 []byte 23 ) 24 25 func TestBlock(t *testing.T) { 26 // expHash := mustDecodeHex("00000000015c8a406ff880c5be4d2ae2744eab8be02a33d0179d68f47e51ea82") 27 const expVersion = 4 28 expPrevBlock, _ := chainhash.NewHashFromStr("000000000136717e394d8de1f86257724bb463348993a1fbb651bd3fd3f1a279") 29 expMerkleRoot, _ := chainhash.NewHashFromStr("43eec2cfd1487ee70ab0277d7f761d99cf6c19d554d2f73959f4af45d516727f") 30 // expHashBlockCommitments := mustDecodeHex("30702d1b320e2ea8f603aa8fb54baea5581761d19fccf5061ac82e81d8fdeea4") 31 expNonce := mustDecodeHex("f5a409400000000000000000000300000000000000000000000000000000d0e2") 32 33 expBits := binary.LittleEndian.Uint32(mustDecodeHex("d0aa011c")) 34 const expTime = 1649294915 35 36 checkHeader := func(hdr *wire.BlockHeader) { 37 if hdr.Version != expVersion { 38 t.Fatalf("wrong version. expected %d, got %d", expVersion, hdr.Version) 39 } 40 41 if *expPrevBlock != hdr.PrevBlock { 42 t.Fatal("wrong previous block", expPrevBlock, hdr.PrevBlock[:]) 43 } 44 45 if *expMerkleRoot != hdr.MerkleRoot { 46 t.Fatal("wrong merkle root", expMerkleRoot, hdr.MerkleRoot[:]) 47 } 48 49 // TODO: Find out why this is not right. 50 // if !bytes.Equal(zecBlock.HashBlockCommitments[:], expHashBlockCommitments) { 51 // t.Fatal("wrong hashBlockCommitments", zecBlock.HashBlockCommitments[:], expHashBlockCommitments, h) 52 // } 53 54 if hdr.Bits != expBits { 55 t.Fatalf("wrong bits") 56 } 57 58 if hdr.Timestamp.Unix() != expTime { 59 t.Fatalf("wrong timestamp") 60 } 61 } 62 63 hdr, err := DeserializeBlockHeader(header1624455) 64 if err != nil { 65 t.Fatalf("DeserializeBlockHeader error: %v", err) 66 } 67 checkHeader(hdr) 68 69 zecBlock, err := DeserializeBlock(block1624455) 70 if err != nil { 71 t.Fatalf("decodeBlockHeader error: %v", err) 72 } 73 checkHeader(&zecBlock.MsgBlock.Header) 74 75 if !bytes.Equal(zecBlock.Nonce[:], expNonce) { 76 t.Fatal("wrong nonce", zecBlock.Nonce[:], expNonce) 77 } 78 79 if !bytes.Equal(zecBlock.Solution[:], solution1624455) { 80 t.Fatal("wrong solution") 81 } 82 83 if len(zecBlock.Transactions) != 1 { 84 t.Fatalf("expected 1 transaction, got %d", len(zecBlock.Transactions)) 85 } 86 87 tx := zecBlock.Transactions[0] 88 89 if len(tx.TxIn) != 1 { 90 t.Fatalf("wrong number of tx inputs. expected 1, got %d", len(tx.TxIn)) 91 } 92 93 if len(tx.TxOut) != 4 { 94 t.Fatalf("wrong number of tx outputs. expected 4, got %d", len(tx.TxOut)) 95 } 96 } 97 98 func TestSimnetBlockHeader(t *testing.T) { 99 zecBlock := &Block{} 100 if err := zecBlock.decodeBlockHeader(bytes.NewReader(simnetBlockHeader)); err != nil { 101 t.Fatalf("decodeBlockHeader error: %v", err) 102 } 103 } 104 105 func mustDecodeHex(hx string) []byte { 106 b, err := hex.DecodeString(hx) 107 if err != nil { 108 panic("mustDecodeHex: " + err.Error()) 109 } 110 return b 111 }