Skip to content

Commit

Permalink
feat: separate generation/writing of config.toml from genesis.json
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Jul 19, 2020
1 parent c4d3cb0 commit eabe493
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 92 deletions.
128 changes: 61 additions & 67 deletions packages/agoric-cli/lib/chain-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,83 +9,77 @@ export const BLOCK_CADENCE_S = 2;
export const ORIG_BLOCK_CADENCE_S = 5;
export const ORIG_SIGNED_BLOCKS_WINDOW = 100;

// Rewrite the config.toml and genesis.json.
export function finishCosmosConfigs({
genesisJson,
// Rewrite the config.toml.
export function finishCosmosConfig({
configToml,
exportedGenesisJson,
portNum = '26657',
persistentPeers = '',
}) {
const rpcPort = Number(portNum);
const ret = {};

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

genesis.app_state.staking.params.bond_denom = STAKING_DENOM;

// We scale this parameter according to our own block cadence, so
// that we tolerate the same downtime as the old genesis.
genesis.app_state.slashing.params.signed_blocks_window = `${Math.ceil(
(ORIG_BLOCK_CADENCE_S * ORIG_SIGNED_BLOCKS_WINDOW) / BLOCK_CADENCE_S,
)}`;

// Zero inflation, for now.
genesis.app_state.mint.minter.inflation = '0.0';
genesis.app_state.mint.params.inflation_rate_change = '0.0';
genesis.app_state.mint.params.inflation_min = '0.0';

// Set the denomination for different modules.
genesis.app_state.mint.params.mint_denom = MINT_DENOM;
genesis.app_state.crisis.constant_fee.denom = MINT_DENOM;
genesis.app_state.gov.deposit_params.min_deposit = GOV_DEPOSIT_COINS;

// Reduce the cost of a transaction.
genesis.app_state.auth.params.tx_size_cost_per_byte = '1';

// We upgrade from export data.
const { app_state: exportedAppState = {} } = exported;
for (const state of Object.keys(exportedAppState)) {
genesis.app_state[state] = exportedAppState[state];
}

// Remove IBC and capability state.
// TODO: This needs much more support to preserve contract state
// between exports in order to be able to carry forward IBC conns.
delete genesis.app_state.capability;
delete genesis.app_state.ibc;

// Use the same consensus_params.
if ('consensus_params' in exported) {
genesis.consensus_params = exported.consensus_params;
}

// This is necessary until https://github.com/cosmos/cosmos-sdk/issues/6446 is closed.
genesis.consensus_params.block.time_iota_ms = '1000';
ret.newGenesisJson = djson.stringify(genesis);
}

if (configToml) {
// Adjust the config.toml.
const config = TOML.parse(configToml);
config.proxy_app = 'kvstore';
// Adjust the config.toml.
const config = TOML.parse(configToml);
config.proxy_app = 'kvstore';

// Enforce our inter-block delays for this node.
config.consensus.timeout_commit = `${BLOCK_CADENCE_S}s`;

// Update addresses in the config.
config.p2p.laddr = `tcp://0.0.0.0:${rpcPort - 1}`;
config.p2p.persistent_peers = persistentPeers;
config.rpc.laddr = `tcp://127.0.0.1:${rpcPort}`;

// Needed for IBC.
config.tx_index.index_all_keys = true;

// Stringify the new config.toml.
return TOML.stringify(config);
}

// Rewrite/import the genesis.json.
export function finishCosmosGenesis({ genesisJson, exportedGenesisJson }) {
const genesis = JSON.parse(genesisJson);
const exported = exportedGenesisJson ? JSON.parse(exportedGenesisJson) : {};

genesis.app_state.staking.params.bond_denom = STAKING_DENOM;

// We scale this parameter according to our own block cadence, so
// that we tolerate the same downtime as the old genesis.
genesis.app_state.slashing.params.signed_blocks_window = `${Math.ceil(
(ORIG_BLOCK_CADENCE_S * ORIG_SIGNED_BLOCKS_WINDOW) / BLOCK_CADENCE_S,
)}`;

// Enforce our inter-block delays for this node.
config.consensus.timeout_commit = `${BLOCK_CADENCE_S}s`;
// Zero inflation, for now.
genesis.app_state.mint.minter.inflation = '0.0';
genesis.app_state.mint.params.inflation_rate_change = '0.0';
genesis.app_state.mint.params.inflation_min = '0.0';

// Update addresses in the config.
config.p2p.laddr = `tcp://0.0.0.0:${rpcPort - 1}`;
config.p2p.persistent_peers = persistentPeers;
config.rpc.laddr = `tcp://127.0.0.1:${rpcPort}`;
// Set the denomination for different modules.
genesis.app_state.mint.params.mint_denom = MINT_DENOM;
genesis.app_state.crisis.constant_fee.denom = MINT_DENOM;
genesis.app_state.gov.deposit_params.min_deposit = GOV_DEPOSIT_COINS;

// Reduce the cost of a transaction.
genesis.app_state.auth.params.tx_size_cost_per_byte = '1';

// We upgrade from export data.
const { app_state: exportedAppState = {} } = exported;
for (const state of Object.keys(exportedAppState)) {
genesis.app_state[state] = exportedAppState[state];
}

// Needed for IBC.
config.tx_index.index_all_keys = true;
// Remove IBC and capability state.
// TODO: This needs much more support to preserve contract state
// between exports in order to be able to carry forward IBC conns.
delete genesis.app_state.capability;
delete genesis.app_state.ibc;

// Stringify the new config.toml.
ret.newConfigToml = TOML.stringify(config);
// Use the same consensus_params.
if ('consensus_params' in exported) {
genesis.consensus_params = exported.consensus_params;
}

return ret;
// This is necessary until https://github.com/cosmos/cosmos-sdk/issues/6446 is closed.
genesis.consensus_params.block.time_iota_ms = '1000';
return djson.stringify(genesis);
}
66 changes: 43 additions & 23 deletions packages/agoric-cli/lib/set-defaults.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { finishCosmosConfigs } from './chain-config';
import { finishCosmosConfig, finishCosmosGenesis } from './chain-config';

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

const [prog, configDir] = rawArgs.slice(1);
Expand All @@ -10,31 +10,51 @@ export default async function setDefaultsMain(progname, rawArgs, powers, opts) {
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, persistentPeers } = 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,
persistentPeers,
});
let configFile;
let genesisFile;
if (path.basename(configDir) === 'config.toml') {
configFile = configDir;
}
if (path.basename(configDir) === 'genesis.json') {
genesisFile = configDir;
}

if (!configFile && !genesisFile) {
// Default behaviour: rewrite both configs.
configFile = `${configDir}/config.toml`;
genesisFile = `${configDir}/genesis.json`;
}

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),
]);
if (configFile) {
log(`read ${configFile}`);
const { persistentPeers } = opts;
const configToml = await fs.readFile(configFile, 'utf-8');

const newConfigToml = finishCosmosConfig({
configToml,
persistentPeers,
});
await create(configFile, newConfigToml);
}

if (genesisFile) {
log(`read ${genesisFile}`);
const { importFrom } = opts;
const [genesisJson, exportedGenesisJson] = await Promise.all([
fs.readFile(genesisFile, 'utf-8'),
importFrom && fs.readFile(`${importFrom}/exported-genesis.json`, 'utf-8'),
]);

const newGenesisJson = finishCosmosGenesis({
genesisJson,
exportedGenesisJson,
});

await create(genesisFile, newGenesisJson);
}
}
11 changes: 9 additions & 2 deletions packages/agoric-cli/lib/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import path from 'path';
import chalk from 'chalk';
import { createHash } from 'crypto';

import { STAKING_DENOM, MINT_DENOM, finishCosmosConfigs } from './chain-config';
import {
STAKING_DENOM,
MINT_DENOM,
finishCosmosConfig,
finishCosmosGenesis,
} from './chain-config';

const PROVISION_COINS = `100000000${STAKING_DENOM},100000000${MINT_DENOM},100provisionpass,100sendpacketpass`;
const DELEGATE0_COINS = `50000000${STAKING_DENOM}`;
Expand Down Expand Up @@ -302,8 +307,10 @@ export default async function startMain(progname, rawArgs, powers, opts) {
fs.readFile(genesisFile, 'utf-8'),
fs.readFile(configFile, 'utf-8'),
]);
const { newGenesisJson, newConfigToml } = finishCosmosConfigs({
const newGenesisJson = finishCosmosGenesis({
genesisJson,
});
const newConfigToml = finishCosmosConfig({
configToml,
portNum,
});
Expand Down

0 comments on commit eabe493

Please sign in to comment.