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

refactor: move atomicRearrange into Zcf #7900

Merged
merged 7 commits into from
Jul 27, 2023

Conversation

Chris-Hibbert
Copy link
Contributor

@Chris-Hibbert Chris-Hibbert commented Jun 8, 2023

refs: #6678
refs: #6577

Description

This is the next step of #6577 (atomic reallocations for contracts). It adds native support in ZCF for atomicReallocate, which makes it possible to update contracts from the helper to use the ZCF-native approach, which is a prerequisite for removing the old hazardous staging-based approach.

This PR only affects ZCF, though it updates types referenced from inter-protocol contracts. This is required in order to keep the mono-repo consistent, and has no run-time impact on the contracts, even if changes are pushed to some live contracts even before the ZOE change takes effect.

The sample contracts in Zoe have also been updated to use the new API. These do not affect the interprotocol contracts. The only zoe contract referenced by inter-protocol contracts is scaledPriceAuthority, which is not changed here.

The new code was tested with versions of the inter-protocol contracts updated to use the ZCF-native API. Those contracts were reverted to their old form before merging this PR since the contracts should be updated separately from Zoe. None of the contracts should be updated to use the new reallocation API until this PR is running on chain. They can be individually updated once this code is in place.

This PR deprecates all the parts of the old approach, as well as the interim helper. (reallocate, getStagedAllocation, atomicRearrange, incrementBy, decrementBy, and hasStagedAllocation.)

Security Considerations

A step toward removing hazard-prone reallocation via staging.

Scaling Considerations

None.

Documentation Considerations

Document the deprecations and the new approach to reallocation.

Testing Considerations

Validated all the existing contracts (zoe examples, as well as Inter-prorotocol contracts) work with the new approach. The Zoe examples are updated with this PR. The Inter-protocl contracts were reverted, and continue to use the helper.

Added new tests for the function that coverts TransferPart descriptions to new allocations.

Release considerations

This PR adds an API to ZCF within Zoe. It can be released by upgrading the Zoe contract. Existing contracts will continue to work after this change. After this change, individual contracts can be upgraded to migrate to the new API.

@Chris-Hibbert Chris-Hibbert added Zoe package: Zoe Zoe Contract Contracts within Zoe labels Jun 8, 2023
@Chris-Hibbert Chris-Hibbert self-assigned this Jun 8, 2023
@Chris-Hibbert Chris-Hibbert marked this pull request as draft June 8, 2023 21:49
@Chris-Hibbert Chris-Hibbert marked this pull request as ready for review June 8, 2023 23:20
@Chris-Hibbert
Copy link
Contributor Author

@turadg, you are the primary reviewer.
@erights, FYI.
@warner, FYI in the context of writing up release plans for PRs that update code on chain.

packages/zoe/src/contractFacet/reallocate.js Outdated Show resolved Hide resolved
packages/zoe/src/contractFacet/reallocate.js Show resolved Hide resolved
packages/zoe/src/contractFacet/types.js Outdated Show resolved Hide resolved
@@ -184,6 +197,14 @@
* @returns {Amount<any>}
*/

/**
* @deprecated Use atomicRearrange instead
Copy link
Member

Choose a reason for hiding this comment

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

I think JSDoc does not support deprecating callbacks. In VSCode there's no strikethrough like other deprecations. I think because incrementBy property itself isn't deprecated. The notice is from line 223.
Screenshot 2023-06-09 at 7 51 51 AM

To mark these two properties as deprecated we'll have to use .ts syntax for the types. Unfortunately, that makes is harder to share them ambiently. We could put them in the global namespace but that's not technically correct.

Alternately, we could define the ZCFSeat type using the concrete implementation, which can also mark things deprecated. I'm fine deferring that.

Meanwhile, please revert making DeprecatedIncrementDecrementBy as it doesn't do anything :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for checking this, and looking for other alternatives. It annoyed me that I couldn't find a way to get a strike-through on incrementBy and decrementBy.

@@ -214,6 +216,10 @@ export const createSeatManager = (

return isOfferSafe(state.proposal, reallocation);
},
/**
* @deprecated switch to zcf.atomicRearrange()
Copy link
Member

@turadg turadg Jun 9, 2023

Choose a reason for hiding this comment

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

This is good but note that consumers of this object aren't getting the type written here. They get ZCFSeat which has the problem discussed above.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed. The comment is mostly for those reading/editing this file. This still seemed the most concise way to express it.

packages/zoe/src/contractFacet/zcfSeat.js Show resolved Hide resolved
packages/zoe/test/unitTests/zcf/test-atomicRearrange.js Outdated Show resolved Hide resolved
packages/zoe/src/contractFacet/zcfSeat.js Outdated Show resolved Hide resolved
@@ -52,6 +43,8 @@ export const TransferPartShape = M.splitArray(
* which will remain helpers. These helper are for convenience
* in expressing atomic rearrangements clearly.
*
* @deprecated use the zcf builtin instead
*
* @param {ZCF} zcf
* @param {TransferPart[]} transfers
*/
Copy link
Member

Choose a reason for hiding this comment

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

If the new code has the same semantics, why not redefine the old code to delegate to the new code?

Suggested change
*/
*/
export const atomicRearrange = (zcf, transfers) => zcf.atomicRearrange(transfers);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Doh! of course.

packages/zoe/src/contractFacet/reallocate.js Outdated Show resolved Hide resolved
packages/zoe/src/contractFacet/reallocate.js Outdated Show resolved Hide resolved
packages/zoe/src/contractFacet/reallocate.js Outdated Show resolved Hide resolved
}

/** @type {[ZCFSeat,AmountKeywordRecord][]} */
const resultingAllocations = [];
Copy link
Member

Choose a reason for hiding this comment

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

resultingAllocations is a different shape than allocations. consider renaming to resultingEntries or resolvedEntries.

consider a functional style using a transform function:

  const resolveAllocation = (seat, [incrList, decrList]) => {};
  const resolvedEntries = allocations.entries().map(resolveAllocation);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think I looked at that, but allocations.entries() isn't an array. Using Array.from() would require declaring more types.

packages/zoe/src/contractFacet/types.js Outdated Show resolved Hide resolved
packages/zoe/src/contractFacet/zcfSeat.js Outdated Show resolved Hide resolved
Comment on lines 357 to 362
([seat, allocation]) => {
const seatHandle = zcfSeatToSeatHandle.get(seat);
return { seatHandle, allocation };
},
Copy link
Member

Choose a reason for hiding this comment

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

nit for concision in map callbacks

Suggested change
([seat, allocation]) => {
const seatHandle = zcfSeatToSeatHandle.get(seat);
return { seatHandle, allocation };
},
([seat, allocation]) => ({
{ allocation, seatHandle: zcfSeatToSeatHandle.get(seat) };
}),

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done.

It doesn't seem any more concise to me. If prettier had plausible (to me, I admit) rules, it would be better as you show, but prettier formats it as

            ([seat, allocation]) => ({
              allocation,
              seatHandle: zcfSeatToSeatHandle.get(seat),
            }),

Which is not an improvement, AFAICT. In fact, seeing this, I might extract seatHandle so the return value will be on one line.

packages/zoe/src/contractFacet/zcfSeat.js Outdated Show resolved Hide resolved
packages/zoe/src/contractFacet/zcfSeat.js Outdated Show resolved Hide resolved
activeZCFSeats.set(seat, allocation),
);

E(zoeInstanceAdmin).replaceAllocations(seatHandleAllocations);
Copy link
Member

Choose a reason for hiding this comment

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

this is a promise that may reject. if so it will fail silently.

I see that reallocate had this same problem. Am I missing something?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We have tried to ensure that ZCF has the authoritative word here. If ZCF and Zoe lose contact then things are very broken and will shortly fall apart.

Short of that, Zoe and ZCF are in close communication and we have tried to ensure that the two sides remain in sync, and for every shared bit of info, we know which side is authoritative.

Copy link
Member

@turadg turadg Jul 21, 2023

Choose a reason for hiding this comment

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

Ok. Please put void and a comment with that justification. In particular, why a failure of replaceAllocations shouldn't be caught by the catch and shutdownWithFailure.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@turadg turadg mentioned this pull request Jun 23, 2023
@erights erights requested review from erights and ivanlei July 16, 2023 01:07
@erights
Copy link
Member

erights commented Jul 16, 2023

Just re-requested a review from myself to make this easier for me to find.

@ivanlei due to finger error, I also re-requested a review from you. I don't know how to undo that.

@turadg turadg removed the request for review from ivanlei July 17, 2023 15:54
This is the next step of #6577 (atomic reallocations for
contracts). It adds native support in ZCF for atomicReallocate, which
makes it possible to update contracts from the helper to use the
ZCF-native approach, which is a prerequisite for removing the old
hazardous staging-based approach.

This PR only affects ZCF, though it updates types referenced from
inter-protocol contracts. This is required in order to keep the
mono-repo consistent, and has no run-time impact on the contracts,
even if changes are pushed to some live contracts even before the ZOE
change takes effect.

The sample contracts in Zoe have also been updated to use the new
API. These do not affect the interprotocol contracts. The only zoe
contract referenced by inter-protocol contracts is
scaledPriceAuthority, which is not changed here.

The new code was tested with versions of the inter-protocol contracts
updated to use the ZCF-native API. Those contracts were reverted to
their old form before merging this PR since the contracts should be
updated separately from Zoe. None of the contracts should be updated
to use the new reallocation API until this PR is running on
chain. They can be individually updated once this code is in place.
Copy link
Member

@turadg turadg left a comment

Choose a reason for hiding this comment

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

One small change requested.

Let's also discuss whether this can land before its upgrade test or should be bundled with it.

Copy link
Member

@turadg turadg left a comment

Choose a reason for hiding this comment

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

🚢

@Chris-Hibbert Chris-Hibbert added this pull request to the merge queue Jul 27, 2023
Merged via the queue into master with commit cc54325 Jul 27, 2023
60 checks passed
@Chris-Hibbert Chris-Hibbert deleted the 6678-intrinsicAtomicTransfer branch July 27, 2023 19:37
mhofman pushed a commit that referenced this pull request Aug 7, 2023
mergify bot added a commit that referenced this pull request Sep 4, 2024
refs: #6678
refs: #7900

## Description

update VaultFactory to use zcf.atomicRearrange.

This change **must not** be pushed to the chain before #7900.

### Security Considerations

None

### Scaling Considerations

None

### Documentation Considerations

None

### Testing Considerations

None

### Release considerations

This requires a change to Zoe (#7900). Since VaultFactory gets upgraded separately from Zoe, this is staged as a separate PR.  Once #7900 is on chain, this update can be made, even if it's in the same release cycle, as long as Zoe is upgraded first. This change is independent of changes to other contracts.
mergify bot added a commit that referenced this pull request Sep 4, 2024
refs: #6678
refs: #7900

## Description

update Auction to use `zcf.atomicRearrange()`.

This change must not be pushed to the chain before #7900.

### Security Considerations

None

### Scaling Considerations

None

### Documentation Considerations

None

### Testing Considerations

None

### Release considerations

This requires a change to Zoe (#7900). Since the Auction gets upgraded separately from Zoe, this is staged as a separate PR. Once #7900 is on chain, this update can be made, even if it's in the same release cycle, as long as Zoe is upgraded first. This change is independent of changes to other contracts.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Zoe Contract Contracts within Zoe Zoe package: Zoe
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants