Skip to content

Conversation

@pbeza
Copy link
Contributor

@pbeza pbeza commented Aug 25, 2025

Fixes #844

@pbeza pbeza requested a review from Copilot August 25, 2025 17:03
@pbeza pbeza marked this pull request as ready for review August 25, 2025 17:07

This comment was marked as outdated.

@pbeza pbeza requested a review from Copilot August 25, 2025 18:16

This comment was marked as outdated.

Copy link
Collaborator

@netrome netrome left a comment

Choose a reason for hiding this comment

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

It seems to me that we're doing the cleanup in the wrong place. Moreover, while there are good unit tests for the cleanup functions I don't see a test for the vote_pk logic. I.e. if the contract is in resharing, and vote_pk is called with the final vote, it cleans up the stale attestations and enters the running state.

Copy link
Contributor

@DSharifi DSharifi left a comment

Choose a reason for hiding this comment

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

I'd like to see an integration test that tests the whole flow E2E. I.e. in the itntegration test you start a resharing, and make sure that the old participants that are not in the new running state are not in the TEE state.

pbeza added 7 commits August 26, 2025 12:34
Address code review comment: The vote_pk method was returning
`InvalidState::ProtocolStateNotResharing` when the protocol state
was not `Initializing`, which is misleading. This method expects
the protocol to be in `Initializing` state (as documented in the
function comment), so it should return `ProtocolStateNotInitializing`
error instead.
@pbeza pbeza requested a review from Copilot August 27, 2025 11:13
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR implements TEE (Trusted Execution Environment) state cleanup functionality when concluding a resharing operation. The changes ensure that only current participants retain their TEE attestations after resharing completes, preventing stale participant data from accumulating.

  • Adds automatic TEE cleanup when vote_reshared transitions back to running state
  • Implements a private clean_tee_status method that removes TEE data for non-participants
  • Adds comprehensive test coverage for both unit and integration scenarios

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
libs/chain-signatures/contract/src/lib.rs Adds TEE cleanup promise after resharing and implements the private cleanup method
libs/chain-signatures/contract/src/tee/tee_state.rs Implements core TEE cleanup logic and helper methods with unit tests
libs/chain-signatures/contract/src/state/mod.rs Fixes error type for vote_pk method validation
libs/chain-signatures/contract/tests/common.rs Adds shared helper functions for TEE testing
libs/chain-signatures/contract/tests/tee.rs Adds unit tests for TEE cleanup access control and functionality
libs/chain-signatures/contract/tests/integration_tee_cleanup_after_resharing.rs Adds comprehensive integration test for the complete resharing flow

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@pbeza pbeza requested a review from netrome August 27, 2025 12:17
@pbeza pbeza requested a review from DSharifi August 27, 2025 12:17
Copy link
Collaborator

@netrome netrome left a comment

Choose a reason for hiding this comment

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

Thank you for updating 🙏

Comment on lines 1142 to 1150
/// Private endpoint to clean up TEE information for non-participants after resharing.
/// This can only be called by the contract itself via a promise.
#[private]
pub fn clean_tee_status(&mut self) {
match self {
Self::V2(contract) => contract.clean_tee_status(),
_ => env::panic_str("expected V2"),
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I know this is what was specified in the issue, but I'm a bit curious why we have a separate endpoint for this and spawn a promise rather than just calling this inline. What are the benefits we get from this approach?

Copy link
Contributor

Choose a reason for hiding this comment

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

We avoid a failing vote_reshared due to insufficient gas.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Makes sense. So what happens if we run out of gas during the cleanup? We'll just leave the information lying around until the next resharing?

Copy link
Contributor

@kevindeforth kevindeforth Aug 27, 2025

Choose a reason for hiding this comment

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

So what happens if we run out of gas during the cleanup? We'll just leave the information lying around until the next resharing?

Yes. The only reason for the call to fail would be if we attached insufficient gas. Since that is a hard-coded constant, any subsequent resharing won't succeed with high probability. But we would have time to push a patch with adjusted gas and it's not the end of the world.

In any case, I would hope we get much better at correctly estimating and tracking gas costs. But it's not trivial and AFAIK, we would first need to build some sort of bench-marking framework to do so effectively.

Copy link
Contributor

Choose a reason for hiding this comment

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

We avoid a failing vote_reshared due to insufficient gas.

I'm also wondering how high of a risk it is that the contract call fails due to insufficient gas because of the cleanup, and if it's worth the complexity. Is this a recommended pattern for smart contracts on NEAR?

Copy link
Contributor

@kevindeforth kevindeforth left a comment

Choose a reason for hiding this comment

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

Good work!
Just a few remarks:

  • method naming
  • please add the new api endpoints to the contract readme
  • can you share some gas usage observations for the cleanup method? How did we arrive at 5Tgas?
  • move the promise creation to the higher-level VersionedMpcContract
  • lets not make the result of vote_reshared depend on the cleanup outcome.

@pbeza pbeza enabled auto-merge August 27, 2025 15:43
@pbeza pbeza dismissed DSharifi’s stale review August 27, 2025 16:02

Taking the liberty to merge since Daniel is OoO today. I’ve addressed his review comments and already got 2 extra approvals.

@pbeza pbeza added this pull request to the merge queue Aug 27, 2025
Merged via the queue into main with commit aace065 Aug 27, 2025
15 checks passed
@pbeza pbeza deleted the pab/844-clean-tee-state-when-concluding-a-resharing branch August 27, 2025 16:23
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.

Clean tee state when concluding a resharing

5 participants