From 92d42257a5f050c9bbbf4c77e0a396db8a5131c5 Mon Sep 17 00:00:00 2001 From: Reuven Podmazo Date: Wed, 22 Jan 2020 18:13:04 +0200 Subject: [PATCH 01/14] after deployment of the salad contracts, we now wait for one epoch to pass. This is a workaround to a current issue where if the deployment and the first task happen in the same epoch, we hit several race conditions in the protocol implementation --- salad/operator/scripts/run.sh | 1 + .../scripts/wait_for_epoch_transition.js | 38 +++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 salad/operator/scripts/wait_for_epoch_transition.js diff --git a/salad/operator/scripts/run.sh b/salad/operator/scripts/run.sh index 833013b..3cbbed5 100755 --- a/salad/operator/scripts/run.sh +++ b/salad/operator/scripts/run.sh @@ -8,6 +8,7 @@ if ! ./scripts/take_salad_contract_addresses_from_environment.js; then echo 'Running deployment...' ENV=$(echo "$ENIGMA_ENV" | tr '[:upper:]' '[:lower:]') npx truffle migrate --network $ENV + ./scripts/wait_for_epoch_transition.js echo 'Done deployment!' fi diff --git a/salad/operator/scripts/wait_for_epoch_transition.js b/salad/operator/scripts/wait_for_epoch_transition.js new file mode 100644 index 0000000..7a69810 --- /dev/null +++ b/salad/operator/scripts/wait_for_epoch_transition.js @@ -0,0 +1,38 @@ +#!/usr/bin/env node + +const Web3 = require('web3'); +const {Enigma} = require('enigma-js/node'); + +const main = async () => { + const provider = new Web3.providers.WebsocketProvider(`ws://${process.env.ETH_HOST}:${process.env.ETH_PORT}`); + const web3 = new Web3(provider); + + let enigmaHost = process.env.ENIGMA_HOST || 'localhost'; + let enigmaPort = process.env.ENIGMA_PORT || '3333'; + const enigmaAddr = process.env.ENIGMA_CONTRACT_ADDRESS; + const enigmaTokenAddr = process.env.ENIGMA_TOKEN_CONTRACT_ADDRESS; + + const enigma = new Enigma( + web3, + enigmaAddr, + enigmaTokenAddr, + 'http://' + enigmaHost + ':' + enigmaPort, + { + gas: 4712388, + from: web3.eth.accounts[0], + }, + ); + + const enigmaContract = enigma.enigmaContract; + console.error('Waiting for an epoch to pass after the contract deployment'); + await new Promise(resolve => + enigmaContract.events.WorkersParameterized({}) + .once('data', data => { + console.error(`got data ${data}`); + resolve(data); + }) + ); + console.error('an epoch has passed'); +}; + +main().catch(err => { console.error(err); process.exit(1) }); From 5c057fb1b384678d9cc3298f48b71c518a742180 Mon Sep 17 00:00:00 2001 From: Reuven Podmazo Date: Sun, 26 Jan 2020 12:32:36 +0200 Subject: [PATCH 02/14] changed several uses of console.error to .log or .info --- .../take_salad_contract_addresses_from_environment.js | 4 ++-- salad/operator/scripts/wait_for_epoch_transition.js | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/salad/operator/scripts/take_salad_contract_addresses_from_environment.js b/salad/operator/scripts/take_salad_contract_addresses_from_environment.js index 087d7c5..86c897e 100755 --- a/salad/operator/scripts/take_salad_contract_addresses_from_environment.js +++ b/salad/operator/scripts/take_salad_contract_addresses_from_environment.js @@ -38,10 +38,10 @@ async function main() { await store.insertSmartContractAddress(process.env.SALAD_SMART_CONTRACT_ADDRESS); await store.insertSecretContractAddress(process.env.SALAD_SECRET_CONTRACT_ADDRESS); } catch (e) { - log.error('Error while taking contract addresses from env', e); + log.info('Error while taking contract addresses from env', e); } finally { await store.closeAsync(); } } -main().catch(err => { log.error(err); process.exit(1) }); +main().catch(err => { log.info(err); process.exit(1) }); diff --git a/salad/operator/scripts/wait_for_epoch_transition.js b/salad/operator/scripts/wait_for_epoch_transition.js index 7a69810..c53e096 100644 --- a/salad/operator/scripts/wait_for_epoch_transition.js +++ b/salad/operator/scripts/wait_for_epoch_transition.js @@ -24,15 +24,15 @@ const main = async () => { ); const enigmaContract = enigma.enigmaContract; - console.error('Waiting for an epoch to pass after the contract deployment'); + console.log('Waiting for an epoch to pass after the contract deployment'); await new Promise(resolve => enigmaContract.events.WorkersParameterized({}) .once('data', data => { - console.error(`got data ${data}`); + console.log(`got data ${data}`); resolve(data); }) ); - console.error('an epoch has passed'); + console.log('an epoch has passed'); }; -main().catch(err => { console.error(err); process.exit(1) }); +main().catch(err => { console.log(err); process.exit(1) }); From 59b1c1697165e30a9bb5302d54ea421dea6062ce Mon Sep 17 00:00:00 2001 From: Reuven Podmazo Date: Sun, 26 Jan 2020 19:14:30 +0200 Subject: [PATCH 03/14] We now generate a custom address for the operator if one was not supplied externally --- salad/operator/scripts/create_account.js | 12 ++++++++++++ salad/operator/scripts/distribute_funds.py | 16 ++++++++++++++++ salad/operator/scripts/run.sh | 11 +++++++++++ 3 files changed, 39 insertions(+) create mode 100644 salad/operator/scripts/create_account.js create mode 100644 salad/operator/scripts/distribute_funds.py diff --git a/salad/operator/scripts/create_account.js b/salad/operator/scripts/create_account.js new file mode 100644 index 0000000..961016f --- /dev/null +++ b/salad/operator/scripts/create_account.js @@ -0,0 +1,12 @@ +#! /usr/bin/env node + +const Web3 = require('web3'); + +function main() { + const web3 = new Web3(); + web3.eth.accounts.wallet.create(1); + let account = web3.eth.accounts.wallet[0]; + console.log(account.address, account.privateKey); +} + +main(); diff --git a/salad/operator/scripts/distribute_funds.py b/salad/operator/scripts/distribute_funds.py new file mode 100644 index 0000000..b862a0e --- /dev/null +++ b/salad/operator/scripts/distribute_funds.py @@ -0,0 +1,16 @@ +#!/usr/bin/env python3 + +import sys +from enigma_docker_common.faucet_api import request_coins + + +def main(): + addresses = sys.argv[1:] + for address in addresses: + for i in range(20): + print(f'{i} - distributing ether to {address}') + request_coins('http://contract.reuven.services.enigma.co:8001', address, 'ether') + + +if __name__ == '__main__': + main() diff --git a/salad/operator/scripts/run.sh b/salad/operator/scripts/run.sh index 3cbbed5..f0ea885 100755 --- a/salad/operator/scripts/run.sh +++ b/salad/operator/scripts/run.sh @@ -12,4 +12,15 @@ if ! ./scripts/take_salad_contract_addresses_from_environment.js; then echo 'Done deployment!' fi +# If the operator's private key has not been supplied externally, provide it here. +if [[ -z "$OPERATOR_ETH_PRIVATE_KEY" ]]; then + ACCOUNT_1="$(./scripts/create_account.js)" + + ADDRESS_1="$(echo "$ACCOUNT_1" | cut -d' ' -f1)" + PRIVATE_KEY_1="$(echo "$ACCOUNT_1" | cut -d' ' -f2)" + export OPERATOR_ETH_PRIVATE_KEY="$PRIVATE_KEY_1" + + ./scripts/distribute_funds.py "$ADDRESS_1" +fi + node ./operator/src/server.js From bb6ffca2d8f2d346bda95043c7d2d5c19c6a62c3 Mon Sep 17 00:00:00 2001 From: Reuven Podmazo Date: Sun, 26 Jan 2020 19:26:41 +0200 Subject: [PATCH 04/14] the operator now clears its cache when starting up --- salad/operator/scripts/run.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/salad/operator/scripts/run.sh b/salad/operator/scripts/run.sh index f0ea885..0c87d00 100755 --- a/salad/operator/scripts/run.sh +++ b/salad/operator/scripts/run.sh @@ -23,4 +23,4 @@ if [[ -z "$OPERATOR_ETH_PRIVATE_KEY" ]]; then ./scripts/distribute_funds.py "$ADDRESS_1" fi -node ./operator/src/server.js +node ./operator/src/server.js -t From 39db8afe3b9051403c6c9a279fba36c88b12e9ad Mon Sep 17 00:00:00 2001 From: Reuven Podmazo Date: Sun, 26 Jan 2020 19:27:49 +0200 Subject: [PATCH 05/14] added exec permissions to a few scripts --- salad/operator/scripts/create_account.js | 0 salad/operator/scripts/distribute_funds.py | 0 2 files changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 salad/operator/scripts/create_account.js mode change 100644 => 100755 salad/operator/scripts/distribute_funds.py diff --git a/salad/operator/scripts/create_account.js b/salad/operator/scripts/create_account.js old mode 100644 new mode 100755 diff --git a/salad/operator/scripts/distribute_funds.py b/salad/operator/scripts/distribute_funds.py old mode 100644 new mode 100755 From 335783372fdf931b33603bb9f4c7439dd01bbf43 Mon Sep 17 00:00:00 2001 From: Reuven Podmazo Date: Mon, 27 Jan 2020 15:34:40 +0200 Subject: [PATCH 06/14] made the wait_for_epoch_transition.js salad operator scrips executable --- salad/operator/scripts/wait_for_epoch_transition.js | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 salad/operator/scripts/wait_for_epoch_transition.js diff --git a/salad/operator/scripts/wait_for_epoch_transition.js b/salad/operator/scripts/wait_for_epoch_transition.js old mode 100644 new mode 100755 From bf301c87a7dccc8f2ed73d30bfcf82e28c455b7c Mon Sep 17 00:00:00 2001 From: Reuven Podmazo Date: Mon, 27 Jan 2020 15:35:15 +0200 Subject: [PATCH 07/14] wait_for_epoch_transition.js now reads the .env file --- salad/operator/scripts/wait_for_epoch_transition.js | 1 + 1 file changed, 1 insertion(+) diff --git a/salad/operator/scripts/wait_for_epoch_transition.js b/salad/operator/scripts/wait_for_epoch_transition.js index c53e096..ee9fe4a 100755 --- a/salad/operator/scripts/wait_for_epoch_transition.js +++ b/salad/operator/scripts/wait_for_epoch_transition.js @@ -1,5 +1,6 @@ #!/usr/bin/env node +require('dotenv').config(); const Web3 = require('web3'); const {Enigma} = require('enigma-js/node'); From db9e2bf5f0d59fee3b676d1c47fa5133cb612154 Mon Sep 17 00:00:00 2001 From: Reuven Podmazo Date: Mon, 27 Jan 2020 15:36:03 +0200 Subject: [PATCH 08/14] wait_for_epoch_transition.js prints the details of the object it receives when an epoch transition event happens --- salad/operator/scripts/wait_for_epoch_transition.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/salad/operator/scripts/wait_for_epoch_transition.js b/salad/operator/scripts/wait_for_epoch_transition.js index ee9fe4a..0cf6313 100755 --- a/salad/operator/scripts/wait_for_epoch_transition.js +++ b/salad/operator/scripts/wait_for_epoch_transition.js @@ -29,7 +29,7 @@ const main = async () => { await new Promise(resolve => enigmaContract.events.WorkersParameterized({}) .once('data', data => { - console.log(`got data ${data}`); + console.log(`got data ${JSON.stringify(data)}`); resolve(data); }) ); From 8b8d305fd74723959b453c39734aeeebeea4aadf Mon Sep 17 00:00:00 2001 From: Reuven Podmazo Date: Mon, 27 Jan 2020 15:36:24 +0200 Subject: [PATCH 09/14] fixed issue where wait_for_epoch_transition.js stalls on startup --- salad/operator/scripts/wait_for_epoch_transition.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/salad/operator/scripts/wait_for_epoch_transition.js b/salad/operator/scripts/wait_for_epoch_transition.js index 0cf6313..71a9c37 100755 --- a/salad/operator/scripts/wait_for_epoch_transition.js +++ b/salad/operator/scripts/wait_for_epoch_transition.js @@ -34,6 +34,8 @@ const main = async () => { }) ); console.log('an epoch has passed'); + await web3.currentProvider.disconnect(); + console.log('provider disconnected'); }; main().catch(err => { console.log(err); process.exit(1) }); From d9196aa81d1119f91236dfe8074d10d0828034f7 Mon Sep 17 00:00:00 2001 From: Reuven Podmazo Date: Mon, 27 Jan 2020 15:37:05 +0200 Subject: [PATCH 10/14] added a log to the operator run.sh script --- salad/operator/scripts/run.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/salad/operator/scripts/run.sh b/salad/operator/scripts/run.sh index 0c87d00..6a5cc85 100755 --- a/salad/operator/scripts/run.sh +++ b/salad/operator/scripts/run.sh @@ -14,6 +14,7 @@ fi # If the operator's private key has not been supplied externally, provide it here. if [[ -z "$OPERATOR_ETH_PRIVATE_KEY" ]]; then + echo 'generating account for operator' ACCOUNT_1="$(./scripts/create_account.js)" ADDRESS_1="$(echo "$ACCOUNT_1" | cut -d' ' -f1)" From 3279c60a4d31195ec12d32d4fff9afac14b74650 Mon Sep 17 00:00:00 2001 From: Reuven Podmazo Date: Mon, 27 Jan 2020 15:37:34 +0200 Subject: [PATCH 11/14] on startup, the operator also receives some eng from the faucet --- salad/operator/scripts/distribute_funds.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/salad/operator/scripts/distribute_funds.py b/salad/operator/scripts/distribute_funds.py index b862a0e..844e013 100755 --- a/salad/operator/scripts/distribute_funds.py +++ b/salad/operator/scripts/distribute_funds.py @@ -10,6 +10,8 @@ def main(): for i in range(20): print(f'{i} - distributing ether to {address}') request_coins('http://contract.reuven.services.enigma.co:8001', address, 'ether') + print(f'{i} - distributing eng to {address}') + request_coins('http://contract.reuven.services.enigma.co:8001', address, 'eng') if __name__ == '__main__': From 768e77623938ef48ceb133e2721d7effad7ec917 Mon Sep 17 00:00:00 2001 From: Reuven Podmazo Date: Tue, 28 Jan 2020 16:30:36 +0200 Subject: [PATCH 12/14] in the salad operator startup, we now access the faucet just once, rather than 20 times --- salad/operator/scripts/distribute_funds.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/salad/operator/scripts/distribute_funds.py b/salad/operator/scripts/distribute_funds.py index 844e013..c9a38d6 100755 --- a/salad/operator/scripts/distribute_funds.py +++ b/salad/operator/scripts/distribute_funds.py @@ -7,11 +7,10 @@ def main(): addresses = sys.argv[1:] for address in addresses: - for i in range(20): - print(f'{i} - distributing ether to {address}') - request_coins('http://contract.reuven.services.enigma.co:8001', address, 'ether') - print(f'{i} - distributing eng to {address}') - request_coins('http://contract.reuven.services.enigma.co:8001', address, 'eng') + print(f'distributing ether to {address}') + request_coins('http://contract.reuven.services.enigma.co:8001', address, 'ether') + print(f'distributing eng to {address}') + request_coins('http://contract.reuven.services.enigma.co:8001', address, 'eng') if __name__ == '__main__': From 4f28dd5c905b657521aab28e85e779e42776b1a4 Mon Sep 17 00:00:00 2001 From: Reuven Podmazo Date: Thu, 30 Jan 2020 15:39:17 +0200 Subject: [PATCH 13/14] added the FAUCET_URL env var to salad operator so it is not hardcoded anymore --- salad/operator/config/compose_config.json | 1 + salad/operator/config/k8s_config.json | 3 ++- salad/operator/scripts/configure.py | 3 ++- salad/operator/scripts/distribute_funds.py | 15 +++++++++++++-- 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/salad/operator/config/compose_config.json b/salad/operator/config/compose_config.json index be40b69..0958cff 100644 --- a/salad/operator/config/compose_config.json +++ b/salad/operator/config/compose_config.json @@ -7,6 +7,7 @@ "ENG_NODE_PORT": "3346", "MONGO_URL": "mongodb://mongo:27017", "DB_NAME": "salad", + "FAUCET_URL": "http://contract:8001", "OPERATOR_ETH_PRIVATE_KEY": null, "SECRET_CONTRACT_BUILD_FOLDER": "..", "SALAD_SMART_CONTRACT_ADDRESS": "", diff --git a/salad/operator/config/k8s_config.json b/salad/operator/config/k8s_config.json index 5b151ab..56fd15b 100644 --- a/salad/operator/config/k8s_config.json +++ b/salad/operator/config/k8s_config.json @@ -7,10 +7,11 @@ "ENG_NODE_PORT": "3346", "MONGO_URL": "mongodb://mongo:27017", "DB_NAME": "salad", + "FAUCET_URL": "http://contract-service:8001", "OPERATOR_ETH_PRIVATE_KEY": null, "SECRET_CONTRACT_BUILD_FOLDER": "..", "SALAD_SMART_CONTRACT_ADDRESS": "", "SALAD_SECRET_CONTRACT_ADDRESS": "", "KEY_MANAGEMENT_DISCOVERY": "unused", "LOG_LEVEL": "debug" -} \ No newline at end of file +} diff --git a/salad/operator/scripts/configure.py b/salad/operator/scripts/configure.py index fb4b4d0..6a5f976 100755 --- a/salad/operator/scripts/configure.py +++ b/salad/operator/scripts/configure.py @@ -54,8 +54,9 @@ def main(): 'SALAD_SMART_CONTRACT_ADDRESS': 'SALAD_SMART_CONTRACT_ADDRESS', 'SALAD_SECRET_CONTRACT_ADDRESS': 'SALAD_SECRET_CONTRACT_ADDRESS', 'NETWORK_ID': 'NETWORK_ID', + 'FAUCET_URL': 'FAUCET_URL' }.items(): - envs[env_var] = config[config_var] + envs[env_var] = config.get(config_var) envs['ENIGMA_CONTRACT_ADDRESS'] = enigma_contract_address envs['ENIGMA_TOKEN_CONTRACT_ADDRESS'] = enigma_token_contract_address diff --git a/salad/operator/scripts/distribute_funds.py b/salad/operator/scripts/distribute_funds.py index c9a38d6..c273d8b 100755 --- a/salad/operator/scripts/distribute_funds.py +++ b/salad/operator/scripts/distribute_funds.py @@ -2,15 +2,26 @@ import sys from enigma_docker_common.faucet_api import request_coins +from enigma_docker_common.config import Config +from enigma_docker_common.logger import get_logger + +logger = get_logger('operator-startup') + +try: + config = Config(required=['FAUCET_URL']) +except (ValueError, IOError) as e: + logger.critical(f'encountered unexpected error while configuring the operator: {e!r}') + raise def main(): addresses = sys.argv[1:] + faucet_url = config['FAUCET_URL'] for address in addresses: print(f'distributing ether to {address}') - request_coins('http://contract.reuven.services.enigma.co:8001', address, 'ether') + request_coins(faucet_url, address, 'ether') print(f'distributing eng to {address}') - request_coins('http://contract.reuven.services.enigma.co:8001', address, 'eng') + request_coins(faucet_url, address, 'eng') if __name__ == '__main__': From 5d4d8f5f8ce54f84f6063332487b6525728790bd Mon Sep 17 00:00:00 2001 From: Reuven Podmazo Date: Sun, 2 Feb 2020 20:21:23 +0200 Subject: [PATCH 14/14] replaced a print with a logger.info --- salad/operator/scripts/distribute_funds.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/salad/operator/scripts/distribute_funds.py b/salad/operator/scripts/distribute_funds.py index c273d8b..7e3e224 100755 --- a/salad/operator/scripts/distribute_funds.py +++ b/salad/operator/scripts/distribute_funds.py @@ -18,9 +18,9 @@ def main(): addresses = sys.argv[1:] faucet_url = config['FAUCET_URL'] for address in addresses: - print(f'distributing ether to {address}') + logger.info(f'distributing ether to {address}') request_coins(faucet_url, address, 'ether') - print(f'distributing eng to {address}') + logger.info(f'distributing eng to {address}') request_coins(faucet_url, address, 'eng')