Skip to content

Commit eabe493

Browse files
committed
feat: separate generation/writing of config.toml from genesis.json
1 parent c4d3cb0 commit eabe493

File tree

3 files changed

+113
-92
lines changed

3 files changed

+113
-92
lines changed

packages/agoric-cli/lib/chain-config.js

+61-67
Original file line numberDiff line numberDiff line change
@@ -9,83 +9,77 @@ export const BLOCK_CADENCE_S = 2;
99
export const ORIG_BLOCK_CADENCE_S = 5;
1010
export const ORIG_SIGNED_BLOCKS_WINDOW = 100;
1111

12-
// Rewrite the config.toml and genesis.json.
13-
export function finishCosmosConfigs({
14-
genesisJson,
12+
// Rewrite the config.toml.
13+
export function finishCosmosConfig({
1514
configToml,
16-
exportedGenesisJson,
1715
portNum = '26657',
1816
persistentPeers = '',
1917
}) {
2018
const rpcPort = Number(portNum);
21-
const ret = {};
22-
23-
if (genesisJson) {
24-
const genesis = JSON.parse(genesisJson);
25-
const exported = exportedGenesisJson ? JSON.parse(exportedGenesisJson) : {};
26-
27-
genesis.app_state.staking.params.bond_denom = STAKING_DENOM;
28-
29-
// We scale this parameter according to our own block cadence, so
30-
// that we tolerate the same downtime as the old genesis.
31-
genesis.app_state.slashing.params.signed_blocks_window = `${Math.ceil(
32-
(ORIG_BLOCK_CADENCE_S * ORIG_SIGNED_BLOCKS_WINDOW) / BLOCK_CADENCE_S,
33-
)}`;
34-
35-
// Zero inflation, for now.
36-
genesis.app_state.mint.minter.inflation = '0.0';
37-
genesis.app_state.mint.params.inflation_rate_change = '0.0';
38-
genesis.app_state.mint.params.inflation_min = '0.0';
39-
40-
// Set the denomination for different modules.
41-
genesis.app_state.mint.params.mint_denom = MINT_DENOM;
42-
genesis.app_state.crisis.constant_fee.denom = MINT_DENOM;
43-
genesis.app_state.gov.deposit_params.min_deposit = GOV_DEPOSIT_COINS;
44-
45-
// Reduce the cost of a transaction.
46-
genesis.app_state.auth.params.tx_size_cost_per_byte = '1';
47-
48-
// We upgrade from export data.
49-
const { app_state: exportedAppState = {} } = exported;
50-
for (const state of Object.keys(exportedAppState)) {
51-
genesis.app_state[state] = exportedAppState[state];
52-
}
53-
54-
// Remove IBC and capability state.
55-
// TODO: This needs much more support to preserve contract state
56-
// between exports in order to be able to carry forward IBC conns.
57-
delete genesis.app_state.capability;
58-
delete genesis.app_state.ibc;
59-
60-
// Use the same consensus_params.
61-
if ('consensus_params' in exported) {
62-
genesis.consensus_params = exported.consensus_params;
63-
}
64-
65-
// This is necessary until https://github.com/cosmos/cosmos-sdk/issues/6446 is closed.
66-
genesis.consensus_params.block.time_iota_ms = '1000';
67-
ret.newGenesisJson = djson.stringify(genesis);
68-
}
6919

70-
if (configToml) {
71-
// Adjust the config.toml.
72-
const config = TOML.parse(configToml);
73-
config.proxy_app = 'kvstore';
20+
// Adjust the config.toml.
21+
const config = TOML.parse(configToml);
22+
config.proxy_app = 'kvstore';
23+
24+
// Enforce our inter-block delays for this node.
25+
config.consensus.timeout_commit = `${BLOCK_CADENCE_S}s`;
26+
27+
// Update addresses in the config.
28+
config.p2p.laddr = `tcp://0.0.0.0:${rpcPort - 1}`;
29+
config.p2p.persistent_peers = persistentPeers;
30+
config.rpc.laddr = `tcp://127.0.0.1:${rpcPort}`;
31+
32+
// Needed for IBC.
33+
config.tx_index.index_all_keys = true;
34+
35+
// Stringify the new config.toml.
36+
return TOML.stringify(config);
37+
}
38+
39+
// Rewrite/import the genesis.json.
40+
export function finishCosmosGenesis({ genesisJson, exportedGenesisJson }) {
41+
const genesis = JSON.parse(genesisJson);
42+
const exported = exportedGenesisJson ? JSON.parse(exportedGenesisJson) : {};
43+
44+
genesis.app_state.staking.params.bond_denom = STAKING_DENOM;
45+
46+
// We scale this parameter according to our own block cadence, so
47+
// that we tolerate the same downtime as the old genesis.
48+
genesis.app_state.slashing.params.signed_blocks_window = `${Math.ceil(
49+
(ORIG_BLOCK_CADENCE_S * ORIG_SIGNED_BLOCKS_WINDOW) / BLOCK_CADENCE_S,
50+
)}`;
7451

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

78-
// Update addresses in the config.
79-
config.p2p.laddr = `tcp://0.0.0.0:${rpcPort - 1}`;
80-
config.p2p.persistent_peers = persistentPeers;
81-
config.rpc.laddr = `tcp://127.0.0.1:${rpcPort}`;
57+
// Set the denomination for different modules.
58+
genesis.app_state.mint.params.mint_denom = MINT_DENOM;
59+
genesis.app_state.crisis.constant_fee.denom = MINT_DENOM;
60+
genesis.app_state.gov.deposit_params.min_deposit = GOV_DEPOSIT_COINS;
61+
62+
// Reduce the cost of a transaction.
63+
genesis.app_state.auth.params.tx_size_cost_per_byte = '1';
64+
65+
// We upgrade from export data.
66+
const { app_state: exportedAppState = {} } = exported;
67+
for (const state of Object.keys(exportedAppState)) {
68+
genesis.app_state[state] = exportedAppState[state];
69+
}
8270

83-
// Needed for IBC.
84-
config.tx_index.index_all_keys = true;
71+
// Remove IBC and capability state.
72+
// TODO: This needs much more support to preserve contract state
73+
// between exports in order to be able to carry forward IBC conns.
74+
delete genesis.app_state.capability;
75+
delete genesis.app_state.ibc;
8576

86-
// Stringify the new config.toml.
87-
ret.newConfigToml = TOML.stringify(config);
77+
// Use the same consensus_params.
78+
if ('consensus_params' in exported) {
79+
genesis.consensus_params = exported.consensus_params;
8880
}
8981

90-
return ret;
82+
// This is necessary until https://github.com/cosmos/cosmos-sdk/issues/6446 is closed.
83+
genesis.consensus_params.block.time_iota_ms = '1000';
84+
return djson.stringify(genesis);
9185
}
+43-23
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { finishCosmosConfigs } from './chain-config';
1+
import { finishCosmosConfig, finishCosmosGenesis } from './chain-config';
22

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

77
const [prog, configDir] = rawArgs.slice(1);
@@ -10,31 +10,51 @@ export default async function setDefaultsMain(progname, rawArgs, powers, opts) {
1010
throw Error(`<prog> must currently be 'ag-chain-cosmos'`);
1111
}
1212

13-
log(`read ${prog} config from ${configDir}`);
14-
15-
const genesisFile = `${configDir}/genesis.json`;
16-
const configFile = `${configDir}/config.toml`;
17-
const { importFrom, persistentPeers } = opts;
18-
const [genesisJson, configToml, exportedGenesisJson] = await Promise.all([
19-
fs.readFile(genesisFile, 'utf-8'),
20-
fs.readFile(configFile, 'utf-8'),
21-
importFrom && fs.readFile(`${importFrom}/exported-genesis.json`, 'utf-8'),
22-
]);
23-
const { newGenesisJson, newConfigToml } = finishCosmosConfigs({
24-
genesisJson,
25-
configToml,
26-
exportedGenesisJson,
27-
persistentPeers,
28-
});
13+
let configFile;
14+
let genesisFile;
15+
if (path.basename(configDir) === 'config.toml') {
16+
configFile = configDir;
17+
}
18+
if (path.basename(configDir) === 'genesis.json') {
19+
genesisFile = configDir;
20+
}
21+
22+
if (!configFile && !genesisFile) {
23+
// Default behaviour: rewrite both configs.
24+
configFile = `${configDir}/config.toml`;
25+
genesisFile = `${configDir}/genesis.json`;
26+
}
2927

3028
const create = (fileName, contents) => {
3129
log('create', fileName);
3230
return fs.writeFile(fileName, contents);
3331
};
3432

35-
// Save all the files to disk.
36-
return Promise.all([
37-
create(configFile, newConfigToml),
38-
create(genesisFile, newGenesisJson),
39-
]);
33+
if (configFile) {
34+
log(`read ${configFile}`);
35+
const { persistentPeers } = opts;
36+
const configToml = await fs.readFile(configFile, 'utf-8');
37+
38+
const newConfigToml = finishCosmosConfig({
39+
configToml,
40+
persistentPeers,
41+
});
42+
await create(configFile, newConfigToml);
43+
}
44+
45+
if (genesisFile) {
46+
log(`read ${genesisFile}`);
47+
const { importFrom } = opts;
48+
const [genesisJson, exportedGenesisJson] = await Promise.all([
49+
fs.readFile(genesisFile, 'utf-8'),
50+
importFrom && fs.readFile(`${importFrom}/exported-genesis.json`, 'utf-8'),
51+
]);
52+
53+
const newGenesisJson = finishCosmosGenesis({
54+
genesisJson,
55+
exportedGenesisJson,
56+
});
57+
58+
await create(genesisFile, newGenesisJson);
59+
}
4060
}

packages/agoric-cli/lib/start.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ import path from 'path';
22
import chalk from 'chalk';
33
import { createHash } from 'crypto';
44

5-
import { STAKING_DENOM, MINT_DENOM, finishCosmosConfigs } from './chain-config';
5+
import {
6+
STAKING_DENOM,
7+
MINT_DENOM,
8+
finishCosmosConfig,
9+
finishCosmosGenesis,
10+
} from './chain-config';
611

712
const PROVISION_COINS = `100000000${STAKING_DENOM},100000000${MINT_DENOM},100provisionpass,100sendpacketpass`;
813
const DELEGATE0_COINS = `50000000${STAKING_DENOM}`;
@@ -302,8 +307,10 @@ export default async function startMain(progname, rawArgs, powers, opts) {
302307
fs.readFile(genesisFile, 'utf-8'),
303308
fs.readFile(configFile, 'utf-8'),
304309
]);
305-
const { newGenesisJson, newConfigToml } = finishCosmosConfigs({
310+
const newGenesisJson = finishCosmosGenesis({
306311
genesisJson,
312+
});
313+
const newConfigToml = finishCosmosConfig({
307314
configToml,
308315
portNum,
309316
});

0 commit comments

Comments
 (0)