Skip to content

Commit

Permalink
fixup! incoherent checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
erights committed Nov 14, 2024
1 parent 2d9ccc3 commit 8aaf3b9
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 61 deletions.
2 changes: 1 addition & 1 deletion packages/orchestration/src/typeGuards.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export const DenomInfoShape = harden({
});

/** @type {TypedPattern<DenomAmount>} */
export const DenomAmountShape = { denom: DenomShape, value: M.nat() };
export const DenomAmountShape = harden({ denom: DenomShape, value: M.nat() });

/** @type {TypedPattern<Amount<'nat'>>} */
export const AnyNatAmountShape = harden({
Expand Down
137 changes: 85 additions & 52 deletions packages/vats/src/orch-purse/mock-orch-chain.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Fail } from '@endo/errors';
import { Far } from '@endo/pass-style';
import { M } from '@endo/patterns';
import { provideLazy } from '@agoric/store';

Expand All @@ -16,76 +18,70 @@ import {
* @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 prepareMinOrchestrator = zone => {
const makeMinOrchAccount = zone.exoClass(
'MinOrchAccount',
MinOrchAccountI,
(chain, ledger) => {
const addrValue = `${ledger.size()}`;
return { chain, ledger, addrValue };
(chains, denoms, accounts, chain) => {
const acctAddrValue = `${accounts.size()}`;
return { chains, denoms, accounts, chain, acctAddrValue };
},
{
getAddress() {
const { chain, addrValue: value } = this.state;
const { chain, acctAddrValue } = this.state;
const { chainId } = chain.getChainInfo();
return harden({ chainId, value });
return harden({ chainId, value: acctAddrValue });
},
getBalances() {
const { ledger, addrValue } = this.state;
const { balances } = ledger.get(addrValue);
async getBalances() {
const { accounts, acctAddrValue } = this.state;
const { balances } = accounts.get(acctAddrValue);
return [...balances.values()];
},
getBalance(denom) {
const { ledger, addrValue } = this.state;
const { balances } = ledger.get(addrValue);
async getBalance(denom) {
const { accounts, acctAddrValue } = this.state;
const { balances } = accounts.get(acctAddrValue);
return balances.get(denom);
},
transfer(destAddr, denomAmount) {
async transfer(destAddr, denomAmount) {
const { denoms, accounts, acctAddrValue } = this.state;
const { balances } = accounts.get(acctAddrValue);

const { chainId: destChainId, value: destAcctAddrValue } = destAddr;
const { denom, value: deltaValue } = denomAmount;

const { value: myOldBalanceValue } = balances.get(denom);
myOldBalanceValue >= deltaValue ||
Fail`overdrawn ${myOldBalanceValue} - ${deltaValue}`;
const myNewBalanceValue = myOldBalanceValue - deltaValue;
},
},
);

const makeMinChain = zone.exoClass(
'MinChain',
MinChainI,
chainName => ({
(chains, denoms, chainName) => ({
chains,
denoms,
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'),
},
}),
}),
{
getChainInfo() {
async getChainInfo() {
const { chainId } = this.state;
return harden({ chainId });
},
makeAccount() {
const { ledger } = this.state;
async makeAccount() {
const { self } = this;
const account = makeMinOrchAccount(self, ledger);
const { value: addrValue } = account.getAddress();
ledger.init(
addrValue,
const { chains, denoms, chainId } = this.state;
const { accounts } = chains.get(chainId);

const account = makeMinOrchAccount(chains, denoms, accounts, self);
const { value: acctAddrValue } = account.getAddress();
accounts.init(
acctAddrValue,
harden({
account,
balances: zone.mapStore('balances', {
Expand All @@ -96,16 +92,19 @@ export const prepareMinOrchestrator = zone => {
);
return account;
},
getDenomInfo(denom) {
const { denoms } = this.state;
const { self } = this;
return provideLazy(denoms, denom, d => makeMinDenomInfo(self, d));
},
asAmount(denomAmount) {
async makeDenom(denom) {
const { self } = this;
const { denom, value } = denomAmount;
const { brand } = self.getDenomInfo(denom);
return AmountMath.make(brand, value);
const { denoms } = this.state;
// TODO make a full OrchIssuerKit
// denoms.init will error if denom is already taken
denoms.init(
denom,
harden({
brand: Far(`fake ${denom} brand`, {}),
chain: self,
}),
);
// TODO return orchIssuerKit
},
},
);
Expand All @@ -115,14 +114,48 @@ export const prepareMinOrchestrator = zone => {
MinOrchestratorI,
() => ({
chains: zone.mapStore('chains', {
keyShape: M.string(),
valueShape: MinChainShape,
keyShape: M.string(), // chainName === chainId
valueShape: {
chain: MinChainShape,
denoms: M.remotable('denoms'),
accounts: M.remotable('accounts'),
},
}),
denoms: zone.mapStore('denoms', {
keyShape: M.string(), // denom
valueShape: MinDenomInfoShape,
}),
}),
{
getChain(chainName) {
const { chains } = this.state;
return provideLazy(chains, chainName, makeMinChain);
const { chains, denoms } = this.state;
const { chain } = provideLazy(chains, chainName, cName => {
const ch = makeMinChain(chains, denoms, cName);
chains.init(
chainName,
harden({
ch,
accounts: zone.mapStore('accounts', {
keyShape: M.string(), // acctAddrValue
valueShape: {
account: MinOrchAccountShape,
balances: M.remotable('balances'),
},
}),
}),
);
});
return chain;
},
getDenomInfo(denom) {
const { denoms } = this.state;
return denoms.get(denom);
},
asAmount(denomAmount) {
const { self } = this;
const { denom, value } = denomAmount;
const { brand } = self.getDenomInfo(denom);
return AmountMath.make(brand, value);
},
},
);
Expand Down
9 changes: 5 additions & 4 deletions packages/vats/src/orch-purse/typeGuards.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const MinOrchestratorShape = M.remotable('MinOrchestrator');
*/
export const MinChainAcctAddrShape = harden({
chainId: M.string(),
value: M.string(),
value: M.string(), // acctAddrValue
});

/**
Expand Down Expand Up @@ -62,9 +62,8 @@ export const MinChainI = M.interface('MinChain', {
getChainInfo: M.call().returns(M.eref(ChainInfoShape)),
makeAccount: M.call().returns(M.eref(MinOrchAccountShape)),

// In the real API, these are on OrchestratorI
getDenomInfo: M.call(DenomShape).returns(MinDenomInfoShape),
asAmount: M.call(DenomAmountShape).returns(AmountShape),
// TODO returns(M.eref(OrchIssuerKitShape))
makeDenom: M.call(DenomShape).returns(M.eref(M.any())),
});

/**
Expand All @@ -73,6 +72,8 @@ export const MinChainI = M.interface('MinChain', {
*/
export const MinOrchestratorI = M.interface('MinOrchestrator', {
getChain: M.call(M.string()).returns(M.eref(MinChainShape)),
getDenomInfo: M.call(DenomShape).returns(MinDenomInfoShape),
asAmount: M.call(DenomAmountShape).returns(AmountShape),
});

// //////////////////////// ERTP-like //////////////////////////////////////////
Expand Down
8 changes: 4 additions & 4 deletions packages/vats/src/orch-purse/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type { Denom, DenomAmount, ChainAddress } from '@agoric/orchestration';
*/
export type MinChainAcctAddr = {
chainId: string;
value: string;
value: string; // acctAddrValue
};

/**
Expand Down Expand Up @@ -50,16 +50,16 @@ export type MinChain = {
getChainInfo(): ERef<MinChainInfo>;
makeAccount(): ERef<MinOrchAccount>;

// In the real API, these are on Orchestrator
getDenomInfo(denom: Denom): MinDenomInfo;
asAmount(denomAmount: DenomAmount): Amount;
makeDenom(denom: Denom): ERef<any>; // TODO OrchIssuerKit
};

/**
* @see {Orchestrator}
*/
export type MinOrchestrator = {
getChain(chainName: string): ERef<MinChain>;
getDenomInfo(denom: Denom): MinDenomInfo;
asAmount(denomAmount: DenomAmount): Amount;
};

// //////////////////////// ERTP-like //////////////////////////////////////////
Expand Down

0 comments on commit 8aaf3b9

Please sign in to comment.