Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin' into development_fix_sync_contr…
Browse files Browse the repository at this point in the history
…acts
  • Loading branch information
sameh-farouk committed Sep 29, 2024
2 parents 180cffd + 08321ba commit 0c3708b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
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

0 comments on commit 0c3708b

Please sign in to comment.