Skip to content

Commit

Permalink
feat(runner): Enable starting testnet follower from state-sync (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
mhofman authored Mar 15, 2023
1 parent d3759a6 commit 7b46e55
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 9 deletions.
6 changes: 6 additions & 0 deletions runner/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,9 @@ const main = async (progName, rawArgs, powers) => {
/** @type {string} */
let testnetOrigin;

/** @type {boolean | undefined} */
let useStateSync;

const profile = argv.profile == null ? 'local' : argv.profile;

switch (profile) {
Expand All @@ -291,6 +294,7 @@ const main = async (progName, rawArgs, powers) => {
case 'stage':
makeTasks = makeTestnetTasks;
testnetOrigin = argv.testnetOrigin || `https://${profile}.agoric.net`;
useStateSync = argv.useStateSync;
break;
default:
throw new Error(`Unexpected profile option: ${profile}`);
Expand Down Expand Up @@ -382,6 +386,7 @@ const main = async (progName, rawArgs, powers) => {
metadata: {
profile,
testnetOrigin,
useStateSync,
...envInfo,
testData: argv.testData,
},
Expand Down Expand Up @@ -961,6 +966,7 @@ const main = async (progName, rawArgs, powers) => {
chainOnly: globalChainOnly,
withMonitor,
testnetOrigin,
useStateSync,
};
logPerfEvent('setup-tasks-start', setupConfig);
({ chainStorageLocation, clientStorageLocation } =
Expand Down
1 change: 1 addition & 0 deletions runner/lib/stats/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ export interface StageStats extends StageStatsInitData {
export interface RunMetadata {
readonly profile: string;
readonly testnetOrigin?: string;
readonly useStateSync?: boolean | undefined;
readonly agChainCosmosVersion?: unknown;
readonly testData?: unknown;
}
Expand Down
58 changes: 49 additions & 9 deletions runner/lib/tasks/testnet.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export const makeTasks = ({
/** @type {Record<string, string>} */
const additionChainEnv = {};

/** @param {import("./types.js").TaskBaseOptions & {config?: {reset?: boolean, chainOnly?: boolean, withMonitor?: boolean, testnetOrigin?: string}}} options */
/** @param {import("./types.js").TaskBaseOptions & {config?: {reset?: boolean, chainOnly?: boolean, withMonitor?: boolean, testnetOrigin?: string, useStateSync?: boolean}}} options */
const setupTasks = async ({
stdout,
stderr,
Expand All @@ -135,6 +135,7 @@ export const makeTasks = ({
chainOnly,
withMonitor = true,
testnetOrigin: testnetOriginOption,
useStateSync,
} = {},
}) => {
const { console, stdio } = getConsoleAndStdio(
Expand Down Expand Up @@ -182,9 +183,6 @@ export const makeTasks = ({
const genesisPath = joinPath(chainStateDir, 'config', 'genesis.json');

if (!(await fsExists(genesisPath))) {
console.log('Fetching genesis');
const genesis = await fetchAsJSON(`${testnetOrigin}/genesis.json`);

await childProcessDone(
printerSpawn(
sdkBinaries.cosmosChain,
Expand All @@ -193,11 +191,6 @@ export const makeTasks = ({
),
);

fs.writeFile(
joinPath(chainStateDir, 'config', 'genesis.json'),
JSON.stringify(genesis),
);

await childProcessDone(
printerSpawn(
sdkBinaries.cosmosChain,
Expand Down Expand Up @@ -227,6 +220,53 @@ export const makeTasks = ({
configP2p.seeds = seeds.join(',');
configP2p.addr_book_strict = false;
delete config.log_level;

if (!useStateSync) {
console.log('Fetching genesis');
const genesis = await fetchAsJSON(`${testnetOrigin}/genesis.json`);

fs.writeFile(
joinPath(chainStateDir, 'config', 'genesis.json'),
JSON.stringify(genesis),
);
} else {
console.log('Fetching state-sync info');
/** @type {any} */
const currentBlockInfo = await fetchAsJSON(
`http://${rpcAddrs[0]}/block`,
);

// `trustHeight` is the block height considered as the "root of trust"
// for state-sync. The node will attempt to find a snapshot offered for
// a block at or after this height, and will validate that block's hash
// using a light client with the configured RPC servers.
// We want to use a block height recent enough, but for which a snapshot
// exists since then.
const stateSyncInterval =
Number(process.env.AG_SETUP_COSMOS_STATE_SYNC_INTERVAL) || 2000;
const trustHeight = Math.max(
1,
Number(currentBlockInfo.result.block.header.height) -
stateSyncInterval,
);

/** @type {any} */
const trustedBlockInfo = await fetchAsJSON(
`http://${rpcAddrs[0]}/block?height=${trustHeight}`,
);
const trustHash = trustedBlockInfo.result.block_id.hash;

const configStatesync = /** @type {import('@iarna/toml').JsonMap} */ (
config.statesync
);
configStatesync.enable = true;
configStatesync.rpc_servers = rpcAddrs
.map((host) => `http://${host}`)
.join(',');
configStatesync.trust_height = trustHeight;
configStatesync.trust_hash = trustHash;
}

await fs.writeFile(configPath, TOML.stringify(config));
}

Expand Down

0 comments on commit 7b46e55

Please sign in to comment.