Skip to content
This repository has been archived by the owner on Dec 1, 2021. It is now read-only.

Commit

Permalink
Make start scripts work better without parameters ("sane defaults")
Browse files Browse the repository at this point in the history
Defaults to running a fresh Ganache
  • Loading branch information
jtakalai committed Feb 27, 2019
1 parent d87c2a2 commit 5dba7d8
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 16 deletions.
5 changes: 4 additions & 1 deletion .eslintrc.js
Expand Up @@ -11,7 +11,10 @@ module.exports = {
"indent": [
"error",
4,
{"SwitchCase": 1}
{
"SwitchCase": 1,
"flatTernaryExpressions": true
},
],
"linebreak-style": [
"error",
Expand Down
8 changes: 1 addition & 7 deletions .vscode/launch.json
Expand Up @@ -65,11 +65,7 @@
"type": "node",
"request": "launch",
"name": "operator (Ganache)",
"program": "${workspaceFolder}/start_operator.js",
"env": {
"GANACHE_PORT": "3010",
"RESET": "1"
}
"program": "${workspaceFolder}/start_operator.js"
}, {
"type": "node",
"request": "launch",
Expand Down Expand Up @@ -98,8 +94,6 @@
"name": "Start Monoplasma validator",
"program": "${workspaceFolder}/start_validator.js",
"env": {
"CONFIG_FILE": "${workspaceFolder}/demo/public/data/state.json",
"ETHEREUM_PRIVATE_KEY": "0x5e98cce00cff5dea6b454889f359a4ec06b9fa6b88e9d69b86de8e1c81887da0",
"WATCHED_ACCOUNTS": "0x41ad2327e5910dcca156dc17d0908b690e2f1f7c,0x0e7a1cf7cf69299c20af39056af232fde05b5204,0x297f393328243147e5fd08bd4b8b3786150635cd,0x1612801262e358bdc6031136c0c2104e7d3bd78e,0x2526401999f6502058d57dee734d2277d8669ce7"
}
}
Expand Down
4 changes: 2 additions & 2 deletions start_operator.js
Expand Up @@ -97,9 +97,9 @@ async function start() {
gasPrice: GAS_PRICE_GWEI || 4000000000,
}

// ignore the saved config / saved state if using ganache
// ignore the saved config / saved state if not using a fresh ganache instance
// augment the config / saved state with variables that may be useful for the validators
const config = RESET || !ethereumServer ? {} : await fileStore.loadState()
const config = RESET || ganache ? {} : await fileStore.loadState()
config.tokenAddress = TOKEN_ADDRESS || config.tokenAddress || await deployDemoToken(web3, TOKEN_NAME, TOKEN_SYMBOL, opts, log)
config.blockFreezeSeconds = +BLOCK_FREEZE_SECONDS || config.blockFreezeSeconds || 20
config.contractAddress = CONTRACT_ADDRESS || config.contractAddress || await deployContract(web3, config.tokenAddress, config.blockFreezeSeconds, opts, log)
Expand Down
22 changes: 16 additions & 6 deletions start_validator.js
Expand Up @@ -28,6 +28,7 @@ function error() {
process.exit(1)
}

const defaultConfigPath = __dirname + "/demo/public/data/state.json"
const storeDir = fs.existsSync(STORE_DIR) ? STORE_DIR : __dirname + "/temp"
const fileStore = require("./src/fileStore")(storeDir)

Expand All @@ -40,8 +41,9 @@ fsEx.copySync(pastEventsDir, eventsDir)
async function start() {
const config = CONFIG_JSON ? JSON.parse(CONFIG_JSON)
: CONFIG_FILE ? await loadStateFromFile(CONFIG_FILE)
: CONFIG_URL ? await loadStateFromUrl(CONFIG_URL)
: {}
: CONFIG_URL ? await loadStateFromUrl(CONFIG_URL)
: fs.existsSync(defaultConfigPath) ? await loadStateFromFile(defaultConfigPath)
: {}
log("Received config:")
log(prettyjson.render(config))

Expand All @@ -59,12 +61,20 @@ async function start() {
if (accountList.length > 0) {
// TODO: guess private key if missing?
// with ganache, operator uses account 0: 0xa3d1f77acff0060f7213d7bf3c7fec78df847de1
// const privateKey = ETHEREUM_PRIVATE_KEY || "0x5e98cce00cff5dea6b454889f359a4ec06b9fa6b88e9d69b86de8e1c81887da0"
if (!ETHEREUM_PRIVATE_KEY) { throw new Error("Environment variable ETHEREUM_PRIVATE_KEY is needed to send exit transaction for the WATCHED_ACCOUNTS!") }
const key = ETHEREUM_PRIVATE_KEY.startsWith("0x") ? ETHEREUM_PRIVATE_KEY : "0x" + ETHEREUM_PRIVATE_KEY
if (key.length !== 66) { throw new Error("Malformed private key, must be 64 hex digits long (optionally prefixed with '0x')") }
let key = "0x5e98cce00cff5dea6b454889f359a4ec06b9fa6b88e9d69b86de8e1c81887da0"
if (ETHEREUM_PRIVATE_KEY) {
key = ETHEREUM_PRIVATE_KEY.startsWith("0x") ? ETHEREUM_PRIVATE_KEY : "0x" + ETHEREUM_PRIVATE_KEY
if (key.length !== 66) { throw new Error("Malformed private key, must be 64 hex digits long (optionally prefixed with '0x')") }
} else {
log("Environment variable ETHEREUM_PRIVATE_KEY not found, using key for address 0xa3d1f77acff0060f7213d7bf3c7fec78df847de1")
}
const account = web3.eth.accounts.wallet.add(key)
address = account.address
const balance = await web3.eth.getBalance(address)
if (+balance === 0) {
log(`Address ${address} has no ether, it is needed to send exit transaction for the WATCHED_ACCOUNTS!`)
//throw new Error("Ether is needed to send exit transaction for the WATCHED_ACCOUNTS!") }
}
}

await throwIfNotContract(web3, config.tokenAddress, "Config variable tokenAddress")
Expand Down

0 comments on commit 5dba7d8

Please sign in to comment.