Skip to content

Commit

Permalink
feat: bootnode mode (#97)
Browse files Browse the repository at this point in the history
* feat: bootnode mode

* feat: assign specified node to be bootnode

* fix: remove console log

* fix: eslink and test

* fix: adjust for pr comments
  • Loading branch information
LinXJ1204 authored Jan 17, 2024
1 parent b8f3b34 commit 3d58125
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 11 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,6 @@ src/command/test.ts


.scannerwork

# Pnpm lockfile
pnpm-lock.yaml
43 changes: 42 additions & 1 deletion src/quorum/command/network/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,47 @@ export const handler = async (argv: Arguments<OptType>) => {
},
], { onCancel })

const { isBootNode } = await prompts({
type: 'select',
name: 'isBootNode',
message: 'Using bootnode?',
choices: [
{
title: 'true',
value: true,
},
{
title: 'false',
value: false,
},
],
initial: 1,
})

const createNode = (type: string, index: number, offset = 0) => ({
title: `${type}${index}`,
value: `${index + offset}`,
})

const nodelist = [
...Array.from({ length: validatorNumber }, (_, i) => createNode('validator', i)),
...Array.from({ length: memberNumber }, (_, i) => createNode('member', i, validatorNumber)),
]

const bootNodeList: boolean[] = Array(validatorNumber + memberNumber).fill(false)
if (isBootNode) {
const isbootNodeList: any = await prompts({
type: 'multiselect',
name: 'isbootNodeList',
message: 'Choose bootnode',
choices: nodelist,
initial: '',
})
Object.values(isbootNodeList).flat().forEach((node: any) => {
bootNodeList[node] = true
})
}

const { walletOwner } = await prompts({
type: 'select',
name: 'walletOwner',
Expand Down Expand Up @@ -122,7 +163,7 @@ export const handler = async (argv: Arguments<OptType>) => {
amount: '1000000000000000000000000000',
}]

return { chainId, validatorNumber, memberNumber, alloc }
return { chainId, validatorNumber, memberNumber, alloc, isBootNode, bootNodeList }
} else {
const { address, privateKey } = wallet.createWalletAddress(WalletType.ETHEREUM)
return defaultNetworkConfig(address, privateKey)
Expand Down
2 changes: 2 additions & 0 deletions src/quorum/model/defaultNetworkConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export function defaultNetworkConfig (address: string, privateKey: string) {
account: address,
amount: '1000000000000000000000000000',
}],
isBootNode: false,
bootNodeList: [],
}

return networkConfig
Expand Down
2 changes: 2 additions & 0 deletions src/quorum/model/type/network.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export interface NetworkCreateType {
validatorNumber: number
memberNumber: number
alloc: AllocType[]
isBootNode: boolean
bootNodeList: boolean[]
}

export interface NetworkGenerateType {
Expand Down
4 changes: 2 additions & 2 deletions src/quorum/model/yaml/docker-compose/memberDockerCompose.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import DockerComposeYaml from './dockerComposeYaml'

class MemberDockerComposeYaml extends DockerComposeYaml {
public addMember (bdkPath: string, memberNum: number, rpcPort: number, chainId: number, peerPort: number) {
public addMember (bdkPath: string, memberNum: number, rpcPort: number, chainId: number, peerPort: number, bootnode: boolean, nodeEncode: string) {
this.addNetwork('quorum', {})
this.addService(`member${memberNum}`, {
image: 'quorumengineering/quorum:23.4.0',
Expand All @@ -20,7 +20,7 @@ class MemberDockerComposeYaml extends DockerComposeYaml {
volumes: [`${bdkPath}/member${memberNum}/data/:/data`],
entrypoint: [
'/bin/sh', '-c',
`geth init --datadir /data /data/genesis.json; geth --datadir /data --networkid ${chainId} --nodiscover --verbosity 3 --syncmode full --nousb --http --http.addr 0.0.0.0 --http.port 8545 --http.corsdomain "*" --http.vhosts "*" --ws --ws.addr 0.0.0.0 --ws.port 8546 --ws.origins "*" --http.api admin,trace,db,eth,debug,miner,net,shh,txpool,personal,web3,quorum,istanbul,qbft --ws.api admin,trace,db,eth,debug,miner,net,shh,txpool,personal,web3,quorum,istanbul,qbft --port ${peerPort}`,
`geth init --datadir /data /data/genesis.json; geth --datadir /data --networkid ${chainId} ${(bootnode) ? '--bootnodes '.concat(nodeEncode) : '--nodiscover'} --verbosity 3 --syncmode full --nousb --http --http.addr 0.0.0.0 --http.port 8545 --http.corsdomain "*" --http.vhosts "*" --ws --ws.addr 0.0.0.0 --ws.port 8546 --ws.origins "*" --http.api admin,trace,db,eth,debug,miner,net,shh,txpool,personal,web3,quorum,istanbul,qbft --ws.api admin,trace,db,eth,debug,miner,net,shh,txpool,personal,web3,quorum,istanbul,qbft --port ${peerPort}`,
],
})
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import DockerComposeYaml from './dockerComposeYaml'

class ValidatorDockerComposeYaml extends DockerComposeYaml {
public addValidator (bdkPath: string, validatorNum: number, rpcPort: number, chainId: number, peerPort: number) {
public addValidator (bdkPath: string, validatorNum: number, rpcPort: number, chainId: number, peerPort: number, bootnode: boolean, nodeEncode: string) {
this.addNetwork('quorum', {})
this.addService(`validator${validatorNum}`, {
image: 'quorumengineering/quorum:23.4.0',
Expand All @@ -20,7 +20,7 @@ class ValidatorDockerComposeYaml extends DockerComposeYaml {
volumes: [`${bdkPath}/validator${validatorNum}/data/:/data`],
entrypoint: [
'/bin/sh', '-c',
`geth init --datadir /data /data/genesis.json; geth --datadir /data --networkid ${chainId} --nodiscover --verbosity 3 --syncmode full --nousb --mine --miner.threads 1 --miner.gasprice 0 --emitcheckpoints --http --http.addr 0.0.0.0 --http.port 8545 --http.corsdomain "*" --http.vhosts "*" --ws --ws.addr 0.0.0.0 --ws.port 8546 --ws.origins "*" --http.api admin,trace,db,eth,debug,miner,net,shh,txpool,personal,web3,quorum,istanbul,qbft --ws.api admin,trace,db,eth,debug,miner,net,shh,txpool,personal,web3,quorum,istanbul,qbft --port ${peerPort} `,
`geth init --datadir /data /data/genesis.json; geth --datadir /data --networkid ${chainId} --verbosity 3 --syncmode full --nousb --mine --miner.threads 1 --miner.gasprice 0 --emitcheckpoints --http --http.addr 0.0.0.0 --http.port 8545 --http.corsdomain "*" ${(bootnode) ? '--bootnodes '.concat(nodeEncode) : '--nodiscover'} --http.vhosts "*" --ws --ws.addr 0.0.0.0 --ws.port 8546 --ws.origins "*" --http.api admin,trace,db,eth,debug,miner,net,shh,txpool,personal,web3,quorum,istanbul,qbft --ws.api admin,trace,db,eth,debug,miner,net,shh,txpool,personal,web3,quorum,istanbul,qbft --port ${peerPort} `,
],
})
}
Expand Down
17 changes: 11 additions & 6 deletions src/quorum/service/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export default class Network extends AbstractService {
this.bdkFile.copyPublicKeyToValidator(i)
this.bdkFile.copyAddressToValidator(i)

validatorDockerComposeYaml.addValidator(bdkPath, i, 8545 + i * 2, networkCreateConfig.chainId, 30303 + i)
validatorDockerComposeYaml.addValidator(bdkPath, i, 8545 + i * 2, networkCreateConfig.chainId, 30303 + i, networkCreateConfig.bootNodeList[i], staticNodesJson[i])
}
this.bdkFile.createValidatorDockerComposeYaml(validatorDockerComposeYaml)

Expand All @@ -90,7 +90,7 @@ export default class Network extends AbstractService {
this.bdkFile.copyPublicKeyToMember(i)
this.bdkFile.copyAddressToMember(i)

memberDockerComposeYaml.addMember(bdkPath, i, 8645 + i * 2, networkCreateConfig.chainId, 30403 + i)
memberDockerComposeYaml.addMember(bdkPath, i, 8645 + i * 2, networkCreateConfig.chainId, 30403 + i, networkCreateConfig.bootNodeList[networkCreateConfig.validatorNumber + i], staticNodesJson[networkCreateConfig.validatorNumber + i])
}
this.bdkFile.createMemberDockerComposeYaml(memberDockerComposeYaml)

Expand Down Expand Up @@ -125,7 +125,9 @@ export default class Network extends AbstractService {
this.bdkFile.copyPermissionedNodesJsonToValidator(nodeNum)

const validatorDockerComposeYaml = new ValidatorDockerComposeYaml()
validatorDockerComposeYaml.addValidator(bdkPath, nodeNum, 8545 + nodeNum * 2, joinNodeConfig.genesisJson.config.chainId, 30303 + nodeNum)

// TODO: add bootnode selection
validatorDockerComposeYaml.addValidator(bdkPath, nodeNum, 8545 + nodeNum * 2, joinNodeConfig.genesisJson.config.chainId, 30303 + nodeNum, false, '')
this.bdkFile.createValidatorDockerComposeYaml(validatorDockerComposeYaml)

await (new ValidatorInstance(this.config, this.infra).upOneService(`${joinNodeConfig.node}`))
Expand Down Expand Up @@ -156,7 +158,8 @@ export default class Network extends AbstractService {
this.bdkFile.copyPermissionedNodesJsonToMember(nodeNum)

const memberDockerComposeYaml = new MemberDockerComposeYaml()
memberDockerComposeYaml.addMember(bdkPath, nodeNum, 8645 + nodeNum * 2, joinNodeConfig.genesisJson.config.chainId, 30403 + nodeNum)
// TODO: add bootnode selection
memberDockerComposeYaml.addMember(bdkPath, nodeNum, 8645 + nodeNum * 2, joinNodeConfig.genesisJson.config.chainId, 30403 + nodeNum, false, '')
this.bdkFile.createMemberDockerComposeYaml(memberDockerComposeYaml)

await (new MemberInstance(this.config, this.infra).upOneService(`${joinNodeConfig.node}`))
Expand Down Expand Up @@ -285,7 +288,8 @@ export default class Network extends AbstractService {
for (let i = 0; i < validatorNum + 1; i += 1) {
this.bdkFile.copyStaticNodesJsonToValidator(i)
this.bdkFile.copyPermissionedNodesJsonToValidator(i)
validatorDockerComposeYaml.addValidator(this.bdkFile.getBdkPath(), i, 8545 + i * 2, chainId, 30303 + i)
// TODO: add bootnode selection
validatorDockerComposeYaml.addValidator(this.bdkFile.getBdkPath(), i, 8545 + i * 2, chainId, 30303 + i, false, '')
}
this.bdkFile.createValidatorDockerComposeYaml(validatorDockerComposeYaml)

Expand Down Expand Up @@ -353,7 +357,8 @@ export default class Network extends AbstractService {
for (let i = 0; i < memberCount + 1; i += 1) {
this.bdkFile.copyStaticNodesJsonToMember(i)
this.bdkFile.copyPermissionedNodesJsonToMember(i)
memberDockerComposeYaml.addMember(this.bdkFile.getBdkPath(), i, 8645 + i * 2, chainId, 30403 + i)
// TODO: add bootnode selection
memberDockerComposeYaml.addMember(this.bdkFile.getBdkPath(), i, 8645 + i * 2, chainId, 30403 + i, false, '')
}
this.bdkFile.createMemberDockerComposeYaml(memberDockerComposeYaml)

Expand Down
2 changes: 2 additions & 0 deletions test/quorum/service/backup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ describe('Quorum.Backup', function () {
validatorNumber: 1,
memberNumber: 1,
alloc: [],
isBootNode: false,
bootNodeList: [],
}
await network.create(networkCreate)
})
Expand Down
4 changes: 4 additions & 0 deletions test/quorum/service/network.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ describe.skip('Quorum.Network.Service', function () {
account: address,
amount: '1000000000000000000000000000',
}],
isBootNode: false,
bootNodeList: [false, false, false, false],
}

before(async () => {
Expand Down Expand Up @@ -85,6 +87,8 @@ describe.skip('Quorum.Network.Service', function () {
memberNumber: 0,
chainId: 1234,
alloc: [],
isBootNode: false,
bootNodeList: [false, false],
}
await network.create(onlyMemberConfig)
const upContainers = await docker.listContainers(dockerdOption)
Expand Down

0 comments on commit 3d58125

Please sign in to comment.