Skip to content

Commit 5ae91ae

Browse files
authored
Integration scripts now do all transfers in python and have better waiting (#410)
Co-authored-by: James Moore <james@sifchain.finance>
1 parent 58133c9 commit 5ae91ae

File tree

4 files changed

+100
-38
lines changed

4 files changed

+100
-38
lines changed

Diff for: test/integration/initial_test_balances.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import time
2+
3+
from test_utilities import network_password, owner_addr, amount_in_wei, user1_addr, \
4+
sif_tx_send, transact_ethereum_currency_to_sifchain_addr, get_shell_output, wait_for_sif_account
5+
6+
7+
def setup_currencies():
8+
print(f"adding eth to {owner_addr}")
9+
transact_ethereum_currency_to_sifchain_addr(owner_addr, "eth", amount_in_wei(10))
10+
print(f"adding eth to {user1_addr}")
11+
transact_ethereum_currency_to_sifchain_addr(user1_addr, "eth", amount_in_wei(13))
12+
time.sleep(15)
13+
sif_tx_send(owner_addr, user1_addr, amount_in_wei(23), "rowan", network_password)
14+
15+
16+
setup_currencies()

Diff for: test/integration/peggy-e2e-test.py

+1-8
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from test_utilities import get_shell_output, SIF_ETH, burn_peggy_coin, ETHEREUM_ETH, owner_addr, moniker, \
77
get_sifchain_addr_balance, wait_for_sifchain_addr_balance, advance_n_ethereum_blocks, n_wait_blocks, \
8-
cd_smart_contracts_dir
8+
cd_smart_contracts_dir, send_eth_lock
99
from test_utilities import print_error_message, get_user_account, get_sifchain_balance, network_password, \
1010
bridge_bank_address, \
1111
smart_contracts_dir, wait_for_sifchain_balance, wait_for_balance
@@ -54,13 +54,6 @@ def get_peggyrwn_balance(account, symbol):
5454
return 0
5555

5656

57-
# Send eth from ETHEREUM_PRIVATE_KEY to BridgeBank, lock the eth on bridgebank, ceth should end up in sifchain_user
58-
def send_eth_lock(sifchain_user, symbol, amount):
59-
command_line = cd_smart_contracts_dir + "yarn peggy:lock {} {} {}".format(
60-
get_user_account(sifchain_user, network_password), symbol, amount)
61-
get_shell_output(command_line)
62-
63-
6457
def burn_peggyrwn(sifchain_user, peggyrwn_contract, amount):
6558
command_line = cd_smart_contracts_dir + "yarn peggy:burn {} {} {}".format(
6659
get_user_account(sifchain_user, network_password), peggyrwn_contract, amount)

Diff for: test/integration/start-integration-env.sh

+13-26
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,23 @@ echo "export YARN_CACHE_DIR=$YARN_CACHE_DIR" >> $envexportfile
3838

3939
docker-compose --project-name genesis -f $BASEDIR/test/integration/docker-compose-ganache.yml up -d --force-recreate
4040

41-
# deploy peggy smart contracts
42-
if [ ! -f .env ]; then
43-
# if you haven't created a .env file, use .env.example
44-
cp $BASEDIR/test/integration/.env.ciExample .env
45-
fi
41+
cp $BASEDIR/test/integration/.env.ciExample .env
4642

4743
# https://www.trufflesuite.com/docs/truffle/overview
4844
# and note that truffle migrate and truffle deploy are the same command
4945
truffle compile
5046
truffle deploy --network develop --reset
51-
ETHEREUM_CONTRACT_ADDRESS=$(cat $BASEDIR/smart-contracts/build/contracts/BridgeRegistry.json | jq '.networks["5777"].address')
47+
# ETHEREUM_CONTRACT_ADDRESS is used for the BridgeRegistry address in many places, so we
48+
# set it and BRIDGE_REGISTRY_ADDRESS to the same value
49+
BRIDGE_REGISTRY_ADDRESS=$(cat $BASEDIR/smart-contracts/build/contracts/BridgeRegistry.json | jq '.networks["5777"].address')
50+
ETHEREUM_CONTRACT_ADDRESS=$BRIDGE_REGISTRY_ADDRESS
5251
if [ -z "$ETHEREUM_CONTRACT_ADDRESS" ]; then
5352
echo ETHEREUM_CONTRACT_ADDRESS cannot be empty
5453
exit 1
5554
fi
5655
echo "export ETHEREUM_CONTRACT_ADDRESS=$ETHEREUM_CONTRACT_ADDRESS" >> $envexportfile
56+
echo "# BRIDGE_REGISTRY_ADDRESS and ETHEREUM_CONTRACT_ADDRESS are synonyms">> $envexportfile
57+
echo "export BRIDGE_REGISTRY_ADDRESS=$BRIDGE_REGISTRY_ADDRESS" >> $envexportfile
5758

5859
export BRIDGE_BANK_ADDRESS=$(cat $BASEDIR/smart-contracts/build/contracts/BridgeBank.json | jq '.networks["5777"].address')
5960
if [ -z "BRIDGE_BANK_ADDRESS" ]; then
@@ -70,6 +71,8 @@ BASEDIR=${BASEDIR} rake genesis:network:scaffold['localnet']
7071
# :chainnet, :eth_bridge_registry_address, :eth_keys, :eth_websocket
7172
BASEDIR=${BASEDIR} rake genesis:network:boot["localnet,${ETHEREUM_CONTRACT_ADDRESS},c87509a1c067bbde78beb793e6fa76530b6382a4c0241e5e4a9ec0a0f44dc0d3,ws://192.168.2.6:7545/"]
7273

74+
sleep 15
75+
7376
#
7477
# Wait for the Websocket subscriptions to be initialized (like 10 seconds)
7578
#
@@ -92,6 +95,8 @@ done
9295

9396
export MONIKER=$(cat ${NETWORKDIR}/network-definition.yml | to_json | jq '.[0].moniker')
9497
echo "export MONIKER=$MONIKER" >> $envexportfile
98+
export MONIKER_ADDR=$(cat ${NETWORKDIR}/network-definition.yml | to_json | jq '.[0].moniker')
99+
echo "export MONIKER=$MONIKER" >> $envexportfile
95100

96101
OWNER_PASSWORD=$(cat $NETDEF | yq r - ".password")
97102
echo "export OWNER_PASSWORD=$OWNER_PASSWORD" >> $envexportfile
@@ -104,33 +109,15 @@ echo "export OWNER_ADDR=$OWNER_ADDR" >> $envexportfile
104109
#
105110
docker exec ${CONTAINER_NAME} bash -c "/test/integration/add-second-account.sh"
106111

107-
#
108-
# Transfer Eth into Ceth in our validator account
109-
#
110-
yarn --cwd $BASEDIR/smart-contracts peggy:lock ${OWNER_ADDR} 0x0000000000000000000000000000000000000000 $(to_wei 10)
111-
112-
# balance:
113-
#
114-
# Transfer Eth into Ceth on our User account
115-
# This also makes the account visible to sifnodecli q auth account <addr>
116-
117112
export USER1ADDR=$(cat $NETDEF | yq r - "[1].address")
118113
echo "export USER1ADDR=$USER1ADDR" >> $envexportfile
119114

120-
sleep 5
121-
yarn --cwd $BASEDIR/smart-contracts peggy:lock ${USER1ADDR} 0x0000000000000000000000000000000000000000 $(to_wei 13)
122-
yarn --cwd $BASEDIR/smart-contracts advance 50
123-
124-
#
125-
# Transfer Rowan from valifadator account to user account
126-
#
127-
docker exec ${CONTAINER_NAME} bash -c "/test/integration/add-rowan-to-account.sh $(to_wei 23) user1"
128-
sleep 5
129-
130115
#
131116
# Run the python tests
132117
#
133118
echo run python tests
119+
docker exec ${CONTAINER_NAME} bash -c ". /test/integration/vagrantenv.sh; cd /sifnode; SMART_CONTRACTS_DIR=/smart-contracts python3 /test/integration/initial_test_balances.py /network-definition.yml"
120+
sleep 15
134121
docker exec ${CONTAINER_NAME} bash -c ". /test/integration/vagrantenv.sh; cd /sifnode; SMART_CONTRACTS_DIR=/smart-contracts python3 /test/integration/peggy-basic-test-docker.py /network-definition.yml"
135122
docker exec ${CONTAINER_NAME} bash -c '. /test/integration/vagrantenv.sh; cd /sifnode; SMART_CONTRACTS_DIR=/smart-contracts python3 /test/integration/peggy-e2e-test.py /network-definition.yml'
136123

Diff for: test/integration/test_utilities.py

+70-4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def get_required_env_var(name):
3535
cd_smart_contracts_dir = f"cd {smart_contracts_dir}; "
3636
moniker = get_required_env_var("MONIKER")
3737
owner_addr = get_required_env_var("OWNER_ADDR")
38+
user1_addr = get_required_env_var("USER1ADDR")
3839

3940

4041
def test_log_line(s):
@@ -112,7 +113,7 @@ def get_sifchain_addr_balance(sifaddress, denom):
112113
# balance_fn is a lambda that takes no arguments
113114
# and returns a result. Runs the function up to
114115
# max_attempts times, or until the result is equal to target_balance
115-
def wait_for_balance(balance_fn, target_balance, max_attempts=30):
116+
def wait_for_balance(balance_fn, target_balance, max_attempts=30, debug_prefix=""):
116117
attempts = 0
117118
while True:
118119
balance = balance_fn()
@@ -121,17 +122,28 @@ def wait_for_balance(balance_fn, target_balance, max_attempts=30):
121122
else:
122123
attempts += 1
123124
if attempts >= max_attempts:
124-
print_error_message(f"Failed to get target balance of {target_balance}, balance is {balance}")
125+
print_error_message(
126+
f"{debug_prefix} Failed to get target balance of {target_balance}, balance is {balance}")
125127
else:
128+
if verbose:
129+
print(
130+
f"waiting for target balance {debug_prefix}: {target_balance}, current balance is {balance}, attempt {attempts}")
126131
time.sleep(1)
127132

128133

129134
def wait_for_sifchain_balance(user, denom, network_password, target_balance, max_attempts=30):
130135
wait_for_balance(lambda: int(get_sifchain_balance(user, denom, network_password)), target_balance, max_attempts)
131136

132137

133-
def wait_for_sifchain_addr_balance(sif_addr, denom, target_balance, max_attempts=30):
134-
wait_for_balance(lambda: int(get_sifchain_addr_balance(sif_addr, denom)), target_balance, max_attempts)
138+
def wait_for_sifchain_addr_balance(sif_addr, denom, target_balance, max_attempts=30, debug_prefix=""):
139+
if not max_attempts: max_attempts = 30
140+
wait_for_balance(lambda: int(get_sifchain_addr_balance(sif_addr, denom)), target_balance, max_attempts,
141+
debug_prefix)
142+
143+
144+
def sif_tx_send(from_address, to_address, amount, currency, network_password):
145+
cmd = f"yes {network_password} | sifnodecli tx send {from_address} {to_address} {amount}{currency} -y"
146+
return get_shell_output(cmd)
135147

136148

137149
def burn_peggy_coin(user, eth_user, amount):
@@ -143,6 +155,60 @@ def burn_peggy_coin(user, eth_user, amount):
143155
return get_shell_output(command_line)
144156

145157

158+
# Send eth from ETHEREUM_PRIVATE_KEY to BridgeBank, lock the eth on bridgebank, ceth should end up in sifchain_user
159+
def send_eth_lock(sifchain_user, symbol, amount):
160+
return send_ethereum_currency_to_sifchain_addr(get_user_account(sifchain_user, network_password), symbol, amount)
161+
162+
163+
def send_ethereum_currency_to_sifchain_addr(sif_addr, symbol, amount):
164+
command_line = f"{cd_smart_contracts_dir} yarn peggy:lock {sif_addr} {symbol} {amount}"
165+
return get_shell_output(command_line)
166+
167+
168+
currency_pairs = {
169+
"eth": "ceth",
170+
"ceth": "eth",
171+
"rowan": "erowan",
172+
"erowan": "rowan"
173+
}
174+
175+
176+
def mirror_of(currency):
177+
return currency_pairs.get(currency)
178+
179+
180+
def wait_for_sif_account(sif_addr, max_attempts=30):
181+
command = f"sifnodecli q account {sif_addr}"
182+
attempts = 0
183+
while True:
184+
try:
185+
result = get_shell_output(command)
186+
print(f"account {sif_addr} is now created")
187+
return result
188+
except:
189+
attempts += 1
190+
if attempts > max_attempts:
191+
raise Exception(f"too many attempts to get sif account {sif_addr}")
192+
time.sleep(1)
193+
194+
195+
def transact_ethereum_currency_to_sifchain_addr(sif_addr, ethereum_symbol, amount):
196+
sifchain_symbol = mirror_of(ethereum_symbol)
197+
try:
198+
starting_balance = get_sifchain_addr_balance(sif_addr, sifchain_symbol)
199+
except:
200+
# Sometimes we're creating an account by sending it currency for the
201+
# first time, so you can't get a balance.
202+
print("exception is OK, we are creating the account now")
203+
starting_balance = 0
204+
print(f"starting balance for {sif_addr} is {starting_balance}")
205+
send_ethereum_currency_to_sifchain_addr(sif_addr, ethereum_symbol, amount)
206+
advance_n_ethereum_blocks(n_wait_blocks)
207+
wait_for_sif_account(sif_addr)
208+
wait_for_sifchain_addr_balance(sif_addr, sifchain_symbol, starting_balance + amount, 6,
209+
f"{sif_addr} / c{sifchain_symbol} / {amount}")
210+
211+
146212
def advance_n_ethereum_blocks(n=50):
147213
return run_yarn_command(f"{cd_smart_contracts_dir} yarn advance {n}")
148214

0 commit comments

Comments
 (0)