Skip to content

Commit

Permalink
common setup for orchestration tests (#9409)
Browse files Browse the repository at this point in the history
refs: #9087

## Description

While getting started on #9087 I thought it would be helpful to have
less boilerplate in tests.

### Security Considerations

<!-- Does this change introduce new assumptions or dependencies that, if
violated, could introduce security vulnerabilities? How does this PR
change the boundaries between mutually-suspicious components? What new
authorities are introduced by this change, perhaps by new API calls?
-->

### Scaling Considerations

<!-- Does this change require or encourage significant increase in
consumption of CPU cycles, RAM, on-chain storage, message exchanges, or
other scarce resources? If so, can that be prevented or mitigated? -->

### Documentation Considerations

<!-- Give our docs folks some hints about what needs to be described to
downstream users.

Backwards compatibility: what happens to existing data or deployments
when this code is shipped? Do we need to instruct users to do something
to upgrade their saved data? If there is no upgrade path possible, how
bad will that be for users?

-->

### Testing Considerations

<!-- Every PR should of course come with tests of its own functionality.
What additional tests are still needed beyond those unit tests? How does
this affect CI, other test automation, or the testnet?
-->

### Upgrade Considerations

<!-- What aspects of this PR are relevant to upgrading live production
systems, and how should they be addressed? -->
  • Loading branch information
mergify[bot] authored May 28, 2024
2 parents 285beb4 + b67308f commit 034d7f6
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 156 deletions.
110 changes: 34 additions & 76 deletions packages/orchestration/test/examples/stake-bld.contract.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
import { test } from '@agoric/zoe/tools/prepare-test-env-ava.js';
import { AmountMath, makeIssuerKit } from '@agoric/ertp';
import { makeFakeStorageKit } from '@agoric/internal/src/storage-test-utils.js';
import { prepareLocalChainTools } from '@agoric/vats/src/localchain.js';
import { makeFakeBoard } from '@agoric/vats/tools/board-utils.js';
import { buildRootObject as buildBankVatRoot } from '@agoric/vats/src/vat-bank.js';

import { AmountMath } from '@agoric/ertp';
import { setUpZoeForTest } from '@agoric/zoe/tools/setup-zoe.js';
import { withAmountUtils } from '@agoric/zoe/tools/test-utils.js';
import buildManualTimer from '@agoric/zoe/tools/manualTimer.js';
import { makeHeapZone } from '@agoric/zone';
import { E } from '@endo/far';
import path from 'path';
import { makeFakeLocalchainBridge } from '../supports.js';
import { commonSetup } from '../supports.js';

const { keys } = Object;
const dirname = path.dirname(new URL(import.meta.url).pathname);
Expand All @@ -19,48 +13,15 @@ const contractFile = `${dirname}/../../src/examples/stakeBld.contract.js`;
type StartFn =
typeof import('@agoric/orchestration/src/examples/stakeBld.contract.js').start;

const bootstrap = async (t, { issuerKit }) => {
t.log('bootstrap vat dependencies');
const zone = makeHeapZone();
const bankManager = await buildBankVatRoot(
undefined,
undefined,
zone.mapStore('bankManager'),
).makeBankManager();
await E(bankManager).addAsset('ubld', 'BLD', 'Staking Token', issuerKit);

const localchainBridge = makeFakeLocalchainBridge(zone);
const localchain = prepareLocalChainTools(
zone.subZone('localchain'),
).makeLocalChain({
bankManager,
system: localchainBridge,
});
const timer = buildManualTimer(t.log);
const marshaller = makeFakeBoard().getReadonlyMarshaller();
const storage = makeFakeStorageKit('mockChainStorageRoot', {
sequence: false,
});
return {
timer,
localchain,
marshaller,
storage,
};
};

const coreEval = async (
t,
{ timer, localchain, marshaller, storage, stake },
) => {
const coreEval = async (t, { timer, localchain, marshaller, storage, bld }) => {
t.log('install stakeBld contract');
const { zoe, bundleAndInstall } = await setUpZoeForTest();
const installation: Installation<StartFn> =
await bundleAndInstall(contractFile);

const { publicFacet } = await E(zoe).startInstance(
installation,
{ In: stake.issuer },
{ In: bld.issuer },
{},
{
localchain,
Expand All @@ -74,72 +35,70 @@ const coreEval = async (
};

test('stakeBld contract - makeAccount, deposit, withdraw', async t => {
const issuerKit = makeIssuerKit('BLD');
const stake = withAmountUtils(issuerKit);

const bootstrapSpace = await bootstrap(t, { issuerKit });
const { publicFacet } = await coreEval(t, { ...bootstrapSpace, stake });
const {
bootstrap,
brands: { bld },
utils,
} = await commonSetup(t);
const { publicFacet } = await coreEval(t, { ...bootstrap, bld });

t.log('make a LocalChainAccount');
const account = await E(publicFacet).makeAccount();
t.truthy(account, 'account is returned');
t.regex(await E(account).getAddress(), /agoric1/);

const oneHundredStakeAmt = stake.make(1_000_000_000n);
const oneHundredStakePmt = issuerKit.mint.mintPayment(oneHundredStakeAmt);
// XXX not observed by vat-bank
const oneHundredStakePmt = bld.issuerKit.mint.mintPayment(bld.units(100));

t.log('deposit 100 bld to account');
const depositResp = await E(account).deposit(oneHundredStakePmt);
t.true(AmountMath.isEqual(depositResp, oneHundredStakeAmt), 'deposit');
t.true(AmountMath.isEqual(depositResp, bld.units(100)), 'deposit');

// TODO validate balance, .getBalance()

t.log('withdraw 1 bld from account');
const withdrawResp = await E(account).withdraw(oneHundredStakeAmt);
const withdrawAmt = await stake.issuer.getAmountOf(withdrawResp);
t.true(AmountMath.isEqual(withdrawAmt, oneHundredStakeAmt), 'withdraw');
t.log('withdraw bld from account');
const withdrawResp = await E(account).withdraw(bld.units(100));
const withdrawAmt = await bld.issuer.getAmountOf(withdrawResp);
t.true(AmountMath.isEqual(withdrawAmt, bld.units(100)), 'withdraw');

t.log('cannot withdraw more than balance');
await t.throwsAsync(
() => E(account).withdraw(oneHundredStakeAmt),
{
message: /Withdrawal of {.*} failed/,
},
() => E(account).withdraw(bld.units(100)),
undefined, // fake bank error messages don't match production
'cannot withdraw more than balance',
);
});

test('stakeBld contract - makeStakeBldInvitation', async t => {
const issuerKit = makeIssuerKit('BLD');
const stake = withAmountUtils(issuerKit);

const bootstrapSpace = await bootstrap(t, { issuerKit });
const { publicFacet, zoe } = await coreEval(t, { ...bootstrapSpace, stake });
const {
bootstrap,
brands: { bld },
} = await commonSetup(t);
const { publicFacet, zoe } = await coreEval(t, { ...bootstrap, bld });

t.log('call makeStakeBldInvitation');
const inv = await E(publicFacet).makeStakeBldInvitation();

const hundred = stake.make(1_000_000_000n);
const hundred = bld.make(1_000_000_000n);

t.log('make an offer for an account');
// Want empty until (at least) #9087
const userSeat = await E(zoe).offer(
inv,
{ give: { In: hundred } },
{ In: stake.mint.mintPayment(hundred) },
{ In: bld.mint.mintPayment(hundred) },
);
const { invitationMakers } = await E(userSeat).getOfferResult();
t.truthy(invitationMakers, 'received continuing invitation');

t.log('make Delegate offer using invitationMakers');
const delegateInv = await E(invitationMakers).Delegate('agoric1validator1', {
brand: stake.brand,
brand: bld.brand,
value: 1_000_000_000n,
});
const delegateOffer = await E(zoe).offer(
delegateInv,
{ give: { In: hundred } },
{ In: stake.mint.mintPayment(hundred) },
{ In: bld.mint.mintPayment(hundred) },
);
const res = await E(delegateOffer).getOfferResult();
t.deepEqual(res, {});
Expand All @@ -154,13 +113,12 @@ test('stakeBld contract - makeStakeBldInvitation', async t => {
});

test('stakeBld contract - makeAccountInvitationMaker', async t => {
const issuerKit = makeIssuerKit('BLD');
const stake = withAmountUtils(issuerKit);

const bootstrapSpace = await bootstrap(t, { issuerKit });
const { publicFacet, zoe } = await coreEval(t, { ...bootstrapSpace, stake });
const {
bootstrap,
brands: { bld },
} = await commonSetup(t);
const { publicFacet, zoe } = await coreEval(t, { ...bootstrap, bld });

t.log('call makeAcountInvitationMaker');
const inv = await E(publicFacet).makeAcountInvitationMaker();

const userSeat = await E(zoe).offer(inv);
Expand Down
64 changes: 21 additions & 43 deletions packages/orchestration/test/examples/swapExample.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
import { test } from '@agoric/zoe/tools/prepare-test-env-ava.js';

import { makeIssuerKit } from '@agoric/ertp';
import { LOCALCHAIN_DEFAULT_ADDRESS } from '@agoric/vats/tools/fake-bridge.js';
import { setUpZoeForTest } from '@agoric/zoe/tools/setup-zoe.js';
import { E } from '@endo/far';
import path from 'path';
import { makeFakeStorageKit } from '@agoric/internal/src/storage-test-utils.js';
import { makeHeapZone } from '@agoric/zone';
import { prepareLocalChainTools } from '@agoric/vats/src/localchain.js';
import { withAmountUtils } from '@agoric/zoe/tools/test-utils.js';
import { makeFakeBankManagerKit } from '@agoric/vats/tools/bank-utils.js';
import { LOCALCHAIN_DEFAULT_ADDRESS } from '@agoric/vats/tools/fake-bridge.js';
import { makeFakeLocalchainBridge } from '../supports.js';
import { commonSetup } from '../supports.js';

const dirname = path.dirname(new URL(import.meta.url).pathname);

Expand All @@ -19,43 +13,27 @@ type StartFn =
typeof import('@agoric/orchestration/src/examples/swapExample.contract.js').start;

test('start', async t => {
const issuerKit = makeIssuerKit('IST');
const stable = withAmountUtils(issuerKit);
const {
bootstrap,
brands: { ist },
utils,
} = await commonSetup(t);

const { zoe, bundleAndInstall } = await setUpZoeForTest();
const installation: Installation<StartFn> =
await bundleAndInstall(contractFile);

const zone = makeHeapZone();
const { makeLocalChain } = prepareLocalChainTools(zone.subZone('localchain'));

const { bankManager, pourPayment } = await makeFakeBankManagerKit();

await E(bankManager).addAsset('uist', 'IST', 'Inter Stable Token', issuerKit);

const localchainBridge = makeFakeLocalchainBridge(zone);

const localchain = makeLocalChain({
bankManager,
system: localchainBridge,
});

const storage = makeFakeStorageKit('mockChainStorageRoot', {
sequence: false,
});

const privateArgs = {
localchain,
orchestrationService: null as any,
storageNode: storage.rootNode,
timerService: null as any,
zone,
};

const { publicFacet } = await E(zoe).startInstance(
installation,
{ Stable: stable.issuer },
{ Stable: ist.issuer },
{},
privateArgs,
{
localchain: bootstrap.localchain,
orchestrationService: null as any,
storageNode: bootstrap.storage.rootNode,
timerService: bootstrap.timer,
zone: bootstrap.rootZone,
},
);

const inv = E(publicFacet).makeSwapAndStakeInvitation();
Expand All @@ -65,19 +43,19 @@ test('start', async t => {
'Swap for TIA and stake',
);

const bank = await E(bankManager).getBankForAddress(
const bank = await E(bootstrap.bankManager).getBankForAddress(
LOCALCHAIN_DEFAULT_ADDRESS,
);

const istPurse = await E(bank).getPurse(issuerKit.brand);
const istPurse = await E(bank).getPurse(ist.brand);
// bank purse is empty
t.like(await E(istPurse).getCurrentAmount(), stable.makeEmpty());
t.like(await E(istPurse).getCurrentAmount(), ist.makeEmpty());

const ten = stable.units(10);
const ten = ist.units(10);
const userSeat = await E(zoe).offer(
inv,
{ give: { Stable: ten } },
{ Stable: await pourPayment(ten) },
{ Stable: await utils.pourPayment(ten) },
{
staked: ten,
validator: {
Expand Down
51 changes: 14 additions & 37 deletions packages/orchestration/test/examples/unbondExample.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
import { test } from '@agoric/zoe/tools/prepare-test-env-ava.js';

import { makeIssuerKit } from '@agoric/ertp';
import { makeFakeStorageKit } from '@agoric/internal/src/storage-test-utils.js';
import { prepareLocalChainTools } from '@agoric/vats/src/localchain.js';
import { buildRootObject as buildBankVatRoot } from '@agoric/vats/src/vat-bank.js';
import { setUpZoeForTest } from '@agoric/zoe/tools/setup-zoe.js';
import { withAmountUtils } from '@agoric/zoe/tools/test-utils.js';
import { makeHeapZone } from '@agoric/zone';
import { E } from '@endo/far';
import path from 'path';
import { makeFakeBankManagerKit } from '@agoric/vats/tools/bank-utils.js';
import { makeFakeLocalchainBridge } from '../supports.js';
import { commonSetup } from '../supports.js';

const dirname = path.dirname(new URL(import.meta.url).pathname);

Expand All @@ -19,42 +12,26 @@ type StartFn =
typeof import('@agoric/orchestration/src/examples/unbondExample.contract.js').start;

test('start', async t => {
const issuerKit = makeIssuerKit('IST');
const stable = withAmountUtils(issuerKit);
const {
bootstrap,
brands: { ist },
} = await commonSetup(t);

const { zoe, bundleAndInstall } = await setUpZoeForTest();
const installation: Installation<StartFn> =
await bundleAndInstall(contractFile);

const zone = makeHeapZone();
const { makeLocalChain } = prepareLocalChainTools(zone.subZone('localchain'));
const { bankManager } = await makeFakeBankManagerKit();

await E(bankManager).addAsset('uist', 'IST', 'Inter Stable Token', issuerKit);

const localchainBridge = makeFakeLocalchainBridge(zone);

const localchain = makeLocalChain({
bankManager,
system: localchainBridge,
});

const storage = makeFakeStorageKit('mockChainStorageRoot', {
sequence: false,
});

const privateArgs = {
localchain,
orchestrationService: null as any,
storageNode: storage.rootNode,
timerService: null as any,
zone,
};

const { publicFacet } = await E(zoe).startInstance(
installation,
{ Stable: stable.issuer },
{ Stable: ist.issuer },
{},
privateArgs,
{
localchain: bootstrap.localchain,
orchestrationService: null as any,
storageNode: bootstrap.storage.rootNode,
timerService: bootstrap.timer,
zone: bootstrap.rootZone,
},
);

const inv = E(publicFacet).makeUnbondAndLiquidStakeInvitation();
Expand Down
Loading

0 comments on commit 034d7f6

Please sign in to comment.