merkledrop_test.gno
2.41 Kb · 83 lines
1package merkledrop
2
3import (
4 "testing"
5
6 "gno.land/p/nt/uassert/v0"
7)
8
9// recipients + proofs generated off-chain with the exact leaf/node scheme in
10// merkledrop.gno, over the same four allocations, resolving to merkleRoot.
11type alloc struct {
12 addr address
13 amount uint64
14 proof string
15}
16
17var fixtures = []alloc{
18 {
19 address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5"), 100,
20 "dd57b89301cc6f1aa5593eb0881cefefcf2ef85ce7629dab61ad0528c301bce9,7c1352bec23bc53d2b573373623cff31174a738de9a257906866c9b598d01253",
21 },
22 {
23 address("g1sss9uxef4l6lwc0mxq8n5v0e4vqpml7c39cxq2"), 250,
24 "0df511d09bf004329ac648f5d029d192cedb7bac0aa04105be45ae9e3f727160,7c1352bec23bc53d2b573373623cff31174a738de9a257906866c9b598d01253",
25 },
26 {
27 address("g1manfred47kzduec920z88wfr64ylksmdcedar8"), 500,
28 "d71269d6916f8f2b80ea94d9fa949d2469f7d8160422c9512162ca6432de8aad,8f128549b4eb5751c7cd8e0ddfb7c5533266f4b5d5ec81b7a5b00dc23671e87e",
29 },
30 {
31 address("g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj"), 750,
32 "7f9354d47613efae448a8f78f8aacb973532c91d7eaf4195bf2ba86f22b27c45,8f128549b4eb5751c7cd8e0ddfb7c5533266f4b5d5ec81b7a5b00dc23671e87e",
33 },
34}
35
36func TestVerifyValidProofs(t *testing.T) {
37 for _, f := range fixtures {
38 uassert.True(t, Verify(f.addr, f.amount, f.proof),
39 "expected valid proof for "+f.addr.String())
40 }
41}
42
43func TestVerifyRejectsWrongAmount(t *testing.T) {
44 f := fixtures[0]
45 uassert.False(t, Verify(f.addr, f.amount+1, f.proof),
46 "tampered amount must not verify")
47}
48
49func TestVerifyRejectsWrongAddress(t *testing.T) {
50 f := fixtures[0]
51 other := fixtures[1].addr
52 uassert.False(t, Verify(other, f.amount, f.proof),
53 "proof bound to a different address must not verify")
54}
55
56func TestVerifyRejectsEmptyProof(t *testing.T) {
57 f := fixtures[0]
58 uassert.False(t, Verify(f.addr, f.amount, ""),
59 "empty proof cannot reach a multi-leaf root")
60}
61
62func TestParseProofRoundTrips(t *testing.T) {
63 nodes := parseProof(fixtures[0].proof)
64 uassert.Equal(t, 2, len(nodes))
65 uassert.Equal(t, 32, len(nodes[0]))
66 uassert.Equal(t, 32, len(nodes[1]))
67}
68
69func TestParseProofEmpty(t *testing.T) {
70 uassert.Equal(t, 0, len(parseProof("")))
71 uassert.Equal(t, 0, len(parseProof(" ")))
72}
73
74func TestHashPairCommutative(t *testing.T) {
75 a := leaf(fixtures[0].addr, fixtures[0].amount)
76 b := leaf(fixtures[1].addr, fixtures[1].amount)
77 left := hashPair(a, b)
78 right := hashPair(b, a)
79 uassert.Equal(t, len(left), len(right))
80 for i := range left {
81 uassert.Equal(t, left[i], right[i])
82 }
83}