Skip to content

Commit

Permalink
Merge pull request #10 from maticnetwork/jdkanani/fixes
Browse files Browse the repository at this point in the history
Fix flags and lint
  • Loading branch information
jdkanani authored Mar 13, 2021
2 parents efb33e2 + c59dbb0 commit 20d58df
Show file tree
Hide file tree
Showing 25 changed files with 374 additions and 207 deletions.
27 changes: 23 additions & 4 deletions src/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,20 @@ import fs from 'fs-extra'
import path from 'path'
import Web3 from 'web3'
import nunjucks from 'nunjucks'
import { toBuffer, privateToPublic, bufferToHex } from 'ethereumjs-util'

//
// Add custom nunjucks filters
//

const env = nunjucks.configure()
env.addFilter('publicKey', (privateKey) => {
return privateKeyToPublicKey(privateKey)
})

//
// other methods
//

const web3 = new Web3()

Expand Down Expand Up @@ -45,7 +59,7 @@ export async function processTemplateFiles(dir, obj = {}) {
// process all njk files
fs.writeFileSync(
path.join(dir, file.replace('.njk', '')),
nunjucks.render(fp, obj)
env.render(fp, obj)
)

// remove njk file
Expand All @@ -70,11 +84,16 @@ export function getKeystoreFile(privateKeyString, password) {
}

// return new generated private key
export async function getNewPrivateKey() {
export function getNewPrivateKey() {
return web3.eth.accounts.create()
}

// return new wallet from private key
export async function getWalletFromPrivateKey(pk) {
// return new account from private key
export function getAccountFromPrivateKey(pk) {
return web3.eth.accounts.privateKeyToAccount(pk)
}

// return public key from private key
export function privateKeyToPublicKey(pk) {
return bufferToHex(privateToPublic(toBuffer(pk)))
}
40 changes: 22 additions & 18 deletions src/setup/bor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import fs from 'fs-extra'

import { loadConfig } from '../config'
import { cloneRepository, getKeystoreFile, processTemplateFiles } from '../../lib/utils'
import { printDependencyInstructions } from '../helper'
import { printDependencyInstructions, getDefaultBranch } from '../helper'
import { Genesis } from '../genesis'

// default password
Expand Down Expand Up @@ -62,9 +62,9 @@ export class Bor {
async print() {
console.log(chalk.gray('Bor data') + ': ' + chalk.bold.green(this.borDataDir))
console.log(chalk.gray('Bor repo') + ': ' + chalk.bold.green(this.repositoryDir))
console.log(chalk.gray('Setup bor chain') + ': ' + chalk.bold.green("bash bor-setup.sh"))
console.log(chalk.gray('Start bor chain') + ': ' + chalk.bold.green("bash bor-start.sh"))
console.log(chalk.gray('Clean bor chain') + ': ' + chalk.bold.green("bash bor-clean.sh"))
console.log(chalk.gray('Setup bor chain') + ': ' + chalk.bold.green('bash bor-setup.sh'))
console.log(chalk.gray('Start bor chain') + ': ' + chalk.bold.green('bash bor-start.sh'))
console.log(chalk.gray('Clean bor chain') + ': ' + chalk.bold.green('bash bor-clean.sh'))
}

async getTasks() {
Expand All @@ -76,23 +76,23 @@ export class Bor {
},
{
title: 'Build Bor',
task: () => execa('make', ['bor'], {
cwd: this.repositoryDir,
task: () => execa('make', ['bor-all'], {
cwd: this.repositoryDir
})
},
{
title: 'Prepare data directory',
task: () => {
return execa('mkdir', ['-p', this.config.dataDir, this.borDataDir, this.keystoreDir], {
cwd: this.config.targetDirectory,
cwd: this.config.targetDirectory
})
}
},
{
title: 'Prepare keystore and password.txt',
task: () => {
// get keystore file and store in keystore file
const keystoreFileObj = getKeystoreFile(this.config.privateKey, this.config.keystorePassword)
const keystoreFileObj = getKeystoreFile(this.config.primaryAccount.privateKey, this.config.keystorePassword)

// resolve promise
return fs.emptyDir(this.keystoreDir).then(() => {
Expand All @@ -110,7 +110,7 @@ export class Bor {
const templateDir = path.resolve(
new URL(import.meta.url).pathname,
'../templates'
);
)

// copy all templates to target directory
await fs.copy(templateDir, this.config.targetDirectory)
Expand All @@ -121,9 +121,9 @@ export class Bor {
}
],
{
exitOnError: true,
exitOnError: true
}
);
)
}
}

Expand All @@ -146,19 +146,19 @@ async function setupBor(config) {
}
],
{
exitOnError: true,
exitOnError: true
}
);
)

await tasks.run();
console.log('%s Bor is ready', chalk.green.bold('DONE'));
await tasks.run()
console.log('%s Bor is ready', chalk.green.bold('DONE'))

// print config
await config.print()
await bor.genesis.print(config)
await bor.print()

return true;
return true
}

export default async function () {
Expand All @@ -167,8 +167,12 @@ export default async function () {
// configuration
const config = await loadConfig({ targetDirectory: process.cwd() })
await config.loadChainIds()
await config.loadAccount()
await config.loadAccounts()

// load branch
const answers = await getDefaultBranch(config)
config.set(answers)

// start setup
await setupBor(config)
}
}
16 changes: 8 additions & 8 deletions src/setup/bor/templates/bor-start.sh.njk
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

set -x #echo on

ADDRESS={{ obj.config.address }}
ADDRESS={{ obj.config.primaryAccount.address }}
BOR_CHAIN_ID={{ obj.config.borChainId }}

if [ -z "$ADDRESS" ]
Expand All @@ -26,19 +26,19 @@ mkdir -p $DATA_DIR/logs

$BUILD_DIR/bor --datadir $BOR_DATA_DIR \
--port 30303 \
--rpc --rpcaddr '0.0.0.0' \
--rpcvhosts '*' \
--rpccorsdomain '*' \
--rpcport 8545 \
--http --http.addr '0.0.0.0' \
--http.vhosts '*' \
--http.corsdomain '*' \
--http.port 8545 \
--http.api 'eth,net,web3,txpool' \
--ipcpath $BOR_DATA_DIR/bor.ipc \
--rpcapi 'db,eth,net,web3,txpool' \
--networkid $BOR_CHAIN_ID \
--gasprice '0' \
--miner.gasprice '0' \
--keystore $DATA_DIR/keystore \
--unlock $ADDRESS \
--password $DATA_DIR/password.txt \
--allow-insecure-unlock \
--maxpeers 200 \
--metrics \
--pprof --pprofport 7071 --pprofaddr '0.0.0.0' \
--pprof --pprof.port 7071 --pprof.addr '0.0.0.0' \
--mine
35 changes: 14 additions & 21 deletions src/setup/config.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import inquirer from 'inquirer'
import fs from 'fs-extra'
import path from 'path'
import execa from 'execa'
import chalk from 'chalk'
import { toBuffer, privateToPublic, bufferToHex } from 'ethereumjs-util'

import { getKeystoreDetails, getChainIds } from './helper'
import { getWalletFromPrivateKey } from '../lib/utils'
import { getAccountFromPrivateKey } from '../lib/utils'

const defaultConfigFileName = 'config.json'

Expand All @@ -22,6 +21,7 @@ export default class Config {

options.defaultStake = options.defaultStake || 10000
options.defaultFee = options.defaultFee || 2000
options.accounts = []

// assign all prop to obj
this.set(options)
Expand Down Expand Up @@ -55,17 +55,8 @@ export default class Config {
return path.join(this.targetDirectory, this.configDirectory)
}

get publicKey() {
return bufferToHex(privateToPublic(toBuffer(this.privateKey)))
}

async getWallet() {
return getWalletFromPrivateKey(this.privateKey)
}

async loadKeystoreDetails() {
const keystoreDetails = await getKeystoreDetails(this)
this.set(keystoreDetails)
get primaryAccount() {
return this.accounts[0]
}

async loadChainIds() {
Expand All @@ -77,24 +68,26 @@ export default class Config {
this.set({ forceAsk: true })
}

async loadAccount() {
async loadAccounts() {
if (!this.privateKey || !this.keystorePassword) {
await this.loadKeystoreDetails()
const keystoreDetails = await getKeystoreDetails(this)
this.accounts.push(getAccountFromPrivateKey(keystoreDetails.privateKey))
this.set({ keystorePassword: keystoreDetails.keystorePassword })
}

// fetch wallet
const wallet = await this.getWallet()
// set genesis address
this.genesisAddresses = [this.primaryAccount.address]
}

// set genesis addresses and address
this.address = wallet.address
this.genesisAddresses = [wallet.address]
async saveConfig() {
await saveConfig(this)
}

print() {
console.log(chalk.gray('Config json file') + ': ' + chalk.bold.green(this.configFilePath))
console.log(chalk.gray('Code directory') + ': ' + chalk.bold.green(this.codeDir))
console.log(chalk.gray('Data directory') + ': ' + chalk.bold.green(this.dataDir))
console.log(chalk.gray('Address') + ': ' + chalk.bold.green(this.address))
console.log(chalk.gray('Address') + ': ' + chalk.bold.green(this.primaryAccount.address))
console.log(chalk.gray('Bor Chain ID') + ': ' + chalk.bold.green(this.borChainId))
console.log(chalk.gray('Heimdall Chain ID') + ': ' + chalk.bold.green(this.heimdallChainId))
}
Expand Down
Loading

0 comments on commit 20d58df

Please sign in to comment.