forked from cosmos/ics23
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompress.go
160 lines (141 loc) · 3.7 KB
/
compress.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package ics23
// IsCompressed returns true if the proof was compressed
func IsCompressed(proof *CommitmentProof) bool {
return proof.GetCompressed() != nil
}
// Compress will return a CompressedBatchProof if the input is BatchProof
// Otherwise it will return the input.
// This is safe to call multiple times (idempotent)
func Compress(proof *CommitmentProof) *CommitmentProof {
batch := proof.GetBatch()
if batch == nil {
return proof
}
return &CommitmentProof{
Proof: &CommitmentProof_Compressed{
Compressed: compress(batch),
},
}
}
// Decompress will return a BatchProof if the input is CompressedBatchProof
// Otherwise it will return the input.
// This is safe to call multiple times (idempotent)
func Decompress(proof *CommitmentProof) *CommitmentProof {
comp := proof.GetCompressed()
if comp != nil {
return &CommitmentProof{
Proof: &CommitmentProof_Batch{
Batch: decompress(comp),
},
}
}
return proof
}
func compress(batch *BatchProof) *CompressedBatchProof {
var centries []*CompressedBatchEntry
var lookup []*InnerOp
registry := make(map[string]int32)
for _, entry := range batch.Entries {
centry := compressEntry(entry, &lookup, registry)
centries = append(centries, centry)
}
return &CompressedBatchProof{
Entries: centries,
LookupInners: lookup,
}
}
func compressEntry(entry *BatchEntry, lookup *[]*InnerOp, registry map[string]int32) *CompressedBatchEntry {
if exist := entry.GetExist(); exist != nil {
return &CompressedBatchEntry{
Proof: &CompressedBatchEntry_Exist{
Exist: compressExist(exist, lookup, registry),
},
}
}
non := entry.GetNonexist()
return &CompressedBatchEntry{
Proof: &CompressedBatchEntry_Nonexist{
Nonexist: &CompressedNonExistenceProof{
Key: non.Key,
Left: compressExist(non.Left, lookup, registry),
Right: compressExist(non.Right, lookup, registry),
},
},
}
}
func compressExist(exist *ExistenceProof, lookup *[]*InnerOp, registry map[string]int32) *CompressedExistenceProof {
if exist == nil {
return nil
}
res := &CompressedExistenceProof{
Key: exist.Key,
Value: exist.Value,
Leaf: exist.Leaf,
Path: make([]int32, len(exist.Path)),
}
for i, step := range exist.Path {
res.Path[i] = compressStep(step, lookup, registry)
}
return res
}
func compressStep(step *InnerOp, lookup *[]*InnerOp, registry map[string]int32) int32 {
bz, err := step.Marshal()
if err != nil {
panic(err)
}
sig := string(bz)
// load from cache if there
if num, ok := registry[sig]; ok {
return num
}
// create new step if not there
num := int32(len(*lookup))
*lookup = append(*lookup, step)
registry[sig] = num
return num
}
func decompress(comp *CompressedBatchProof) *BatchProof {
lookup := comp.LookupInners
var entries []*BatchEntry
for _, centry := range comp.Entries {
entry := decompressEntry(centry, lookup)
entries = append(entries, entry)
}
return &BatchProof{
Entries: entries,
}
}
func decompressEntry(entry *CompressedBatchEntry, lookup []*InnerOp) *BatchEntry {
if exist := entry.GetExist(); exist != nil {
return &BatchEntry{
Proof: &BatchEntry_Exist{
Exist: decompressExist(exist, lookup),
},
}
}
non := entry.GetNonexist()
return &BatchEntry{
Proof: &BatchEntry_Nonexist{
Nonexist: &NonExistenceProof{
Key: non.Key,
Left: decompressExist(non.Left, lookup),
Right: decompressExist(non.Right, lookup),
},
},
}
}
func decompressExist(exist *CompressedExistenceProof, lookup []*InnerOp) *ExistenceProof {
if exist == nil {
return nil
}
res := &ExistenceProof{
Key: exist.Key,
Value: exist.Value,
Leaf: exist.Leaf,
Path: make([]*InnerOp, len(exist.Path)),
}
for i, step := range exist.Path {
res.Path[i] = lookup[step]
}
return res
}