diff --git a/packages/agoric-cli/lib/chain-config.js b/packages/agoric-cli/lib/chain-config.js index d15b6436ed4..cc7a5bbe199 100644 --- a/packages/agoric-cli/lib/chain-config.js +++ b/packages/agoric-cli/lib/chain-config.js @@ -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); @@ -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: { @@ -78,12 +86,6 @@ 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. @@ -91,18 +93,38 @@ export function finishCosmosConfigs({ 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); diff --git a/packages/agoric-cli/lib/main.js b/packages/agoric-cli/lib/main.js index 41b579a2992..285779c1ee9 100644 --- a/packages/agoric-cli/lib/main.js +++ b/packages/agoric-cli/lib/main.js @@ -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'; @@ -79,6 +80,18 @@ const main = async (progname, rawArgs, powers) => { return subMain(initMain, ['init', project], opts); }); + program + .command('set-defaults ') + .description('update the configuration files for in ') + .option( + '--import-from ', + 'import the exported configuration from ', + ) + .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') diff --git a/packages/agoric-cli/lib/set-defaults.js b/packages/agoric-cli/lib/set-defaults.js new file mode 100644 index 00000000000..48a95afaf65 --- /dev/null +++ b/packages/agoric-cli/lib/set-defaults.js @@ -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(` 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), + ]); +} diff --git a/packages/agoric-cli/lib/start.js b/packages/agoric-cli/lib/start.js index 5ff03239eca..9ceb54de9f1 100644 --- a/packages/agoric-cli/lib/start.js +++ b/packages/agoric-cli/lib/start.js @@ -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'), @@ -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); };