-
Notifications
You must be signed in to change notification settings - Fork 105
/
types.go
155 lines (120 loc) · 3.85 KB
/
types.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package types
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/axelarnetwork/axelar-core/utils"
"github.com/axelarnetwork/axelar-core/x/nexus/exported"
)
const maxBitmapSize = 1 << 15 // 32,768
// NewMaintainerState is the constructor for MaintainerState
func NewMaintainerState(address sdk.ValAddress) MaintainerState {
return MaintainerState{
Address: address,
MissingVotes: utils.NewBitmap(maxBitmapSize),
IncorrectVotes: utils.NewBitmap(maxBitmapSize),
}
}
// ValidateBasic returns error if the given MaintainerState is invalid, nil otherwise
func (m MaintainerState) ValidateBasic() error {
if err := sdk.VerifyAddressFormat(m.Address); err != nil {
return err
}
return nil
}
// NewLinkedAddresses is the constructor of LinkedAddresses
func NewLinkedAddresses(depositAddress, recepientAddress exported.CrossChainAddress) LinkedAddresses {
return LinkedAddresses{
DepositAddress: depositAddress,
RecipientAddress: recepientAddress,
}
}
// Validate validates the LinkedAddresses
func (m LinkedAddresses) Validate() error {
if err := m.DepositAddress.Validate(); err != nil {
return err
}
if err := m.RecipientAddress.Validate(); err != nil {
return err
}
return nil
}
// IndexOfMaintainer returns the index of the maintainer in the given chain state
func (m ChainState) IndexOfMaintainer(address sdk.ValAddress) int {
for i, ms := range m.MaintainerStates {
if ms.Address.Equals(address) {
return i
}
}
return -1
}
func (m ChainState) indexOfAsset(asset string) int {
for i := range m.Assets {
if m.Assets[i].Denom == asset {
return i
}
}
return -1
}
// Validate validates the ChainState
func (m ChainState) Validate() error {
if err := m.Chain.Validate(); err != nil {
return err
}
for _, ms := range m.MaintainerStates {
if err := ms.ValidateBasic(); err != nil {
return err
}
}
if len(m.Assets) == 0 {
return fmt.Errorf("no assets found")
}
seenDenoms := make(map[string]bool)
for _, asset := range m.Assets {
if !asset.IsNativeAsset && !m.Chain.SupportsForeignAssets {
return fmt.Errorf("chain does not support foreign assets")
}
if err := asset.Validate(); err != nil {
return sdkerrors.Wrap(err, "invalid asset")
}
if seenDenoms[asset.Denom] {
return fmt.Errorf("duplicate asset found")
}
seenDenoms[asset.Denom] = true
}
return nil
}
// HasAsset returns true if the chain state has the given asset registered; false otherwise
func (m ChainState) HasAsset(asset string) bool {
return m.indexOfAsset(asset) != -1
}
// AddAsset registers the given asset in chain state
func (m *ChainState) AddAsset(asset exported.Asset) error {
if m.HasAsset(asset.Denom) {
return fmt.Errorf("asset %s is already registered for chain %s", asset.Denom, m.Chain.Name)
}
m.Assets = append(m.Assets, asset)
return nil
}
// HasMaintainer returns true if the given maintainer is registered for the chain; false otherwise
func (m ChainState) HasMaintainer(address sdk.ValAddress) bool {
return m.IndexOfMaintainer(address) != -1
}
// AddMaintainer registers the given maintainer for the chain
func (m *ChainState) AddMaintainer(address sdk.ValAddress) error {
if m.HasMaintainer(address) {
return fmt.Errorf("maintainer %s is already registered for chain %s", address.String(), m.Chain.Name)
}
m.MaintainerStates = append(m.MaintainerStates, NewMaintainerState(address))
return nil
}
// RemoveMaintainer deregisters the given maintainer for the chain
func (m *ChainState) RemoveMaintainer(address sdk.ValAddress) error {
i := m.IndexOfMaintainer(address)
if i == -1 {
return fmt.Errorf("maintainer %s is not registered for chain %s", address.String(), m.Chain.Name)
}
m.MaintainerStates[i] = m.MaintainerStates[len(m.MaintainerStates)-1]
m.MaintainerStates = m.MaintainerStates[:len(m.MaintainerStates)-1]
return nil
}