Skip to content

Commit

Permalink
feat: add agoric set-defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Jul 19, 2020
1 parent cba5ae0 commit 98e5fe9
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 8 deletions.
36 changes: 29 additions & 7 deletions packages/agoric-cli/lib/chain-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@ export const STAKING_DENOM = 'uagstake';
export const GOV_DEPOSIT_COINS = [{ amount: '10000000', denom: MINT_DENOM }];
export const BLOCK_CADENCE_S = 2;

// TODO: When Tendermint non-zero height exports work, we can improve.
// TODO: When we export Agoric vat state as well, don't blacklist.
const EXPORTED_APP_STATE_BLACKLIST = ['capability', 'ibc'];

// Rewrite the config.toml and genesis.json.
export function finishCosmosConfigs({
genesisJson,
configToml,
exportedGenesisJson,
portNum = '26657',
persistentPeers = '',
}) {
const genesis = JSON.parse(genesisJson);
const config = TOML.parse(configToml);
Expand All @@ -28,6 +34,8 @@ export function finishCosmosConfigs({
const ORIG_BLOCK_CADENCE_S = match ? Number(match[1]) : 5;
const ORIG_SIGNED_BLOCKS_WINDOW = Number(signed_blocks_window);

const exported = exportedGenesisJson ? JSON.parse(exportedGenesisJson) : {};

const genesisMergePatch = {
app_state: {
staking: {
Expand Down Expand Up @@ -78,31 +86,45 @@ export function finishCosmosConfigs({
capability: null,
ibc: null,
},
consensus_params: {
block: {
// This is necessary until https://github.com/cosmos/cosmos-sdk/issues/6446 is closed.
time_iota_ms: '1000',
},
},
};

// The JSON merge patch for the config.toml.
const rpcPort = Number(portNum);
const configMergePatch = {
proxy_app: 'kvstore',
consensus: {
// Make blocks run faster than normal.
// Enforce our inter-block delays for this node.
timeout_commit: `${BLOCK_CADENCE_S}s`,
},
p2p: {
laddr: `tcp://0.0.0.0:${rpcPort - 1}`,
persistent_peers: persistentPeers,
},
rpc: {
laddr: `tcp://127.0.0.1:${rpcPort}`,
},
tx_index: {
// Needed for IBC.
index_all_keys: true,
},
};

const finishedGenesis = jsonmergepatch.apply(genesis, genesisMergePatch);

// We upgrade from export data, blacklisting states we don't support.
const { app_state: exportedAppState = {} } = exported;
for (const state in exportedAppState) {
if (!EXPORTED_APP_STATE_BLACKLIST.includes(state)) {
finishedGenesis.app_state[state] = exportedAppState[state];
}
}
if ('consensus_params' in exported) {
finishedGenesis.consensus_params = exported.consensus_params;
}

// This is necessary until https://github.com/cosmos/cosmos-sdk/issues/6446 is closed.
finishedGenesis.consensus_params.block.time_iota_ms = '1000';

const newGenesisJson = djson.stringify(finishedGenesis);

const finishedConfig = jsonmergepatch.apply(config, configMergePatch);
Expand Down
13 changes: 13 additions & 0 deletions packages/agoric-cli/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import cosmosMain from './cosmos';
import deployMain from './deploy';
import initMain from './init';
import installMain from './install';
import setDefaultsMain from './set-defaults';
import startMain from './start';

const DEFAULT_DAPP_TEMPLATE = 'dapp-encouragement';
Expand Down Expand Up @@ -79,6 +80,18 @@ const main = async (progname, rawArgs, powers) => {
return subMain(initMain, ['init', project], opts);
});

program
.command('set-defaults <program> <config-dir>')
.description('update the configuration files for <program> in <config-dir>')
.option(
'--import-from <dir>',
'import the exported configuration from <dir>',
)
.action(async (prog, configDir, cmd) => {
const opts = { ...program.opts(), ...cmd.opts() };
return subMain(setDefaultsMain, ['set-defaults', prog, configDir], opts);
});

program
.command('install')
.description('install Dapp dependencies')
Expand Down
39 changes: 39 additions & 0 deletions packages/agoric-cli/lib/set-defaults.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { finishCosmosConfigs } from './chain-config';

export default async function setDefaultsMain(progname, rawArgs, powers, opts) {
const { anylogger, fs } = powers;
const log = anylogger('agoric:set-defaults');

const [prog, configDir] = rawArgs.slice(1);

if (prog !== 'ag-chain-cosmos') {
throw Error(`<prog> must currently be 'ag-chain-cosmos'`);
}

log(`read ${prog} config from ${configDir}`);

const genesisFile = `${configDir}/genesis.json`;
const configFile = `${configDir}/config.toml`;
const { importFrom } = opts;
const [genesisJson, configToml, exportedGenesisJson] = await Promise.all([
fs.readFile(genesisFile, 'utf-8'),
fs.readFile(configFile, 'utf-8'),
importFrom && fs.readFile(`${importFrom}/exported-genesis.json`, 'utf-8'),
]);
const { newGenesisJson, newConfigToml } = finishCosmosConfigs({
genesisJson,
configToml,
exportedGenesisJson,
});

const create = (fileName, contents) => {
log('create', fileName);
return fs.writeFile(fileName, contents);
};

// Save all the files to disk.
return Promise.all([
create(configFile, newConfigToml),
create(genesisFile, newGenesisJson),
]);
}
3 changes: 2 additions & 1 deletion packages/agoric-cli/lib/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ export default async function startMain(progname, rawArgs, powers, opts) {
}

// Complete the genesis file and launch the chain.
log('read ag-chain-cosmos config');
const configFile = `${localAgServer}/config/config.toml`;
const [genesisJson, configToml] = await Promise.all([
fs.readFile(genesisFile, 'utf-8'),
Expand All @@ -308,7 +309,7 @@ export default async function startMain(progname, rawArgs, powers, opts) {
});

const create = (fileName, contents) => {
log('creating', fileName);
log('create', fileName);
return fs.writeFile(fileName, contents);
};

Expand Down

0 comments on commit 98e5fe9

Please sign in to comment.