forked from akash-network/node
/
types.go
211 lines (172 loc) · 5.48 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package v1beta1
import (
"bytes"
sdk "github.com/cosmos/cosmos-sdk/types"
types "github.com/akash-network/node/types/v1beta1"
atypes "github.com/akash-network/node/x/audit/types/v1beta1"
)
type attributesMatching map[string]types.Attributes
const (
// ManifestVersionLength is the length of manifest version
ManifestVersionLength = 32
// DefaultOrderBiddingDuration is the default time limit for an Order being active.
// After the duration, the Order is automatically closed.
// ( 24(hr) * 3600(seconds per hour) ) / 7s-Block
DefaultOrderBiddingDuration = int64(12342)
// MaxBiddingDuration is roughly 30 days of block height
MaxBiddingDuration = DefaultOrderBiddingDuration * int64(30)
)
// ID method returns DeploymentID details of specific deployment
func (obj Deployment) ID() DeploymentID {
return obj.DeploymentID
}
// ValidateBasic asserts non-zero values
// TODO: This is causing an import cycle. I think there is some pattern here I'm missing tho..
func (g GroupSpec) ValidateBasic() error {
return validateDeploymentGroup(g)
}
// GetResources method returns resources list in group
func (g GroupSpec) GetResources() []types.Resources {
resources := make([]types.Resources, 0, len(g.Resources))
for _, r := range g.Resources {
resources = append(resources, types.Resources{
Resources: r.Resources,
Count: r.Count,
})
}
return resources
}
// GetName method returns group name
func (g GroupSpec) GetName() string {
return g.Name
}
// Price method returns price of group
func (g GroupSpec) Price() sdk.Coin {
var price sdk.Coin
for idx, resource := range g.Resources {
if idx == 0 {
price = resource.FullPrice()
continue
}
price = price.Add(resource.FullPrice())
}
return price
}
// MatchRequirements method compares provided attributes with specific group attributes.
// Argument provider is a bit cumbersome. First element is attributes from x/provider store
// in case tenant does not need signed attributes at all
// rest of elements (if any) are attributes signed by various auditors
func (g GroupSpec) MatchRequirements(provider []atypes.Provider) bool {
if (len(g.Requirements.SignedBy.AnyOf) != 0) || (len(g.Requirements.SignedBy.AllOf) != 0) {
// we cannot match if there is no signed attributes
if len(provider) < 2 {
return false
}
existingRequirements := make(attributesMatching)
for _, existing := range provider[1:] {
existingRequirements[existing.Auditor] = existing.Attributes
}
if len(g.Requirements.SignedBy.AllOf) != 0 {
for _, validator := range g.Requirements.SignedBy.AllOf {
// if at least one signature does not exist or no match on attributes - requirements cannot match
if existingAttr, exists := existingRequirements[validator]; !exists ||
!types.AttributesSubsetOf(g.Requirements.Attributes, existingAttr) {
return false
}
}
}
if len(g.Requirements.SignedBy.AnyOf) != 0 {
for _, validator := range g.Requirements.SignedBy.AnyOf {
if existingAttr, exists := existingRequirements[validator]; exists &&
types.AttributesSubsetOf(g.Requirements.Attributes, existingAttr) {
return true
}
}
return false
}
return true
}
return types.AttributesSubsetOf(g.Requirements.Attributes, provider[0].Attributes)
}
// MatchAttributes method compares provided attributes with specific group attributes
func (g GroupSpec) MatchAttributes(attr types.Attributes) bool {
return types.AttributesSubsetOf(g.Requirements.Attributes, attr)
}
// ID method returns GroupID details of specific group
func (g Group) ID() GroupID {
return g.GroupID
}
// ValidateClosable provides error response if group is already closed,
// and thus should not be closed again, else nil.
func (g Group) ValidateClosable() error {
switch g.State {
case GroupClosed:
return ErrGroupClosed
default:
return nil
}
}
// ValidatePausable provides error response if group is not pausable
func (g Group) ValidatePausable() error {
switch g.State {
case GroupClosed:
return ErrGroupClosed
case GroupPaused:
return ErrGroupPaused
default:
return nil
}
}
// ValidatePausable provides error response if group is not pausable
func (g Group) ValidateStartable() error {
switch g.State {
case GroupClosed:
return ErrGroupClosed
case GroupOpen:
return ErrGroupOpen
default:
return nil
}
}
// GetName method returns group name
func (g Group) GetName() string {
return g.GroupSpec.Name
}
// GetResources method returns resources list in group
func (g Group) GetResources() []types.Resources {
return g.GroupSpec.GetResources()
}
// FullPrice method returns full price of resource
func (r Resource) FullPrice() sdk.Coin {
return sdk.NewCoin(r.Price.Denom, r.Price.Amount.MulRaw(int64(r.Count)))
}
// DeploymentResponses is a collection of DeploymentResponse
type DeploymentResponses []QueryDeploymentResponse
func (ds DeploymentResponses) String() string {
var buf bytes.Buffer
const sep = "\n\n"
for _, d := range ds {
buf.WriteString(d.String())
buf.WriteString(sep)
}
if len(ds) > 0 {
buf.Truncate(buf.Len() - len(sep))
}
return buf.String()
}
// Accept returns whether deployment filters valid or not
func (filters DeploymentFilters) Accept(obj Deployment, stateVal Deployment_State) bool {
// Checking owner filter
if filters.Owner != "" && filters.Owner != obj.DeploymentID.Owner {
return false
}
// Checking dseq filter
if filters.DSeq != 0 && filters.DSeq != obj.DeploymentID.DSeq {
return false
}
// Checking state filter
if stateVal != 0 && stateVal != obj.State {
return false
}
return true
}