Skip to content

Commit

Permalink
refactor: Decouple RainbowConfig (#412)
Browse files Browse the repository at this point in the history
# Testing Plan

* [x] The existing end-to-end testing suite pass
* [x] Manual testing of README pass
  • Loading branch information
frol committed Nov 16, 2020
1 parent c27b179 commit cf20de6
Show file tree
Hide file tree
Showing 26 changed files with 960 additions and 1,321 deletions.
18 changes: 11 additions & 7 deletions cli/commands/danger-deploy-myerc20.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
const { Web3, RainbowConfig, normalizeEthKey } = require('rainbow-bridge-utils')
const { Web3, normalizeEthKey } = require('rainbow-bridge-utils')
const { BN } = require('ethereumjs-util')
const fs = require('fs')

class DangerDeployMyERC20 {
static async execute () {
const web3 = new Web3(RainbowConfig.getParam('eth-node-url'))
static async execute ({
ethNodeUrl,
ethMasterSk,
ethErc20AbiPath,
ethGasMultiplier
}) {
const web3 = new Web3(ethNodeUrl)
let ethMasterAccount = web3.eth.accounts.privateKeyToAccount(
normalizeEthKey(RainbowConfig.getParam('eth-master-sk'))
normalizeEthKey(ethMasterSk)
)
web3.eth.accounts.wallet.add(ethMasterAccount)
web3.eth.defaultAccount = ethMasterAccount.address
ethMasterAccount = ethMasterAccount.address

// use default ERC20 ABI
const abiPath = RainbowConfig.getParam('eth-erc20-abi-path')
const binPath = '../testing/ci/MyERC20.full.bin'

const tokenContract = new web3.eth.Contract(
JSON.parse(fs.readFileSync(abiPath))
JSON.parse(fs.readFileSync(ethErc20AbiPath))
)
const txContract = await tokenContract
.deploy({
Expand All @@ -28,7 +32,7 @@ class DangerDeployMyERC20 {
from: ethMasterAccount,
gas: 3000000,
gasPrice: new BN(await web3.eth.getGasPrice()).mul(
new BN(RainbowConfig.getParam('eth-gas-multiplier'))
new BN(ethGasMultiplier)
)
})

Expand Down
30 changes: 27 additions & 3 deletions cli/commands/danger-submit-invalid-near-block.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,34 @@
const { Near2EthRelay } = require('rainbow-bridge-near2eth-block-relay')

class DangerSubmitInvalidNearBlock {
static async execute () {
static async execute ({
nearNodeUrl,
nearNetworkId,
ethNodeUrl,
ethMasterSk,
ethClientAbiPath,
ethClientAddress,
ethGasMultiplier,
near2ethRelayMinDelay,
near2ethRelayMaxDelay,
near2ethRelayErrorDelay
}) {
const relay = new Near2EthRelay()
await relay.initialize()
await relay.DANGERsubmitInvalidNearBlock()
await relay.initialize({
nearNodeUrl,
nearNetworkId,
ethNodeUrl,
ethMasterSk,
ethClientAbiPath,
ethClientAddress,
ethGasMultiplier
})
await relay.DANGERsubmitInvalidNearBlock({
near2ethRelayMinDelay,
near2ethRelayMaxDelay,
near2ethRelayErrorDelay,
ethGasMultiplier
})
}
}

Expand Down
5 changes: 1 addition & 4 deletions cli/commands/near-dump.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const fs = require('fs').promises
const Path = require('path')
const { RainbowConfig } = require('rainbow-bridge-utils')
const fetch = require('node-fetch')

async function getLatestBlock (nearNodeUrl) {
Expand Down Expand Up @@ -92,7 +91,7 @@ async function getNextLightClientBlock (nearNodeUrl, blockHash) {
}

class NearDump {
static async execute (kindOfData, { path, numBlocks }) {
static async execute (kindOfData, { path, numBlocks, nearNodeUrl }) {
if (kindOfData !== 'headers' && kindOfData !== 'proofs') {
console.log(
'Usage: node index.js near-dump headers\n node index.js near-dump proofs'
Expand All @@ -103,8 +102,6 @@ class NearDump {
if (!numBlocks) {
numBlocks = 100
}
const nearNodeUrl = RainbowConfig.getParam('near-node-url')

const latestBlock = await getLatestBlock(nearNodeUrl)

if (kindOfData === 'headers') {
Expand Down
9 changes: 4 additions & 5 deletions cli/commands/prepare.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const { exec } = require('child_process')
const path = require('path')
const { RainbowConfig, getScript } = require('rainbow-bridge-utils')

const { getScript } = require('rainbow-bridge-utils')

class PrepareCommand {
static execute () {
static execute ({ coreSrc, nearupSrc }) {
const scriptDir = getScript('prepare')

const shell = ['bash', scriptDir].join(' ')
Expand All @@ -13,9 +14,7 @@ class PrepareCommand {
env[e] = process.env[e]
}

env.LOCAL_CORE_SRC =
RainbowConfig.getParam('core-src') &&
path.resolve(RainbowConfig.getParam('core-src'))
env.LOCAL_CORE_SRC = coreSrc && path.resolve(coreSrc)

// @ts-ignore
const prepareScript = exec(shell, { env: env })
Expand Down
45 changes: 30 additions & 15 deletions cli/commands/start/eth2near-relay.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@ const {
EthOnNearClientContract,
Eth2NearRelay
} = require('rainbow-bridge-eth2near-block-relay')
const { nearAPI, RainbowConfig } = require('rainbow-bridge-utils')
const { nearAPI } = require('rainbow-bridge-utils')
const path = require('path')

class StartEth2NearRelayCommand {
static async execute () {
if (RainbowConfig.getParam('daemon') === 'true') {
static async execute ({
daemon,
nearNetworkId,
nearNodeUrl,
nearMasterAccount,
nearMasterSk,
nearClientAccount,
ethNodeUrl
}) {
if (daemon === 'true') {
ProcessManager.connect((err) => {
if (err) {
console.log(
Expand All @@ -23,38 +31,45 @@ class StartEth2NearRelayCommand {
interpreter: 'node',
error_file: '~/.rainbow/logs/eth2near-relay/err.log',
out_file: '~/.rainbow/logs/eth2near-relay/out.log',
args: ['start', 'eth2near-relay', ...RainbowConfig.getArgsNoDaemon()],
args: [
'start', 'eth2near-relay',
'--near-network-id', nearNetworkId,
'--near-node-url', nearNodeUrl,
'--near-master-account', nearMasterAccount,
'--near-master-sk', nearMasterSk,
'--near-client-account', nearClientAccount,
'--eth-node-url', ethNodeUrl,
'--daemon', 'false'
],
wait_ready: true,
kill_timeout: 60000,
logDateFormat: 'YYYY-MM-DD HH:mm:ss.SSS'
})
})
} else {
const masterAccount = RainbowConfig.getParam('near-master-account')
const masterSk = RainbowConfig.getParam('near-master-sk')
const keyStore = new nearAPI.keyStores.InMemoryKeyStore()
await keyStore.setKey(
RainbowConfig.getParam('near-network-id'),
masterAccount,
nearAPI.KeyPair.fromString(masterSk)
nearNetworkId,
nearMasterAccount,
nearAPI.KeyPair.fromString(nearMasterSk)
)
const near = await nearAPI.connect({
nodeUrl: RainbowConfig.getParam('near-node-url'),
networkId: RainbowConfig.getParam('near-network-id'),
masterAccount: masterAccount,
nodeUrl: nearNodeUrl,
networkId: nearNetworkId,
masterAccount: nearMasterAccount,
deps: {
keyStore: keyStore
}
})

const relay = new Eth2NearRelay()
const clientContract = new EthOnNearClientContract(
new nearAPI.Account(near.connection, masterAccount),
RainbowConfig.getParam('near-client-account')
new nearAPI.Account(near.connection, nearMasterAccount),
nearClientAccount
)
await clientContract.accessKeyInit()
console.log('Initializing eth2near-relay...')
relay.initialize(clientContract, RainbowConfig.getParam('eth-node-url'))
relay.initialize(clientContract, { ethNodeUrl })
console.log('Starting eth2near-relay...')
await relay.run()
}
Expand Down
14 changes: 6 additions & 8 deletions cli/commands/start/ganache.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const ProcessManager = require('pm2')
const { spawnProcess } = require('./helpers')
const { RainbowConfig, getScript } = require('rainbow-bridge-utils')
const { getScript } = require('rainbow-bridge-utils')

class StartGanacheNodeCommand {
static async execute () {
Expand All @@ -21,13 +21,11 @@ class StartGanacheNodeCommand {
logDateFormat: 'YYYY-MM-DD HH:mm:ss.SSS'
})
})
RainbowConfig.setParam('eth-node-url', 'ws://localhost:9545')
RainbowConfig.setParam(
'eth-master-sk',
'0x2bdd21761a483f71054e14f5b827213567971c676928d9a1808cbfa4b7501200'
)
RainbowConfig.setParam('near-client-validate-ethash', 'false')
RainbowConfig.saveConfig()
return {
ethNodeUrl: 'ws://localhost:9545',
ethMasterSk: '0x2bdd21761a483f71054e14f5b827213567971c676928d9a1808cbfa4b7501200',
nearClientValidateEthash: 'false'
}
}
}

Expand Down
16 changes: 7 additions & 9 deletions cli/commands/start/near.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const util = require('util')
const { execSync } = require('child_process')
const request = require('request')

const { getLocalNearNodeURL } = require('./helpers')
const { RainbowConfig } = require('rainbow-bridge-utils')

class StartLocalNearNodeCommand {
static execute () {
Expand All @@ -17,14 +17,12 @@ class StartLocalNearNodeCommand {
console.log('Local Node is already running. Skipping...')
}
})
RainbowConfig.setParam('near-node-url', 'http://localhost:3030')
RainbowConfig.setParam('near-network-id', 'local')
RainbowConfig.setParam('near-master-account', 'node0')
RainbowConfig.setParam(
'near-master-sk',
'ed25519:3D4YudUQRE39Lc4JHghuB5WM8kbgDDa34mnrEP5DdTApVH81af7e2dWgNPEaiQfdJnZq1CNPp5im4Rg5b733oiMP'
)
RainbowConfig.saveConfig()
return {
nearNodeUrl: 'http://localhost:3030',
nearNetworkId: 'local',
nearMasterAccount: 'node0',
nearMasterSk: 'ed25519:3D4YudUQRE39Lc4JHghuB5WM8kbgDDa34mnrEP5DdTApVH81af7e2dWgNPEaiQfdJnZq1CNPp5im4Rg5b733oiMP'
}
}
}

Expand Down
49 changes: 43 additions & 6 deletions cli/commands/start/near2eth-relay.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
const ProcessManager = require('pm2')
const { spawnProcess } = require('./helpers')
const { Near2EthRelay } = require('rainbow-bridge-near2eth-block-relay')
const { RainbowConfig } = require('rainbow-bridge-utils')
const path = require('path')

class StartNear2EthRelayCommand {
static async execute () {
if (RainbowConfig.getParam('daemon') === 'true') {
static async execute ({
daemon,
nearNodeUrl,
nearNetworkId,
ethNodeUrl,
ethMasterSk,
ethClientAbiPath,
ethClientAddress,
ethGasMultiplier,
near2ethRelayMinDelay,
near2ethRelayMaxDelay,
near2ethRelayErrorDelay
}) {
if (daemon === 'true') {
ProcessManager.connect((err) => {
if (err) {
console.log(
Expand All @@ -20,16 +31,42 @@ class StartNear2EthRelayCommand {
interpreter: 'node',
error_file: '~/.rainbow/logs/near2eth-relay/err.log',
out_file: '~/.rainbow/logs/near2eth-relay/out.log',
args: ['start', 'near2eth-relay', ...RainbowConfig.getArgsNoDaemon()],
args: [
'start', 'near2eth-relay',
'--near-node-url', nearNodeUrl,
'--near-network-id', nearNetworkId,
'--eth-node-url', ethNodeUrl,
'--eth-master-sk', ethMasterSk,
'--eth-client-abi-path', ethClientAbiPath,
'--eth-client-address', ethClientAddress,
'--eth-gas-multiplier', ethGasMultiplier,
'--near2eth-relay-min-delay', near2ethRelayMinDelay,
'--near2eth-relay-max-delay', near2ethRelayMaxDelay,
'--near2eth-relay-error-delay', near2ethRelayErrorDelay,
'--daemon', 'false'
],
wait_ready: true,
kill_timeout: 60000,
logDateFormat: 'YYYY-MM-DD HH:mm:ss.SSS'
})
})
} else {
const relay = new Near2EthRelay()
await relay.initialize()
await relay.run()
await relay.initialize({
nearNodeUrl,
nearNetworkId,
ethNodeUrl,
ethMasterSk,
ethClientAbiPath,
ethClientAddress,
ethGasMultiplier
})
await relay.run({
near2ethRelayMinDelay,
near2ethRelayMaxDelay,
near2ethRelayErrorDelay,
ethGasMultiplier
})
}
}
}
Expand Down
Loading

0 comments on commit cf20de6

Please sign in to comment.