Skip to content

Commit

Permalink
Add scripts to run relays in testnets
Browse files Browse the repository at this point in the history
  • Loading branch information
k06a committed Feb 14, 2020
1 parent 700378b commit a83a71f
Show file tree
Hide file tree
Showing 12 changed files with 315 additions and 68 deletions.
24 changes: 24 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ node_js:
- '10'
env:
- ETHBRIDGE=1
- ETHRELAY=1
- NEARBRIDGE=1
- NEARBRIDGE_LINT=1
- NEARBRIDGE_COVERAGE=1
- NEARRELAY=1
script:
- if [ $ETHBRIDGE == "1" ]; then
cd ./ethbridge;
Expand All @@ -21,6 +23,18 @@ script:
rustup target add wasm32-unknown-unknown --toolchain stable;
./test.sh;
fi
- if [ $ETHRELAY == "1" ]; then
cd ./ethbridge;
sh <(curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs) -y && export PATH=$PATH:~/.cargo/bin;
rustup toolchain install stable;
rustup default stable;
rustup target add wasm32-unknown-unknown --toolchain stable;
./build.sh;
cd ../ethrelay/ethashproof;
./build.sh;
cd ..;
yarn && yarn run test;
fi
- if [ $NEARBRIDGE == "1" ]; then
cd ./nearbridge;
yarn;
Expand All @@ -37,3 +51,13 @@ script:
yarn;
yarn run coverage && cat coverage/lcov.info | coveralls;
fi
- if [ $NEARRELAY == "1" ]; then
cd ./ethbridge;
sh <(curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs) -y && export PATH=$PATH:~/.cargo/bin;
rustup toolchain install stable;
rustup default stable;
rustup target add wasm32-unknown-unknown --toolchain stable;
./build.sh;
cd ../nearrelay;
yarn && yarn run test;
fi
16 changes: 3 additions & 13 deletions ethrelay/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,6 @@ function subscribeOnBlocksRangesFrom(web3, block_number, handler) {
});
}

function arrayPrefixU32Length(array) {
return [
Math.trunc(array.length) % 256,
Math.trunc(array.length / 256) % 256,
Math.trunc(array.length / 256 / 256) % 256,
Math.trunc(array.length / 256 / 256 / 256) % 256,
].concat(...array);
}

const hexToBuffer = (hex) => Buffer.from(Web3.utils.hexToBytes(hex));
const readerToHex = (len) => (reader) => Web3.utils.bytesToHex(reader.read_fixed_array(len));

Expand Down Expand Up @@ -341,16 +332,15 @@ class EthBridgeContract extends Contract {

(async function () {

const web3 = new Web3("wss://mainnet.infura.io/ws/v3/b5f870422ee5454fb11937e947154cd2");
const web3 = new Web3(process.env.ETHEREUM_NODE_URL);
const near = await nearlib.connect({
nodeUrl: 'http://localhost:3030', //'https://rpc.nearprotocol.com',
networkId: 'local', // TODO: detect automatically
nodeUrl: process.env.NEAR_NODE_URL, // 'https://rpc.nearprotocol.com',
networkId: process.env.NEAR_NODE_NETWORK_ID, // TODO: detect automatically
deps: {
keyStore: new nearlib.keyStores.UnencryptedFileSystemKeyStore(__dirname + '/neardev')
}
});


const account = new nearlib.Account(near.connection, 'ethbridge');

const ethBridgeContract = new EthBridgeContract(account, 'ethbridge');
Expand Down
4 changes: 3 additions & 1 deletion ethrelay/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
"description": "Ethereum >> Near relay",
"main": "index.js",
"scripts": {
"start": "scripts/start.sh"
"test": "scripts/run_localnet.sh",
"start": "scripts/run_testnet.sh"
},
"author": "",
"license": "MIT",
"dependencies": {
"near-shell": "^0.20.1",
"nearlib": "^0.21.0",
"ganache-cli": "^6.9.0",
"web3": "^1.2.4"
}
}
89 changes: 89 additions & 0 deletions ethrelay/scripts/run_localnet.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#!/usr/bin/env bash

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"

# Exit script as soon as a command fails.
set -o errexit

# Executes cleanup function at script exit.
trap cleanup EXIT

waitport() {
while ! nc -z localhost $1 ; do sleep 1 ; done
}

cleanup() {
# Kill the nearnode instance that we started (if we started one and if it's still running).
if [ -n "$node_started" ]; then
docker kill nearcore watchtower > /dev/null &
fi

# Kill the ganache instance that we started (if we started one and if it's still running).
if [ -n "$ganache_pid" ] && ps -p $ganache_pid > /dev/null; then
kill $ganache_pid
fi
}

nearnode_port=24567

nearnode_running() {
nc -z localhost "$nearnode_port"
}

start_nearnode() {
echo "ethrelay" | "$DIR/start_localnet.py" --home "$DIR/.near" --image "nearprotocol/nearcore:ethdenver"
waitport $nearnode_port
}

if nearnode_running; then
echo "Using existing nearnode instance"
else
echo "Starting our own nearnode instance"
rm -rf "$DIR/.near"
start_nearnode
node_started=1
fi

ganache_port=9545

ganache_running() {
nc -z localhost "$ganache_port"
}

start_ganache() {
local accounts=(
--account="0x2bdd21761a483f71054e14f5b827213567971c676928d9a1808cbfa4b7501200,1000000000000000000000000"
--account="0x2bdd21761a483f71054e14f5b827213567971c676928d9a1808cbfa4b7501201,1000000000000000000000000"
--account="0x2bdd21761a483f71054e14f5b827213567971c676928d9a1808cbfa4b7501202,1000000000000000000000000"
--account="0x2bdd21761a483f71054e14f5b827213567971c676928d9a1808cbfa4b7501203,1000000000000000000000000"
--account="0x2bdd21761a483f71054e14f5b827213567971c676928d9a1808cbfa4b7501204,1000000000000000000000000"
--account="0x2bdd21761a483f71054e14f5b827213567971c676928d9a1808cbfa4b7501205,1000000000000000000000000"
--account="0x2bdd21761a483f71054e14f5b827213567971c676928d9a1808cbfa4b7501206,1000000000000000000000000"
--account="0x2bdd21761a483f71054e14f5b827213567971c676928d9a1808cbfa4b7501207,1000000000000000000000000"
--account="0x2bdd21761a483f71054e14f5b827213567971c676928d9a1808cbfa4b7501208,1000000000000000000000000"
--account="0x2bdd21761a483f71054e14f5b827213567971c676928d9a1808cbfa4b7501209,1000000000000000000000000"
)

yarn run ganache-cli --blockTime 12 --gasLimit 10000000 -p "$ganache_port" "${accounts[@]}" > /dev/null &
ganache_pid=$!
waitport $ganache_port
}

if ganache_running; then
echo "Using existing ganache instance"
else
echo "Starting our own ganache instance"
start_ganache
fi

NODE_URL="http://localhost:3030"

echo "Creating account for smart contract:"
NODE_ENV=local yarn run near --nodeUrl=$NODE_URL --homeDir "$DIR/.near" --keyPath "$DIR/.near/validator_key.json" create_account ethbridge --masterAccount=ethrelay --initialBalance 100000000 || echo "Skip creating ethbridge accout"
echo "Deploying smart contract:"
NODE_ENV=local yarn run near --nodeUrl=$NODE_URL --homeDir "$DIR/.near" --keyPath "$DIR/.near/validator_key.json" deploy --contractName ethbridge --wasmFile "$DIR/../../ethbridge/res/eth_bridge.wasm" || echo "Skip deploying ethbridge smart contract"

NEAR_NODE_URL="http://localhost:3030" \
NEAR_NODE_NETWORK_ID=local \
ETHEREUM_NODE_URL="ws://localhost:$ganache_port" \
node "$DIR/../index.js"
21 changes: 21 additions & 0 deletions ethrelay/scripts/run_testnet.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env bash

# Exit script as soon as a command fails.
set -o errexit

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
NODE_URL="https://rpc.nearprotocol.com"

echo "Creating account for smart contract:"
NODE_ENV=local yarn run near --nodeUrl=$NODE_URL --homeDir "$DIR/.near" --keyPath "$DIR/.near/validator_key.json" create_account ethbridge --masterAccount=ethrelay --initialBalance 100000000 || echo "Skip creating ethbridge accout"
echo "Deploying smart contract:"
NODE_ENV=local yarn run near --nodeUrl=$NODE_URL --homeDir "$DIR/.near" --keyPath "$DIR/.near/validator_key.json" deploy --contractName ethbridge --wasmFile "$DIR/../../ethbridge/res/eth_bridge.wasm" || echo "Skip deploying ethbridge smart contract"

# Successfully stop after 5m
{ sleep 5m && kill -l 0 $$ && echo "Successfully worked for 5m, stopped" } &

# Launch EthRelay
NEAR_NODE_URL="https://rpc.nearprotocol.com" \
NEAR_NODE_NETWORK_ID=local \
ETHEREUM_NODE_URL="wss://ropsten.infura.io/ws/v3/b5f870422ee5454fb11937e947154cd2" \
node "$DIR/../index.js"
45 changes: 0 additions & 45 deletions ethrelay/scripts/start.sh

This file was deleted.

Loading

0 comments on commit a83a71f

Please sign in to comment.