From 8297940090b3ab5544bffd25ef8415b59638c460 Mon Sep 17 00:00:00 2001 From: brandonpille Date: Mon, 14 Nov 2022 17:59:52 +0100 Subject: [PATCH] fixed merge conflicts --- substrate-node/pallets/pallet-dao/src/lib.rs | 6 +- .../pallets/pallet-smart-contract/src/cost.rs | 4 +- .../pallets/pallet-smart-contract/src/lib.rs | 6 +- .../src/migrations/v7.rs | 4 +- .../pallet-smart-contract/src/types.rs | 2 +- .../pallets/pallet-tfgrid/src/lib.rs | 30 ++-- .../pallet-tfgrid/src/migrations/mod.rs | 1 + .../pallet-tfgrid/src/migrations/v12.rs | 11 +- .../pallet-tfgrid/src/migrations/v13.rs | 131 ++++++++++++++++++ substrate-node/support/src/resources.rs | 59 +++++++- substrate-node/support/src/types.rs | 64 +-------- 11 files changed, 231 insertions(+), 87 deletions(-) create mode 100644 substrate-node/pallets/pallet-tfgrid/src/migrations/v13.rs diff --git a/substrate-node/pallets/pallet-dao/src/lib.rs b/substrate-node/pallets/pallet-dao/src/lib.rs index c57a5e3af..82e9ebef5 100644 --- a/substrate-node/pallets/pallet-dao/src/lib.rs +++ b/substrate-node/pallets/pallet-dao/src/lib.rs @@ -527,10 +527,10 @@ impl for Pallet { fn node_changed(old_node: Option<&TfgridNode>, new_node: &TfgridNode) { - let new_node_weight = new_node.resources.get_node_weight(); + let new_node_weight = new_node.resources.total_resources.get_node_weight(); match old_node { Some(node) => { - let old_node_weight = node.resources.get_node_weight(); + let old_node_weight = node.resources.total_resources.get_node_weight(); if node.farm_id != new_node.farm_id { let mut old_farm_weight = FarmWeight::::get(node.farm_id); @@ -558,7 +558,7 @@ impl } fn node_deleted(node: &TfgridNode) { - let node_weight = node.resources.get_node_weight(); + let node_weight = node.resources.total_resources.get_node_weight(); let mut farm_weight = FarmWeight::::get(node.farm_id); farm_weight = farm_weight.checked_sub(node_weight).unwrap_or(0); FarmWeight::::insert(node.farm_id, farm_weight); diff --git a/substrate-node/pallets/pallet-smart-contract/src/cost.rs b/substrate-node/pallets/pallet-smart-contract/src/cost.rs index b7dd4d7b5..8872ccb46 100644 --- a/substrate-node/pallets/pallet-smart-contract/src/cost.rs +++ b/substrate-node/pallets/pallet-smart-contract/src/cost.rs @@ -65,7 +65,7 @@ impl Contract { .ok_or(Error::::NodeNotExists)?; let contract_cost = calculate_resources_cost::( - capacity_reservation_contract.resources.total_resources, + &capacity_reservation_contract.resources.total_resources, capacity_reservation_contract.public_ips, seconds_elapsed, &pricing_policy, @@ -94,7 +94,7 @@ impl Contract { // Calculates the total cost of a node contract. pub fn calculate_resources_cost( - resources: Resources, + resources: &Resources, ipu: u32, seconds_elapsed: u64, pricing_policy: &pallet_tfgrid_types::PricingPolicy, diff --git a/substrate-node/pallets/pallet-smart-contract/src/lib.rs b/substrate-node/pallets/pallet-smart-contract/src/lib.rs index 6c3ff99f5..29237fa2b 100644 --- a/substrate-node/pallets/pallet-smart-contract/src/lib.rs +++ b/substrate-node/pallets/pallet-smart-contract/src/lib.rs @@ -21,6 +21,7 @@ use frame_system::{ }; pub use pallet::*; use pallet_tfgrid; +use pallet_tfgrid::farm::FarmName; use pallet_tfgrid::pallet::{InterfaceOf, LocationOf, PubConfigOf, SerialNumberOf, TfgridNode}; use pallet_tfgrid::types as pallet_tfgrid_types; use pallet_timestamp as timestamp; @@ -32,8 +33,9 @@ use sp_runtime::{ use substrate_fixed::types::U64F64; use tfchain_support::{ + resources::Resources, traits::{ChangeNode, Tfgrid}, - types::{CapacityReservationPolicy, ConsumableResources, Node, NodeFeatures, Resources}, + types::{CapacityReservationPolicy, ConsumableResources, NodeFeatures}, }; pub const KEY_TYPE: KeyTypeId = KeyTypeId(*b"smct"); @@ -79,10 +81,10 @@ pub mod crypto { } pub mod cost; +pub mod migrations; pub mod name_contract; pub mod types; pub mod weights; -pub mod migrations; #[frame_support::pallet] pub mod pallet { diff --git a/substrate-node/pallets/pallet-smart-contract/src/migrations/v7.rs b/substrate-node/pallets/pallet-smart-contract/src/migrations/v7.rs index 654c09c53..925afbdc9 100644 --- a/substrate-node/pallets/pallet-smart-contract/src/migrations/v7.rs +++ b/substrate-node/pallets/pallet-smart-contract/src/migrations/v7.rs @@ -14,7 +14,7 @@ use frame_support::{ use frame_system::Pallet; use log::info; use sp_std::{cmp::max, collections::btree_map::BTreeMap, marker::PhantomData, vec, vec::Vec}; -use tfchain_support::types::{ConsumableResources, Resources}; +use tfchain_support::{resources::Resources, types::ConsumableResources}; pub mod deprecated { use crate::pallet::{ @@ -140,7 +140,7 @@ impl OnRuntimeUpgrade for ContractMigrationV6 { #[cfg(feature = "try-runtime")] fn post_upgrade() -> Result<(), &'static str> { info!("current pallet version: {:?}", PalletVersion::::get()); - + info!( "👥 Smart Contract pallet to {:?} passes POST migrate checks ✅", PalletVersion::::get() diff --git a/substrate-node/pallets/pallet-smart-contract/src/types.rs b/substrate-node/pallets/pallet-smart-contract/src/types.rs index f57509818..7914a1b68 100644 --- a/substrate-node/pallets/pallet-smart-contract/src/types.rs +++ b/substrate-node/pallets/pallet-smart-contract/src/types.rs @@ -7,7 +7,7 @@ use frame_support::{BoundedVec, RuntimeDebugNoBound}; use scale_info::TypeInfo; use sp_std::prelude::*; use substrate_fixed::types::U64F64; -use tfchain_support::types::{ConsumableResources, Resources}; +use tfchain_support::{resources::Resources, types::ConsumableResources}; pub type BlockNumber = u64; diff --git a/substrate-node/pallets/pallet-tfgrid/src/lib.rs b/substrate-node/pallets/pallet-tfgrid/src/lib.rs index 2ca851035..15b40d251 100644 --- a/substrate-node/pallets/pallet-tfgrid/src/lib.rs +++ b/substrate-node/pallets/pallet-tfgrid/src/lib.rs @@ -8,14 +8,17 @@ use sp_std::prelude::*; use codec::Encode; use frame_support::dispatch::DispatchErrorWithPostInfo; -use frame_support::{ensure, traits::ConstU32, traits::EnsureOrigin, weights::Pays, BoundedVec}; +use frame_support::{ + ensure, pallet_prelude::DispatchResultWithPostInfo, traits::EnsureOrigin, + weights::Pays, BoundedVec, +}; use frame_system::{self as system, ensure_signed}; use hex::FromHex; use pallet_timestamp as timestamp; use sp_runtime::SaturatedConversion; use tfchain_support::{ resources::Resources, - types::{Interface, PublicConfig, PublicIP, IP}, + types::{Interface, Power, PowerState, PowerTarget, PublicConfig, PublicIP, IP}, }; // Re-export pallet items so that they can be accessed from the crate namespace. @@ -27,8 +30,8 @@ mod mock; #[cfg(test)] mod tests; -pub mod weights; pub mod types; +pub mod weights; pub mod farm; pub mod interface; @@ -52,10 +55,11 @@ pub mod pallet { use pallet_timestamp as timestamp; use sp_std::{convert::TryInto, fmt::Debug, vec::Vec}; use tfchain_support::{ + resources::Resources, traits::ChangeNode, types::{ - ConsumableResources, Farm, FarmCertification, FarmingPolicyLimit, Interface, Location, - Node, NodeCertification, PublicConfig, PublicIP, Resources, IP, + ConsumableResources, Farm, FarmCertification, FarmingPolicyLimit, Interface, Node, + NodeCertification, PublicConfig, PublicIP, IP, }, }; @@ -660,7 +664,7 @@ pub mod pallet { DocumentHashInputTooShort, DocumentHashInputTooLong, InvalidDocumentHashInput, - + UnauthorizedToChangePowerState, UnauthorizedToChangePowerTarget, } @@ -1101,7 +1105,10 @@ pub mod pallet { id, farm_id, twin_id, - resources: node_resources, + resources: ConsumableResources { + total_resources: node_resources, + used_resources: Resources::empty(), + }, location: node_location, power: Power { state: PowerState::Up, @@ -1206,7 +1213,8 @@ pub mod pallet { stored_node.farm_id = farm_id; //TODO check that updating resources is possible! - stored_node.resources.total_resources = resources; + stored_node.resources.total_resources = node_resources; + stored_node.location = node_location; stored_node.interfaces = node_interfaces; stored_node.secure_boot = secure_boot; stored_node.virtualized = virtualized; @@ -2170,7 +2178,7 @@ impl Pallet { } fn _change_power_target_on_node( - node: &mut Node, pallet::InterfaceOf>, + node: &mut TfgridNode, power_target: PowerTarget, ) { Self::deposit_event(Event::PowerTargetChanged { @@ -2261,7 +2269,7 @@ impl Pallet { match limits.cu { Some(cu_limit) => { - let cu = node.resources.get_cu(); + let cu = node.resources.total_resources.get_cu(); if cu > cu_limit { return Self::get_default_farming_policy(); } @@ -2272,7 +2280,7 @@ impl Pallet { match limits.su { Some(su_limit) => { - let su = node.resources.get_su(); + let su = node.resources.total_resources.get_su(); if su > su_limit { return Self::get_default_farming_policy(); } diff --git a/substrate-node/pallets/pallet-tfgrid/src/migrations/mod.rs b/substrate-node/pallets/pallet-tfgrid/src/migrations/mod.rs index e1129c3cd..b6f7795eb 100644 --- a/substrate-node/pallets/pallet-tfgrid/src/migrations/mod.rs +++ b/substrate-node/pallets/pallet-tfgrid/src/migrations/mod.rs @@ -1,3 +1,4 @@ pub mod v10; pub mod v11; pub mod v12; +pub mod v13; diff --git a/substrate-node/pallets/pallet-tfgrid/src/migrations/v12.rs b/substrate-node/pallets/pallet-tfgrid/src/migrations/v12.rs index 9a6e8ec93..a733eeb43 100644 --- a/substrate-node/pallets/pallet-tfgrid/src/migrations/v12.rs +++ b/substrate-node/pallets/pallet-tfgrid/src/migrations/v12.rs @@ -5,6 +5,7 @@ use crate::*; use frame_support::{traits::Get, traits::OnRuntimeUpgrade, weights::Weight, BoundedVec}; use log::info; use sp_std::marker::PhantomData; +use tfchain_support::types::ConsumableResources; pub mod deprecated { use codec::{Decode, Encode}; @@ -181,7 +182,15 @@ fn migrate_nodes() -> frame_support::weights::Weight { id: node.id, farm_id: node.farm_id, twin_id: node.twin_id, - resources: node.resources, + resources: ConsumableResources { + total_resources: node.resources, + used_resources: Resources::empty(), + }, + power: Power { + target: PowerTarget::Up, + state: PowerState::Up, + last_uptime: 0, + }, location, public_config: node.public_config, created: node.created, diff --git a/substrate-node/pallets/pallet-tfgrid/src/migrations/v13.rs b/substrate-node/pallets/pallet-tfgrid/src/migrations/v13.rs new file mode 100644 index 000000000..9c85fa74f --- /dev/null +++ b/substrate-node/pallets/pallet-tfgrid/src/migrations/v13.rs @@ -0,0 +1,131 @@ +use crate::{ + types::StorageVersion, Config, InterfaceOf, Nodes, PalletVersion, PubConfigOf, LocationOf, SerialNumberOf, + TFGRID_NODE_VERSION, +}; +use frame_support::{pallet_prelude::Weight, traits::Get, traits::OnRuntimeUpgrade}; +use log::info; +use sp_std::marker::PhantomData; +use tfchain_support::resources::Resources; +use tfchain_support::types::{ConsumableResources, Node, Power, PowerState, PowerTarget}; + +pub mod deprecated { + use crate::Config; + use codec::{Decode, Encode}; + use frame_support::decl_module; + + use scale_info::TypeInfo; + use sp_std::prelude::*; + + use tfchain_support::{resources::Resources, types::NodeCertification}; + + #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Encode, Decode, Default, Debug, TypeInfo)] + pub struct NodeV11Struct { + pub version: u32, + pub id: u32, + pub farm_id: u32, + pub twin_id: u32, + pub resources: Resources, + pub location: Location, + pub country: Vec, + pub city: Vec, + // optional public config + pub public_config: Option, + pub created: u64, + pub farming_policy_id: u32, + pub interfaces: Vec, + pub certification: NodeCertification, + pub secure_boot: bool, + pub virtualized: bool, + pub serial_number: SerialNumber, + pub connection_price: u32, + } + + decl_module! { + pub struct Module for enum Call where origin: T::Origin { } + } +} +pub struct NodeMigrationV11(PhantomData); + +impl OnRuntimeUpgrade for NodeMigrationV11 { + #[cfg(feature = "try-runtime")] + fn pre_upgrade() -> Result<(), &'static str> { + info!("current pallet version: {:?}", PalletVersion::::get()); + info!("👥 Tfgrid pallet to V11 passes PRE migrate checks ✅"); + Ok(()) + } + + fn on_runtime_upgrade() -> Weight { + if PalletVersion::::get() == StorageVersion::V11Struct { + migrate_to_version_12::() + } else { + info!(" >>> Unused migration"); + 0 + } + } + + #[cfg(feature = "try-runtime")] + fn post_upgrade() -> Result<(), &'static str> { + info!("current pallet version: {:?}", PalletVersion::::get()); + info!( + "👥 Tfgrid pallet to {:?} passes POST migrate checks ✅", + PalletVersion::::get() + ); + + Ok(()) + } +} + +pub fn migrate_to_version_12() -> frame_support::weights::Weight { + info!( + " >>> Starting tfgrid pallet migration, pallet version: {:?}", + PalletVersion::::get() + ); + + let mut migrated_count = 0; + + Nodes::::translate::, PubConfigOf, InterfaceOf, SerialNumberOf >, _>( + |k, n| { + migrated_count += 1; + let migrated_contract = Node { + version: TFGRID_NODE_VERSION, + id: n.id, + farm_id: n.farm_id, + twin_id: n.twin_id, + resources: ConsumableResources { + total_resources: n.resources, + used_resources: Resources::empty(), + }, + location: n.location, + power: Power { + target: PowerTarget::Up, + state: PowerState::Up, + last_uptime: 0, + }, + // optional public config + public_config: n.public_config, + created: n.created, + farming_policy_id: n.farming_policy_id, + interfaces: n.interfaces, + certification: n.certification, + secure_boot: n.secure_boot, + virtualized: n.virtualized, + serial_number: n.serial_number, + connection_price: n.connection_price, + }; + info!("Node: {:?} succesfully migrated", k); + Some(migrated_contract) + }, + ); + + info!( + " <<< Node storage updated! Migrated {} Nodes ✅", + migrated_count + ); + + // Update pallet storage version + PalletVersion::::set(StorageVersion::V12Struct); + info!(" <<< Storage version upgraded"); + + // Return the weight consumed by the migration. + T::DbWeight::get().reads_writes(migrated_count as Weight + 1, migrated_count as Weight + 1) +} diff --git a/substrate-node/support/src/resources.rs b/substrate-node/support/src/resources.rs index e8f023e08..99f1187ce 100644 --- a/substrate-node/support/src/resources.rs +++ b/substrate-node/support/src/resources.rs @@ -3,7 +3,7 @@ use scale_info::TypeInfo; /// A resources capacity that countains HRU, SRU, CRU and MRU in integer values. #[derive( - PartialEq, Eq, PartialOrd, Ord, Clone, Encode, Decode, Default, Debug, TypeInfo, MaxEncodedLen, + PartialEq, Eq, PartialOrd, Ord, Clone, Encode, Decode, Default, Debug, TypeInfo, MaxEncodedLen, Copy )] pub struct Resources { pub hru: u64, @@ -16,12 +16,11 @@ pub const ONE_THOUSAND: u128 = 1_000; pub const GIGABYTE: u128 = 1024 * 1024 * 1024; impl Resources { - pub fn add(mut self, other: &Resources) -> Resources { + pub fn add(&mut self, other: &Resources) { self.cru += other.cru; self.sru += other.sru; self.hru += other.hru; self.mru += other.mru; - self } pub fn validate_hru(&self) -> bool { @@ -79,6 +78,60 @@ impl Resources { let su = self.get_su(); cu * 2 + su } + pub fn empty() -> Resources { + Resources { + hru: 0, + sru: 0, + cru: 0, + mru: 0, + } + } + + pub fn sum(a: &Resources, b: &Resources) -> Resources { + let mut sum = a.clone(); + sum.add(b); + sum + } + + pub fn subtraction(a: &Resources, b: &Resources) -> Resources { + let mut subtraction = a.clone(); + subtraction.substract(b); + subtraction + } + + pub fn is_empty(&self) -> bool { + self.cru == 0 && self.sru == 0 && self.hru == 0 && self.mru == 0 + } + + pub fn can_substract(self, other: &Resources) -> bool { + self.cru >= other.cru + && self.sru >= other.sru + && self.hru >= other.hru + && self.mru >= other.mru + } + + pub fn substract(&mut self, other: &Resources) { + self.cru = if self.cru < other.cru { + 0 + } else { + self.cru - other.cru + }; + self.sru = if self.sru < other.sru { + 0 + } else { + self.sru - other.sru + }; + self.hru = if self.hru < other.hru { + 0 + } else { + self.hru - other.hru + }; + self.mru = if self.mru < other.mru { + 0 + } else { + self.mru - other.mru + }; + } } #[cfg(test)] diff --git a/substrate-node/support/src/types.rs b/substrate-node/support/src/types.rs index 6fcee69b5..1c9654b89 100644 --- a/substrate-node/support/src/types.rs +++ b/substrate-node/support/src/types.rs @@ -162,7 +162,7 @@ impl ConsumableResources { pub fn calculate_reduction_in_resources(&self, resources: &Resources) -> Resources { let mut reduction = self.total_resources.clone(); reduction.substract(&resources); - reduction + reduction } } @@ -187,7 +187,7 @@ pub struct Node { pub connection_price: u32, } -impl Node { +impl Node { pub fn can_be_shutdown(&self) -> bool { self.resources.used_resources.is_empty() } @@ -215,66 +215,6 @@ pub struct IP { pub ip: IpAddr, pub gw: Gw, } - pub fn empty() -> Resources { - Resources { - hru: 0, - sru: 0, - cru: 0, - mru: 0, - } - } - - pub fn sum(a: &Resources, b: &Resources) -> Resources { - let mut sum = a.clone(); - sum.add(b); - sum - } - pub fn subtraction(a: &Resources, b: &Resources) -> Resources { - let mut subtraction = a.clone(); - subtraction.substract(b); - subtraction - } - - pub fn is_empty(self) -> bool { - self.cru == 0 && self.sru == 0 && self.hru == 0 && self.mru == 0 - } - - pub fn add(&mut self, other: &Resources) { - self.cru += other.cru; - self.sru += other.sru; - self.hru += other.hru; - self.mru += other.mru; - } - - pub fn can_substract(self, other: &Resources) -> bool { - self.cru >= other.cru - && self.sru >= other.sru - && self.hru >= other.hru - && self.mru >= other.mru - } - - pub fn substract(&mut self, other: &Resources) { - self.cru = if self.cru < other.cru { - 0 - } else { - self.cru - other.cru - }; - self.sru = if self.sru < other.sru { - 0 - } else { - self.sru - other.sru - }; - self.hru = if self.hru < other.hru { - 0 - } else { - self.hru - other.hru - }; - self.mru = if self.mru < other.mru { - 0 - } else { - self.mru - other.mru - }; - } #[derive(PartialEq, Eq, Clone, Encode, Decode, Debug, TypeInfo, Copy)] pub enum NodeCertification {