From a16493d6b55bcac2cb9c1a3573394f724e410143 Mon Sep 17 00:00:00 2001 From: Chris Hibbert Date: Fri, 23 Jun 2023 16:30:42 -0700 Subject: [PATCH] chore: local review clean-ups --- packages/zoe/src/contractFacet/reallocate.js | 13 +++----- packages/zoe/src/contractFacet/types.js | 3 +- packages/zoe/src/contractFacet/zcfSeat.js | 32 +++++++++++--------- 3 files changed, 22 insertions(+), 26 deletions(-) diff --git a/packages/zoe/src/contractFacet/reallocate.js b/packages/zoe/src/contractFacet/reallocate.js index 5eedaa9cf45..f09b7f031c4 100644 --- a/packages/zoe/src/contractFacet/reallocate.js +++ b/packages/zoe/src/contractFacet/reallocate.js @@ -13,7 +13,7 @@ const { Fail } = assert; * each of the seats mentioned. * * @param {Array} transfers - * @returns {[ZCFSeat,AmountKeywordRecord][]} + * @returns {[ZCFSeat, AmountKeywordRecord][]} */ export const makeAllocationMap = transfers => { /** @type {MapStore} */ @@ -30,22 +30,18 @@ export const makeAllocationMap = transfers => { return pair; }; - const updateAllocations = (seat, newAllocation) => { - allocations.set(seat, newAllocation); - }; - const decrementAllocation = (seat, decrement) => { const [incr, decr] = getAllocations(seat); const newDecr = [...decr, decrement]; - updateAllocations(seat, [incr, newDecr]); + allocations.set(seat, [incr, newDecr]); }; const incrementAllocation = (seat, increment) => { const [incr, decr] = getAllocations(seat); const newIncr = [...incr, increment]; - updateAllocations(seat, [newIncr, decr]); + allocations.set(seat, [newIncr, decr]); }; for (const [fromSeat, toSeat, fromAmounts, toAmounts] of transfers) { @@ -84,8 +80,7 @@ export const makeAllocationMap = transfers => { /** @type {[ZCFSeat,AmountKeywordRecord][]} */ const resultingAllocations = []; - for (const seat of allocations.keys()) { - const [incrList, decrList] = getAllocations(seat); + for (const [seat, [incrList, decrList]] of allocations.entries()) { let newAlloc = seat.getCurrentAllocation(); for (const incr of incrList) { newAlloc = addToAllocation(newAlloc, incr); diff --git a/packages/zoe/src/contractFacet/types.js b/packages/zoe/src/contractFacet/types.js index aa6dee2f73d..7999bb3650c 100644 --- a/packages/zoe/src/contractFacet/types.js +++ b/packages/zoe/src/contractFacet/types.js @@ -60,8 +60,7 @@ */ /** - * @deprecated reallocate() will be supported until at least 2023/09/01. It may - * be removed without further warning any time after 2023/11/01. + * @deprecated reallocate(). Use zcf.atomicRearrange() instead * * @typedef {(seat1: ZCFSeat, seat2: ZCFSeat, ...seatRest: * Array) => void} Reallocate diff --git a/packages/zoe/src/contractFacet/zcfSeat.js b/packages/zoe/src/contractFacet/zcfSeat.js index b9be8729f07..c355d6788a9 100644 --- a/packages/zoe/src/contractFacet/zcfSeat.js +++ b/packages/zoe/src/contractFacet/zcfSeat.js @@ -328,36 +328,38 @@ export const createSeatManager = ( const newAllocations = makeAllocationMap(transfers); // ////// All Seats are active ///////////////////////////////// - newAllocations.forEach(([seat]) => { + for (const [seat] of newAllocations) { assertActive(seat); !seat.hasStagedAllocation() || Fail`Cannot mix atomicRearrange with seat stagings: ${seat}`; zcfSeatToSeatHandle.has(seat) || Fail`The seat ${seat} was not recognized`; - }); + } // ////// Ensure that rights are conserved overall ///////////// - const flattenAllocations = allocations => + + // convert array of keywordAmountRecords to 1-level array of Amounts + const flattenAmounts = allocations => allocations.flatMap(Object.values); - const previousAmounts = flattenAllocations( + const previousAmounts = flattenAmounts( newAllocations.map(([seat]) => seat.getCurrentAllocation()), ); - const newAmounts = flattenAllocations( + const newAmounts = flattenAmounts( newAllocations.map(([_, allocation]) => allocation), ); assertRightsConserved(previousAmounts, newAmounts); // ////// Ensure that offer safety holds /////////////////////// - newAllocations.forEach(([seat, allocation]) => { + for (const [seat, allocation] of newAllocations) { isOfferSafe(seat.getProposal(), allocation) || Fail`Offer safety was violated by the proposed allocation: ${allocation}. Proposal was ${seat.getProposal()}`; - }); + } const seatHandleAllocations = newAllocations.map( - ([seat, allocation]) => { - const seatHandle = zcfSeatToSeatHandle.get(seat); - return { seatHandle, allocation }; - }, + ([seat, allocation]) => ({ + allocation, + seatHandle: zcfSeatToSeatHandle.get(seat), + }), ); try { // No side effects above. All conditions checked which could have @@ -369,15 +371,15 @@ export const createSeatManager = ( // // The effects must succeed atomically. The call to // replaceAllocations() will be processed in the order of updates - // from zcf to zoe, its effects must occur immediately in zoe on + // from ZCF to Zoe. Its effects must occur immediately in Zoe on // reception, and must not fail. // // Commit the new allocations (currentAllocation is replaced // for each of the seats) and inform Zoe of the new allocation. - newAllocations.forEach(([seat, allocation]) => - activeZCFSeats.set(seat, allocation), - ); + for (const [seat, allocation] of newAllocations) { + activeZCFSeats.set(seat, allocation); + } E(zoeInstanceAdmin).replaceAllocations(seatHandleAllocations); } catch (err) {