-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move atomicRearrange into Zcf
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.
- Loading branch information
1 parent
2043349
commit b6e326b
Showing
24 changed files
with
516 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { makeScalarMapStore } from '@agoric/vat-data'; | ||
|
||
import { assertRightsConserved } from './rightsConservation.js'; | ||
import { addToAllocation, subtractFromAllocation } from './allocationMath.js'; | ||
|
||
const { Fail } = assert; | ||
|
||
/** @typedef {Array<AmountKeywordRecord>} TransactionList */ | ||
|
||
/** | ||
* Convert from a list of transfer descriptions ([fromSeat, toSeat, fromAmount, | ||
* toAmount], with many parts optional) to a list of resulting allocations for | ||
* each of the seats mentioned. | ||
* | ||
* @param {Array<TransferPart>} transfers | ||
* @returns {[ZCFSeat,AmountKeywordRecord][]} | ||
*/ | ||
export const makeAllocationMap = transfers => { | ||
/** @type {MapStore<ZCFSeat, [TransactionList, TransactionList]>} */ | ||
const allocations = makeScalarMapStore(); | ||
|
||
const getAllocations = seat => { | ||
if (allocations.has(seat)) { | ||
return allocations.get(seat); | ||
} | ||
|
||
/** @type {[TransactionList, TransactionList]} */ | ||
const pair = [[], []]; | ||
allocations.init(seat, pair); | ||
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]); | ||
}; | ||
|
||
const incrementAllocation = (seat, increment) => { | ||
const [incr, decr] = getAllocations(seat); | ||
|
||
const newIncr = [...incr, increment]; | ||
updateAllocations(seat, [newIncr, decr]); | ||
}; | ||
|
||
for (const [ | ||
fromSeat = undefined, | ||
toSeat = undefined, | ||
fromAmounts = undefined, | ||
toAmounts = undefined, | ||
] of transfers) { | ||
if (fromSeat) { | ||
if (!fromAmounts) { | ||
throw Fail`Transfer from ${fromSeat} must say how much`; | ||
} | ||
decrementAllocation(fromSeat, fromAmounts); | ||
if (toSeat) { | ||
// Conserved transfer between seats | ||
if (toAmounts) { | ||
// distinct amounts, so we check conservation. | ||
assertRightsConserved( | ||
Object.values(fromAmounts), | ||
Object.values(toAmounts), | ||
); | ||
incrementAllocation(toSeat, toAmounts); | ||
} else { | ||
// fromAmounts will be used for toAmounts as well | ||
incrementAllocation(toSeat, fromAmounts); | ||
} | ||
} else { | ||
// Transfer only from fromSeat | ||
!toAmounts || | ||
Fail`Transfer without toSeat cannot have toAmounts ${toAmounts}`; | ||
} | ||
} else { | ||
toSeat || Fail`Transfer must have at least one of fromSeat or toSeat`; | ||
// Transfer only to toSeat | ||
!fromAmounts || | ||
Fail`Transfer without fromSeat cannot have fromAmounts ${fromAmounts}`; | ||
toAmounts || Fail`Transfer to ${toSeat} must say how much`; | ||
incrementAllocation(toSeat, toAmounts); | ||
} | ||
} | ||
|
||
/** @type {[ZCFSeat,AmountKeywordRecord][]} */ | ||
const resultingAllocations = []; | ||
for (const seat of allocations.keys()) { | ||
const [incrList, decrList] = getAllocations(seat); | ||
let newAlloc = seat.getCurrentAllocation(); | ||
for (const incr of incrList) { | ||
newAlloc = addToAllocation(newAlloc, incr); | ||
} | ||
for (const decr of decrList) { | ||
newAlloc = subtractFromAllocation(newAlloc, decr); | ||
} | ||
resultingAllocations.push([seat, newAlloc]); | ||
} | ||
return resultingAllocations; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.