Skip to content

Commit

Permalink
Merge pull request #8856 from Agoric/gibson-2024-02-remove-arraytoobj
Browse files Browse the repository at this point in the history
refactor(zoe): Replace arrayToObj with objectMap
  • Loading branch information
mergify[bot] authored Feb 11, 2024
2 parents b9ffe48 + 2165e1e commit cd8e0aa
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 121 deletions.
15 changes: 5 additions & 10 deletions packages/zoe/src/cleanProposal.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { assert, q, Fail } from '@agoric/assert';
import { AmountMath, getAssetKind } from '@agoric/ertp';
import { objectMap } from '@agoric/internal';
import { assertRecord } from '@endo/marshal';
import { assertKey, assertPattern, mustMatch, isKey } from '@agoric/store';
import { FullProposalShape } from './typeGuards.js';
import { arrayToObj } from './objArrayConversion.js';

import './internal-types.js';

Expand Down Expand Up @@ -56,12 +56,10 @@ export const coerceAmountPatternKeywordRecord = (
allegedAmountKeywordRecord,
getAssetKindByBrand,
) => {
const keywords = cleanKeywords(allegedAmountKeywordRecord);

const amounts = Object.values(allegedAmountKeywordRecord);
// Check that each value can be coerced using the AmountMath
// indicated by brand. `AmountMath.coerce` throws if coercion fails.
const coercedAmounts = amounts.map(amount => {
cleanKeywords(allegedAmountKeywordRecord);
return objectMap(allegedAmountKeywordRecord, amount => {
// Check that each value can be coerced using the AmountMath
// indicated by brand. `AmountMath.coerce` throws if coercion fails.
if (isKey(amount)) {
const brandAssetKind = getAssetKindByBrand(amount.brand);
const assetKind = getAssetKind(amount);
Expand All @@ -75,9 +73,6 @@ export const coerceAmountPatternKeywordRecord = (
return amount;
}
});

// Recreate the amountKeywordRecord with coercedAmounts.
return harden(arrayToObj(coercedAmounts, keywords));
};

export const coerceAmountKeywordRecord = (
Expand Down
35 changes: 8 additions & 27 deletions packages/zoe/src/issuerStorage.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { deeplyFulfilledObject, objectMap } from '@agoric/internal';
import { provideDurableWeakMapStore } from '@agoric/vat-data';
import { E } from '@endo/eventual-send';

import { arrayToObj } from './objArrayConversion.js';
import { cleanKeywords } from './cleanProposal.js';
import { makeIssuerRecord } from './issuerRecord.js';

Expand Down Expand Up @@ -124,11 +124,6 @@ export const provideIssuerStorage = zcfBaggage => {
return getByBrand(brand);
};

const storeIssuers = issuers => {
assertInstantiated();
return Promise.all(issuers.map(storeIssuer));
};

/** @type {GetAssetKindByBrand} */
const getAssetKindByBrand = brand => {
assertInstantiated();
Expand All @@ -143,28 +138,14 @@ export const provideIssuerStorage = zcfBaggage => {
*/
const storeIssuerKeywordRecord = async uncleanIssuerKeywordRecord => {
assertInstantiated();
const keywords = cleanKeywords(uncleanIssuerKeywordRecord);
const issuerPs = keywords.map(
keyword => uncleanIssuerKeywordRecord[keyword],
cleanKeywords(uncleanIssuerKeywordRecord);
const issuerRecordPs = objectMap(uncleanIssuerKeywordRecord, issuerP =>
storeIssuer(issuerP),
);
// The issuers may not have been seen before, so we must wait for the
// issuer records to be available synchronously
const issuerRecords = await storeIssuers(issuerPs);
// AWAIT ///

const issuers = arrayToObj(
issuerRecords.map(record => record.issuer),
keywords,
);
const brands = arrayToObj(
issuerRecords.map(record => record.brand),
keywords,
);

return harden({
issuers,
brands,
});
const issuerRecords = await deeplyFulfilledObject(issuerRecordPs);
const issuers = objectMap(issuerRecords, ({ issuer }) => issuer);
const brands = objectMap(issuerRecords, ({ brand }) => brand);
return harden({ issuers, brands });
};

/**
Expand Down
35 changes: 0 additions & 35 deletions packages/zoe/src/objArrayConversion.js

This file was deleted.

43 changes: 18 additions & 25 deletions packages/zoe/src/zoeService/escrowStorage.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { AmountMath } from '@agoric/ertp';
import { E } from '@endo/eventual-send';
import { q, Fail } from '@agoric/assert';
import { objectMap } from '@agoric/internal';
import { deeplyFulfilledObject, objectMap } from '@agoric/internal';
import { provideDurableWeakMapStore } from '@agoric/vat-data';

/// <reference path="./types.js" />
import './internal-types.js';

import { cleanKeywords } from '../cleanProposal.js';
import { arrayToObj } from '../objArrayConversion.js';

/**
* Store the pool purses whose purpose is to escrow assets, with one
Expand Down Expand Up @@ -76,7 +75,6 @@ export const provideEscrowStorage = baggage => {
const depositPayments = async (proposal, payments) => {
const { give, want } = proposal;
const giveKeywords = Object.keys(give);
const wantKeywords = Object.keys(want);
const paymentKeywords = cleanKeywords(payments);

// Assert that all of the payment keywords are present in the give
Expand All @@ -91,35 +89,30 @@ export const provideEscrowStorage = baggage => {
)}`;
});

const proposalKeywords = harden([...giveKeywords, ...wantKeywords]);

// If any of these deposits hang or fail, then depositPayments
// If any of these deposits hang or fail, then this `await` also
// hangs or fails, the offer does not succeed, and any funds that
// were deposited into the pool purses are lost. We have a ticket
// for giving the user a refund of what was already deposited, and
// offer safety and payout liveness are still meaningful as long
// as issuers are well-behaved. For more, see
// https://github.com/Agoric/agoric-sdk/issues/1271
const amountsDeposited = await Promise.all(
giveKeywords.map(keyword => {
payments[keyword] !== undefined ||
Fail`The ${q(
keyword,
)} keyword in proposal.give did not have an associated payment in the paymentKeywordRecord, which had keywords: ${q(
paymentKeywords,
)}`;
return doDepositPayment(payments[keyword], give[keyword]);
}),
);

const emptyAmountsForWantKeywords = wantKeywords.map(keyword =>
AmountMath.makeEmptyFromAmount(want[keyword]),
);
const depositPs = objectMap(give, (amount, keyword) => {
payments[keyword] !== undefined ||
Fail`The ${q(
keyword,
)} keyword in proposal.give did not have an associated payment in the paymentKeywordRecord, which had keywords: ${q(
paymentKeywords,
)}`;
return doDepositPayment(payments[keyword], amount);
});
const deposits = await deeplyFulfilledObject(depositPs);

const initialAllocation = arrayToObj(
[...amountsDeposited, ...emptyAmountsForWantKeywords],
proposalKeywords,
);
const initialAllocation = harden({
...objectMap(want, amount => AmountMath.makeEmptyFromAmount(amount)),
// Deposits should win in case of overlapping give/want keywords
// (which are not allowed as of 2024-01).
...deposits,
});

return initialAllocation;
};
Expand Down
24 changes: 0 additions & 24 deletions packages/zoe/test/unitTests/test-objArrayConversion.js

This file was deleted.

0 comments on commit cd8e0aa

Please sign in to comment.