Skip to content

Commit

Permalink
feat: have the bank use normal purses when not on chain
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed May 1, 2021
1 parent 04b004c commit 90ab888
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 37 deletions.
4 changes: 2 additions & 2 deletions packages/agoric-cli/lib/chain-config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import djson from 'deterministic-json';
import TOML from '@iarna/toml';

export const MINT_DENOM = 'urun';
export const MINT_DENOM = 'ubld';
export const STAKING_DENOM = 'ubld';
export const STAKING_MAX_VALIDATORS = 150;

export const GOV_DEPOSIT_COINS = [{ amount: '10000000', denom: MINT_DENOM }];
export const GOV_DEPOSIT_COINS = [{ amount: '1000000', denom: MINT_DENOM }];

// Can't beat the speed of light, we need 600ms round trip time for the other
// side of Earth, and multiple round trips per block.
Expand Down
4 changes: 2 additions & 2 deletions packages/cosmic-swingset/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ scenario2-setup-nobuild:
# Create the bootstrap account.
$(AGCH) --home=t1/bootstrap keys add bootstrap --keyring-backend=test
$(AGCH) --home=t1/bootstrap keys show -a bootstrap --keyring-backend=test > t1/bootstrap-address
$(AGCH) --home=t1/n0 add-genesis-account `cat t1/bootstrap-address` 100000000ubld,100provisionpass,100sendpacketpass
$(AGCH) --home=t1/n0 add-genesis-account `cat t1/bootstrap-address` 1000000000000000ubld,100provisionpass,100sendpacketpass
# Create the (singleton) chain node.
$(AGCH) --home=t1/n0 --keyring-dir=t1/bootstrap gentx --keyring-backend=test bootstrap 1000000ubld --chain-id=$(CHAIN_ID)
$(AGCH) --home=t1/n0 --keyring-dir=t1/bootstrap gentx --keyring-backend=test bootstrap 73000000ubld --chain-id=$(CHAIN_ID)
$(AGCH) --home=t1/n0 collect-gentxs
$(AGCH) --home=t1/n0 validate-genesis
../agoric-cli/bin/agoric set-defaults --export-metrics ag-chain-cosmos t1/n0/config
Expand Down
5 changes: 5 additions & 0 deletions packages/cosmic-swingset/src/sim-chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ export async function connectToFakeChain(basedir, GCI, delay, inbound) {
giveMeAllTheAgoricPowers: true,
hardcodedClientAddresses: [bootAddress],
noFakeCurrencies: process.env.NO_FAKE_CURRENCIES,
bootMsg: {
bootstrapAddress: 'agoric1simboot',
bootstrapValue: `${50000n * 10n ** 6n}`,
donationValue: `${5n * 10n ** 6n}`,
},
};
const stateDBdir = path.join(basedir, `fake-chain-${GCI}-state`);
function flushChainSends(replay) {
Expand Down
2 changes: 1 addition & 1 deletion packages/deployment/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import chalk from 'chalk';

export const ACCOUNT_JSON = `account.json`;
export const DEFAULT_BOOT_TOKENS = `10000000000000000000000000urun`;
export const DEFAULT_BOOT_TOKENS = `10000000000000000000000000ubld`;
export const PLAYBOOK_WRAPPER = `./ansible-playbook.sh`;
export const SSH_TYPE = 'ecdsa';

Expand Down
38 changes: 30 additions & 8 deletions packages/vats/src/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export function buildRootObject(vatPowers, vatParameters) {
zoe,
{ priceAuthority, adminFacet: priceAuthorityAdmin },
] = await Promise.all([
bridgeManager ? E(bankVat).makeBankManager(bridgeManager) : undefined,
E(bankVat).makeBankManager(bridgeManager),
E(vats.sharing).getSharingService(),
E(vats.registrar).getSharedRegistrar(),
E(vats.board).getBoard(),
Expand Down Expand Up @@ -161,7 +161,7 @@ export function buildRootObject(vatPowers, vatParameters) {
// We just transfer the bootstrapValue in central tokens to the low-level
// bootstrapAddress.
async function depositCentralBootstrapPayment() {
if (!bankManager || !bootstrapAddress || !bootstrapPaymentValue) {
if (!bootstrapAddress || !bootstrapPaymentValue) {
return;
}
await E(bankManager).addAsset(
Expand Down Expand Up @@ -218,7 +218,7 @@ export function buildRootObject(vatPowers, vatParameters) {
issuerName,
harden({ ...record, brand, issuer }),
);
if (!record.bankDenom || !record.bankPurse || !bankManager) {
if (!record.bankDenom || !record.bankPurse) {
return issuer;
}

Expand Down Expand Up @@ -466,12 +466,16 @@ export function buildRootObject(vatPowers, vatParameters) {
const mintIssuerNames = [];
const mintPurseNames = [];
const mintValues = [];
const payToBank = [];
issuerNames.forEach(issuerName => {
const record = issuerNameToRecord.get(issuerName);
if (!record.defaultPurses) {
return;
}
record.defaultPurses.forEach(([purseName, value]) => {
// Only pay to the bank if we don't have an actual bridge to the
// underlying chain (from which we'll get the assets).
payToBank.push(!bridgeManager && purseName === record.bankPurse);
mintIssuerNames.push(issuerName);
mintPurseNames.push(purseName);
mintValues.push(value);
Expand All @@ -482,17 +486,38 @@ export function buildRootObject(vatPowers, vatParameters) {
mintValues,
);

const paymentInfo = mintIssuerNames.map((issuerName, i) => ({
const allPayments = mintIssuerNames.map((issuerName, i) => ({
issuer: issuerNameToRecord.get(issuerName).issuer,
issuerPetname: issuerName,
payment: payments[i],
brand: issuerNameToRecord.get(issuerName).brand,
pursePetname: mintPurseNames[i],
}));

const bank = await E(bankManager).getBankForAddress(address);

// Separate out the purse-creating payments from the bank payments.
const faucetPaymentInfo = [];
await Promise.all(
allPayments.map(async (record, i) => {
if (!payToBank[i]) {
// Just a faucet payment to be claimed by a wallet.
faucetPaymentInfo.push(record);
return;
}
const { brand, payment } = record;

// Deposit the payment in the bank now.
assert(brand);
const purse = E(bank).getPurse(brand);
await E(purse).deposit(payment);
}),
);

const faucet = Far('faucet', {
// A method to reap the spoils of our on-chain provisioning.
async tapFaucet() {
return paymentInfo;
return faucetPaymentInfo;
},
});

Expand All @@ -512,9 +537,6 @@ export function buildRootObject(vatPowers, vatParameters) {
},
};

const bank = await (bankManager &&
E(bankManager).getBankForAddress(address));

/** @param {NatValue} value */
async function donateCentralFromBootstrap(value) {
if (!bank || !centralBootstrapPurse) {
Expand Down
1 change: 1 addition & 0 deletions packages/vats/src/issuers.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ const fromCosmosIssuerEntries = [
'BLD',
{
issuerArgs: [undefined, { decimalPlaces: 6 }],
defaultPurses: [['Agoric staking token', scaleMicro(62)]],
bankDenom: 'ubld',
bankPurse: 'Agoric staking token',
collateralConfig: {
Expand Down
70 changes: 46 additions & 24 deletions packages/vats/src/vat-bank.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,40 +85,54 @@ const makePurseController = (
export function buildRootObject(_vatPowers) {
return Far('bankMaker', {
/**
* @param {import('./bridge').BridgeManager} bridgeMgr
* @param {import('./bridge').BridgeManager} bridgeManager
*/
async makeBankManager(bridgeMgr) {
const bankCall = obj => E(bridgeMgr).toBridge('bank', obj);

async makeBankManager(bridgeManager) {
/** @type {Store<Brand, AssetRecord>} */
const brandToAssetRecord = makeStore('brand');

/** @type {Store<string, Store<string, (amount: any) => void>>} */
const denomToAddressUpdater = makeStore('denom');

const handler = Far('bankHandler', {
async fromBridge(_srcID, obj) {
switch (obj.type) {
case 'VPURSE_BALANCE_UPDATE': {
for (const update of obj.updated) {
try {
const { address, denom, amount: value } = update;
const addressToUpdater = denomToAddressUpdater.get(denom);
const updater = addressToUpdater.get(address);

updater(value);
} catch (e) {
console.error('Unregistered update', update);
/**
* @param {import('./bridge').BridgeManager} bridgeMgr
*/
async function getRemoteBankCall(bridgeMgr) {
if (!bridgeMgr) {
return undefined;
}

// We need to synchronise with the remote bank.
const handler = Far('bankHandler', {
async fromBridge(_srcID, obj) {
switch (obj.type) {
case 'VPURSE_BALANCE_UPDATE': {
for (const update of obj.updated) {
try {
const { address, denom, amount: value } = update;
const addressToUpdater = denomToAddressUpdater.get(denom);
const updater = addressToUpdater.get(address);

updater(value);
} catch (e) {
console.error('Unregistered update', update);
}
}
break;
}
break;
default:
assert.fail(X`Unrecognized request ${obj.type}`);
}
default:
assert.fail(X`Unrecognized request ${obj.type}`);
}
},
});
},
});

await E(bridgeMgr).register('bank', handler);

// We can only downcall to the bank if there exists a bridge manager.
return obj => E(bridgeMgr).toBridge('bank', obj);
}

await E(bridgeMgr).register('bank', handler);
const bankCall = await getRemoteBankCall(bridgeManager);

/**
* @typedef {Object} AssetDescriptor
Expand Down Expand Up @@ -203,7 +217,15 @@ export function buildRootObject(_vatPowers) {
if (brandToVPurse.has(brand)) {
return brandToVPurse.get(brand);
}

const assetRecord = brandToAssetRecord.get(brand);
if (!bankCall) {
// Just emulate with a real purse.
const purse = await E(assetRecord.issuer).makeEmptyPurse();
brandToVPurse.init(brand, purse);
return purse;
}

const addressToUpdater = denomToAddressUpdater.get(
assetRecord.denom,
);
Expand Down

0 comments on commit 90ab888

Please sign in to comment.