Skip to content

Commit

Permalink
fixed merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonpille committed Nov 14, 2022
1 parent f84a7e0 commit 8297940
Show file tree
Hide file tree
Showing 11 changed files with 231 additions and 87 deletions.
6 changes: 3 additions & 3 deletions substrate-node/pallets/pallet-dao/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,10 +527,10 @@ impl<T: Config>
for Pallet<T>
{
fn node_changed(old_node: Option<&TfgridNode<T>>, new_node: &TfgridNode<T>) {
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::<T>::get(node.farm_id);
Expand Down Expand Up @@ -558,7 +558,7 @@ impl<T: Config>
}

fn node_deleted(node: &TfgridNode<T>) {
let node_weight = node.resources.get_node_weight();
let node_weight = node.resources.total_resources.get_node_weight();
let mut farm_weight = FarmWeight::<T>::get(node.farm_id);
farm_weight = farm_weight.checked_sub(node_weight).unwrap_or(0);
FarmWeight::<T>::insert(node.farm_id, farm_weight);
Expand Down
4 changes: 2 additions & 2 deletions substrate-node/pallets/pallet-smart-contract/src/cost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl<T: Config> Contract<T> {
.ok_or(Error::<T>::NodeNotExists)?;

let contract_cost = calculate_resources_cost::<T>(
capacity_reservation_contract.resources.total_resources,
&capacity_reservation_contract.resources.total_resources,
capacity_reservation_contract.public_ips,
seconds_elapsed,
&pricing_policy,
Expand Down Expand Up @@ -94,7 +94,7 @@ impl<T: Config> Contract<T> {

// Calculates the total cost of a node contract.
pub fn calculate_resources_cost<T: Config>(
resources: Resources,
resources: &Resources,
ipu: u32,
seconds_elapsed: u64,
pricing_policy: &pallet_tfgrid_types::PricingPolicy<T::AccountId>,
Expand Down
6 changes: 4 additions & 2 deletions substrate-node/pallets/pallet-smart-contract/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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");
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -140,7 +140,7 @@ impl<T: Config> OnRuntimeUpgrade for ContractMigrationV6<T> {
#[cfg(feature = "try-runtime")]
fn post_upgrade() -> Result<(), &'static str> {
info!("current pallet version: {:?}", PalletVersion::<T>::get());

info!(
"👥 Smart Contract pallet to {:?} passes POST migrate checks ✅",
PalletVersion::<T>::get()
Expand Down
2 changes: 1 addition & 1 deletion substrate-node/pallets/pallet-smart-contract/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
30 changes: 19 additions & 11 deletions substrate-node/pallets/pallet-tfgrid/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
Expand All @@ -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,
},
};

Expand Down Expand Up @@ -660,7 +664,7 @@ pub mod pallet {
DocumentHashInputTooShort,
DocumentHashInputTooLong,
InvalidDocumentHashInput,

UnauthorizedToChangePowerState,
UnauthorizedToChangePowerTarget,
}
Expand Down Expand Up @@ -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,
Expand Down Expand 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;
Expand Down Expand Up @@ -2170,7 +2178,7 @@ impl<T: Config> Pallet<T> {
}

fn _change_power_target_on_node(
node: &mut Node<pallet::PubConfigOf<T>, pallet::InterfaceOf<T>>,
node: &mut TfgridNode<T>,
power_target: PowerTarget,
) {
Self::deposit_event(Event::PowerTargetChanged {
Expand Down Expand Up @@ -2261,7 +2269,7 @@ impl<T: Config> Pallet<T> {

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();
}
Expand All @@ -2272,7 +2280,7 @@ impl<T: Config> Pallet<T> {

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();
}
Expand Down
1 change: 1 addition & 0 deletions substrate-node/pallets/pallet-tfgrid/src/migrations/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod v10;
pub mod v11;
pub mod v12;
pub mod v13;
11 changes: 10 additions & 1 deletion substrate-node/pallets/pallet-tfgrid/src/migrations/v12.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -181,7 +182,15 @@ fn migrate_nodes<T: Config>() -> 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,
Expand Down
131 changes: 131 additions & 0 deletions substrate-node/pallets/pallet-tfgrid/src/migrations/v13.rs
Original file line number Diff line number Diff line change
@@ -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<Location, PubConfig, If, SerialNumber> {
pub version: u32,
pub id: u32,
pub farm_id: u32,
pub twin_id: u32,
pub resources: Resources,
pub location: Location,
pub country: Vec<u8>,
pub city: Vec<u8>,
// optional public config
pub public_config: Option<PubConfig>,
pub created: u64,
pub farming_policy_id: u32,
pub interfaces: Vec<If>,
pub certification: NodeCertification,
pub secure_boot: bool,
pub virtualized: bool,
pub serial_number: SerialNumber,
pub connection_price: u32,
}

decl_module! {
pub struct Module<T: Config> for enum Call where origin: T::Origin { }
}
}
pub struct NodeMigrationV11<T: Config>(PhantomData<T>);

impl<T: Config> OnRuntimeUpgrade for NodeMigrationV11<T> {
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<(), &'static str> {
info!("current pallet version: {:?}", PalletVersion::<T>::get());
info!("👥 Tfgrid pallet to V11 passes PRE migrate checks ✅");
Ok(())
}

fn on_runtime_upgrade() -> Weight {
if PalletVersion::<T>::get() == StorageVersion::V11Struct {
migrate_to_version_12::<T>()
} else {
info!(" >>> Unused migration");
0
}
}

#[cfg(feature = "try-runtime")]
fn post_upgrade() -> Result<(), &'static str> {
info!("current pallet version: {:?}", PalletVersion::<T>::get());
info!(
"👥 Tfgrid pallet to {:?} passes POST migrate checks ✅",
PalletVersion::<T>::get()
);

Ok(())
}
}

pub fn migrate_to_version_12<T: Config>() -> frame_support::weights::Weight {
info!(
" >>> Starting tfgrid pallet migration, pallet version: {:?}",
PalletVersion::<T>::get()
);

let mut migrated_count = 0;

Nodes::<T>::translate::<deprecated::NodeV11Struct<LocationOf<T>, PubConfigOf<T>, InterfaceOf<T>, SerialNumberOf<T> >, _>(
|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::<T>::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)
}
Loading

0 comments on commit 8297940

Please sign in to comment.