Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(ERTP): AmountMath compare should be same as Key compare #6769

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions packages/ERTP/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@
"@endo/far": "^0.2.18",
"@endo/marshal": "^0.8.5",
"@endo/nat": "^4.1.27",
"@endo/promise-kit": "^0.2.56"
"@endo/promise-kit": "^0.2.56",
"@fast-check/ava": "^1.1.3"
},
"devDependencies": {
"@endo/bundle-source": "^2.5.1",
"@fast-check/ava": "^1.1.3",
"ava": "^5.2.0",
"tsd": "^0.28.1"
},
Expand All @@ -67,7 +67,8 @@
"# fast-check unsupported",
"test/unitTests/test-amountProperties.js",
"test/unitTests/test-inputValidation.js",
"test/unitTests/test-issuerObj.js"
"test/unitTests/test-issuerObj.js",
"test/unitTests/test-amount-key-arith.js"
]
},
"ava": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
} from '@agoric/store';

import { AmountMath as m, AssetKind } from '../../../src/index.js';
import { mockBrand } from './mockBrand.js';
import { mockBrand } from '../../../tools/mockBrand.js';

// The "unit tests" for MathHelpers actually make the calls through
// AmountMath so that we can test that any duplication is handled
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { test } from '@agoric/swingset-vat/tools/prepare-test-env-ava.js';
import { getCopySetKeys, makeCopySet } from '@agoric/store';

import { AmountMath as m, AssetKind } from '../../../src/index.js';
import { mockBrand } from './mockBrand.js';
import { mockBrand } from '../../../tools/mockBrand.js';

// The "unit tests" for MathHelpers actually make the calls through
// AmountMath so that we can test that any duplication is handled
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { M } from '@agoric/store';

import { Far } from '@endo/marshal';
import { AmountMath as m, AssetKind } from '../../../src/index.js';
import { mockBrand } from './mockBrand.js';
import { mockBrand } from '../../../tools/mockBrand.js';

// The "unit tests" for MathHelpers actually make the calls through
// AmountMath so that we can test that any duplication is handled
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { test } from '@agoric/swingset-vat/tools/prepare-test-env-ava.js';
import { Far } from '@endo/marshal';

import { AmountMath as m, AssetKind } from '../../../src/index.js';
import { mockBrand } from './mockBrand.js';
import { mockBrand } from '../../../tools/mockBrand.js';

// The "unit tests" for MathHelpers actually make the calls through
// AmountMath so that we can test that any duplication is handled
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { test } from '@agoric/swingset-vat/tools/prepare-test-env-ava.js';

import { AmountMath as m, AssetKind } from '../../../src/index.js';
import { mockBrand } from './mockBrand.js';
import { mockBrand } from '../../../tools/mockBrand.js';

// The "unit tests" for MathHelpers actually make the calls through
// AmountMath so that we can test that any duplication is handled
Expand Down
22 changes: 22 additions & 0 deletions packages/ERTP/test/unitTests/test-amount-key-arith.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { test } from '@agoric/swingset-vat/tools/prepare-test-env-ava.js';
import { fc } from '@fast-check/ava';
import { keyEQ, keyGTE } from '@agoric/store';

import { AmountMath as m } from '../../src/index.js';
import { arbAmount } from '../../tools/arb-amount.js';

test('amount equality iff key equality', async t => {
await fc.assert(
fc.property(fc.record({ x: arbAmount, y: arbAmount }), ({ x, y }) => {
return t.true(m.isEqual(x, y) === keyEQ(x, y));
}),
);
});

test('amount >= iff key >=', async t => {
await fc.assert(
fc.property(fc.record({ x: arbAmount, y: arbAmount }), ({ x, y }) => {
return t.true(m.isGTE(x, y) === keyGTE(x, y));
}),
);
});
Comment on lines +8 to +22
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, the premise of this PR is wrong, and these tests need to be revised to reflect that.

We currently have four kinds of Amounts:

  • nats
  • sets represented as arrays -- deprecated but widely used
  • copySets, which should be used instead of arrays representing sets
  • copyBags, for semi-fungibles

The premise of this PR is true for nats, copySets, and copyBags. But it is not true for deprecated arrays representing sets, so we need to exclude them from these tests.

(Separately, we will be deprecating copySets as well, switching all uses of arrays-representing-sets and copySets to copyBags. But this is not a concern of this PR.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #7193

24 changes: 2 additions & 22 deletions packages/ERTP/test/unitTests/test-amountProperties.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,9 @@
import { test } from '@agoric/swingset-vat/tools/prepare-test-env-ava.js';
import { makeCopyBag } from '@agoric/store';
import { fc } from '@fast-check/ava';

import { AmountMath as m, AssetKind } from '../../src/index.js';
import { mockBrand } from './mathHelpers/mockBrand.js';

// Perhaps makeCopyBag should coalesce duplicate labels, but for now, it does
// not.
const distinctLabels = pairs =>
new Set(pairs.map(([label, _qty]) => label)).size === pairs.length;
const positiveCounts = pairs =>
pairs.filter(([_l, qty]) => qty > 0n).length === pairs.length;
const arbBagContents = fc
.nat(7)
.chain(size =>
fc.array(
fc.tuple(fc.string(), fc.bigUint({ max: 1_000_000_000_000_000n })),
{ minLength: size, maxLength: size },
),
)
.filter(pairs => distinctLabels(pairs) && positiveCounts(pairs));

const arbAmount = arbBagContents.map(contents =>
m.make(mockBrand, harden(makeCopyBag(contents))),
);
import { mockBrand } from '../../tools/mockBrand.js';
import { arbAmount } from '../../tools/arb-amount.js';

// Note: we write P => Q as !P || Q since JS has no logical => operator
const implies = (p, q) => !p || q;
Expand Down
26 changes: 26 additions & 0 deletions packages/ERTP/tools/arb-amount.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { makeCopyBag } from '@agoric/store';
import { fc } from '@fast-check/ava';

import { AmountMath as m } from '../src/index.js';
import { mockBrand } from './mockBrand.js';

// Perhaps makeCopyBag should coalesce duplicate labels, but for now, it does
// not.
const distinctLabels = pairs =>
new Set(pairs.map(([label, _qty]) => label)).size === pairs.length;
const positiveCounts = pairs =>
pairs.filter(([_l, qty]) => qty > 0n).length === pairs.length;
const arbBagContents = fc
.nat(7)
.chain(size =>
fc.array(
fc.tuple(fc.string(), fc.bigUint({ max: 1_000_000_000_000_000n })),
{ minLength: size, maxLength: size },
),
)
.filter(pairs => distinctLabels(pairs) && positiveCounts(pairs));

// TODO: should include many non-bag amounts too
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the TODO

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

export const arbAmount = arbBagContents.map(contents =>
m.make(mockBrand, harden(makeCopyBag(contents))),
);
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Far } from '@endo/marshal';
import { AssetKind } from '../../../src/index.js';
import { AssetKind } from '../src/index.js';

/** @type {Brand<AssetKind>} */
export const mockBrand = Far('brand', {
// eslint-disable-next-line no-unused-vars
isMyIssuer: async allegedIssuer => false,
isMyIssuer: async _allegedIssuer => false,
getAllegedName: () => 'mock',
getAmountShape: () => {},
getDisplayInfo: () => ({
Expand Down