Skip to content

Commit

Permalink
fixup! a lot
Browse files Browse the repository at this point in the history
  • Loading branch information
erights committed Nov 12, 2024
1 parent bbda682 commit 8a7a38f
Show file tree
Hide file tree
Showing 7 changed files with 247 additions and 165 deletions.
20 changes: 11 additions & 9 deletions packages/orchestration/src/typeGuards.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ import { M } from '@endo/patterns';

/**
* @import {TypedPattern} from '@agoric/internal';
* @import {ChainAddress, CosmosAssetInfo, Chain, ChainInfo, CosmosChainInfo, DenomAmount, DenomDetail, DenomInfo, AmountArg, CosmosValidatorAddress} from './types.js';
* @import {ChainAddress, CosmosAssetInfo, Chain, ChainInfo, CosmosChainInfo, DenomAmount, DenomInfo, AmountArg, CosmosValidatorAddress} from './types.js';
* @import {Any as Proto3Msg} from '@agoric/cosmic-proto/google/protobuf/any.js';
* @import {Delegation} from '@agoric/cosmic-proto/cosmos/staking/v1beta1/staking.js';
* @import {TxBody} from '@agoric/cosmic-proto/cosmos/tx/v1beta1/tx.js';
* @import {TypedJson} from '@agoric/cosmic-proto';
*/
Expand All @@ -26,17 +25,17 @@ export const OutboundConnectionHandlerI = M.interface(
);

/** @type {TypedPattern<ChainAddress>} */
export const ChainAddressShape = {
export const ChainAddressShape = harden({
chainId: M.string(),
encoding: M.string(),
value: M.string(),
};
});

/** @type {TypedPattern<Proto3Msg>} */
export const Proto3Shape = {
export const Proto3Shape = harden({
typeUrl: M.string(),
value: M.string(),
};
});

/** @internal */
export const IBCTransferOptionsShape = M.splitRecord(
Expand Down Expand Up @@ -109,12 +108,12 @@ export const ChainInfoShape = M.splitRecord({
export const DenomShape = M.string();

/** @type {TypedPattern<DenomInfo<any, any>>} */
export const DenomInfoShape = {
export const DenomInfoShape = harden({
chain: M.remotable('Chain'),
base: M.remotable('Chain'),
brand: M.or(M.remotable('Brand'), M.undefined()),
baseDenom: M.string(),
};
});

/** @type {TypedPattern<DenomAmount>} */
export const DenomAmountShape = { denom: DenomShape, value: M.bigint() };
Expand Down Expand Up @@ -164,7 +163,10 @@ export const chainFacadeMethods = harden({
* `seconds` is a big integer but since it goes through JSON it is encoded as
* string
*/
export const TimestampProtoShape = { seconds: M.string(), nanos: M.number() };
export const TimestampProtoShape = harden({
seconds: M.string(),
nanos: M.number(),
});

/**
* see {@link TxBody} for more details
Expand Down
1 change: 1 addition & 0 deletions packages/vats/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"@agoric/internal": "^0.3.2",
"@agoric/network": "^0.1.0",
"@agoric/notifier": "^0.6.2",
"@agoric/orchestration": "^0.1.0",
"@agoric/store": "^0.9.2",
"@agoric/swingset-vat": "^0.32.2",
"@agoric/time": "^0.3.2",
Expand Down
59 changes: 3 additions & 56 deletions packages/vats/src/orch-purse/issuerKit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import { assert, Fail } from '@endo/errors';
import { assertPattern } from '@endo/patterns';
import { makeScalarBigMapStore } from '@agoric/vat-data';
import { makeDurableZone } from '@agoric/zone/durable.js';

import { AssetKind, assertAssetKind } from '@agoric/ertp';
Expand Down Expand Up @@ -109,7 +108,7 @@ const RECOVERY_SETS_STATE = 'recoverySetsState';
* and optional. See `RecoverySetsOption` for defaulting behavior.
* @returns {IssuerKit<K>}
*/
export const upgradeIssuerKit = (
const upgradeIssuerKit = (
issuerBaggage,
optShutdownWithFailure = undefined,
recoverySetsOption = undefined,
Expand Down Expand Up @@ -143,7 +142,7 @@ harden(upgradeIssuerKit);
*
* @param {import('@agoric/vat-data').Baggage} baggage
*/
export const hasIssuer = baggage => baggage.has(INSTANCE_KEY);
const hasIssuer = baggage => baggage.has(INSTANCE_KEY);

/**
* `elementShape`, may only be present for collection-style amounts. If present,
Expand Down Expand Up @@ -195,7 +194,7 @@ export const hasIssuer = baggage => baggage.has(INSTANCE_KEY);
* @param {IssuerOptionsRecord} [options]
* @returns {IssuerKit<K>}
*/
export const makeDurableIssuerKit = (
const makeDurableIssuerKit = (
issuerBaggage,
name,
// @ts-expect-error K could be instantiated with a different subtype of AssetKind
Expand Down Expand Up @@ -292,55 +291,3 @@ export const prepareIssuerKit = (
}
};
harden(prepareIssuerKit);

/**
* Used _only_ to make a new issuerKit that is effectively non-durable. This is
* currently done by making a durable one in a baggage not reachable from
* anywhere. TODO Once rebuilt on zones, this should instead just build on the
* virtual zone. See https://github.com/Agoric/agoric-sdk/pull/7116
*
* Currently used for testing only. Should probably continue to be used for
* testing only.
*
* @template {AssetKind} [K='nat'] The name becomes part of the brand in asset
* descriptions. The name is useful for debugging and double-checking
* assumptions, but should not be trusted wrt any external namespace. For
* example, anyone could create a new issuer kit with name 'BTC', but it is
* not bitcoin or even related. It is only the name according to that issuer
* and brand.
*
* The assetKind will be used to import a specific mathHelpers from the
* mathHelpers library. For example, natMathHelpers, the default, is used for
* basic fungible tokens.
*
* `displayInfo` gives information to the UI on how to display the amount.
* @param {string} name
* @param {K} [assetKind]
* @param {AdditionalDisplayInfo} [displayInfo]
* @param {ShutdownWithFailure} [optShutdownWithFailure] If this issuer fails in
* the middle of an atomic action (which btw should never happen), it
* potentially leaves its ledger in a corrupted state. If this function was
* provided, then the failed atomic action will call it, so that some larger
* unit of computation, like the enclosing vat, can be shutdown before
* anything else is corrupted by that corrupted state. See
* https://github.com/Agoric/agoric-sdk/issues/3434
* @param {IssuerOptionsRecord} [options]
* @returns {IssuerKit<K, any>}
*/
export const makeIssuerKit = (
name,
// @ts-expect-error K could be instantiated with a different subtype of AssetKind
assetKind = AssetKind.NAT,
displayInfo = harden({}),
optShutdownWithFailure = undefined,
{ elementShape = undefined, recoverySetsOption = undefined } = {},
) =>
makeDurableIssuerKit(
makeScalarBigMapStore('dropped issuer kit', { durable: true }),
name,
assetKind,
displayInfo,
optShutdownWithFailure,
{ elementShape, recoverySetsOption },
);
harden(makeIssuerKit);
160 changes: 107 additions & 53 deletions packages/vats/src/orch-purse/mock-orch-chain.js
Original file line number Diff line number Diff line change
@@ -1,77 +1,131 @@
import { M } from '@endo/patterns';
import { provideLazy } from '@agoric/store';

import { AmountMath, AmountShape } from '@agoric/ertp';

import { AmountMath } from '@agoric/ertp';
import { DenomAmountShape } from '@agoric/orchestration';
import {
MinOrchAccountAddressI,
MinChainI,
MinChainShape,
MinDenomInfoShape,
MinOrchAccountI,
MinOrchChainI,
MinOrchAccountShape,
MinOrchestratorI,
} from './typeGuards.js';

/**
* @import {Zone} from '@agoric/zone'
*
* @import {MinOrchAccountAddress} from './types.js'
* @import {Zone} from '@agoric/base-zone'
*/

const makeMinDenomInfo = (chain, _denom) => {
const brand = {}; // TODO the whole thing
return harden({
brand,
chain,
});
};

/**
* @param {Zone} zone
*/
export const prepareMockOrchChain = zone => {
let amp;
const makeAccountKit = zone.exoClassKit(
'MockOrchAccount',
{
account: MinOrchAccountI,
address: MinOrchAccountAddressI,
incrFacet: M.interface('Incr', {
incr: M.call(AmountShape).returns(),
}),
export const prepareMinOrchestrator = zone => {
const makeMinOrchAccount = zone.exoClass(
'MinOrchAccount',
MinOrchAccountI,
(chain, ledger) => {
const addrValue = `${ledger.size()}`;
return { chain, ledger, addrValue };
},
brand => ({
fullBalance: AmountMath.makeEmpty(brand),
}),
{
account: {
async getFullBalance() {
return this.state.fullBalance;
},
/**
* @param {MinOrchAccountAddress} dest
* @param {Amount} depositAmount
*/
async transfer(dest, depositAmount) {
const destIncrFacet = amp(dest).incrFacet;
this.state.fullBalance = AmountMath.subtract(
this.state.fullBalance,
depositAmount,
);
destIncrFacet.incr(depositAmount);
},
getAddress() {
return this.facets.address;
},
getAddress() {
const { chain, addrValue: value } = this.state;
const { chainId } = chain.getChainInfo();
return harden({ chainId, value });
},
address: {},
incrFacet: {
incr(amount) {
this.state.fullBalance = AmountMath.add(
this.state.fullBalance,
amount,
);
},
getBalances() {
const { ledger, addrValue } = this.state;
const { balances } = ledger.get(addrValue);
return [...balances.values()];
},
getBalance(denom) {
const { ledger, addrValue } = this.state;
const { balances } = ledger.get(addrValue);
return balances.get(denom);
},
transfer(destAddr, denomAmount) {

},
},
);

const makeMinChain = zone.exoClass(
'MinChain',
MinChainI,
chainName => ({
chainId: chainName,
denoms: zone.mapStore('denoms', {
keyShape: M.string(), // denom
valueShape: MinDenomInfoShape,
}),
ledger: zone.mapStore('accounts', {
keyShape: M.string(), // addrValue
valueShape: {
account: MinOrchAccountShape,
balances: M.remotable('balances'),
},
}),
}),
{
receiveAmplifier(a) {
amp = a;
getChainInfo() {
const { chainId } = this.state;
return harden({ chainId });
},
makeAccount() {
const { ledger } = this.state;
const { self } = this;
const account = makeMinOrchAccount(self, ledger);
const { value: addrValue } = account.getAddress();
ledger.init(
addrValue,
harden({
account,
balances: zone.mapStore('balances', {
keyShape: M.string(), // denom
valueShape: DenomAmountShape,
}),
}),
);
return account;
},
getDenomInfo(denom) {
const { denoms } = this.state;
const { self } = this;
return provideLazy(denoms, denom, d => makeMinDenomInfo(self, d));
},
asAmount(denomAmount) {
const { self } = this;
const { denom, value } = denomAmount;
const { brand } = self.getDenomInfo(denom);
return AmountMath.make(brand, value);
},
},
);

return zone.exo('MockOrchChain', MinOrchChainI, {
makeAccount(brand) {
return Promise.resolve(makeAccountKit(brand).account);
const makeMinOrchestrator = zone.exoClass(
'MinOrchestrator',
MinOrchestratorI,
() => ({
chains: zone.mapStore('chains', {
keyShape: M.string(),
valueShape: MinChainShape,
}),
}),
{
getChain(chainName) {
const { chains } = this.state;
return provideLazy(chains, chainName, makeMinChain);
},
},
});
);
return makeMinOrchestrator;
};
harden(prepareMinOrchestrator);
8 changes: 6 additions & 2 deletions packages/vats/src/orch-purse/purse.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ import { makeTransientNotifierKit } from '@agoric/ertp/src/transientNotifier.js'

/**
* @import {AssetKind, RecoverySetsOption, Brand, Payment} from '@agoric/ertp'
*
* @import {MinOrchChain} from './types.js'
*/

const EMPTY_COPY_SET = makeCopySet([]);

// TODO Type InterfaceGuard better than InterfaceGuard<any>
/**
* @param {import('@agoric/zone').Zone} issuerZone
* @param {MinOrchChain} orchChain
* @param {string} name
* @param {AssetKind} assetKind
* @param {Brand} brand
Expand All @@ -30,6 +33,7 @@ const EMPTY_COPY_SET = makeCopySet([]);
*/
export const prepareOrchPurseKind = (
issuerZone,
orchChain,
name,
assetKind,
brand,
Expand Down Expand Up @@ -84,13 +88,13 @@ export const prepareOrchPurseKind = (
`${name} OrchPurse`,
OrchPurseIKit,
() => {
const currentBalance = AmountMath.makeEmpty(brand, assetKind);
const orchAccount = orchChain.makeAccount(brand, assetKind);

/** @type {SetStore<Payment>} */
const recoverySet = issuerZone.detached().setStore('recovery set');

return {
currentBalance,
orchAccount,
recoverySet,
};
},
Expand Down
Loading

0 comments on commit 8a7a38f

Please sign in to comment.