Skip to content

Commit

Permalink
chore: local review clean-ups
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris-Hibbert committed Jun 23, 2023
1 parent 7c0d656 commit a16493d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 26 deletions.
13 changes: 4 additions & 9 deletions packages/zoe/src/contractFacet/reallocate.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const { Fail } = assert;
* each of the seats mentioned.
*
* @param {Array<TransferPart>} transfers
* @returns {[ZCFSeat,AmountKeywordRecord][]}
* @returns {[ZCFSeat, AmountKeywordRecord][]}
*/
export const makeAllocationMap = transfers => {
/** @type {MapStore<ZCFSeat, [TransactionList, TransactionList]>} */
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions packages/zoe/src/contractFacet/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<ZCFSeat>) => void} Reallocate
Expand Down
32 changes: 17 additions & 15 deletions packages/zoe/src/contractFacet/zcfSeat.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down

0 comments on commit a16493d

Please sign in to comment.