Skip to content

Commit 20d58df

Browse files
authored
Merge pull request #10 from maticnetwork/jdkanani/fixes
Fix flags and lint
2 parents efb33e2 + c59dbb0 commit 20d58df

25 files changed

+374
-207
lines changed

src/lib/utils.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,20 @@ import fs from 'fs-extra'
33
import path from 'path'
44
import Web3 from 'web3'
55
import nunjucks from 'nunjucks'
6+
import { toBuffer, privateToPublic, bufferToHex } from 'ethereumjs-util'
7+
8+
//
9+
// Add custom nunjucks filters
10+
//
11+
12+
const env = nunjucks.configure()
13+
env.addFilter('publicKey', (privateKey) => {
14+
return privateKeyToPublicKey(privateKey)
15+
})
16+
17+
//
18+
// other methods
19+
//
620

721
const web3 = new Web3()
822

@@ -45,7 +59,7 @@ export async function processTemplateFiles(dir, obj = {}) {
4559
// process all njk files
4660
fs.writeFileSync(
4761
path.join(dir, file.replace('.njk', '')),
48-
nunjucks.render(fp, obj)
62+
env.render(fp, obj)
4963
)
5064

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

7286
// return new generated private key
73-
export async function getNewPrivateKey() {
87+
export function getNewPrivateKey() {
7488
return web3.eth.accounts.create()
7589
}
7690

77-
// return new wallet from private key
78-
export async function getWalletFromPrivateKey(pk) {
91+
// return new account from private key
92+
export function getAccountFromPrivateKey(pk) {
7993
return web3.eth.accounts.privateKeyToAccount(pk)
8094
}
95+
96+
// return public key from private key
97+
export function privateKeyToPublicKey(pk) {
98+
return bufferToHex(privateToPublic(toBuffer(pk)))
99+
}

src/setup/bor/index.js

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import fs from 'fs-extra'
66

77
import { loadConfig } from '../config'
88
import { cloneRepository, getKeystoreFile, processTemplateFiles } from '../../lib/utils'
9-
import { printDependencyInstructions } from '../helper'
9+
import { printDependencyInstructions, getDefaultBranch } from '../helper'
1010
import { Genesis } from '../genesis'
1111

1212
// default password
@@ -62,9 +62,9 @@ export class Bor {
6262
async print() {
6363
console.log(chalk.gray('Bor data') + ': ' + chalk.bold.green(this.borDataDir))
6464
console.log(chalk.gray('Bor repo') + ': ' + chalk.bold.green(this.repositoryDir))
65-
console.log(chalk.gray('Setup bor chain') + ': ' + chalk.bold.green("bash bor-setup.sh"))
66-
console.log(chalk.gray('Start bor chain') + ': ' + chalk.bold.green("bash bor-start.sh"))
67-
console.log(chalk.gray('Clean bor chain') + ': ' + chalk.bold.green("bash bor-clean.sh"))
65+
console.log(chalk.gray('Setup bor chain') + ': ' + chalk.bold.green('bash bor-setup.sh'))
66+
console.log(chalk.gray('Start bor chain') + ': ' + chalk.bold.green('bash bor-start.sh'))
67+
console.log(chalk.gray('Clean bor chain') + ': ' + chalk.bold.green('bash bor-clean.sh'))
6868
}
6969

7070
async getTasks() {
@@ -76,23 +76,23 @@ export class Bor {
7676
},
7777
{
7878
title: 'Build Bor',
79-
task: () => execa('make', ['bor'], {
80-
cwd: this.repositoryDir,
79+
task: () => execa('make', ['bor-all'], {
80+
cwd: this.repositoryDir
8181
})
8282
},
8383
{
8484
title: 'Prepare data directory',
8585
task: () => {
8686
return execa('mkdir', ['-p', this.config.dataDir, this.borDataDir, this.keystoreDir], {
87-
cwd: this.config.targetDirectory,
87+
cwd: this.config.targetDirectory
8888
})
8989
}
9090
},
9191
{
9292
title: 'Prepare keystore and password.txt',
9393
task: () => {
9494
// get keystore file and store in keystore file
95-
const keystoreFileObj = getKeystoreFile(this.config.privateKey, this.config.keystorePassword)
95+
const keystoreFileObj = getKeystoreFile(this.config.primaryAccount.privateKey, this.config.keystorePassword)
9696

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

115115
// copy all templates to target directory
116116
await fs.copy(templateDir, this.config.targetDirectory)
@@ -121,9 +121,9 @@ export class Bor {
121121
}
122122
],
123123
{
124-
exitOnError: true,
124+
exitOnError: true
125125
}
126-
);
126+
)
127127
}
128128
}
129129

@@ -146,19 +146,19 @@ async function setupBor(config) {
146146
}
147147
],
148148
{
149-
exitOnError: true,
149+
exitOnError: true
150150
}
151-
);
151+
)
152152

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

156156
// print config
157157
await config.print()
158158
await bor.genesis.print(config)
159159
await bor.print()
160160

161-
return true;
161+
return true
162162
}
163163

164164
export default async function () {
@@ -167,8 +167,12 @@ export default async function () {
167167
// configuration
168168
const config = await loadConfig({ targetDirectory: process.cwd() })
169169
await config.loadChainIds()
170-
await config.loadAccount()
170+
await config.loadAccounts()
171+
172+
// load branch
173+
const answers = await getDefaultBranch(config)
174+
config.set(answers)
171175

172176
// start setup
173177
await setupBor(config)
174-
}
178+
}

src/setup/bor/templates/bor-start.sh.njk

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
set -x #echo on
44

5-
ADDRESS={{ obj.config.address }}
5+
ADDRESS={{ obj.config.primaryAccount.address }}
66
BOR_CHAIN_ID={{ obj.config.borChainId }}
77

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

2727
$BUILD_DIR/bor --datadir $BOR_DATA_DIR \
2828
--port 30303 \
29-
--rpc --rpcaddr '0.0.0.0' \
30-
--rpcvhosts '*' \
31-
--rpccorsdomain '*' \
32-
--rpcport 8545 \
29+
--http --http.addr '0.0.0.0' \
30+
--http.vhosts '*' \
31+
--http.corsdomain '*' \
32+
--http.port 8545 \
33+
--http.api 'eth,net,web3,txpool' \
3334
--ipcpath $BOR_DATA_DIR/bor.ipc \
34-
--rpcapi 'db,eth,net,web3,txpool' \
3535
--networkid $BOR_CHAIN_ID \
36-
--gasprice '0' \
36+
--miner.gasprice '0' \
3737
--keystore $DATA_DIR/keystore \
3838
--unlock $ADDRESS \
3939
--password $DATA_DIR/password.txt \
4040
--allow-insecure-unlock \
4141
--maxpeers 200 \
4242
--metrics \
43-
--pprof --pprofport 7071 --pprofaddr '0.0.0.0' \
43+
--pprof --pprof.port 7071 --pprof.addr '0.0.0.0' \
4444
--mine

src/setup/config.js

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import inquirer from 'inquirer'
21
import fs from 'fs-extra'
32
import path from 'path'
43
import execa from 'execa'
54
import chalk from 'chalk'
65
import { toBuffer, privateToPublic, bufferToHex } from 'ethereumjs-util'
76

87
import { getKeystoreDetails, getChainIds } from './helper'
9-
import { getWalletFromPrivateKey } from '../lib/utils'
8+
import { getAccountFromPrivateKey } from '../lib/utils'
109

1110
const defaultConfigFileName = 'config.json'
1211

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

2322
options.defaultStake = options.defaultStake || 10000
2423
options.defaultFee = options.defaultFee || 2000
24+
options.accounts = []
2525

2626
// assign all prop to obj
2727
this.set(options)
@@ -55,17 +55,8 @@ export default class Config {
5555
return path.join(this.targetDirectory, this.configDirectory)
5656
}
5757

58-
get publicKey() {
59-
return bufferToHex(privateToPublic(toBuffer(this.privateKey)))
60-
}
61-
62-
async getWallet() {
63-
return getWalletFromPrivateKey(this.privateKey)
64-
}
65-
66-
async loadKeystoreDetails() {
67-
const keystoreDetails = await getKeystoreDetails(this)
68-
this.set(keystoreDetails)
58+
get primaryAccount() {
59+
return this.accounts[0]
6960
}
7061

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

80-
async loadAccount() {
71+
async loadAccounts() {
8172
if (!this.privateKey || !this.keystorePassword) {
82-
await this.loadKeystoreDetails()
73+
const keystoreDetails = await getKeystoreDetails(this)
74+
this.accounts.push(getAccountFromPrivateKey(keystoreDetails.privateKey))
75+
this.set({ keystorePassword: keystoreDetails.keystorePassword })
8376
}
8477

85-
// fetch wallet
86-
const wallet = await this.getWallet()
78+
// set genesis address
79+
this.genesisAddresses = [this.primaryAccount.address]
80+
}
8781

88-
// set genesis addresses and address
89-
this.address = wallet.address
90-
this.genesisAddresses = [wallet.address]
82+
async saveConfig() {
83+
await saveConfig(this)
9184
}
9285

9386
print() {
9487
console.log(chalk.gray('Config json file') + ': ' + chalk.bold.green(this.configFilePath))
9588
console.log(chalk.gray('Code directory') + ': ' + chalk.bold.green(this.codeDir))
9689
console.log(chalk.gray('Data directory') + ': ' + chalk.bold.green(this.dataDir))
97-
console.log(chalk.gray('Address') + ': ' + chalk.bold.green(this.address))
90+
console.log(chalk.gray('Address') + ': ' + chalk.bold.green(this.primaryAccount.address))
9891
console.log(chalk.gray('Bor Chain ID') + ': ' + chalk.bold.green(this.borChainId))
9992
console.log(chalk.gray('Heimdall Chain ID') + ': ' + chalk.bold.green(this.heimdallChainId))
10093
}

0 commit comments

Comments
 (0)