github.com/btcsuite/btcd@v0.24.0/blockchain/rolling_merkle_test.go (about) 1 package blockchain 2 3 import ( 4 "testing" 5 6 "github.com/btcsuite/btcd/chaincfg/chainhash" 7 "github.com/stretchr/testify/require" 8 ) 9 10 func TestRollingMerkleAdd(t *testing.T) { 11 tests := []struct { 12 leaves []chainhash.Hash 13 expectedRoots []chainhash.Hash 14 expectedNumLeaves uint64 15 }{ 16 // 00 (00 is also a root) 17 { 18 leaves: []chainhash.Hash{ 19 {0x00}, 20 }, 21 expectedRoots: []chainhash.Hash{ 22 {0x00}, 23 }, 24 expectedNumLeaves: 1, 25 }, 26 27 // root 28 // |---\ 29 // 00 01 30 { 31 leaves: []chainhash.Hash{ 32 {0x00}, 33 {0x01}, 34 }, 35 expectedRoots: []chainhash.Hash{ 36 func() chainhash.Hash { 37 hash, err := chainhash.NewHashFromStr( 38 "c2bf026e62af95cd" + 39 "7b785e2cd5a5f1ec" + 40 "01fafda85886a8eb" + 41 "d34482c0b05dc2c2") 42 require.NoError(t, err) 43 return *hash 44 }(), 45 }, 46 expectedNumLeaves: 2, 47 }, 48 49 // root 50 // |---\ 51 // 00 01 02 52 { 53 leaves: []chainhash.Hash{ 54 {0x00}, 55 {0x01}, 56 {0x02}, 57 }, 58 expectedRoots: []chainhash.Hash{ 59 func() chainhash.Hash { 60 hash, err := chainhash.NewHashFromStr( 61 "c2bf026e62af95cd" + 62 "7b785e2cd5a5f1ec" + 63 "01fafda85886a8eb" + 64 "d34482c0b05dc2c2") 65 require.NoError(t, err) 66 return *hash 67 }(), 68 {0x02}, 69 }, 70 expectedNumLeaves: 3, 71 }, 72 73 // root 74 // |-------\ 75 // br br 76 // |---\ |---\ 77 // 00 01 02 03 78 { 79 leaves: []chainhash.Hash{ 80 {0x00}, 81 {0x01}, 82 {0x02}, 83 {0x03}, 84 }, 85 expectedRoots: []chainhash.Hash{ 86 func() chainhash.Hash { 87 hash, err := chainhash.NewHashFromStr( 88 "270714425ea73eb8" + 89 "5942f0f705788f25" + 90 "1fefa3f533410a3f" + 91 "338de46e641082c4") 92 require.NoError(t, err) 93 return *hash 94 }(), 95 }, 96 expectedNumLeaves: 4, 97 }, 98 99 // root 100 // |-------\ 101 // br br 102 // |---\ |---\ 103 // 00 01 02 03 04 104 { 105 leaves: []chainhash.Hash{ 106 {0x00}, 107 {0x01}, 108 {0x02}, 109 {0x03}, 110 {0x04}, 111 }, 112 expectedRoots: []chainhash.Hash{ 113 func() chainhash.Hash { 114 hash, err := chainhash.NewHashFromStr( 115 "270714425ea73eb8" + 116 "5942f0f705788f25" + 117 "1fefa3f533410a3f" + 118 "338de46e641082c4") 119 require.NoError(t, err) 120 return *hash 121 }(), 122 {0x04}, 123 }, 124 expectedNumLeaves: 5, 125 }, 126 127 // root 128 // |-------\ 129 // br br root 130 // |---\ |---\ |---\ 131 // 00 01 02 03 04 05 132 { 133 leaves: []chainhash.Hash{ 134 {0x00}, 135 {0x01}, 136 {0x02}, 137 {0x03}, 138 {0x04}, 139 {0x05}, 140 }, 141 expectedRoots: []chainhash.Hash{ 142 func() chainhash.Hash { 143 hash, err := chainhash.NewHashFromStr( 144 "270714425ea73eb8" + 145 "5942f0f705788f25" + 146 "1fefa3f533410a3f" + 147 "338de46e641082c4") 148 require.NoError(t, err) 149 return *hash 150 }(), 151 func() chainhash.Hash { 152 hash, err := chainhash.NewHashFromStr( 153 "e5c2407ba454ffeb" + 154 "28cf0c50c5c293a8" + 155 "4c9a75788f8a8f35" + 156 "ccb974e606280377") 157 require.NoError(t, err) 158 return *hash 159 }(), 160 }, 161 expectedNumLeaves: 6, 162 }, 163 } 164 165 for _, test := range tests { 166 s := newRollingMerkleTreeStore(uint64(len(test.leaves))) 167 for _, leaf := range test.leaves { 168 s.add(leaf) 169 } 170 171 require.Equal(t, s.roots, test.expectedRoots) 172 require.Equal(t, s.numLeaves, test.expectedNumLeaves) 173 } 174 }