Skip to content

Commit

Permalink
feat: cleanup contracts if node is deleted (#308)
Browse files Browse the repository at this point in the history
also moves common types to the support package
  • Loading branch information
DylanVerstraete authored May 31, 2022
1 parent cc9c7e8 commit 072bdc8
Show file tree
Hide file tree
Showing 20 changed files with 523 additions and 116 deletions.
15 changes: 15 additions & 0 deletions substrate-node/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions substrate-node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ panic = 'unwind'
members = [
'node',
'runtime',
'pallets/*',
'support'
]
4 changes: 3 additions & 1 deletion substrate-node/pallets/pallet-smart-contract/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ sp-io = { version = '3.0.0', default-features = false }
hex = { version = "0.4", default-features = false }
lite-json = { version = "0.1.3", default-features = false }
serde = { version = '1.0.100', default-features = false, features = ['derive'] }
tfchain-support = { path = "../../support", default-features = false }

[dependencies.pallet-tft-price]
default-features = false
Expand All @@ -62,7 +63,8 @@ std = [
'sp-io/std',
'lite-json/std',
'pallet-tfgrid/std',
'pallet-tft-price/std'
'pallet-tft-price/std',
'tfchain-support/std'
]
runtime-benchmarks = [
"frame-benchmarking",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use frame_support::traits::{Box, Vec};
use frame_system::RawOrigin;
use sp_std::vec;

use tfchain_support::types::{Location, Resources, PublicIP};
use pallet_tfgrid;

benchmarks! {
Expand Down Expand Up @@ -113,12 +114,12 @@ pub fn prepare_farm_and_node<T: Config>(source: T::AccountId) {
prepare_farm::<T>(source.clone());

// random location
let location = pallet_tfgrid_types::Location {
let location = Location {
longitude: "12.233213231".as_bytes().to_vec(),
latitude: "32.323112123".as_bytes().to_vec(),
};

let resources = pallet_tfgrid_types::Resources {
let resources = Resources {
hru: 1,
sru: 1,
cru: 1,
Expand All @@ -145,7 +146,7 @@ pub fn prepare_farm_and_node<T: Config>(source: T::AccountId) {
pub fn prepare_farm<T: Config>(source: T::AccountId) {
let farm_name = "test_farm";
let mut pub_ips = Vec::new();
pub_ips.push(pallet_tfgrid_types::PublicIP {
pub_ips.push(PublicIP {
ip: "1.1.1.0".as_bytes().to_vec(),
gateway: "1.1.1.1".as_bytes().to_vec(),
contract_id: 0,
Expand Down
41 changes: 36 additions & 5 deletions substrate-node/pallets/pallet-smart-contract/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ use pallet_tfgrid::types as pallet_tfgrid_types;
use pallet_tft_price;
use pallet_timestamp as timestamp;
use substrate_fixed::types::U64F64;
use tfchain_support::{
traits::ChangeNode,
types::{Node, PublicIP, Resources, CertificationType}
};

#[cfg(test)]
mod mock;
Expand All @@ -42,6 +46,7 @@ pub trait Config:
type DistributionFrequency: Get<u16>;
type GracePeriod: Get<u64>;
type WeightInfo: WeightInfo;
type NodeChanged: ChangeNode;
}

pub const CONTRACT_VERSION: u32 = 3;
Expand All @@ -59,7 +64,7 @@ decl_event!(
ContractUpdated(types::Contract),
NodeContractCanceled(u64, u32, u32),
NameContractCanceled(u64),
IPsReserved(u64, Vec<pallet_tfgrid_types::PublicIP>),
IPsReserved(u64, Vec<PublicIP>),
IPsFreed(u64, Vec<Vec<u8>>),
ContractDeployed(u64, AccountId),
// Deprecated
Expand Down Expand Up @@ -810,7 +815,7 @@ impl<T: Config> Module<T> {
) -> Result<(BalanceOf<T>, types::DiscountLevel), DispatchError> {
// Fetch the default pricing policy and certification type
let pricing_policy = pallet_tfgrid::PricingPolicies::<T>::get(1);
let certification_type = pallet_tfgrid_types::CertificationType::Diy;
let certification_type = CertificationType::Diy;

// Calculate the cost for a contract, can be any of:
// - NodeContract
Expand Down Expand Up @@ -898,7 +903,7 @@ impl<T: Config> Module<T> {

// Calculates the total cost of a node contract.
pub fn calculate_resources_cost(
resources: pallet_tfgrid_types::Resources,
resources: Resources,
ipu: u32,
seconds_elapsed: u64,
pricing_policy: pallet_tfgrid_types::PricingPolicy<T::AccountId>,
Expand Down Expand Up @@ -1078,7 +1083,7 @@ impl<T: Config> Module<T> {
pub fn calculate_discount(
amount_due: u64,
balance: BalanceOf<T>,
certification_type: pallet_tfgrid_types::CertificationType,
certification_type: CertificationType,
) -> (BalanceOf<T>, types::DiscountLevel) {
let balance_as_u128: u128 = balance.saturated_into::<u128>();

Expand All @@ -1104,7 +1109,7 @@ impl<T: Config> Module<T> {
let mut amount_due = U64F64::from_num(amount_due) * discount_received.price_multiplier();

// Certified capacity costs 25% more
if certification_type == pallet_tfgrid_types::CertificationType::Certified {
if certification_type == CertificationType::Certified {
amount_due = amount_due * U64F64::from_num(1.25);
}

Expand Down Expand Up @@ -1336,3 +1341,29 @@ impl<T: Config> Module<T> {
cu
}
}

impl<T: Config> ChangeNode for Module<T> {
fn node_changed(_node: Option<&Node>, _new_node: &Node) {}

fn node_deleted(node: &Node) {
// Clean up all active contracts
let active_node_contracts = ActiveNodeContracts::get(node.id);
for node_contract_id in active_node_contracts {
let mut contract = Contracts::get(node_contract_id);
// Bill contract
let _ = Self::_update_contract_state(&mut contract, &types::ContractState::Deleted(types::Cause::CanceledByUser));
let _ = Self::bill_contract(&mut contract);
Self::remove_contract(node_contract_id);
}

// First clean up rent contract if it exists
let mut rent_contract = ActiveRentContractForNode::get(node.id);
if rent_contract.contract_id != 0 {
// Bill contract
let _ = Self::_update_contract_state(&mut rent_contract, &types::ContractState::Deleted(types::Cause::CanceledByUser));
let _ = Self::bill_contract(&mut rent_contract);
Self::remove_contract(rent_contract.contract_id);
}

}
}
18 changes: 18 additions & 0 deletions substrate-node/pallets/pallet-smart-contract/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ use sp_runtime::{
};
use sp_std::prelude::*;
use frame_system::EnsureRoot;
use tfchain_support::{
traits::ChangeNode,
types::Node,
};

pub type Signature = MultiSignature;

Expand Down Expand Up @@ -83,10 +87,23 @@ impl pallet_balances::Config for TestRuntime {
type WeightInfo = pallet_balances::weights::SubstrateWeight<TestRuntime>;
}

pub struct NodeChanged;
impl ChangeNode for NodeChanged {
fn node_changed(
_old_node: Option<&Node>,
_new_node: &Node,
) {}

fn node_deleted(node: &Node) {
SmartContractModule::node_deleted(node);
}
}

impl pallet_tfgrid::Config for TestRuntime {
type Event = Event;
type RestrictedOrigin = EnsureRoot<Self::AccountId>;
type WeightInfo = pallet_tfgrid::weights::SubstrateWeight<TestRuntime>;
type NodeChanged = NodeChanged;
}

impl pallet_tft_price::Config for TestRuntime {
Expand Down Expand Up @@ -118,6 +135,7 @@ impl Config for TestRuntime {
type DistributionFrequency = DistributionFrequency;
type GracePeriod = GracePeriod;
type WeightInfo = weights::SubstrateWeight<TestRuntime>;
type NodeChanged = NodeChanged;
}

type AccountPublic = <MultiSignature as Verify>::Signer;
Expand Down
Loading

0 comments on commit 072bdc8

Please sign in to comment.