-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7900 from Agoric/6678-intrinsicAtomicTransfer
refactor: move atomicRearrange into Zcf
- Loading branch information
Showing
25 changed files
with
511 additions
and
151 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
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,94 @@ | ||
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 decrementAllocation = (seat, decrement) => { | ||
const [incr, decr] = getAllocations(seat); | ||
|
||
const newDecr = [...decr, decrement]; | ||
allocations.set(seat, [incr, newDecr]); | ||
}; | ||
|
||
const incrementAllocation = (seat, increment) => { | ||
const [incr, decr] = getAllocations(seat); | ||
|
||
const newIncr = [...incr, increment]; | ||
allocations.set(seat, [newIncr, decr]); | ||
}; | ||
|
||
for (const [fromSeat, toSeat, fromAmounts, toAmounts] 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, [incrList, decrList]] of allocations.entries()) { | ||
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
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
Oops, something went wrong.