Skip to content

Commit

Permalink
reorg 1
Browse files Browse the repository at this point in the history
  • Loading branch information
wanwiset25 committed Dec 8, 2023
1 parent 02e1c9d commit d7ee17e
Show file tree
Hide file tree
Showing 8 changed files with 197 additions and 188 deletions.
8 changes: 4 additions & 4 deletions subnet/deployment-generator/docker.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ NUM_MACHINE=3
NUM_SUBNET=5
MAIN_IP=192.168.1.1

#parentchain config
PARENTCHAIN=devnet
PARENTCHAIN_WALLET=0x0000000000000000000000000000000000000000
PARENTCHAIN_WALLET_PK=0x0000000000000000000000000000000000000000000000000000000000000000
#parentnet config
PARENTNET=devnet
PARENTNET=0x0000000000000000000000000000000000000000
PARENTNET=0x0000000000000000000000000000000000000000000000000000000000000000

#docker image versions, default to stable
# VERSION_SUBNET
Expand Down
2 changes: 1 addition & 1 deletion subnet/deployment-generator/docker/deploy_csc.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash
cd /app/contract
remove0x=${PARENTCHAIN_WALLET_PK:2}
remove0x=${PARENTNET:2}
echo "PRIVATE_KEY=${remove0x}" >> .env
cp /app/generated/deployment.config.json /app/contract/deployment.config.json
echo "RELAYER_MODE=${RELAYER_MODE}"
Expand Down
170 changes: 170 additions & 0 deletions subnet/deployment-generator/script/config_gen.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
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)

var 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),
network_id: parseInt(process.env.NETWORK_ID || Math.floor(Math.random() * (65536 - 1) + 1)),
secret_string: (process.env.SERVICES_SECRET || crypto.randomBytes(10).toString('hex')),
relayer_mode: (process.env.RELAYER_MODE || 'full'), //full or lite
docker_image_name: (process.env.IMAGE_NAME || 'xinfinorg/subnet-generator:latest'),
operating_system: (process.env.OS || 'linux'),
version: {
subnet: (process.env.VERSION_SUBNET || 'v0.2.1'),
bootnode: (process.env.VERSION_BOOTNODE || 'v0.2.1'),
observer: (process.env.VERSION_OBSERVER || 'latest'),
relayer: (process.env.VERSION_RELAYER || 'v0.2.1'),
stats: (process.env.VERSION_STATS || 'v0.1.8'),
frontend: (process.env.VERSION_FRONTEND || 'v0.1.8')
},
parentnet:{
network: (process.env.PARENTNET || 'devnet'),
// url: '',
pubkey: '' ,
privatekey: (process.env.PARENTNET_WALLET_PK || '') ,
},
keys: {
subnets_addr: [] ,
subnets_pk: (process.env.SUBNETS_PK || ''),
grandmaster_addr: '' ,
grandmaster_pk: (process.env.GRANDMASTER_PK || ''),
}
};

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

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){
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){
console.log('NUM_MACHINE and NUM_SUBNET cannot be 0')
process.exit(1)
}

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

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) {
console.log('NETWORK_ID should be in range of 1 to 65536')
process.exit(1)
}

if (config.secret_string=='') {
console.log('SERVICES_SECRET cannot be empty string')
process.exit(1)
}

if (!(config.relayer_mode == 'full' || config.relayer_mode == 'lite')) {
console.log('RELAYER_MODE only accepts \'full\' or \'lite\' (default full)')
process.exit(1)
}

if (!(config.parentnet.network == 'devnet' ||
config.parentnet.network == 'testnet' ||
config.parentnet.network == 'mainnet' )){
console.log('PARENTNET must be devnet, testnet, or mainnet ')
process.exit(1)
}

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

if (config.keys.grandmaster_pk != ''){
try{
[config.keys.grandmaster_addr, config.keys.grandmaster_pk] = validatePK(config.keys.grandmaster_pk)
}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)
}

if (config.keys.subnets_pk != ''){
try{
let output_pk = []
let output_wallet = []
let pks = config.keys.subnets_pk.split(',')
pks.forEach(pk => {
const [address, private_key] = validatePK(pk)
output_wallet.push(address)
output_pk.push(private_key)
})
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) {
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){
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');
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

}

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`)
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.`)
}
return true
}

43 changes: 22 additions & 21 deletions subnet/deployment-generator/script/gen.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ const fs = require('fs');
const yaml = require('js-yaml');
const { exit } = require('process');
const net = require('net');
const config = require('./gen_config')
const config = require('./config_gen')
const gen_compose = require('./gen_compose')
const gen_env = require('./gen_env')
const gen_other = require('./gen_other')

Object.freeze(config)
// console.log(config)




const num_machines = config.num_machines
const num_subnet = config.num_subnet
const ip_1 = config.ip_1
Expand All @@ -18,8 +20,6 @@ const secret_string = config.secret_string
const output_path = `${__dirname}/../generated/`




keys = genSubnetKeys()

var num_per_machine = Array(num_machines)
Expand Down Expand Up @@ -224,6 +224,7 @@ function genServices(machine_id) {
frontend = {
'image': `xinfinorg/subnet-frontend:${config.version.frontend}`,
'restart': 'always',
'env_file': config_path, //not used directly (injected via volume) but required to trigger restart if common.env changes
'volumes': [`${config_path}:/app/.env.local`],
'ports': ['5000:5000'],
'profiles': [machine]
Expand Down Expand Up @@ -315,7 +316,7 @@ return config_env

function genServicesConfig(){
var url = ''
switch (config.parentchain.network){
switch (config.parentnet.network){
case 'devnet':
url='https://devnetstats.apothem.network/devnet'
break
Expand All @@ -326,7 +327,7 @@ function genServicesConfig(){
url='https://devnetstats.apothem.network/mainnet' //confirm url
break
default:
console.error('PARENTCHAIN invalid, should be devnet, testnet, or mainnet') //should not reach this case
console.error('PARENTNET invalid, should be devnet, testnet, or mainnet') //should not reach this case
exit()
}

Expand All @@ -336,42 +337,42 @@ EXTIP=${config.ip_1}
BOOTNODE_PORT=20301
# Stats and relayer
PARENTCHAIN_URL=${url}
PARENTCHAIN_WALLET=${config.parentchain.pubkey}
PARENTCHAIN_WALLET_PK=${config.parentchain.privatekey}
PARENTNET_URL=${url}
PARENTNET_WALLET=${config.parentnet.pubkey}
PARENTNET_WALLET_PK=${config.parentnet.privatekey}
SUBNET_URL=http://${config.ip_1}:8545
RELAYER_MODE=${config.relayer_mode}
CHECKPOINT_CONTRACT=0x0000000000000000000000000000000000000000
SLACK_WEBHOOK=https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX
CORS_ALLOW_ORIGIN=*
# Parent Chain Observe Node
PARENTCHAIN_NODE_NAME=mainnet_observer
PRIVATE_KEYS=1111111111111111111111111111111111111111111111111111111111111111
# Frontend
VITE_SUBNET_URL=http://${config.ip_1}:3000
# Share Variable
STATS_SECRET=${config.secret_string}
`
// # Parent Chain Observe Node
// PARENTNET_NODE_NAME=mainnet_observer
// PRIVATE_KEYS=11111111111111111111111111111111111111111111111111111111111111

return config_env
}

function genServicesConfigMac(ip_record){
var url = ''
switch (config.parentchain.network){
switch (config.parentnet.network){
case 'devnet':
url='https://devnetstats.apothem.network/devnet'
break
case 'testnet':
url='https://devnetstats.apothem.network/testnet' //confirm url
url='https://erpc.apothem.network/'
break
case 'mainnet':
url='https://devnetstats.apothem.network/mainnet' //confirm url
break
default:
console.error('PARENTCHAIN invalid, should be devnet, testnet, or mainnet') //should not reach this case
console.error('PARENTNET invalid, should be devnet, testnet, or mainnet') //should not reach this case
exit()
}

Expand All @@ -381,17 +382,17 @@ EXTIP=${ip_record["bootnode"]}
BOOTNODE_PORT=20301
# Stats and relayer
PARENTCHAIN_URL=${url}
PARENTCHAIN_WALLET=${config.parentchain.pubkey}
PARENTCHAIN_WALLET_PK=${config.parentchain.privatekey}
PARENTNET_URL=${url}
PARENTNET_WALLET=${config.parentnet.pubkey}
PARENTNET_WALLET_PK=${config.parentnet.privatekey}
SUBNET_URL=http://${ip_record["subnet1"]}:8545
RELAYER_MODE=${config.relayer_mode}
CHECKPOINT_CONTRACT=0x0000000000000000000000000000000000000000
SLACK_WEBHOOK=https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX
CORS_ALLOW_ORIGIN=*
# Parent Chain Observe Node
PARENTCHAIN_NODE_NAME=mainnet_observer
PARENTNET_NODE_NAME=mainnet_observer
PRIVATE_KEYS=1111111111111111111111111111111111111111111111111111111111111111
# Frontend
Expand Down
Empty file.
Loading

0 comments on commit d7ee17e

Please sign in to comment.