-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathfarm.go
268 lines (224 loc) · 5.92 KB
/
farm.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package substrate
import (
"fmt"
"github.com/centrifuge/go-substrate-rpc-client/v4/scale"
"github.com/centrifuge/go-substrate-rpc-client/v4/types"
"github.com/pkg/errors"
)
// NodeCertification is a substrate enum
type NodeCertification struct {
IsDiy bool `json:"is_diy"`
IsCertified bool `json:"is_certified"`
}
// Decode implementation for the enum type
func (p *NodeCertification) Decode(decoder scale.Decoder) error {
b, err := decoder.ReadOneByte()
if err != nil {
return err
}
switch b {
case 0:
p.IsDiy = true
case 1:
p.IsCertified = true
default:
return fmt.Errorf("unknown NodeCertification value %d", b)
}
return nil
}
// Decode implementation for the enum type
func (p NodeCertification) Encode(encoder scale.Encoder) (err error) {
if p.IsDiy {
err = encoder.PushByte(0)
} else if p.IsCertified {
err = encoder.PushByte(1)
}
return
}
// NodeCertification is a substrate enum
type FarmCertification struct {
IsNotCertified bool `json:"is_not_certified"`
IsGold bool `json:"is_gold"`
}
// Decode implementation for the enum type
func (p *FarmCertification) Decode(decoder scale.Decoder) error {
b, err := decoder.ReadOneByte()
if err != nil {
return err
}
switch b {
case 0:
p.IsNotCertified = true
case 1:
p.IsGold = true
default:
return fmt.Errorf("unknown FarmCertification value")
}
return nil
}
// Decode implementation for the enum type
func (p FarmCertification) Encode(encoder scale.Encoder) (err error) {
if p.IsNotCertified {
err = encoder.PushByte(0)
} else if p.IsGold {
err = encoder.PushByte(1)
}
return
}
// Farm type
type Farm struct {
Versioned
ID types.U32 `json:"id"`
Name string `json:"name"`
TwinID types.U32 `json:"twin_id"`
PricingPolicyID types.U32 `json:"pricing_policy_id"`
CertificationType FarmCertification `json:"certification_type"`
PublicIPs []PublicIP `json:"public_ips"`
DedicatedFarm bool `json:"dedicated_farm"`
FarmingPoliciesLimit OptionFarmingPolicyLimit `json:"farming_policies_limit"`
}
type FarmingPolicyLimit struct {
FarmingPolicyID types.U32 `json:"farming_policy_id"`
Cu types.OptionU64 `json:"cu"`
Su types.OptionU64 `json:"su"`
End types.OptionU64 `json:"end"`
NodeCount types.OptionU32 `json:"node_count"`
NodeCertification bool `json:"node_certification"`
}
// OptionFarmingPolicyLimit type
type OptionFarmingPolicyLimit struct {
HasValue bool `json:"has_value"`
AsValue FarmingPolicyLimit `json:"as_value"`
}
// Encode implementation
func (m OptionFarmingPolicyLimit) Encode(encoder scale.Encoder) (err error) {
var i byte
if m.HasValue {
i = 1
}
err = encoder.PushByte(i)
if err != nil {
return err
}
if m.HasValue {
err = encoder.Encode(m.AsValue)
}
return
}
// Decode implementation
func (m *OptionFarmingPolicyLimit) Decode(decoder scale.Decoder) (err error) {
var i byte
if err := decoder.Decode(&i); err != nil {
return err
}
switch i {
case 0:
return nil
case 1:
m.HasValue = true
return decoder.Decode(&m.AsValue)
default:
return fmt.Errorf("unknown value for Option")
}
}
// PublicIP structure
type PublicIP struct {
IP string `json:"ip"`
Gateway string `json:"gateway"`
ContractID types.U64 `json:"contract_id"`
}
// PublicIPInput structure
type PublicIPInput struct {
IP string `json:"ip"`
Gateway string `json:"gateway"`
}
// GetFarm gets a farm with ID
func (s *Substrate) GetFarm(id uint32) (*Farm, error) {
cl, meta, err := s.GetClient()
if err != nil {
return nil, err
}
bytes, err := Encode(id)
if err != nil {
return nil, errors.Wrap(err, "substrate: encoding error building query arguments")
}
key, err := types.CreateStorageKey(meta, "TfgridModule", "Farms", bytes, nil)
if err != nil {
return nil, errors.Wrap(err, "failed to create substrate query key")
}
raw, err := cl.RPC.State.GetStorageRawLatest(key)
if err != nil {
return nil, errors.Wrap(err, "failed to lookup entity")
}
if len(*raw) == 0 {
return nil, errors.Wrap(ErrNotFound, "farm not found")
}
version, err := s.getVersion(*raw)
if err != nil {
return nil, err
}
var farm Farm
switch version {
case 4:
fallthrough
case 3:
fallthrough
case 2:
fallthrough
case 1:
if err := Decode(*raw, &farm); err != nil {
return nil, errors.Wrap(err, "failed to load object")
}
default:
return nil, ErrUnknownVersion
}
return &farm, nil
}
// GetFarm gets a farm with ID
func (s *Substrate) GetFarmByName(name string) (uint32, error) {
cl, meta, err := s.GetClient()
if err != nil {
return 0, err
}
bytes, err := Encode(name)
if err != nil {
return 0, errors.Wrap(err, "substrate: encoding error building query arguments")
}
key, err := types.CreateStorageKey(meta, "TfgridModule", "FarmIdByName", bytes, nil)
if err != nil {
return 0, errors.Wrap(err, "failed to create substrate query key")
}
raw, err := cl.RPC.State.GetStorageRawLatest(key)
if err != nil {
return 0, errors.Wrap(err, "failed to lookup entity")
}
if len(*raw) == 0 {
return 0, errors.Wrap(ErrNotFound, "farm not found")
}
var id uint32
if err := Decode(*raw, &id); err != nil {
return 0, errors.Wrap(err, "failed to decode farm id")
}
return id, nil
}
// CreateFarm creates a farm
// takes in a name and public ip list
func (s *Substrate) CreateFarm(identity Identity, name string, publicIps []PublicIPInput) error {
cl, meta, err := s.GetClient()
if err != nil {
return err
}
if name == "" {
return fmt.Errorf("name cannot be empty")
}
c, err := types.NewCall(meta, "TfgridModule.create_farm",
name, publicIps,
)
if err != nil {
return errors.Wrap(err, "failed to create call")
}
if _, err := s.Call(cl, meta, identity, c); err != nil {
return errors.Wrap(err, "failed to create farm")
}
return nil
}