Skip to content

Commit

Permalink
Verify rent contract state on node contract creation (#1008)
Browse files Browse the repository at this point in the history
* fix: verify rent contract state on node contract creation

* chore: update benchmark `weights.rs` file for all pallets
  • Loading branch information
sameh-farouk authored Sep 24, 2024
1 parent 1c787e1 commit 08321ba
Show file tree
Hide file tree
Showing 11 changed files with 399 additions and 358 deletions.
12 changes: 6 additions & 6 deletions substrate-node/pallets/pallet-burning/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
//! Autogenerated weights for pallet_burning
//!
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
//! DATE: 2024-08-27, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! DATE: 2024-09-24, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! WORST CASE MAP SIZE: `1000000`
//! HOSTNAME: `66e77d0da08f`, CPU: `AMD Ryzen 7 5800X 8-Core Processor`
//! HOSTNAME: `da081090f983`, CPU: `AMD Ryzen 7 5800X 8-Core Processor`
//! EXECUTION: , WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 1024

// Executed Command:
Expand Down Expand Up @@ -45,8 +45,8 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
// Proof Size summary in bytes:
// Measured: `109`
// Estimated: `1594`
// Minimum execution time: 26_780_000 picoseconds.
Weight::from_parts(27_291_000, 1594)
// Minimum execution time: 26_741_000 picoseconds.
Weight::from_parts(27_212_000, 1594)
.saturating_add(T::DbWeight::get().reads(1_u64))
.saturating_add(T::DbWeight::get().writes(1_u64))
}
Expand All @@ -60,8 +60,8 @@ impl WeightInfo for () {
// Proof Size summary in bytes:
// Measured: `109`
// Estimated: `1594`
// Minimum execution time: 26_780_000 picoseconds.
Weight::from_parts(27_291_000, 1594)
// Minimum execution time: 26_741_000 picoseconds.
Weight::from_parts(27_212_000, 1594)
.saturating_add(RocksDbWeight::get().reads(1_u64))
.saturating_add(RocksDbWeight::get().writes(1_u64))
}
Expand Down
36 changes: 18 additions & 18 deletions substrate-node/pallets/pallet-dao/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
//! Autogenerated weights for pallet_dao
//!
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
//! DATE: 2024-08-27, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! DATE: 2024-09-24, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! WORST CASE MAP SIZE: `1000000`
//! HOSTNAME: `66e77d0da08f`, CPU: `AMD Ryzen 7 5800X 8-Core Processor`
//! HOSTNAME: `da081090f983`, CPU: `AMD Ryzen 7 5800X 8-Core Processor`
//! EXECUTION: , WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 1024

// Executed Command:
Expand Down Expand Up @@ -58,8 +58,8 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
// Proof Size summary in bytes:
// Measured: `208`
// Estimated: `4687`
// Minimum execution time: 19_277_000 picoseconds.
Weight::from_parts(20_188_000, 4687)
// Minimum execution time: 19_888_000 picoseconds.
Weight::from_parts(20_368_000, 4687)
.saturating_add(T::DbWeight::get().reads(4_u64))
.saturating_add(T::DbWeight::get().writes(5_u64))
}
Expand All @@ -77,8 +77,8 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
// Proof Size summary in bytes:
// Measured: `979`
// Estimated: `4444`
// Minimum execution time: 26_390_000 picoseconds.
Weight::from_parts(26_840_000, 4444)
// Minimum execution time: 26_991_000 picoseconds.
Weight::from_parts(27_542_000, 4444)
.saturating_add(T::DbWeight::get().reads(5_u64))
.saturating_add(T::DbWeight::get().writes(1_u64))
}
Expand All @@ -92,8 +92,8 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
// Proof Size summary in bytes:
// Measured: `487`
// Estimated: `4687`
// Minimum execution time: 18_616_000 picoseconds.
Weight::from_parts(18_985_000, 4687)
// Minimum execution time: 18_766_000 picoseconds.
Weight::from_parts(19_256_000, 4687)
.saturating_add(T::DbWeight::get().reads(3_u64))
.saturating_add(T::DbWeight::get().writes(1_u64))
}
Expand All @@ -111,8 +111,8 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
// Proof Size summary in bytes:
// Measured: `521`
// Estimated: `4687`
// Minimum execution time: 25_117_000 picoseconds.
Weight::from_parts(25_498_000, 4687)
// Minimum execution time: 25_348_000 picoseconds.
Weight::from_parts(26_310_000, 4687)
.saturating_add(T::DbWeight::get().reads(3_u64))
.saturating_add(T::DbWeight::get().writes(4_u64))
}
Expand All @@ -136,8 +136,8 @@ impl WeightInfo for () {
// Proof Size summary in bytes:
// Measured: `208`
// Estimated: `4687`
// Minimum execution time: 19_277_000 picoseconds.
Weight::from_parts(20_188_000, 4687)
// Minimum execution time: 19_888_000 picoseconds.
Weight::from_parts(20_368_000, 4687)
.saturating_add(RocksDbWeight::get().reads(4_u64))
.saturating_add(RocksDbWeight::get().writes(5_u64))
}
Expand All @@ -155,8 +155,8 @@ impl WeightInfo for () {
// Proof Size summary in bytes:
// Measured: `979`
// Estimated: `4444`
// Minimum execution time: 26_390_000 picoseconds.
Weight::from_parts(26_840_000, 4444)
// Minimum execution time: 26_991_000 picoseconds.
Weight::from_parts(27_542_000, 4444)
.saturating_add(RocksDbWeight::get().reads(5_u64))
.saturating_add(RocksDbWeight::get().writes(1_u64))
}
Expand All @@ -170,8 +170,8 @@ impl WeightInfo for () {
// Proof Size summary in bytes:
// Measured: `487`
// Estimated: `4687`
// Minimum execution time: 18_616_000 picoseconds.
Weight::from_parts(18_985_000, 4687)
// Minimum execution time: 18_766_000 picoseconds.
Weight::from_parts(19_256_000, 4687)
.saturating_add(RocksDbWeight::get().reads(3_u64))
.saturating_add(RocksDbWeight::get().writes(1_u64))
}
Expand All @@ -189,8 +189,8 @@ impl WeightInfo for () {
// Proof Size summary in bytes:
// Measured: `521`
// Estimated: `4687`
// Minimum execution time: 25_117_000 picoseconds.
Weight::from_parts(25_498_000, 4687)
// Minimum execution time: 25_348_000 picoseconds.
Weight::from_parts(26_310_000, 4687)
.saturating_add(RocksDbWeight::get().reads(3_u64))
.saturating_add(RocksDbWeight::get().writes(4_u64))
}
Expand Down
20 changes: 10 additions & 10 deletions substrate-node/pallets/pallet-kvstore/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
//! Autogenerated weights for pallet_kvstore
//!
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
//! DATE: 2024-08-27, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! DATE: 2024-09-24, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! WORST CASE MAP SIZE: `1000000`
//! HOSTNAME: `66e77d0da08f`, CPU: `AMD Ryzen 7 5800X 8-Core Processor`
//! HOSTNAME: `da081090f983`, CPU: `AMD Ryzen 7 5800X 8-Core Processor`
//! EXECUTION: , WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 1024

// Executed Command:
Expand Down Expand Up @@ -46,8 +46,8 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
// Proof Size summary in bytes:
// Measured: `0`
// Estimated: `0`
// Minimum execution time: 11_372_000 picoseconds.
Weight::from_parts(12_032_000, 0)
// Minimum execution time: 7_054_000 picoseconds.
Weight::from_parts(7_274_000, 0)
.saturating_add(T::DbWeight::get().writes(1_u64))
}
/// Storage: `TFKVStore::TFKVStore` (r:1 w:1)
Expand All @@ -56,8 +56,8 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
// Proof Size summary in bytes:
// Measured: `146`
// Estimated: `3611`
// Minimum execution time: 12_414_000 picoseconds.
Weight::from_parts(12_854_000, 3611)
// Minimum execution time: 12_263_000 picoseconds.
Weight::from_parts(12_824_000, 3611)
.saturating_add(T::DbWeight::get().reads(1_u64))
.saturating_add(T::DbWeight::get().writes(1_u64))
}
Expand All @@ -71,8 +71,8 @@ impl WeightInfo for () {
// Proof Size summary in bytes:
// Measured: `0`
// Estimated: `0`
// Minimum execution time: 11_372_000 picoseconds.
Weight::from_parts(12_032_000, 0)
// Minimum execution time: 7_054_000 picoseconds.
Weight::from_parts(7_274_000, 0)
.saturating_add(RocksDbWeight::get().writes(1_u64))
}
/// Storage: `TFKVStore::TFKVStore` (r:1 w:1)
Expand All @@ -81,8 +81,8 @@ impl WeightInfo for () {
// Proof Size summary in bytes:
// Measured: `146`
// Estimated: `3611`
// Minimum execution time: 12_414_000 picoseconds.
Weight::from_parts(12_854_000, 3611)
// Minimum execution time: 12_263_000 picoseconds.
Weight::from_parts(12_824_000, 3611)
.saturating_add(RocksDbWeight::get().reads(1_u64))
.saturating_add(RocksDbWeight::get().writes(1_u64))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,15 @@ impl<T: Config> Pallet<T> {
let node_is_dedicated =
DedicatedNodesExtraFee::<T>::get(node_id) > 0 || farm.dedicated_farm;

// In case there is a rent contract make sure only the contract owner can deploy
// In case there is a rent contract:
// 1. Ensure only the contract owner can deploy, and
// 2. The rent contract is in the 'created' state
// If not, allow to deploy only if node is not dedicated
match ActiveRentContractForNode::<T>::get(node_id) {
Some(contract_id) => {
let rent_contract =
Contracts::<T>::get(contract_id).ok_or(Error::<T>::ContractNotExists)?;
if rent_contract.twin_id != twin_id {
if rent_contract.twin_id != twin_id || !matches!(rent_contract.state, types::ContractState::Created) {
return Err(Error::<T>::NodeNotAvailableToDeploy.into());
}
}
Expand Down
39 changes: 39 additions & 0 deletions substrate-node/pallets/pallet-smart-contract/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,45 @@ fn test_create_node_contract_on_dedicated_node_rented_by_other_fails() {
})
}

#[test]
fn test_create_node_contract_when_having_a_rentcontract_in_graceperiod_fails() {
let (mut ext, mut pool_state) = new_test_ext_with_pool_state(0);
ext.execute_with(|| {
run_to_block(1, None);
prepare_dedicated_farm_and_node();
let node_id = 1;
let contract_id = 1;
assert_ok!(SmartContractModule::create_rent_contract(
RuntimeOrigin::signed(charlie()),
node_id,
None
));

// Cycle 1
// User does not have enough funds to pay
pool_state
.write()
.should_call_bill_contract(contract_id, Ok(Pays::Yes.into()), 11);
run_to_block(11, Some(&mut pool_state));

let r = SmartContractModule::contracts(1).unwrap();
assert_eq!(r.state, types::ContractState::GracePeriod(11));

// try to create node contract
assert_noop!(
SmartContractModule::create_node_contract(
RuntimeOrigin::signed(charlie()),
node_id,
generate_deployment_hash(),
get_deployment_data(),
1,
None
),
Error::<TestRuntime>::NodeNotAvailableToDeploy
);
})
}

#[test]
fn test_cancel_rent_contract_with_active_node_contracts_fails() {
new_test_ext().execute_with(|| {
Expand Down
Loading

0 comments on commit 08321ba

Please sign in to comment.