This repository has been archived by the owner on Dec 8, 2022. It is now read-only.
forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 1
/
decoder.go
64 lines (50 loc) · 2.1 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
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
package simulation
import (
"bytes"
"fmt"
"github.com/Pylons-tech/cosmos-sdk/codec"
sdk "github.com/Pylons-tech/cosmos-sdk/types"
"github.com/Pylons-tech/cosmos-sdk/types/kv"
"github.com/Pylons-tech/cosmos-sdk/x/staking/types"
)
// NewDecodeStore returns a decoder function closure that unmarshals the KVPair's
// Value to the corresponding staking type.
func NewDecodeStore(cdc codec.Codec) func(kvA, kvB kv.Pair) string {
return func(kvA, kvB kv.Pair) string {
switch {
case bytes.Equal(kvA.Key[:1], types.LastTotalPowerKey):
var powerA, powerB sdk.IntProto
cdc.MustUnmarshal(kvA.Value, &powerA)
cdc.MustUnmarshal(kvB.Value, &powerB)
return fmt.Sprintf("%v\n%v", powerA, powerB)
case bytes.Equal(kvA.Key[:1], types.ValidatorsKey):
var validatorA, validatorB types.Validator
cdc.MustUnmarshal(kvA.Value, &validatorA)
cdc.MustUnmarshal(kvB.Value, &validatorB)
return fmt.Sprintf("%v\n%v", validatorA, validatorB)
case bytes.Equal(kvA.Key[:1], types.LastValidatorPowerKey),
bytes.Equal(kvA.Key[:1], types.ValidatorsByConsAddrKey),
bytes.Equal(kvA.Key[:1], types.ValidatorsByPowerIndexKey):
return fmt.Sprintf("%v\n%v", sdk.ValAddress(kvA.Value), sdk.ValAddress(kvB.Value))
case bytes.Equal(kvA.Key[:1], types.DelegationKey):
var delegationA, delegationB types.Delegation
cdc.MustUnmarshal(kvA.Value, &delegationA)
cdc.MustUnmarshal(kvB.Value, &delegationB)
return fmt.Sprintf("%v\n%v", delegationA, delegationB)
case bytes.Equal(kvA.Key[:1], types.UnbondingDelegationKey),
bytes.Equal(kvA.Key[:1], types.UnbondingDelegationByValIndexKey):
var ubdA, ubdB types.UnbondingDelegation
cdc.MustUnmarshal(kvA.Value, &ubdA)
cdc.MustUnmarshal(kvB.Value, &ubdB)
return fmt.Sprintf("%v\n%v", ubdA, ubdB)
case bytes.Equal(kvA.Key[:1], types.RedelegationKey),
bytes.Equal(kvA.Key[:1], types.RedelegationByValSrcIndexKey):
var redA, redB types.Redelegation
cdc.MustUnmarshal(kvA.Value, &redA)
cdc.MustUnmarshal(kvB.Value, &redB)
return fmt.Sprintf("%v\n%v", redA, redB)
default:
panic(fmt.Sprintf("invalid staking key prefix %X", kvA.Key[:1]))
}
}
}