-
Notifications
You must be signed in to change notification settings - Fork 380
/
genesis.go
88 lines (78 loc) · 2.3 KB
/
genesis.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
package types
import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
type Sequence struct {
IDKey []byte `json:"id_key"`
Value uint64 `json:"value"`
}
func (s Sequence) ValidateBasic() error {
if len(s.IDKey) == 0 {
return sdkerrors.Wrap(ErrEmpty, "id key")
}
return nil
}
// GenesisState is the struct representation of the export genesis
type GenesisState struct {
Codes []Code `json:"codes"`
Contracts []Contract `json:"contracts"`
Sequences []Sequence `json:"sequences"`
}
func (s GenesisState) ValidateBasic() error {
for i := range s.Codes {
if err := s.Codes[i].ValidateBasic(); err != nil {
return sdkerrors.Wrapf(err, "code: %d", i)
}
}
for i := range s.Contracts {
if err := s.Contracts[i].ValidateBasic(); err != nil {
return sdkerrors.Wrapf(err, "contract: %d", i)
}
}
for i := range s.Sequences {
if err := s.Sequences[i].ValidateBasic(); err != nil {
return sdkerrors.Wrapf(err, "sequence: %d", i)
}
}
return nil
}
// Code struct encompasses CodeInfo and CodeBytes
type Code struct {
CodeInfo CodeInfo `json:"code_info"`
CodesBytes []byte `json:"code_bytes"`
}
func (c Code) ValidateBasic() error {
if err := c.CodeInfo.ValidateBasic(); err != nil {
return sdkerrors.Wrap(err, "code info")
}
if err := validateWasmCode(c.CodesBytes); err != nil {
return sdkerrors.Wrap(err, "code bytes")
}
return nil
}
// Contract struct encompasses ContractAddress, ContractInfo, and ContractState
type Contract struct {
ContractAddress sdk.AccAddress `json:"contract_address"`
ContractInfo ContractInfo `json:"contract_info"`
ContractState []Model `json:"contract_state"`
}
func (c Contract) ValidateBasic() error {
if err := sdk.VerifyAddressFormat(c.ContractAddress); err != nil {
return sdkerrors.Wrap(err, "contract address")
}
if err := c.ContractInfo.ValidateBasic(); err != nil {
return sdkerrors.Wrap(err, "contract info")
}
for i := range c.ContractState {
if err := c.ContractState[i].ValidateBasic(); err != nil {
return sdkerrors.Wrapf(err, "contract state %d", i)
}
}
return nil
}
// ValidateGenesis performs basic validation of supply genesis data returning an
// error for any failed validation criteria.
func ValidateGenesis(data GenesisState) error {
return data.ValidateBasic()
}