Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Verify rent contract state on node contract creation #1008

Merged
merged 2 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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