-
Notifications
You must be signed in to change notification settings - Fork 98
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Simplify local boot and enable multi node #1751
Merged
Merged
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7485004
launch WIP taking out the functions to work with multiple nodes
sabau a6947d9
init and boot are now split, still buggy
sabau 006cfe1
Fixed cliHome and number of nodes counter
sabau 1806dde
N validators created, we are missing the main account and the side ac…
sabau ae3b1d6
localized data into project folder, to help with automation of many n…
sabau 5180a13
Merge branch 'develop' into sabau/1661-multinode-local-testnet
sabau 9daac58
update spec file for localized root
sabau f543966
Merge remote-tracking branch 'origin/sabau/1661-multinode-local-testn…
sabau File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,90 +1,15 @@ | ||
"use strict" | ||
|
||
const fs = require(`fs-extra`) | ||
const buildLocalNode = require(`./helper`).buildLocalNode | ||
const { cli } = require(`@nodeguy/cli`) | ||
const path = require(`path`) | ||
const homeDir = require(`os`).homedir() | ||
const appDir = path.resolve(__dirname + `/../../../`) | ||
|
||
let { | ||
initNode, | ||
createKey, | ||
initGenesis, | ||
makeExec, | ||
nodeBinary | ||
} = require(`../../gaia.js`) | ||
const appDir = path.resolve(`${__dirname}/../../`) | ||
const buildTestnetPath = `${appDir}builds/testnets` | ||
|
||
const optionsSpecification = { | ||
overwrite: [`overwrite ~/.gaiad-testnet/`, false], | ||
password: [`custom password, default is 1234567890`, 1234567890] | ||
password: [`custom password, default is 1234567890`, 1234567890], | ||
nodes: [`number of validators in the network`, 1] | ||
} | ||
|
||
cli(optionsSpecification, async options => { | ||
try { | ||
// remove existing config | ||
if (options.overwrite) { | ||
if (fs.existsSync(appDir + `/builds/testnets/local-testnet`)) { | ||
await makeExec(`rm -r builds/testnets/local-testnet`) | ||
} | ||
if (fs.existsSync(homeDir + `/.gaiad-testnet`)) { | ||
await makeExec(`rm -r ~/.gaiad-testnet`) | ||
} | ||
if (fs.existsSync(homeDir + `/.cosmos-voyager-dev/local-testnet`)) { | ||
await makeExec(`rm -r ~/.cosmos-voyager-dev/local-testnet`) | ||
} | ||
} | ||
|
||
const chainId = `local-testnet` | ||
const moniker = `local` | ||
const clientHome = `./builds/testnets/local-testnet/lcd` | ||
const nodeHome = `${homeDir}/.gaiad-testnet` | ||
const defaultAccountInfo = { | ||
keyName: `local`, | ||
password: options.password, | ||
clientHomeDir: clientHome | ||
} | ||
await initNode( | ||
chainId, | ||
moniker, | ||
`${homeDir}/.gaiad-testnet`, | ||
options.password, | ||
options.overwrite | ||
) | ||
const { address } = await createKey(defaultAccountInfo) | ||
await initGenesis(defaultAccountInfo, address, nodeHome) | ||
await moveFiles() | ||
console.log(`\n 🎉 SUCCESS 🎉\n`) | ||
console.log( | ||
`To start Voyager with a local node please run: | ||
yarn start local-testnet | ||
|
||
Default account: | ||
username: '${defaultAccountInfo.keyName}' | ||
password: '${defaultAccountInfo.password}' | ||
` | ||
) | ||
} catch (error) { | ||
console.log(`Encountered an Error:`) | ||
console.error(error.msg ? error : error.toString()) | ||
} | ||
}) | ||
|
||
async function moveFiles() { | ||
fs.ensureDirSync(`builds/testnets/local-testnet`) | ||
|
||
await makeExec( | ||
`cp ~/.gaiad-testnet/config/{genesis.json,config.toml} builds/testnets/local-testnet/` | ||
) | ||
|
||
await makeExec( | ||
`sed -i.bak 's/seeds = ""/seeds = "localhost"/g' ./builds/testnets/local-testnet/config.toml` | ||
) | ||
|
||
await makeExec( | ||
`sed -i.bak 's/index_all_tags = false/index_all_tags = true/g' ${homeDir}/.gaiad-testnet/config/config.toml` | ||
) | ||
|
||
await makeExec( | ||
`${nodeBinary} version > ./builds/testnets/local-testnet/gaiaversion.txt` | ||
) | ||
} | ||
cli(optionsSpecification, buildLocalNode(buildTestnetPath)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
"use strict" | ||
|
||
const fs = require(`fs-extra`) | ||
const path = require(`path`) | ||
const startLocalNode = require(`../../gaia`).startLocalNode | ||
const getNodeId = require(`../../gaia`).getNodeId | ||
const makeValidator = require(`../../gaia`).makeValidator | ||
|
||
let { | ||
initNode, | ||
createKey, | ||
initGenesis, | ||
makeExec, | ||
nodeBinary | ||
} = require(`../../gaia.js`) | ||
|
||
const buildLocalNode = buildTestnetPath => async options => { | ||
try { | ||
const chainId = `local-testnet` | ||
const moniker = `local` | ||
const localTestnetPath = `${buildTestnetPath}/${chainId}` | ||
// remove existing config | ||
if (options.overwrite) { | ||
fs.removeSync(localTestnetPath) | ||
} | ||
|
||
const clientHome = `./${localTestnetPath}/main-node-cli/lcd` | ||
const nodeHome = `${localTestnetPath}/main-node-home` | ||
const defaultAccountInfo = { | ||
keyName: `local`, | ||
password: options.password, | ||
clientHomeDir: clientHome | ||
} | ||
await initNode( | ||
chainId, | ||
moniker, | ||
nodeHome, | ||
options.password, | ||
options.overwrite | ||
) | ||
const { address } = await createKey(defaultAccountInfo) | ||
await initGenesis(defaultAccountInfo, address, nodeHome) | ||
await makeExec( | ||
`sed -i.bak 's/seeds = ""/seeds = "localhost"/g' ${nodeHome}/config.toml` | ||
) | ||
saveVersion(nodeHome) | ||
console.log(`\n 🎉 SUCCESS 🎉\n`) | ||
console.log( | ||
`To start Voyager with a local node please run: | ||
yarn start local-testnet | ||
|
||
Default account: | ||
username: '${defaultAccountInfo.keyName}' | ||
password: '${defaultAccountInfo.password}' | ||
` | ||
) | ||
} catch (error) { | ||
console.log(`Encountered an Error:`) | ||
console.error(error.msg ? error : error.toString()) | ||
} | ||
} | ||
|
||
// save the version of the currently used gaia into the newly created network config folder | ||
const saveVersion = nodeHome => { | ||
const versionPath = path.join(nodeHome, `config`) | ||
let versionFilePath = path.join(versionPath, `gaiaversion.txt`) // nodeHome/config is used to copy created config files from | ||
return makeExec( | ||
`mkdir -p ${versionPath} && ${nodeBinary} version > ${versionFilePath}` | ||
) | ||
} | ||
|
||
// nodes[0] is a placeholder just to be aligned with the enumeration used in gaia | ||
// TODO: next PR refactor also gaia to simplify numeration | ||
const startNodes = async (nodes, mainAccountSignInfo) => { | ||
for (let i = 1; i < nodes.length; i++) { | ||
// start secondary nodes and connect to node one | ||
// wait until all nodes are showing blocks, so we know they are running | ||
await startLocalNode(nodes[i].home, i, i > 1 ? nodes[1].id : ``) | ||
// make our secondary nodes also to validators | ||
i > 1 && | ||
(await makeValidator( | ||
mainAccountSignInfo, | ||
nodes[i].home, | ||
nodes[i].cliHome, | ||
`local_${i}` | ||
)) | ||
} | ||
} | ||
|
||
const buildNodes = async (targetDir, chainId, numberNodes = 1) => { | ||
const cliHomePrefix = path.join(targetDir, `cli_home`) | ||
const nodeHomePrefix = path.join(targetDir, `node_home`) | ||
|
||
fs.removeSync(targetDir) | ||
|
||
// create address to delegate staking tokens to 2nd and 3rd validator | ||
let mainAccountSignInfo = undefined | ||
let genesis = undefined | ||
|
||
const nodes = [{ id: `dummy` }] | ||
for (let i = 1; i < numberNodes; i++) { | ||
// setup additional nodes | ||
const home = `${nodeHomePrefix}_${i}` | ||
nodes.push({ | ||
home, | ||
cliHome: `${cliHomePrefix}_${i}`, | ||
id: await setupLocalNode(home, i, chainId, true, genesis) | ||
}) | ||
if (i === 1) { | ||
await saveVersion(home) | ||
mainAccountSignInfo = { | ||
keyName: `testkey`, | ||
password: `1234567890`, | ||
clientHomeDir: home | ||
} | ||
let { address } = await createKey(mainAccountSignInfo) | ||
genesis = await initGenesis(mainAccountSignInfo, address, home) | ||
} | ||
} | ||
|
||
return { nodes, mainAccountSignInfo, genesis, cliHomePrefix } | ||
} | ||
|
||
// init a node and define it as a validator | ||
async function setupLocalNode( | ||
nodeHome, | ||
number = 1, | ||
chainId = `local-testnet`, | ||
isTest = false, | ||
mainGenesis = undefined | ||
) { | ||
await initNode(chainId, `local_${number}`, nodeHome, `1234567890`, true) | ||
mainGenesis && | ||
(await fs.writeJSON( | ||
path.join(nodeHome, `config`, `genesis.json`), | ||
mainGenesis | ||
)) | ||
isTest && (await reduceTimeouts(nodeHome)) | ||
|
||
return await getNodeId(nodeHome) | ||
} | ||
|
||
// declare candidacy for node | ||
|
||
function reduceTimeouts(nodeHome, strictAddressbook = false) { | ||
const configPath = path.join(nodeHome, `config`, `config.toml`) | ||
let configToml = fs.readFileSync(configPath, `utf8`) | ||
|
||
const timeouts = [ | ||
`timeout_propose`, | ||
`timeout_propose_delta`, | ||
`timeout_prevote`, | ||
`timeout_prevote_delta`, | ||
`timeout_precommit`, | ||
`timeout_precommit_delta`, | ||
`timeout_commit`, | ||
`flush_throttle_timeout` | ||
] | ||
const updatedConfigToml = configToml | ||
.split(`\n`) | ||
.map(line => { | ||
let [key, value] = line.split(` = `) | ||
|
||
if (key === `addr_book_strict`) { | ||
return `${key} = ${strictAddressbook ? `true` : `false`}` | ||
} | ||
|
||
if (!timeouts.includes(key)) { | ||
return line | ||
} | ||
|
||
// timeouts are in the format "100ms" or "5s" | ||
value = value.replace(/"/g, ``) | ||
if (value.trim().endsWith(`ms`)) { | ||
value = parseInt(value.trim().substr(0, value.length - 2)) | ||
} else if (value.trim().endsWith(`s`)) { | ||
value = parseInt(value.trim().substr(0, value.length - 1)) * 1000 | ||
} | ||
|
||
return `${key} = "${value / 10}ms"` | ||
}) | ||
.join(`\n`) | ||
|
||
fs.writeFileSync(configPath, updatedConfigToml, `utf8`) | ||
} | ||
|
||
module.exports = { | ||
buildLocalNode, | ||
buildNodes, | ||
saveVersion, | ||
startNodes | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍