-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathfarm.rs
432 lines (358 loc) · 14.1 KB
/
farm.rs
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
use crate::*;
use frame_support::{
dispatch::{DispatchErrorWithPostInfo, DispatchResultWithPostInfo},
ensure,
sp_runtime::SaturatedConversion,
traits::Get,
BoundedVec, RuntimeDebug,
};
use frame_system::pallet_prelude::BlockNumberFor;
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
use sp_std::{marker::PhantomData, vec, vec::Vec};
use tfchain_support::{
traits::ChangeNode,
types::{Farm, FarmCertification, FarmingPolicyLimit, PublicIP, IP4},
};
impl<T: Config> Pallet<T> {
pub fn _create_farm(
account_id: T::AccountId,
name: FarmNameInput<T>,
public_ips: PublicIpListInput<T>,
) -> DispatchResultWithPostInfo {
let twin_id = TwinIdByAccountID::<T>::get(&account_id).ok_or(Error::<T>::TwinNotExists)?;
let twin = Twins::<T>::get(twin_id).ok_or(Error::<T>::TwinNotExists)?;
ensure!(
twin.account_id == account_id,
Error::<T>::CannotCreateFarmWrongTwin
);
ensure!(
!FarmIdByName::<T>::contains_key(name.clone()),
Error::<T>::FarmExists
);
let mut id = FarmID::<T>::get();
id = id + 1;
let farm_name = Self::get_farm_name(name.clone())?;
let public_ips = Self::get_public_ips(public_ips)?;
let new_farm = Farm {
version: TFGRID_FARM_VERSION,
id,
twin_id,
name: farm_name,
pricing_policy_id: 1,
certification: FarmCertification::NotCertified,
public_ips,
dedicated_farm: false,
farming_policy_limits: None,
};
Farms::<T>::insert(id, &new_farm);
FarmIdByName::<T>::insert(name.to_vec(), id);
FarmID::<T>::put(id);
Self::deposit_event(Event::FarmStored(new_farm));
Ok(().into())
}
pub fn _update_farm(
account_id: T::AccountId,
farm_id: u32,
name: FarmNameInput<T>,
) -> DispatchResultWithPostInfo {
let twin_id = TwinIdByAccountID::<T>::get(&account_id).ok_or(Error::<T>::TwinNotExists)?;
let mut farm = Farms::<T>::get(farm_id).ok_or(Error::<T>::FarmNotExists)?;
ensure!(
farm.twin_id == twin_id,
Error::<T>::CannotUpdateFarmWrongTwin
);
let new_name = Self::get_farm_name(name.clone())?;
match FarmIdByName::<T>::get(name.clone()) {
// Continue updating
id if id == 0 => (),
// Leave function since new name and old name are same
id if id == farm_id => return Ok(().into()),
// Return error since farm name is already used by other farm
_ => return Err(Error::<T>::InvalidFarmName.into()),
}
// Remove old entry from "id by name" storage
let old_name: Vec<u8> = farm.name.into();
FarmIdByName::<T>::remove(old_name);
// Insert new entry into "id by name" storage
FarmIdByName::<T>::insert(name, farm_id);
// Update farm object in storage
farm.name = new_name;
Farms::<T>::insert(farm_id, &farm);
Self::deposit_event(Event::FarmUpdated(farm));
Ok(().into())
}
pub fn _add_stellar_payout_v2address(
account_id: T::AccountId,
farm_id: u32,
stellar_address: Vec<u8>,
) -> DispatchResultWithPostInfo {
let twin_id = TwinIdByAccountID::<T>::get(&account_id).ok_or(Error::<T>::TwinNotExists)?;
let farm = Farms::<T>::get(farm_id).ok_or(Error::<T>::FarmNotExists)?;
ensure!(
farm.twin_id == twin_id,
Error::<T>::CannotUpdateFarmWrongTwin
);
FarmPayoutV2AddressByFarmID::<T>::insert(&farm_id, &stellar_address);
Self::deposit_event(Event::FarmPayoutV2AddressRegistered(
farm_id,
stellar_address,
));
Ok(().into())
}
pub fn _set_farm_certification(
farm_id: u32,
certification: FarmCertification,
) -> DispatchResultWithPostInfo {
let mut farm = Farms::<T>::get(farm_id).ok_or(Error::<T>::FarmNotExists)?;
farm.certification = certification;
Farms::<T>::insert(farm_id, &farm);
Self::deposit_event(Event::FarmCertificationSet(farm_id, certification));
Ok(().into())
}
pub fn _add_farm_ip(
account_id: T::AccountId,
farm_id: u32,
ip: Ip4Input,
gw: Gw4Input,
) -> DispatchResultWithPostInfo {
let mut farm = Farms::<T>::get(farm_id).ok_or(Error::<T>::FarmNotExists)?;
let twin = Twins::<T>::get(farm.twin_id).ok_or(Error::<T>::TwinNotExists)?;
ensure!(
twin.account_id == account_id,
Error::<T>::CannotUpdateFarmWrongTwin
);
// Check if it's a valid IP4
let ip4 = IP4 { ip, gw };
ip4.is_valid().map_err(|_| Error::<T>::InvalidPublicIP)?;
let new_ip = PublicIP {
ip: ip4.ip,
gateway: ip4.gw,
contract_id: 0,
};
match farm
.public_ips
.iter()
.position(|public_ip| public_ip.ip == new_ip.ip)
{
Some(_) => return Err(Error::<T>::IpExists.into()),
None => {
farm.public_ips
.try_push(new_ip)
.map_err(|_| Error::<T>::InvalidPublicIP)?;
Farms::<T>::insert(farm.id, &farm);
Self::deposit_event(Event::FarmUpdated(farm));
return Ok(().into());
}
};
}
pub fn _remove_farm_ip(
account_id: T::AccountId,
id: u32,
ip: Ip4Input,
) -> DispatchResultWithPostInfo {
let mut farm = Farms::<T>::get(id).ok_or(Error::<T>::FarmNotExists)?;
let twin = Twins::<T>::get(farm.twin_id).ok_or(Error::<T>::TwinNotExists)?;
ensure!(
twin.account_id == account_id,
Error::<T>::CannotUpdateFarmWrongTwin
);
match farm
.public_ips
.iter()
.position(|pubip| pubip.ip == ip && pubip.contract_id == 0)
{
Some(index) => {
farm.public_ips.remove(index);
Farms::<T>::insert(farm.id, &farm);
Self::deposit_event(Event::FarmUpdated(farm));
Ok(().into())
}
None => Err(Error::<T>::IpNotExists.into()),
}
}
pub fn _delete_node_farm(account_id: T::AccountId, node_id: u32) -> DispatchResultWithPostInfo {
let twin_id = TwinIdByAccountID::<T>::get(&account_id).ok_or(Error::<T>::TwinNotExists)?;
let node = Nodes::<T>::get(&node_id).ok_or(Error::<T>::NodeNotExists)?;
let farm = Farms::<T>::get(node.farm_id).ok_or(Error::<T>::FarmNotExists)?;
// Make sure the caller is the farmer
ensure!(twin_id == farm.twin_id, Error::<T>::FarmerNotAuthorized);
// Remove node id from "nodes in farm" list
let mut nodes_by_farm = NodesByFarmID::<T>::get(node.farm_id);
if let Ok(position) = nodes_by_farm.binary_search(&node_id) {
nodes_by_farm.remove(position);
NodesByFarmID::<T>::insert(node.farm_id, nodes_by_farm);
}
// Call node deleted
T::NodeChanged::node_deleted(&node);
Nodes::<T>::remove(node_id);
NodeIdByTwinID::<T>::remove(node.twin_id);
Self::deposit_event(Event::NodeDeleted(node_id));
Ok(().into())
}
pub fn _set_farm_dedicated(farm_id: u32, dedicated: bool) -> DispatchResultWithPostInfo {
let mut farm = Farms::<T>::get(farm_id).ok_or(Error::<T>::FarmNotExists)?;
farm.dedicated_farm = dedicated;
Farms::<T>::insert(farm_id, &farm);
Self::deposit_event(Event::FarmUpdated(farm));
Ok(().into())
}
pub fn _force_reset_farm_ip(farm_id: u32, ip: Ip4Input) -> DispatchResultWithPostInfo {
let mut farm = Farms::<T>::get(farm_id).ok_or(Error::<T>::FarmNotExists)?;
match farm.public_ips.iter_mut().find(|pubip| pubip.ip == ip) {
Some(ip) => {
ip.contract_id = 0;
}
None => return Err(Error::<T>::IpNotExists.into()),
};
Farms::<T>::insert(farm.id, &farm);
Self::deposit_event(Event::FarmUpdated(farm));
Ok(().into())
}
pub fn _attach_policy_to_farm(
farm_id: u32,
limits: Option<FarmingPolicyLimit>,
) -> DispatchResultWithPostInfo {
if let Some(policy_limits) = limits {
let farming_policy = FarmingPoliciesMap::<T>::get(policy_limits.farming_policy_id);
let now = frame_system::Pallet::<T>::block_number();
// Policy end is expressed in number of blocks
if farming_policy.policy_end != BlockNumberFor::<T>::from(0 as u32)
&& now >= farming_policy.policy_created + farming_policy.policy_end
{
return Err(DispatchErrorWithPostInfo::from(
Error::<T>::FarmingPolicyExpired,
));
}
let mut farm = Farms::<T>::get(farm_id).ok_or(Error::<T>::FarmNotExists)?;
// Save the policy limits and farm certification on the Farm object
farm.farming_policy_limits = Some(policy_limits.clone());
farm.certification = farming_policy.farm_certification;
Farms::<T>::insert(farm_id, &farm);
Self::deposit_event(Event::FarmUpdated(farm));
// Give all the nodes in this farm the policy that is attached
for node_id in NodesByFarmID::<T>::get(farm_id) {
match Nodes::<T>::get(node_id) {
Some(mut node) => {
let current_node_policy =
FarmingPoliciesMap::<T>::get(node.farming_policy_id);
// If the current policy attached to the node is default one, assign it the newly created policy
// because we wouldn't wanna override any existing non-default policies
if current_node_policy.default {
let policy = Self::get_farming_policy(&node)?;
// Save the new policy ID and certification on the Node object
node.farming_policy_id = policy.id;
node.certification = policy.node_certification;
Nodes::<T>::insert(node_id, &node);
Self::deposit_event(Event::NodeUpdated(node))
}
}
None => continue,
}
}
Self::deposit_event(Event::FarmingPolicySet(farm_id, Some(policy_limits)));
}
Ok(().into())
}
fn get_farm_name(name: FarmNameInput<T>) -> Result<FarmNameOf<T>, DispatchErrorWithPostInfo> {
let name_parsed = <T as Config>::FarmName::try_from(name)?;
Ok(name_parsed)
}
fn get_public_ips(
public_ips: PublicIpListInput<T>,
) -> Result<PublicIpListOf, DispatchErrorWithPostInfo> {
let mut public_ips_list: PublicIpListOf =
vec![].try_into().map_err(|_| Error::<T>::InvalidPublicIP)?;
for ip4 in public_ips {
// Check if it's a valid IP4
ip4.is_valid().map_err(|_| Error::<T>::InvalidPublicIP)?;
let pub_ip = PublicIP {
ip: ip4.ip,
gateway: ip4.gw,
contract_id: 0,
};
if public_ips_list.contains(&pub_ip) {
return Err(DispatchErrorWithPostInfo::from(Error::<T>::IpExists));
}
public_ips_list
.try_push(pub_ip)
.map_err(|_| Error::<T>::InvalidPublicIP)?;
}
Ok(public_ips_list)
}
}
impl<T: Config> tfchain_support::traits::Tfgrid<T::AccountId, T::FarmName> for Pallet<T> {
fn is_farm_owner(farm_id: u32, who: T::AccountId) -> bool {
let farm = Farms::<T>::get(farm_id);
if let Some(f) = farm {
match Twins::<T>::get(f.twin_id) {
Some(twin) => twin.account_id == who,
None => false,
}
} else {
false
}
}
}
/// A Farm name (ASCI Characters).
///
/// It is bounded in size (inclusive range [MinLength, MaxLength]) and must be a valid ipv6
#[derive(Encode, Decode, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
#[scale_info(skip_type_params(T))]
#[codec(mel_bound())]
pub struct FarmName<T: Config>(
pub(crate) BoundedVec<u8, T::MaxFarmNameLength>,
PhantomData<(T, T::MaxFarmNameLength)>,
);
pub const MIN_FARM_NAME_LENGTH: u32 = 3;
impl<T: Config> TryFrom<FarmNameInput<T>> for FarmName<T> {
type Error = Error<T>;
/// Fallible initialization from a provided byte vector if it is below the
/// minimum or exceeds the maximum allowed length or contains invalid ASCII
/// characters.
fn try_from(value: FarmNameInput<T>) -> Result<Self, Self::Error> {
ensure!(
value.len() >= MIN_FARM_NAME_LENGTH.saturated_into(),
Self::Error::FarmNameTooShort
);
ensure!(
value.len() <= T::MaxFarmNameLength::get() as usize,
Self::Error::FarmNameTooLong
);
ensure!(validate_farm_name(&value), Self::Error::InvalidFarmName);
Ok(Self(value, PhantomData))
}
}
impl<T: Config> From<FarmName<T>> for Vec<u8> {
fn from(value: FarmName<T>) -> Self {
value.0.to_vec()
}
}
// FIXME: did not find a way to automatically implement this.
impl<T: Config> PartialEq for FarmName<T> {
fn eq(&self, other: &Self) -> bool {
self.0 == other.0
}
}
// FIXME: did not find a way to automatically implement this.
impl<T: Config> Clone for FarmName<T> {
fn clone(&self) -> Self {
Self(self.0.clone(), self.1)
}
}
pub fn replace_farm_name_invalid_characters(input: &[u8]) -> Vec<u8> {
input
.iter()
.map(|c| match c {
b' ' => b'_',
b'\'' => b'-',
b';' => b'_',
_ => *c,
})
.collect()
}
fn validate_farm_name(input: &[u8]) -> bool {
input
.iter()
.all(|c| matches!(c, b'a'..=b'z' | b'A'..=b'Z' | b'0'..=b'9' | b'-' | b'_'))
}