forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollections.go
52 lines (47 loc) · 1.34 KB
/
collections.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
package simulation
import (
"bytes"
"fmt"
"cosmossdk.io/collections"
collcodec "cosmossdk.io/collections/codec"
"github.com/cosmos/cosmos-sdk/types/kv"
)
func NewStoreDecoderFuncFromCollectionsSchema(schema collections.Schema) func(kvA, kvB kv.Pair) string {
colls := schema.ListCollections()
prefixes := make([][]byte, len(colls))
valueCodecs := make([]collcodec.UntypedValueCodec, len(colls))
for i, coll := range colls {
prefixes[i] = coll.GetPrefix()
valueCodecs[i] = coll.ValueCodec()
}
return func(kvA, kvB kv.Pair) string {
for i, prefix := range prefixes {
if bytes.HasPrefix(kvA.Key, prefix) {
if !bytes.HasPrefix(kvB.Key, prefix) {
panic(fmt.Sprintf("prefix mismatch, keyA has prefix %x (%s), but keyB does not %x (%s)", prefix, prefix, kvB.Key, kvB.Key))
}
vc := valueCodecs[i]
// unmarshal kvA.Value to the corresponding type
vA, err := vc.Decode(kvA.Value)
if err != nil {
panic(err)
}
// unmarshal kvB.Value to the corresponding type
vB, err := vc.Decode(kvB.Value)
if err != nil {
panic(err)
}
vAString, err := vc.Stringify(vA)
if err != nil {
panic(err)
}
vBString, err := vc.Stringify(vB)
if err != nil {
panic(err)
}
return vAString + "\n" + vBString
}
}
panic(fmt.Errorf("unexpected key %X (%s)", kvA.Key, kvA.Key))
}
}