Skip to content

Commit

Permalink
use eslint and fix style
Browse files Browse the repository at this point in the history
  • Loading branch information
wanwiset25 committed Jan 5, 2024
1 parent 5b00304 commit f3c9104
Show file tree
Hide file tree
Showing 7 changed files with 307 additions and 310 deletions.
13 changes: 13 additions & 0 deletions subnet/deployment-generator/src/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"env": {
"browser": true,
"es2021": true
},
"extends": "standard",
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"rules": {
}
}
94 changes: 44 additions & 50 deletions subnet/deployment-generator/src/config_gen.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
const crypto = require('crypto');
const net = require('net');
const dotenv = require('dotenv');
const ethers = require('ethers');
const { off } = require('process');
dotenv.config({ path: `${__dirname}/gen.env` });
const crypto = require('crypto')
const net = require('net')
const dotenv = require('dotenv')
const ethers = require('ethers')
dotenv.config({ path: `${__dirname}/gen.env` })
// console.log(__dirname)

let config = {
deployment_path: (process.env.CONFIG_PATH || ''),
num_machines: parseInt(process.env.NUM_MACHINE),
const config = {
deployment_path: (process.env.CONFIG_PATH || ''),
num_machines: parseInt(process.env.NUM_MACHINE),
num_subnet: parseInt(process.env.NUM_SUBNET),
ip_1: (process.env.MAIN_IP || ''),
network_name: (process.env.NETWORK_NAME),
Expand All @@ -31,7 +30,7 @@ let config = {
network: (process.env.PARENTNET || 'devnet'),
url: '',
pubkey: '' ,
privatekey: (process.env.PARENTNET_WALLET_PK || '') ,
privatekey: (process.env.PARENTNET_WALLET_PK || ''),
},
keys: {
subnets_addr: [] ,
Expand All @@ -40,49 +39,49 @@ let config = {
grandmaster_pk: (process.env.GRANDMASTER_PK || ''),
},
generator: {
output_path: `${__dirname}/../generated/`
output_path: `${__dirname}/../generated/`
}
};
}

if (configSanityCheck(config) === true){
if (configSanityCheck(config) === true) {
module.exports = config
} else {
console.log('bad config init file, please check again')
process.exit()
}

function validatePK(private_key){
function validatePK (private_key) {
let wallet = new ethers.Wallet(private_key)
return [wallet.address, wallet.privateKey]
}

function configSanityCheck(config){
if (!config.num_machines || !config.num_subnet || !config.network_name){
function configSanityCheck (config) {
if (!config.num_machines || !config.num_subnet || !config.network_name) {
console.log('NUM_MACHINE and NUM_SUBNET and NETWORK_NAME must be set')
process.exit(1)
}

if (config.num_machines===0 || config.num_subnet ===0){
if (config.num_machines === 0 || config.num_subnet === 0) {
console.log('NUM_MACHINE and NUM_SUBNET cannot be 0')
process.exit(1)
}

if (!net.isIP(config.ip_1)){
if (!net.isIP(config.ip_1)) {
console.log('MAIN_IP Invalid IP address')
process.exit(1)
}

if (!config.network_name || config.network_name===''){
if (!config.network_name || config.network_name === '') {
console.log('NETWORK_NAME cannot be empty')
process.exit(1)
}

if (config.network_id<1 || config.network_id >= 65536) {
if (config.network_id < 1 || config.network_id >= 65536) {
console.log('NETWORK_ID should be in range of 1 to 65536')
process.exit(1)
}

if (config.secret_string==='') {
if (config.secret_string === '') {
console.log('SERVICES_SECRET cannot be empty string')
process.exit(1)
}
Expand All @@ -106,29 +105,29 @@ function configSanityCheck(config){
process.exit(1)
}

if (config.parentnet.privatekey != ''){
try{
if (config.parentnet.privatekey !== '') {
try {
config.parentnet.pubkey = validatePK(config.parentnet.privatekey)[0]
}catch{
} catch {
console.log('Invalid PARENTNET_WALLET_PK')
process.exit(1)
}
}

if (config.keys.grandmaster_pk != ''){
try{
if (config.keys.grandmaster_pk !== '') {
try {
[config.keys.grandmaster_addr, config.keys.grandmaster_pk] = validatePK(config.keys.grandmaster_pk)
}catch{
} catch {
console.log('Invalid GRANDMASTER_PK')
process.exit(1)
}
} else {
const privatekey = crypto.randomBytes(32).toString('hex');
[config.keys.grandmaster_addr, config.keys.grandmaster_pk] = validatePK(privatekey)
const privatekey = crypto.randomBytes(32).toString('hex');
[config.keys.grandmaster_addr, config.keys.grandmaster_pk] = validatePK(privatekey)
}

if (config.keys.subnets_pk != ''){
try{
if (config.keys.subnets_pk !== '') {
try {
let output_pk = []
let output_wallet = []
let pks = config.keys.subnets_pk.split(',')
Expand All @@ -137,47 +136,42 @@ function configSanityCheck(config){
output_wallet.push(address)
output_pk.push(private_key)
})
config.keys.subnets_pk=output_pk
config.keys.subnets_addr=output_wallet

}catch{
config.keys.subnets_pk = output_pk
config.keys.subnets_addr = output_wallet
} catch {
console.log('Invalid SUBNETS_PK please make sure keys are correct length, comma separated with no whitespace or invalid characters')
process.exit(1)
}

if (config.keys.subnets_addr.length != config.num_subnet) {
if (config.keys.subnets_addr.length !== config.num_subnet) {
console.log(`number of keys in SUBNETS_PK (${config.keys.subnets_addr.length}) does not match NUM_SUBNET (${config.num_subnet})`)
process.exit(1)
}

const setPK = new Set(config.keys.subnets_pk)
if (setPK.size != config.keys.subnets_pk.length){
const setPK = new Set(config.keys.subnets_pk)
if (setPK.size !== config.keys.subnets_pk.length) {
console.log('found duplicate keys in SUBNETS_PK')
process.exit(1)
}

} else {
let output_pk = []
let output_wallet = []
for (let i=0; i<config.num_subnet; i++){
const privatekey = crypto.randomBytes(32).toString('hex');
for (let i = 0; i < config.num_subnet; i++) {
const privatekey = crypto.randomBytes(32).toString('hex')
const [address, private_key] = validatePK(privatekey)
output_wallet.push(address)
output_pk.push(private_key)

}
config.keys.subnets_pk=output_pk
config.keys.subnets_addr=output_wallet

config.keys.subnets_pk = output_pk
config.keys.subnets_addr = output_wallet
}

if (config.operating_system === 'mac') {
if (config.num_machines != 1){
console.log(`OS=mac requires NUM_MACHINE=1. Due to Docker network limitation, Subnets on MacOS can only communicate within its own machine. This option is intended for single machine testing environment only`)
if (config.num_machines !== 1) {
console.log('OS=mac requires NUM_MACHINE=1. Due to Docker network limitation, Subnets on MacOS can only communicate within its own machine. This option is intended for single machine testing environment only')
process.exit()
}
console.log(`OS=mac specified, this option is intended for single machine testing environment only. Due to Docker network limitation, Subnets on MacOS can only communicate within its own machine.`)
}
console.log('OS=mac specified, this option is intended for single machine testing environment only. Due to Docker network limitation, Subnets on MacOS can only communicate within its own machine.')
}
return true
}

Loading

0 comments on commit f3c9104

Please sign in to comment.