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

Fix inconsistencies in node contracts states #1010

Merged
merged 10 commits into from
Oct 2, 2024

Conversation

sameh-farouk
Copy link
Member

@sameh-farouk sameh-farouk commented Sep 22, 2024

Description

  • A defensive check has been added to ensure that node contracts verify the state of the associated rent contract before transitioning to the created or resumed state.
  • When a rent contract attempts to resume a node contract, it now checks for any unsettled funds associated with that node contract.
  • The deletion of rent contracts is now delayed until all associated node contracts (with incurred costs) are independently billed and deleted. This ensures that no active node contracts have reserved balances before proceeding with rent contract deletion.
  • State transitions from gracePeriod(x) to gracePeriod(y) are now blocked to prevent improper state changes.

Related Issues:

#1002

Checklist:

Please delete options that are not relevant.

@sameh-farouk sameh-farouk marked this pull request as ready for review September 29, 2024 17:57
Copy link
Collaborator

@renauter renauter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests well cover the cases related to issue #1002
Nice work !!

Added my comments

substrate-node/pallets/pallet-smart-contract/src/tests.rs Outdated Show resolved Hide resolved
substrate-node/pallets/pallet-smart-contract/src/tests.rs Outdated Show resolved Hide resolved
substrate-node/pallets/pallet-smart-contract/src/tests.rs Outdated Show resolved Hide resolved
substrate-node/pallets/pallet-smart-contract/src/tests.rs Outdated Show resolved Hide resolved
substrate-node/pallets/pallet-smart-contract/src/tests.rs Outdated Show resolved Hide resolved
substrate-node/pallets/pallet-smart-contract/src/tests.rs Outdated Show resolved Hide resolved
substrate-node/pallets/pallet-smart-contract/src/tests.rs Outdated Show resolved Hide resolved
substrate-node/pallets/pallet-smart-contract/src/tests.rs Outdated Show resolved Hide resolved
substrate-node/pallets/pallet-smart-contract/src/tests.rs Outdated Show resolved Hide resolved
let node_contract = SmartContractModule::contracts(node_contract_id).unwrap();
assert_eq!(node_contract.state, types::ContractState::GracePeriod(11));

// Transfer some balance to the owner of the contract to settle only the cost of node resources (rent contract)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... for 2 billing cycles

.write()
.should_call_bill_contract(rent_contract_id, Ok(Pays::Yes.into()), 11);
run_to_block(11, Some(&mut pool_state));

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would have been nice to have some overdraft check here:

assert_ne!(rent_contract_overdraft, 0);
assert_eq!(rent_contract_reserve, spendable_balance);

or even:

assert_eq!(rent_contract_overdraft + rent_contract_reserve, rent_contract_cost_1_cycle);

.write()
.should_call_bill_contract(node_contract_id, Ok(Pays::Yes.into()), 12);
run_to_block(12, Some(&mut pool_state));

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also here something like:

assert_ne!(node_contract_overdraft, 0);
assert_eq!(node_contract_reserve, 0);
assert_eq!(rent_contract_overdraft, node_contract_ip_cost_1_cycle);

to be able to understand better what is happening in "backstage"

// Case 2: see https://github.com/threefoldtech/tfchain/issues/1002
// Calculate the rent contract cost
let charlie_twin_id = 3;
let (contract_cost, discount_level) = calculate_tft_cost(rent_contract_id, charlie_twin_id, 20);
Copy link
Collaborator

@renauter renauter Sep 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename contract_cost to rent_contract_cost_2_cycles ?
and amount_due to rent_amount_due

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’ve decided to skip these changes because the current names reflect their purpose without adding unnecessary complexity. The additional qualifiers don’t seem essential as the context is clear.


let node_contract = SmartContractModule::contracts(rent_contract_id).unwrap();
assert_eq!(node_contract.state, types::ContractState::Created);

Copy link
Collaborator

@renauter renauter Sep 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assert_eq!(rent_contract_overdraft, 0);
assert_eq!(rent_contract_reserve, rent_contract_cost_2_cycles);
assert_eq!(balance_reserve_charlie, rent_contract_cost_2_cycles);

Copy link
Collaborator

@renauter renauter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed test case 2
Test flow is great !
See my suggestions for improvement

@sameh-farouk
Copy link
Member Author

sameh-farouk commented Oct 1, 2024

@renauter Thank you for your thoughtful review. I’ve addressed most of your comments.

I also appreciate the suggestion to add more assertions, but I believe these extra checks go beyond the scope of these test cases. The existing assertions already verify the core functionality being fixed, and adding more may detract from the specific focus of the test.

@renauter renauter self-requested a review October 2, 2024 15:16
@sameh-farouk sameh-farouk merged commit ac1b8d6 into development Oct 2, 2024
1 check passed
@sameh-farouk sameh-farouk deleted the development_fix_sync_contracts branch October 2, 2024 19:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants