Skip to content

Commit

Permalink
chore: advancer validates settlementAddress
Browse files Browse the repository at this point in the history
  • Loading branch information
0xpatrickdev authored and turadg committed Dec 18, 2024
1 parent 5ad59bd commit e06f12a
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 53 deletions.
11 changes: 9 additions & 2 deletions packages/boot/test/fast-usdc/fast-usdc.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { test as anyTest } from '@agoric/zoe/tools/prepare-test-env-ava.js';

import type { TestFn } from 'ava';
import { encodeAddressHook } from '@agoric/cosmic-proto/address-hooks.js';
import { configurations } from '@agoric/fast-usdc/src/utils/deploy-config.js';
import { MockCctpTxEvidences } from '@agoric/fast-usdc/test/fixtures.js';
import { documentStorageSchema } from '@agoric/governance/tools/storageDoc.js';
Expand All @@ -12,7 +13,7 @@ import {
defaultSerializer,
} from '@agoric/internal/src/storage-test-utils.js';
import { eventLoopIteration } from '@agoric/internal/src/testing-utils.js';
import { BridgeId } from '@agoric/internal';
import { BridgeId, NonNullish } from '@agoric/internal';
import {
makeWalletFactoryContext,
type WalletFactoryTestContext,
Expand Down Expand Up @@ -261,7 +262,13 @@ test.serial('makes usdc advance', async t => {
'FastLP balance not in wallet record',
);

const evidence = MockCctpTxEvidences.AGORIC_PLUS_OSMO();
const EUD = 'dydx1anything';
const lastNodeValue = storage.getValues('published.fastUsdc').at(-1);
const { settlementAccount } = JSON.parse(NonNullish(lastNodeValue));
const evidence = MockCctpTxEvidences.AGORIC_PLUS_OSMO(
// mock with the read settlementAccount address
encodeAddressHook(settlementAccount, { EUD }),
);

harness?.useRunPolicy(true);
await Promise.all(
Expand Down
11 changes: 9 additions & 2 deletions packages/fast-usdc/src/exos/advancer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { pickFacet } from '@agoric/vat-data';
import { VowShape } from '@agoric/vow';
import { E } from '@endo/far';
import { M, mustMatch } from '@endo/patterns';
import { Fail, q } from '@endo/errors';
import {
CctpTxEvidenceShape,
AddressHookShape,
Expand Down Expand Up @@ -116,6 +117,7 @@ export const prepareAdvancerKit = (
* notifyFacet: import('./settler.js').SettlerKit['notify'];
* borrowerFacet: LiquidityPoolKit['borrower'];
* poolAccount: HostInterface<OrchestrationAccount<{chainId: 'agoric'}>>;
* settlementAddress: ChainAddress;
* intermediateRecipient?: ChainAddress;
* }} config
*/
Expand Down Expand Up @@ -145,10 +147,14 @@ export const prepareAdvancerKit = (
return;
}

const { borrowerFacet, poolAccount } = this.state;
const { borrowerFacet, poolAccount, settlementAddress } =
this.state;
const { recipientAddress } = evidence.aux;
const decoded = decodeAddressHook(recipientAddress);
mustMatch(decoded, AddressHookShape);
if (decoded.baseAddress !== settlementAddress.value) {
throw Fail`⚠️ baseAddress of address hook ${q(decoded.baseAddress)} does not match the expected address ${q(settlementAddress.value)}`;
}
const { EUD } = /** @type {AddressHook['query']} */ (decoded.query);
log(`decoded EUD: ${EUD}`);
// throws if the bech32 prefix is not found
Expand All @@ -172,10 +178,10 @@ export const prepareAdvancerKit = (
harden({ USDC: advanceAmount }),
);
void watch(depositV, this.facets.depositHandler, {
fullAmount,
advanceAmount,
destination,
forwardingAddress: evidence.tx.forwardingAddress,
fullAmount,
tmpSeat,
txHash: evidence.txHash,
});
Expand Down Expand Up @@ -271,6 +277,7 @@ export const prepareAdvancerKit = (
borrowerFacet: M.remotable(),
poolAccount: M.remotable(),
intermediateRecipient: M.opt(ChainAddressShape),
settlementAddress: M.opt(ChainAddressShape),
}),
},
);
Expand Down
15 changes: 7 additions & 8 deletions packages/fast-usdc/src/fast-usdc.contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,16 +179,12 @@ export const contract = async (zcf, privateArgs, zone, tools) => {
},
async publishAddresses() {
!baggage.has(ADDRESSES_BAGGAGE_KEY) || Fail`Addresses already published`;
const [poolAccountAddress, settlementAccountAddress] =
await vowTools.when(
vowTools.all([
E(poolAccount).getAddress(),
E(settlementAccount).getAddress(),
]),
);
const [poolAccountAddress] = await vowTools.when(
vowTools.all([E(poolAccount).getAddress()]),
);
const addresses = harden({
poolAccount: poolAccountAddress.value,
settlementAccount: settlementAccountAddress.value,
settlementAccount: settlementAddress.value,
});
baggage.init(ADDRESSES_BAGGAGE_KEY, addresses);
await publishAddresses(storageNode, addresses);
Expand Down Expand Up @@ -284,6 +280,8 @@ export const contract = async (zcf, privateArgs, zone, tools) => {
);
trace('settlementAccount', settlementAccount);
trace('poolAccount', poolAccount);
const settlementAddress = await E(settlementAccount).getAddress();
trace('settlementAddress', settlementAddress);

const [_agoric, _noble, agToNoble] = await vowTools.when(
chainHub.getChainsAndConnection('agoric', 'noble'),
Expand All @@ -300,6 +298,7 @@ export const contract = async (zcf, privateArgs, zone, tools) => {
borrowerFacet: poolKit.borrower,
notifyFacet: settlerKit.notify,
poolAccount,
settlementAddress,
}),
);
// Connect evidence stream to advancer
Expand Down
7 changes: 3 additions & 4 deletions packages/fast-usdc/test/cli/transfer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
makeFetchMock,
makeMockSigner,
} from '../../testing/mocks.js';
import { settlementAddress } from '../fixtures.js';

test('Errors if config missing', async t => {
const path = 'config/dir/.fast-usdc/config.json';
Expand Down Expand Up @@ -73,8 +74,7 @@ test('Transfer registers the noble forwarding account if it does not exist', asy
};
const out = mockOut();
const file = mockFile(path, JSON.stringify(config));
const agoricSettlementAccount =
'agoric16kv2g7snfc4q24vg3pjdlnnqgngtjpwtetd2h689nz09lcklvh5s8u37ek';
const agoricSettlementAccount = settlementAddress.value;
const settlementAccountVstoragePath = 'published.fastUsdc.settlementAccount';
const vstorageMock = makeVstorageMock({
[settlementAccountVstoragePath]: agoricSettlementAccount,
Expand Down Expand Up @@ -150,8 +150,7 @@ test('Transfer signs and broadcasts the depositForBurn message on Ethereum', asy
};
const out = mockOut();
const file = mockFile(path, JSON.stringify(config));
const agoricSettlementAccount =
'agoric16kv2g7snfc4q24vg3pjdlnnqgngtjpwtetd2h689nz09lcklvh5s8u37ek';
const agoricSettlementAccount = settlementAddress.value;
const settlementAccountVstoragePath = 'published.fastUsdc.settlementAccount';
const vstorageMock = makeVstorageMock({
[settlementAccountVstoragePath]: agoricSettlementAccount,
Expand Down
47 changes: 42 additions & 5 deletions packages/fast-usdc/test/exos/advancer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ import type { SettlerKit } from '../../src/exos/settler.js';
import { prepareStatusManager } from '../../src/exos/status-manager.js';
import type { LiquidityPoolKit } from '../../src/types.js';
import { makeFeeTools } from '../../src/utils/fees.js';
import { MockCctpTxEvidences, intermediateRecipient } from '../fixtures.js';
import {
MockCctpTxEvidences,
settlementAddress,
intermediateRecipient,
} from '../fixtures.js';
import {
makeTestFeeConfig,
makeTestLogger,
Expand Down Expand Up @@ -127,6 +131,7 @@ const createTestExtensions = (t, common: CommonSetup) => {
notifyFacet: mockNotifyF,
poolAccount: mockAccounts.mockPoolAccount.account,
intermediateRecipient,
settlementAddress,
});

return {
Expand All @@ -141,6 +146,7 @@ const createTestExtensions = (t, common: CommonSetup) => {
},
mocks: {
...mockAccounts,
mockBorrowerF,
mockNotifyF,
resolveLocalTransferV,
rejectLocalTransfeferV,
Expand Down Expand Up @@ -260,6 +266,7 @@ test('updates status to OBSERVED on insufficient pool funds', async t => {
notifyFacet: mockNotifyF,
poolAccount: mockPoolAccount.account,
intermediateRecipient,
settlementAddress,
});

const evidence = MockCctpTxEvidences.AGORIC_PLUS_DYDX();
Expand Down Expand Up @@ -393,10 +400,10 @@ test('updates status to OBSERVED if pre-condition checks fail', async t => {

await advancer.handleTransactionEvent({
...MockCctpTxEvidences.AGORIC_NO_PARAMS(
encodeAddressHook(
'agoric16kv2g7snfc4q24vg3pjdlnnqgngtjpwtetd2h689nz09lcklvh5s8u37ek',
{ EUD: 'osmo1234', extra: 'value' },
),
encodeAddressHook(settlementAddress.value, {
EUD: 'osmo1234',
extra: 'value',
}),
),
txHash:
'0xc81bc6105b60a234c7c50ac17816ebcd5561d366df8bf3be59ff387552761799',
Expand Down Expand Up @@ -550,6 +557,7 @@ test('alerts if `returnToPool` fallback fails', async t => {
notifyFacet: mockNotifyF,
poolAccount: mockPoolAccount.account,
intermediateRecipient,
settlementAddress,
});

const mockEvidence = MockCctpTxEvidences.AGORIC_PLUS_OSMO();
Expand Down Expand Up @@ -588,3 +596,32 @@ test('alerts if `returnToPool` fallback fails', async t => {
'Advancing tx is recorded as AdvanceFailed',
);
});

test('rejects advances to unknown settlementAccount', async t => {
const {
extensions: {
services: { advancer },
helpers: { inspectLogs },
},
} = t.context;

const invalidSettlementAcct =
'agoric1ax7hmw49tmqrdld7emc5xw3wf43a49rtkacr9d5nfpqa0y7k6n0sl8v94h';
t.not(settlementAddress.value, invalidSettlementAcct);
const mockEvidence = MockCctpTxEvidences.AGORIC_PLUS_OSMO(
encodeAddressHook(invalidSettlementAcct, {
EUD: 'osmo183dejcnmkka5dzcu9xw6mywq0p2m5peks28men',
}),
);

void advancer.handleTransactionEvent(mockEvidence);
await eventLoopIteration();
t.deepEqual(inspectLogs(), [
[
'Advancer error:',
Error(
'⚠️ baseAddress of address hook "agoric1ax7hmw49tmqrdld7emc5xw3wf43a49rtkacr9d5nfpqa0y7k6n0sl8v94h" does not match the expected address "agoric16kv2g7snfc4q24vg3pjdlnnqgngtjpwtetd2h689nz09lcklvh5s8u37ek"',
),
],
]);
});
40 changes: 25 additions & 15 deletions packages/fast-usdc/test/fast-usdc.contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
encodeAddressHook,
} from '@agoric/cosmic-proto/address-hooks.js';
import { AmountMath } from '@agoric/ertp/src/amountMath.js';
import type { makeFakeStorageKit } from '@agoric/internal/src/storage-test-utils.js';
import {
eventLoopIteration,
inspectMapStore,
Expand All @@ -18,6 +19,7 @@ import {
} from '@agoric/notifier';
import fetchedChainInfo from '@agoric/orchestration/src/fetched-chain-info.js';
import { buildVTransferEvent } from '@agoric/orchestration/tools/ibc-mocks.js';
import { makeTestAddress } from '@agoric/orchestration/tools/make-test-address.js';
import { heapVowE as VE } from '@agoric/vow/vat.js';
import {
divideBy,
Expand Down Expand Up @@ -136,12 +138,15 @@ const makeTestContext = async (t: ExecutionContext) => {
};

const mint = async (e: CctpTxEvidence) => {
const settlerAddr = 'agoric1fakeLCAAddress1'; // TODO: get from contract
const rxd = await receiveUSDCAt(settlerAddr, e.tx.amount);
const accountsData = common.bootstrap.storage.data.get('fun');
const { settlementAccount } = JSON.parse(
JSON.parse(accountsData!).values[0],
);
const rxd = await receiveUSDCAt(settlementAccount, e.tx.amount);
await VE(transferBridge).fromBridge(
buildVTransferEvent({
receiver: e.aux.recipientAddress,
target: settlerAddr,
target: settlementAccount,
sourceChannel: agToNoble.transferChannel.counterPartyChannelId,
denom: 'uusdc',
amount: e.tx.amount,
Expand All @@ -155,7 +160,8 @@ const makeTestContext = async (t: ExecutionContext) => {
return { bridges: { snapshot, since }, common, evm, mint, startKit, sync };
};

const test = anyTest as TestFn<Awaited<ReturnType<typeof makeTestContext>>>;
type FucContext = Awaited<ReturnType<typeof makeTestContext>>;
const test = anyTest as TestFn<FucContext>;
test.before(async t => (t.context = await makeTestContext(t)));

test('baggage', async t => {
Expand Down Expand Up @@ -190,8 +196,8 @@ test('getStaticInfo', async t => {

t.deepEqual(await E(publicFacet).getStaticInfo(), {
addresses: {
poolAccount: 'agoric1fakeLCAAddress',
settlementAccount: 'agoric1fakeLCAAddress1',
poolAccount: makeTestAddress(),
settlementAccount: makeTestAddress(1),
},
});
});
Expand Down Expand Up @@ -346,7 +352,6 @@ const makeLP = async (
};

const makeEVM = (template = MockCctpTxEvidences.AGORIC_PLUS_OSMO()) => {
const [settleAddr] = template.aux.recipientAddress.split('?');
let nonce = 0;

const makeTx = (amount: bigint, recipientAddress: string): CctpTxEvidence => {
Expand Down Expand Up @@ -378,10 +383,6 @@ const makeCustomer = (
const feeTools = makeFeeTools(feeConfig);
const sent = [] as CctpTxEvidence[];

// TODO: get settlerAddr from vstorage
const [settleAddr] =
MockCctpTxEvidences.AGORIC_PLUS_OSMO().aux.recipientAddress.split('?');

const me = harden({
checkPoolAvailable: async (
t: ExecutionContext,
Expand All @@ -394,8 +395,17 @@ const makeCustomer = (
t.log(who, 'sees', poolBalance.value, enough ? '>' : 'NOT >', want);
return enough;
},
sendFast: async (t: ExecutionContext, amount: bigint, EUD: string) => {
const recipientAddress = encodeAddressHook(settleAddr, { EUD });
sendFast: async (
t: ExecutionContext<FucContext>,
amount: bigint,
EUD: string,
) => {
const { storage } = t.context.common.bootstrap;
const accountsData = storage.data.get('fun');
const { settlementAccount } = JSON.parse(
JSON.parse(accountsData!).values[0],
);
const recipientAddress = encodeAddressHook(settlementAccount, { EUD });
// KLUDGE: UI would ask noble for a forwardingAddress
// "cctp" here has some noble stuff mixed in.
const tx = cctp.makeTx(amount, recipientAddress);
Expand Down Expand Up @@ -559,7 +569,7 @@ test.serial('STORY01: advancing happy path for 100 USDC', async t => {
t.deepEqual(inspectBankBridge().at(-1), {
amount: String(expectedAdvance.value),
denom: uusdcOnAgoric,
recipient: 'agoric1fakeLCAAddress',
recipient: makeTestAddress(),
type: 'VBANK_GIVE',
});

Expand Down Expand Up @@ -791,7 +801,7 @@ test.serial('Settlement for unknown transaction (operator down)', async t => {
},
{
amount: '20000000',
recipient: 'agoric1fakeLCAAddress1',
recipient: 'agoric1qyqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqc09z0g',
type: 'VBANK_GIVE',
},
],
Expand Down
Loading

0 comments on commit e06f12a

Please sign in to comment.