@@ -1147,3 +1147,97 @@ func Benchmark_NodeIndex(b *testing.B) {
11471147 require .GreaterOrEqual (b , index , 0 )
11481148 }
11491149}
1150+
1151+ func TestGetMerkleProofOddLeaves (t * testing.T ) {
1152+ t .Run ("odd number of leaves in power-of-two tree" , func (t * testing.T ) {
1153+ // Create a subtree with capacity for 4 nodes but only add 3 (odd number)
1154+ tree , err := NewTree (2 ) // height 2 = 2^2 = 4 leaves
1155+ require .NoError (t , err )
1156+
1157+ // Add 3 nodes (odd number)
1158+ hash1 , _ := chainhash .NewHashFromStr ("1111111111111111111111111111111111111111111111111111111111111111" )
1159+ hash2 , _ := chainhash .NewHashFromStr ("2222222222222222222222222222222222222222222222222222222222222222" )
1160+ hash3 , _ := chainhash .NewHashFromStr ("3333333333333333333333333333333333333333333333333333333333333333" )
1161+
1162+ err = tree .AddNode (* hash1 , 100 , 250 )
1163+ require .NoError (t , err )
1164+ err = tree .AddNode (* hash2 , 200 , 300 )
1165+ require .NoError (t , err )
1166+ err = tree .AddNode (* hash3 , 150 , 275 )
1167+ require .NoError (t , err )
1168+
1169+ // Test GetMerkleProof for the last node (index 2)
1170+ // This should duplicate the last node as its sibling
1171+ proof , err := tree .GetMerkleProof (2 )
1172+ require .NoError (t , err )
1173+ require .NotNil (t , proof )
1174+
1175+ // The first hash in the proof should be the duplicate of the last node
1176+ require .Equal (t , hash3 .String (), proof [0 ].String ())
1177+ })
1178+
1179+ t .Run ("even number of leaves" , func (t * testing.T ) {
1180+ // Create a subtree with 4 nodes (even number)
1181+ tree , err := NewTreeByLeafCount (4 )
1182+ require .NoError (t , err )
1183+
1184+ // Add 4 nodes
1185+ hash1 , _ := chainhash .NewHashFromStr ("1111111111111111111111111111111111111111111111111111111111111111" )
1186+ hash2 , _ := chainhash .NewHashFromStr ("2222222222222222222222222222222222222222222222222222222222222222" )
1187+ hash3 , _ := chainhash .NewHashFromStr ("3333333333333333333333333333333333333333333333333333333333333333" )
1188+ hash4 , _ := chainhash .NewHashFromStr ("4444444444444444444444444444444444444444444444444444444444444444" )
1189+
1190+ err = tree .AddNode (* hash1 , 100 , 250 )
1191+ require .NoError (t , err )
1192+ err = tree .AddNode (* hash2 , 200 , 300 )
1193+ require .NoError (t , err )
1194+ err = tree .AddNode (* hash3 , 150 , 275 )
1195+ require .NoError (t , err )
1196+ err = tree .AddNode (* hash4 , 175 , 325 )
1197+ require .NoError (t , err )
1198+
1199+ // Test GetMerkleProof for the second-to-last node (index 2)
1200+ proof , err := tree .GetMerkleProof (2 )
1201+ require .NoError (t , err )
1202+ require .NotNil (t , proof )
1203+
1204+ // The first hash in the proof should be hash4 (its sibling)
1205+ require .Equal (t , hash4 .String (), proof [0 ].String ())
1206+
1207+ // Test GetMerkleProof for the last node (index 3)
1208+ proof , err = tree .GetMerkleProof (3 )
1209+ require .NoError (t , err )
1210+ require .NotNil (t , proof )
1211+
1212+ // The first hash in the proof should be hash3 (its sibling)
1213+ require .Equal (t , hash3 .String (), proof [0 ].String ())
1214+ })
1215+
1216+ t .Run ("large odd number of leaves" , func (t * testing.T ) {
1217+ // Test with 541 nodes (the example that was failing)
1218+ // Tree needs power of 2 capacity: 2^10 = 1024 > 541
1219+ tree , err := NewTree (10 )
1220+ require .NoError (t , err )
1221+
1222+ // Add 541 nodes (odd number)
1223+ for i := uint64 (0 ); i < uint64 (541 ); i ++ {
1224+ hash , _ := chainhash .NewHashFromStr ("0000000000000000000000000000000000000000000000000000000000000001" )
1225+ err = tree .AddNode (* hash , i , i * 100 )
1226+ require .NoError (t , err )
1227+ }
1228+
1229+ // Test GetMerkleProof for the last node (index 540)
1230+ // This was the one causing the panic
1231+ proof , err := tree .GetMerkleProof (540 )
1232+ require .NoError (t , err )
1233+ require .NotNil (t , proof )
1234+
1235+ // Should not panic and should return a valid proof
1236+ require .NotEmptyf (t , proof , "Merkle proof should not be empty" )
1237+
1238+ // The first hash in the proof should be the duplicate of the last node
1239+ // since 540 is even and has no sibling at index 541
1240+ lastNodeHash := tree .Nodes [540 ].Hash
1241+ require .Equal (t , lastNodeHash .String (), proof [0 ].String ())
1242+ })
1243+ }
0 commit comments