-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathvectors_data_test.go
114 lines (105 loc) · 3.19 KB
/
vectors_data_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package ics23
import (
"encoding/hex"
"encoding/json"
"os"
"path/filepath"
"testing"
)
// TestVector is what is stored in the file
type TestVector struct {
RootHash string `json:"root"`
Proof string `json:"proof"`
Key string `json:"key"`
Value string `json:"value"`
}
// RefData is parsed version of everything except the CommitmentProof itself
type RefData struct {
RootHash []byte
Key []byte
Value []byte
}
type TestVectorsStruct struct {
Dir string
Filename string
Spec *ProofSpec
}
func VectorsTestData() []TestVectorsStruct {
iavl := filepath.Join("..", "testdata", "iavl")
tendermint := filepath.Join("..", "testdata", "tendermint")
smt := filepath.Join("..", "testdata", "smt")
cases := []TestVectorsStruct{
{Dir: iavl, Filename: "exist_left.json", Spec: IavlSpec},
{Dir: iavl, Filename: "exist_right.json", Spec: IavlSpec},
{Dir: iavl, Filename: "exist_middle.json", Spec: IavlSpec},
{Dir: iavl, Filename: "nonexist_left.json", Spec: IavlSpec},
{Dir: iavl, Filename: "nonexist_right.json", Spec: IavlSpec},
{Dir: iavl, Filename: "nonexist_middle.json", Spec: IavlSpec},
{Dir: tendermint, Filename: "exist_left.json", Spec: TendermintSpec},
{Dir: tendermint, Filename: "exist_right.json", Spec: TendermintSpec},
{Dir: tendermint, Filename: "exist_middle.json", Spec: TendermintSpec},
{Dir: tendermint, Filename: "nonexist_left.json", Spec: TendermintSpec},
{Dir: tendermint, Filename: "nonexist_right.json", Spec: TendermintSpec},
{Dir: tendermint, Filename: "nonexist_middle.json", Spec: TendermintSpec},
{Dir: smt, Filename: "exist_left.json", Spec: SmtSpec},
{Dir: smt, Filename: "exist_right.json", Spec: SmtSpec},
{Dir: smt, Filename: "exist_middle.json", Spec: SmtSpec},
{Dir: smt, Filename: "nonexist_left.json", Spec: SmtSpec},
{Dir: smt, Filename: "nonexist_right.json", Spec: SmtSpec},
{Dir: smt, Filename: "nonexist_middle.json", Spec: SmtSpec},
}
return cases
}
// BatchVector is what is stored in the file
type BatchVector struct {
RootHash string `json:"root"`
Proof string `json:"proof"`
Items []struct {
Key string `json:"key"`
Value string `json:"value"`
}
}
type BatchVectorData struct {
Spec *ProofSpec
Proof *CommitmentProof
Ref *RefData
Invalid bool // default is valid
}
func LoadFile(tb testing.TB, dir string, filename string) (*CommitmentProof, *RefData) {
tb.Helper()
// load the file into a json struct
name := filepath.Join(dir, filename)
bz, err := os.ReadFile(name)
if err != nil {
tb.Fatalf("Read file: %+v", err)
}
var data TestVector
err = json.Unmarshal(bz, &data)
if err != nil {
tb.Fatalf("Unmarshal json: %+v", err)
}
// parse the protobuf object
var proof CommitmentProof
err = proof.Unmarshal(mustHex(tb, data.Proof))
if err != nil {
tb.Fatalf("Unmarshal protobuf: %+v", err)
}
var ref RefData
ref.RootHash = CommitmentRoot(mustHex(tb, data.RootHash))
ref.Key = mustHex(tb, data.Key)
if data.Value != "" {
ref.Value = mustHex(tb, data.Value)
}
return &proof, &ref
}
func mustHex(tb testing.TB, data string) []byte {
tb.Helper()
if data == "" {
return nil
}
res, err := hex.DecodeString(data)
if err != nil {
tb.Fatalf("decoding hex: %v", err)
}
return res
}