-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy pathccv.go
105 lines (89 loc) · 2.95 KB
/
ccv.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
package types
import (
"fmt"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
abci "github.com/tendermint/tendermint/abci/types"
)
func NewValidatorSetChangePacketData(valUpdates []abci.ValidatorUpdate, valUpdateID uint64, SlashAcks []string) ValidatorSetChangePacketData {
return ValidatorSetChangePacketData{
ValidatorUpdates: valUpdates,
ValsetUpdateId: valUpdateID,
SlashAcks: SlashAcks,
}
}
// ValidateBasic is used for validating the CCV packet data.
func (vsc ValidatorSetChangePacketData) ValidateBasic() error {
if len(vsc.ValidatorUpdates) == 0 {
return sdkerrors.Wrap(ErrInvalidPacketData, "validator updates cannot be empty")
}
if vsc.ValsetUpdateId == 0 {
return sdkerrors.Wrap(ErrInvalidPacketData, "valset update id cannot be equal to zero")
}
return nil
}
func (vsc ValidatorSetChangePacketData) GetBytes() []byte {
valUpdateBytes := ModuleCdc.MustMarshalJSON(&vsc)
return valUpdateBytes
}
func NewVSCMaturedPacketData(valUpdateID uint64) *VSCMaturedPacketData {
return &VSCMaturedPacketData{
ValsetUpdateId: valUpdateID,
}
}
// ValidateBasic is used for validating the VSCMatured packet data.
func (mat VSCMaturedPacketData) ValidateBasic() error {
if mat.ValsetUpdateId == 0 {
return sdkerrors.Wrap(ErrInvalidPacketData, "vscId cannot be equal to zero")
}
return nil
}
func (mat VSCMaturedPacketData) GetBytes() []byte {
bytes := ModuleCdc.MustMarshalJSON(&mat)
return bytes
}
func NewSlashPacketData(validator abci.Validator, valUpdateId uint64, infractionType stakingtypes.InfractionType) *SlashPacketData {
return &SlashPacketData{
Validator: validator,
ValsetUpdateId: valUpdateId,
Infraction: infractionType,
}
}
func (vdt SlashPacketData) ValidateBasic() error {
if len(vdt.Validator.Address) == 0 || vdt.Validator.Power == 0 {
return sdkerrors.Wrap(ErrInvalidPacketData, "validator fields cannot be empty")
}
if vdt.Infraction == stakingtypes.InfractionEmpty {
return sdkerrors.Wrap(ErrInvalidPacketData, "invalid infraction type")
}
return nil
}
func (vdt SlashPacketData) GetBytes() []byte {
valDowntimeBytes := ModuleCdc.MustMarshalJSON(&vdt)
return valDowntimeBytes
}
func (cp ConsumerPacketData) ValidateBasic() (err error) {
switch cp.Type {
case VscMaturedPacket:
// validate VSCMaturedPacket
vscPacket := cp.GetVscMaturedPacketData()
if vscPacket == nil {
return fmt.Errorf("invalid consumer packet data: VscMaturePacketData data cannot be empty")
}
err = vscPacket.ValidateBasic()
case SlashPacket:
// validate SlashPacket
slashPacket := cp.GetSlashPacketData()
if slashPacket == nil {
return fmt.Errorf("invalid consumer packet data: SlashPacketData data cannot be empty")
}
err = slashPacket.ValidateBasic()
default:
err = fmt.Errorf("invalid consumer packet type: %q", cp.Type)
}
return
}
func (cp ConsumerPacketData) GetBytes() []byte {
bytes := ModuleCdc.MustMarshalJSON(&cp)
return bytes
}