-
Notifications
You must be signed in to change notification settings - Fork 0
/
decoder.go
37 lines (31 loc) · 1.04 KB
/
decoder.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
package simulation
import (
"bytes"
"fmt"
"github.com/blockgenx/blockgen-sdk/types/kv"
"github.com/blockgenx/blockgen-sdk/x/evidence/exported"
"github.com/blockgenx/blockgen-sdk/x/evidence/types"
)
type EvidenceUnmarshaler interface {
UnmarshalEvidence([]byte) (exported.Evidence, error)
}
// NewDecodeStore returns a decoder function closure that unmarshals the KVPair's
// Value to the corresponding evidence type.
func NewDecodeStore(cdc EvidenceUnmarshaler) func(kvA, kvB kv.Pair) string {
return func(kvA, kvB kv.Pair) string {
switch {
case bytes.Equal(kvA.Key[:1], types.KeyPrefixEvidence):
evidenceA, err := cdc.UnmarshalEvidence(kvA.Value)
if err != nil {
panic(fmt.Sprintf("cannot unmarshal evidence: %s", err.Error()))
}
evidenceB, err := cdc.UnmarshalEvidence(kvB.Value)
if err != nil {
panic(fmt.Sprintf("cannot unmarshal evidence: %s", err.Error()))
}
return fmt.Sprintf("%v\n%v", evidenceA, evidenceB)
default:
panic(fmt.Sprintf("invalid %s key prefix %X", types.ModuleName, kvA.Key[:1]))
}
}
}