From b67308f068aaad8cdb87a5e34686494a862ed483 Mon Sep 17 00:00:00 2001 From: Turadg Aleahmad Date: Fri, 24 May 2024 12:52:08 -0700 Subject: [PATCH] test: common setup --- .../test/examples/stake-bld.contract.test.ts | 110 ++++++------------ .../test/examples/swapExample.test.ts | 64 ++++------ .../test/examples/unbondExample.test.ts | 51 +++----- packages/orchestration/test/supports.ts | 65 +++++++++++ 4 files changed, 134 insertions(+), 156 deletions(-) diff --git a/packages/orchestration/test/examples/stake-bld.contract.test.ts b/packages/orchestration/test/examples/stake-bld.contract.test.ts index ca2b057da67..4d9fb218cf5 100644 --- a/packages/orchestration/test/examples/stake-bld.contract.test.ts +++ b/packages/orchestration/test/examples/stake-bld.contract.test.ts @@ -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); @@ -19,40 +13,7 @@ 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 = @@ -60,7 +21,7 @@ const coreEval = async ( const { publicFacet } = await E(zoe).startInstance( installation, - { In: stake.issuer }, + { In: bld.issuer }, {}, { localchain, @@ -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, {}); @@ -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); diff --git a/packages/orchestration/test/examples/swapExample.test.ts b/packages/orchestration/test/examples/swapExample.test.ts index 05c8cb4bdd3..f973a47f733 100644 --- a/packages/orchestration/test/examples/swapExample.test.ts +++ b/packages/orchestration/test/examples/swapExample.test.ts @@ -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); @@ -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 = 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(); @@ -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: { diff --git a/packages/orchestration/test/examples/unbondExample.test.ts b/packages/orchestration/test/examples/unbondExample.test.ts index 6af00453834..dc874ca5f33 100644 --- a/packages/orchestration/test/examples/unbondExample.test.ts +++ b/packages/orchestration/test/examples/unbondExample.test.ts @@ -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); @@ -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 = 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(); diff --git a/packages/orchestration/test/supports.ts b/packages/orchestration/test/supports.ts index 7e8600ad2f4..e680caa45f4 100644 --- a/packages/orchestration/test/supports.ts +++ b/packages/orchestration/test/supports.ts @@ -1 +1,66 @@ +import { makeIssuerKit } from '@agoric/ertp'; +import { makeFakeStorageKit } from '@agoric/internal/src/storage-test-utils.js'; +import { prepareLocalChainTools } from '@agoric/vats/src/localchain.js'; +import { makeFakeBankManagerKit } from '@agoric/vats/tools/bank-utils.js'; +import { makeFakeBoard } from '@agoric/vats/tools/board-utils.js'; +import { makeFakeLocalchainBridge } from '@agoric/vats/tools/fake-bridge.js'; +import type { Installation } from '@agoric/zoe/src/zoeService/utils.js'; +import buildManualTimer from '@agoric/zoe/tools/manualTimer.js'; +import { withAmountUtils } from '@agoric/zoe/tools/test-utils.js'; +import { makeHeapZone } from '@agoric/zone'; +import { E } from '@endo/far'; + export { makeFakeLocalchainBridge } from '@agoric/vats/tools/fake-bridge.js'; + +export const commonSetup = async t => { + t.log('bootstrap vat dependencies'); + // The common setup cannot support a durable zone because many of the fakes are not durable. + // They were made before we had durable kinds (and thus don't take a zone or baggage). + // To test durability in unit tests, test a particular entity with `relaxDurabilityRules: false`. + // To test durability integrating multiple vats, use a RunUtils/bootstrap test. + const rootZone = makeHeapZone(); + const bld = withAmountUtils(makeIssuerKit('BLD')); + const ist = withAmountUtils(makeIssuerKit('IST')); + + const { bankManager, pourPayment } = await makeFakeBankManagerKit(); + await E(bankManager).addAsset('ubld', 'BLD', 'Staking Token', bld.issuerKit); + await E(bankManager).addAsset( + 'uist', + 'IST', + 'Inter Stable Token', + ist.issuerKit, + ); + + const localchainBridge = makeFakeLocalchainBridge(rootZone); + const localchain = prepareLocalChainTools( + rootZone.subZone('localchain'), + ).makeLocalChain({ + bankManager, + system: localchainBridge, + }); + const timer = buildManualTimer(t.log); + const marshaller = makeFakeBoard().getReadonlyMarshaller(); + const storage = makeFakeStorageKit('mockChainStorageRoot', { + sequence: false, + }); + return { + bootstrap: { + bankManager, + timer, + localchain, + marshaller, + rootZone, + storage, + }, + brands: { + // TODO consider omitting `issuer` to prevent minting, which the bank can't observe + bld, + ist, + }, + utils: { + pourPayment, + }, + }; +}; + +export const makeDefaultContext = (contract: Installation) => {};