Skip to content

Commit

Permalink
test(smart-wallet): avoid O(wallets) storage writes for a new asset
Browse files Browse the repository at this point in the history
failing test case for #6652
  • Loading branch information
dckc committed Jan 10, 2023
1 parent 229708b commit 83257c7
Showing 1 changed file with 84 additions and 0 deletions.
84 changes: 84 additions & 0 deletions packages/smart-wallet/test/test-addAsset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// @ts-check
import { test as anyTest } from '@agoric/zoe/tools/prepare-test-env-ava.js';
import { E } from '@endo/far';
import { buildRootObject as buildBankVatRoot } from '@agoric/vats/src/vat-bank.js';
import { makeIssuerKit } from '@agoric/ertp';
import { eventLoopIteration } from '@agoric/zoe/tools/eventLoopIteration.js';
import { makeDefaultTestContext } from './contexts.js';
import { makeMockTestSpace } from './supports.js';

/** @type {import('ava').TestFn<Awaited<ReturnType<makeDefaultTestContext>>>} */
const test = anyTest;

const TODO = undefined;

test.before(async t => {
const withBankManager = async () => {
const bridge = TODO;
const bankManager = E(buildBankVatRoot()).makeBankManager(bridge);
const noop = () => {};
const space0 = await makeMockTestSpace(noop);
space0.produce.bankManager.reset();
space0.produce.bankManager.resolve(bankManager);
return space0;
};
t.context = await makeDefaultTestContext(t, withBankManager);
});

const bigIntReplacer = (_key, val) =>
typeof val === 'bigint' ? Number(val) : val;

const range = qty => [...Array(qty).keys()];

/**
* NOTE: this doesn't test all forms of work.
* A better test would measure inter-vat messages or some such.
*/
test.failing('avoid O(wallets) storage writes for a new asset', async t => {
const bankManager = t.context.consume.bankManager;

let chainStorageWrites = 0;

const startUser = async ix => {
const address = `agoric1u${ix}`;
const smartWallet = t.context.simpleProvideWallet(address);

// stick around waiting for things to happen
const current = await E(smartWallet).getCurrentSubscriber();
/** @type {bigint | undefined} */
let publishCount;
for (;;) {
// eslint-disable-next-line no-await-in-loop
const news = await E(current).subscribeAfter(publishCount);
publishCount = news.publishCount;
chainStorageWrites += 1;
// console.log(JSON.stringify(news.head, bigIntReplacer, 2));
}
};

const simulate = async (qty, denom, name) => {
range(qty).forEach(startUser);
await eventLoopIteration();
const initialWrites = chainStorageWrites;

const kit = makeIssuerKit(name);
await E(bankManager).addAsset(denom, name, name, kit);
await eventLoopIteration();
return {
qty,
initialWrites,
addedWrites: chainStorageWrites - initialWrites,
};
};
const base = await simulate(2, 'ibc/dia1', 'DAI_axl');
const exp = await simulate(6, 'ibc/dia2', 'DAI_grv');

t.log({
base: { wallets: base.qty, writes: base.addedWrites },
test: { wallets: exp.qty + base.qty, writes: exp.addedWrites },
});
t.true(
exp.addedWrites < (base.addedWrites * exp.qty) / base.qty / 2,
'actual writes should be less than half of linear growth',
);
});

0 comments on commit 83257c7

Please sign in to comment.