Skip to content

Commit 9c4d5c9

Browse files
committed
proof: unit test CommitmentProofs en-/decoder
This commit adds a unit test that does a roundtrip for the `CommitmentProofsEncoder` and `CommitmentProofsDecoder`to check if the random commitment proofs come out at the other end unscathed.
1 parent 6359e46 commit 9c4d5c9

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

proof/encoding_test.go

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package proof
2+
3+
import (
4+
"bytes"
5+
"reflect"
6+
"testing"
7+
8+
"github.com/lightninglabs/taproot-assets/asset"
9+
"github.com/lightninglabs/taproot-assets/commitment"
10+
"github.com/lightninglabs/taproot-assets/internal/test"
11+
"github.com/stretchr/testify/require"
12+
)
13+
14+
func TestCommitmentProofsDecoderRoundTrip(t *testing.T) {
15+
t.Parallel()
16+
17+
testBlocks := readTestData(t)
18+
oddTxBlock := testBlocks[0]
19+
20+
numProofs := 4
21+
proofs := make(map[asset.SerializedKey]commitment.Proof, numProofs)
22+
for range numProofs {
23+
genesis := asset.RandGenesis(t, asset.Collectible)
24+
scriptKey := test.RandPubKey(t)
25+
randProof := RandProof(t, genesis, scriptKey, oddTxBlock, 0, 1)
26+
randCommitmentProof := randProof.InclusionProof.CommitmentProof
27+
serializedKey := asset.SerializedKey(
28+
test.RandPubKey(t).SerializeCompressed(),
29+
)
30+
proofs[serializedKey] = randCommitmentProof.Proof
31+
}
32+
33+
var buf [8]byte
34+
35+
// Helper function to encode a map of commitment proofs.
36+
encodeProofs := func(proofs map[asset.SerializedKey]commitment.Proof,
37+
) []byte {
38+
39+
var b bytes.Buffer
40+
err := CommitmentProofsEncoder(&b, &proofs, &buf)
41+
require.NoError(t, err)
42+
return b.Bytes()
43+
}
44+
45+
// Helper function to decode a map of commitment proofs.
46+
decodeProofs := func(encoded []byte,
47+
) map[asset.SerializedKey]commitment.Proof {
48+
49+
var decodedProofs map[asset.SerializedKey]commitment.Proof
50+
err := CommitmentProofsDecoder(
51+
bytes.NewReader(encoded), &decodedProofs, &buf,
52+
uint64(len(encoded)),
53+
)
54+
require.NoError(t, err)
55+
return decodedProofs
56+
}
57+
58+
// Test case: round trip encoding and decoding.
59+
t.Run(
60+
"encode and decode map of 4 random commitment proofs",
61+
func(t *testing.T,
62+
) {
63+
64+
// Encode the proofs.
65+
encoded := encodeProofs(proofs)
66+
67+
// Decode the proofs.
68+
decodedProofs := decodeProofs(encoded)
69+
70+
// Assert the decoded proofs match the original.
71+
require.True(t, reflect.DeepEqual(
72+
proofs, decodedProofs,
73+
))
74+
})
75+
76+
// Test case: empty map.
77+
t.Run(
78+
"encode and decode empty map of commitment proofs",
79+
func(t *testing.T,
80+
) {
81+
// Create an empty map of commitment emptyMap.
82+
emptyMap := map[asset.SerializedKey]commitment.Proof{}
83+
84+
// Encode the proofs.
85+
encoded := encodeProofs(emptyMap)
86+
87+
// Decode the proofs.
88+
decodedMap := decodeProofs(encoded)
89+
90+
// Assert the decoded proofs match the original.
91+
require.True(t, reflect.DeepEqual(
92+
emptyMap, decodedMap,
93+
))
94+
})
95+
}

0 commit comments

Comments
 (0)