Skip to content

Commit

Permalink
feat: optimize create network process
Browse files Browse the repository at this point in the history
  • Loading branch information
kidneyweakx committed Jan 29, 2024
1 parent 3d58125 commit e71d7af
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 76 deletions.
9 changes: 7 additions & 2 deletions src/quorum/command/network/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ethers } from 'ethers'
import prompts from 'prompts'
import { Argv, Arguments } from 'yargs'
import Network from '../../service/network'
import Backup from '../../service/backup'
import { onCancel } from '../../../util/error'
import { NetworkCreateType } from '../../model/type/network.type'
import config from '../../config'
Expand All @@ -26,11 +27,12 @@ export const builder = (yargs: Argv<OptType>) => {

export const handler = async (argv: Arguments<OptType>) => {
const network = new Network(config)
const backup = new Backup(config)
const wallet = new Wallet()
// check bdkPath files exist or not (include useless file e.g. .DS_Store)
const confirm: boolean = await (async () => {
network.createBdkFolder()
const fileList = network.getBdkFiles()
const fileList = network.getNetworkFiles()
if (fileList.length !== 0) {
const confirmDelete = (await prompts({
type: 'confirm',
Expand All @@ -40,8 +42,11 @@ export const handler = async (argv: Arguments<OptType>) => {
}, { onCancel })).value
if (confirmDelete) {
const spinner = ora('Quorum Network Create ...').start()
// backup before remove
await backup.exportAll()

network.removeBdkFiles(fileList)
spinner.succeed('Remove all existing files!')
spinner.succeed('Backup and remove all existing files!')
}
return confirmDelete
} else {
Expand Down
9 changes: 7 additions & 2 deletions src/quorum/command/network/generate.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import prompts from 'prompts'
import { Argv, Arguments } from 'yargs'
import Network from '../../service/network'
import Backup from '../../service/backup'
import { onCancel, ParamsError } from '../../../util/error'
import { NetworkGenerateType } from '../../model/type/network.type'
import config from '../../config'
Expand All @@ -22,6 +23,7 @@ export const builder = (yargs: Argv<OptType>) => {

export const handler = async (argv: Arguments<OptType>) => {
const network = new Network(config)
const backup = new Backup(config)
// check bdkPath files exist or not (include useless file e.g. .DS_Store)
const confirm: boolean = await (async () => {
network.createBdkFolder()
Expand All @@ -34,9 +36,12 @@ export const handler = async (argv: Arguments<OptType>) => {
initial: false,
}, { onCancel })).value
if (confirmDelete) {
const spinner = ora('Quorum Network Generate ...').start()
const spinner = ora('Quorum Network Create ...').start()
// backup before remove
await backup.exportAll()

network.removeBdkFiles(fileList)
spinner.succeed('Remove all existing files!')
spinner.succeed('Backup and remove all existing files!')
}
return confirmDelete
} else {
Expand Down
12 changes: 11 additions & 1 deletion src/quorum/instance/bdkFile.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import ExplorerDockerComposeYaml from '../model/yaml/docker-compose/explorerDockerComposeYaml'
import fs from 'fs-extra'
import { Config } from '../config'
import { GenesisJsonType } from '../model/type/network.type'
import { GenesisJsonType, NetworkInfoItem } from '../model/type/network.type'
import ValidatorDockerComposeYaml from '../model/yaml/docker-compose/validatorDockerComposeYaml'
import MemberDockerComposeYaml from '../model/yaml/docker-compose/memberDockerCompose'
import { PathError } from '../../util/error'
Expand Down Expand Up @@ -50,6 +50,10 @@ export default class BdkFile {
fs.writeFileSync(`${this.bdkPath}/artifacts/goQuorum/static-nodes.json`, JSON.stringify(staticNodesJson, null, 2))
}

public createNetworkInfoJson (networkInfoJson: Array<NetworkInfoItem>) {
fs.writeFileSync(`${this.bdkPath}/network-info.json`, JSON.stringify(networkInfoJson, null, 2))
}

public createPrivateKey (dir: string, privateKey: string) {
fs.mkdirSync(`${this.bdkPath}/${dir}`, { recursive: true })
fs.writeFileSync(`${this.bdkPath}/${dir}/nodekey`, privateKey)
Expand Down Expand Up @@ -217,6 +221,12 @@ export default class BdkFile {
return JSON.parse(staticNodesJson)
}

public getNetworkInfoJson () {
this.checkPathExist(this.bdkPath)
const networkInfoJson = fs.readFileSync(`${this.bdkPath}/network-info.json`, 'utf8')
return JSON.parse(networkInfoJson)
}

public getPermissionedNodesJson () {
this.checkPathExist(this.bdkPath)
const permissionedNodesJson = fs.readFileSync(`${this.bdkPath}/artifacts/goQuorum/permissioned-nodes.json`, 'utf8')
Expand Down
5 changes: 5 additions & 0 deletions src/quorum/model/type/network.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,8 @@ export interface AddMemberRemoteType {
discoveryPort: string
ipAddress: string
}

export interface NetworkInfoItem {
label: string
value: string
}
Loading

0 comments on commit e71d7af

Please sign in to comment.