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

Implement CAP-58 (constructor support) #1447

Merged
merged 7 commits into from
Aug 26, 2024
Merged

Implement CAP-58 (constructor support) #1447

merged 7 commits into from
Aug 26, 2024

Conversation

dmkozh
Copy link
Contributor

@dmkozh dmkozh commented Aug 21, 2024

What

The implementation itself is fairly simple (thanks to not needing to support v21 semantics) and follows the CAP-58. It only has a few caveats:

  • Pre-v22 protocol Wasms are treated as if they don't have a constructor (they shouldn't have it anyway, but just in case they do we ignore it in order to not break the contract semantics). Thus we don't need to even try calling constructor for them
  • We try to call constructor for all v22+ contracts, so we charge for VM instantiation, which increases the creation cost to some degree. I'm not sure if we can discount calling non-existent functions; if we find that necessary, we can still improve this before the protocol release.

Bulk of this change is tests, and testing is really not trivial as we want to cover a set product of several different options:

  • p21 vs p22 contracts
  • v1 vs v2 host function
  • Constructor return values and errors
  • constructor arguments present/not present/'default' constructor
  • auth for constructor itself and inside the constructor
  • deployer support
  • invoker auth support
  • custom account support

To make things worse, a contract may only have a single constructor, so this needed a lot of new test Wasms.

Why

See CAP-58 motivation.

Known limitations

I'm sure I haven't covered every possible combination of parameters in tests, but at least tried to have some basic coverage for most of the cases and most obvious combinations.

@dmkozh dmkozh added the skip-env-interface-version-bump Skip the env-interface bump version check label Aug 21, 2024
@anupsdf
Copy link
Contributor

anupsdf commented Aug 21, 2024

If a pre-v22 contract gets its wasm updated in v22+ protocol with update_current_contract_wasm host fn call and the new wasm has a constructor, we will not be calling the constructor right?

@anupsdf
Copy link
Contributor

anupsdf commented Aug 21, 2024

If a pre-v22 contract gets its wasm updated in v22+ protocol with update_current_contract_wasm host fn call and the new wasm has a constructor, we will not be calling the constructor right?

Nevermind, I see this mentioned in the CAP,
When the contract has its code updated it is not considered created and thus constructor won't be called.

@dmkozh dmkozh force-pushed the ctor branch 2 times, most recently from 2b4a5b0 to 25551c6 Compare August 21, 2024 18:12
dmkozh added 2 commits August 21, 2024 14:31
The implementation itself is fairly simple (thanks to not needing to support v21 semantics) and follows the CAP. It only has a few caveats:

- Pre-v22 protocol Wasms are treated as if they don't have a constructor (they shouldn't have it anyway, but just in case they do we ignore it in order to not break the contract semantics). Thus we don't need to even try calling constructor for them
- We try to call constructor for all v22+ contracts, so we charge for VM instantiation, which increases the creation cost to some degree. I'm not sure if we can discount calling non-existent functions; if we find that necessary, we can still improve this before the protocol release.

Bulk of this change is tests, and testing is really not trivial as we want to cover a set product of several different options:
- p21 vs p22 contracts
- v1 vs v2 host function
- Constructor return values and errors
- constructor arguments present/not present/'default' constructor
- auth for constructor itself and inside the constructor
- deployer support
- invoker auth support
- custom account support

To make things worse, a contract may only have a single constructor, so this needed a lot of new test Wasms.

I'm sure I haven't covered every possible combination, but at least tried to have some basic coverage for most of the cases and most obvious combinations.
The diffs are quite significant, but generally seem to fall into the following categories:
- Changed auth data structure hashes (as we use the new XDR structure for the host fn)
- Slightly increased instructions and memory for the CreateContract host fn and other XDRs that include it (+24 bytes)
- New calls to the 'default' constructor for applicable contracts (typically generated Wasms that target the current protocol)
@dmkozh dmkozh marked this pull request as ready for review August 21, 2024 18:53
@dmkozh dmkozh requested review from graydon, sisuresh and a team as code owners August 21, 2024 18:53
Copy link
Contributor

@sisuresh sisuresh left a comment

Choose a reason for hiding this comment

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

Just a couple comments. I still need to look at the auth changes.

soroban-env-host/src/auth.rs Outdated Show resolved Hide resolved
soroban-env-host/src/vm.rs Show resolved Hide resolved
dmkozh added 4 commits August 22, 2024 12:05
After thinking for a bit, it doesn't seem like this could cause any security or logic issues, so we can improve backwards compatibility without much added maintenance cost.
@dmkozh dmkozh added this pull request to the merge queue Aug 26, 2024
Merged via the queue into stellar:main with commit 5ed156b Aug 26, 2024
13 checks passed
@dmkozh dmkozh deleted the ctor branch August 26, 2024 19:40
sisuresh added a commit to sisuresh/rs-soroban-env that referenced this pull request Aug 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
skip-env-interface-version-bump Skip the env-interface bump version check
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants