Skip to content

Commit e1dac35

Browse files
authored
chore: mocknet improvements (near#8709)
As discussed in near#8707 (comment) refactoring some code around neard_amend_genesis_path. I kept this function itself by now it can only return neard.upgrade or raise an exception.
1 parent eebfe23 commit e1dac35

File tree

5 files changed

+205
-231
lines changed

5 files changed

+205
-231
lines changed

pytest/lib/mocknet.py

Lines changed: 83 additions & 164 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,14 @@ def setup_python_environments(nodes, wasm_contract):
165165
pmap(lambda n: setup_python_environment(n, wasm_contract), nodes)
166166

167167

168-
def start_load_test_helper_script(script, node_account_id, rpc_nodes, num_nodes,
169-
max_tps, leader_account_id):
168+
def start_load_test_helper_script(
169+
script,
170+
node_account_id,
171+
rpc_nodes,
172+
num_nodes,
173+
max_tps,
174+
leader_account_id,
175+
):
170176
s = '''
171177
cd {dir}
172178
nohup ./venv/bin/python {script} {node_account_id} {rpc_nodes} {num_nodes} {max_tps} {leader_account_id} 1>load_test.out 2>load_test.err < /dev/null &
@@ -218,7 +224,9 @@ def start_load_test_helpers(nodes, script, rpc_nodes, num_nodes, max_tps):
218224
num_nodes,
219225
max_tps,
220226
lead_account_id=account.account_id,
221-
), nodes)
227+
),
228+
nodes,
229+
)
222230

223231

224232
def get_log(node):
@@ -321,8 +329,10 @@ def get_tx_events_single_node(node, tx_filename):
321329
def get_tx_events(nodes, tx_filename):
322330
run('mkdir ./logs/')
323331
run('rm -rf ./logs/*_txs')
324-
all_events = pmap(lambda node: get_tx_events_single_node(node, tx_filename),
325-
nodes)
332+
all_events = pmap(
333+
lambda node: get_tx_events_single_node(node, tx_filename),
334+
nodes,
335+
)
326336
return sorted(data.flatten(all_events))
327337

328338

@@ -470,15 +480,7 @@ def redownload_neard(nodes, binary_url):
470480

471481

472482
# Check /home/ubuntu/neard.upgrade to see whether the amend-genesis command is
473-
# available. If it is, then we'll use that to update the genesis files,
474-
# otherwise we'll use the python create_genesis_file() function. We can't just
475-
# check this individually on each machine since the two functions have slightly
476-
# different behavior (for example, neard amend-genesis will set the storage
477-
# usage fields in account records), so the resulting genesis block hashes will
478-
# not match up.
479-
#
480-
# Return value is None if it's not available, otherwise the path to the binary
481-
# where it should be available
483+
# available. Returns the path to the neard binary or throws an exception.
482484
def neard_amend_genesis_path(node):
483485
r = node.machine.run(
484486
'/home/ubuntu/neard.upgrade amend-genesis --help',
@@ -487,7 +489,7 @@ def neard_amend_genesis_path(node):
487489
)
488490
if r.exitcode == 0:
489491
return '/home/ubuntu/neard.upgrade'
490-
return None
492+
raise Exception(f'`neard.upgrade amend-genesis` not available')
491493

492494

493495
# We assume that the nodes already have the .near directory with the files
@@ -524,12 +526,27 @@ def create_and_upload_genesis(
524526
neard = neard_amend_genesis_path(validator_nodes[1])
525527
pmap(
526528
lambda node: start_genesis_updater(
527-
node, 'genesis_updater.py', genesis_filename_in,
528-
records_filename_in, config_filename_in, '/home/ubuntu/.near/',
529-
chain_id, validator_keys, rpc_node_names, done_filename,
530-
epoch_length, node_pks, increasing_stakes, num_seats,
531-
single_shard, all_node_pks, node_ips, neard),
532-
validator_nodes + rpc_nodes)
529+
node=node,
530+
script='genesis_updater.py',
531+
genesis_filename_in=genesis_filename_in,
532+
records_filename_in=records_filename_in,
533+
config_filename_in=config_filename_in,
534+
out_dir='/home/ubuntu/.near/',
535+
chain_id=chain_id,
536+
validator_keys=validator_keys,
537+
rpc_nodes=rpc_node_names,
538+
done_filename=done_filename,
539+
epoch_length=epoch_length,
540+
node_pks=node_pks,
541+
increasing_stakes=increasing_stakes,
542+
num_seats=num_seats,
543+
single_shard=single_shard,
544+
all_node_pks=all_node_pks,
545+
node_ips=node_ips,
546+
neard=neard,
547+
),
548+
validator_nodes + rpc_nodes,
549+
)
533550
pmap(lambda node: wait_genesis_updater_done(node, done_filename),
534551
validator_nodes + rpc_nodes)
535552

@@ -735,128 +752,13 @@ def neard_amend_genesis(neard, validator_keys, genesis_filename_in,
735752
subprocess.run(cmd, text=True)
736753

737754

738-
def do_create_genesis_file(validator_keys,
739-
genesis_filename_in,
740-
genesis_filename_out,
741-
records_filename_in,
742-
records_filename_out,
743-
rpc_node_names=None,
744-
chain_id=None,
745-
append=False,
746-
epoch_length=None,
747-
node_pks=None,
748-
increasing_stakes=0.0,
749-
num_seats=None,
750-
single_shard=False):
751-
logger.info(f'create_genesis_file: validators: {validator_keys}')
752-
logger.info(f'create_genesis_file: rpc_node_names: {rpc_node_names}')
753-
with open(genesis_filename_in) as f:
754-
genesis_config = json.load(f)
755-
if append:
756-
with open(records_filename_in) as f:
757-
records = json.load(f)
758-
else:
759-
records = []
760-
761-
if chain_id:
762-
if append:
763-
assert genesis_config[
764-
'chain_id'] != chain_id, 'Can only append to the original genesis once'
765-
766-
genesis_config['chain_id'] = chain_id
767-
768-
if append:
769-
# Unstake all tokens from all existing accounts.
770-
for record in records:
771-
if 'Account' in record:
772-
account = record['Account'].get('account', {})
773-
locked = int(account.get('locked', 0))
774-
if locked > 0:
775-
amount = int(account.get('amount', 0))
776-
account['amount'] = str(amount + locked)
777-
account['locked'] = 0
778-
779-
seen_accounts = set()
780-
for record in records:
781-
if 'Account' in record:
782-
account_record = record['Account']
783-
account_id = account_record.get('account_id', '')
784-
if account_id in ACCOUNTS:
785-
seen_accounts.add(account_id)
786-
account = account_record.get('account', {})
787-
account['amount'] = str(ACCOUNTS[account_id])
788-
789-
extra_records, validators = extra_genesis_records(validator_keys,
790-
rpc_node_names, node_pks,
791-
seen_accounts, num_seats,
792-
increasing_stakes)
793-
794-
records.extend(extra_records)
795-
796-
genesis_config['validators'] = validators
797-
total_supply = 0
798-
for record in records:
799-
account = record.get('Account', {}).get('account', {})
800-
total_supply += int(account.get('locked', 0))
801-
total_supply += int(account.get('amount', 0))
802-
genesis_config['total_supply'] = str(total_supply)
803-
# Testing simple nightshade.
804-
genesis_config['protocol_version'] = 49
805-
genesis_config['epoch_length'] = int(epoch_length)
806-
genesis_config['num_block_producer_seats'] = int(num_seats)
807-
# Mainnet genesis disables protocol rewards, override to a value used today on mainnet.
808-
genesis_config['protocol_reward_rate'] = [1, 10]
809-
# Loadtest helper signs all transactions using the same block.
810-
# Extend validity period to allow the same hash to be used for the whole duration of the test.
811-
genesis_config['transaction_validity_period'] = 10**9
812-
# Protocol upgrades require downtime, therefore make it harder to kickout validators.
813-
# The default value of this parameter is 90.
814-
genesis_config['block_producer_kickout_threshold'] = 10
815-
816-
if single_shard:
817-
genesis_config['shard_layout'] = {'V0': {'num_shards': 1, 'version': 0}}
818-
genesis_config['simple_nightshade_shard_layout'] = {}
819-
820-
# The json object gets truncated if I don't close and reopen the file.
821-
with open(genesis_filename_out, 'w') as f:
822-
json.dump(genesis_config, f, indent=2)
823-
with open(records_filename_out, 'w') as f:
824-
json.dump(records, f)
825-
826-
827-
def create_genesis_file(validator_keys,
828-
genesis_filename_in,
829-
records_filename_in,
830-
out_dir,
831-
rpc_node_names=None,
832-
chain_id=None,
833-
append=False,
834-
epoch_length=None,
835-
node_pks=None,
836-
increasing_stakes=0.0,
837-
num_seats=None,
838-
single_shard=False,
839-
neard=None):
840-
if append and neard is not None:
841-
neard_amend_genesis(neard, validator_keys, genesis_filename_in,
842-
records_filename_in, out_dir, rpc_node_names,
843-
chain_id, epoch_length, node_pks, increasing_stakes,
844-
num_seats, single_shard)
845-
else:
846-
genesis_filename_out = os.path.join(out_dir, 'genesis.json')
847-
records_filename_out = os.path.join(out_dir, 'records.json')
848-
do_create_genesis_file(validator_keys, genesis_filename_in,
849-
genesis_filename_out, records_filename_in,
850-
records_filename_out, rpc_node_names, chain_id,
851-
append, epoch_length, node_pks,
852-
increasing_stakes, num_seats, single_shard)
853-
854-
855-
def create_and_upload_genesis_file_from_empty_genesis(validator_node_and_stakes,
856-
rpc_nodes,
857-
chain_id=None,
858-
epoch_length=None,
859-
num_seats=None):
755+
def create_and_upload_genesis_file_from_empty_genesis(
756+
validator_node_and_stakes,
757+
rpc_nodes,
758+
chain_id=None,
759+
epoch_length=None,
760+
num_seats=None,
761+
):
860762
node0 = validator_node_and_stakes[0][0]
861763
node0.machine.run(
862764
'rm -rf /home/ubuntu/.near-tmp && mkdir /home/ubuntu/.near-tmp && /home/ubuntu/neard --home /home/ubuntu/.near-tmp init --chain-id {}'
@@ -1202,12 +1104,25 @@ def reset_data(node, retries=0):
12021104
)
12031105

12041106

1205-
def start_genesis_updater_script(script, genesis_filename_in,
1206-
records_filename_in, config_filename_in,
1207-
out_dir, chain_id, validator_keys, rpc_nodes,
1208-
done_filename, epoch_length, node_pks,
1209-
increasing_stakes, num_seats, single_shard,
1210-
all_node_pks, node_ips, neard):
1107+
def start_genesis_updater_script(
1108+
script,
1109+
genesis_filename_in,
1110+
records_filename_in,
1111+
config_filename_in,
1112+
out_dir,
1113+
chain_id,
1114+
validator_keys,
1115+
rpc_nodes,
1116+
done_filename,
1117+
epoch_length,
1118+
node_pks,
1119+
increasing_stakes,
1120+
num_seats,
1121+
single_shard,
1122+
all_node_pks,
1123+
node_ips,
1124+
neard,
1125+
):
12111126
validators = ','.join(
12121127
[f'{account_id}={key}' for (account_id, key) in validator_keys.items()])
12131128
cmd = ' '.join([
@@ -1301,36 +1216,40 @@ def wait_node_up(node):
13011216
attempt = 0
13021217
while True:
13031218
try:
1219+
result = node.machine.run('ps -A | grep neard')
1220+
if result.returncode != 0:
1221+
raise Exception(f'{msg} - failed. The neard process crashed.')
1222+
13041223
response = node.get_validators()
13051224

13061225
if 'error' in response:
1307-
raise Exception(
1308-
f'Received error from node {node.instance_name}. Error: {response["error"]}'
1309-
)
1226+
attempt += 1
1227+
logger.info(f'{msg}, attempt {attempt} error response.')
1228+
continue
13101229
if 'result' not in response:
1311-
raise Exception(
1312-
f'Received no result in response. Response: {response}')
1230+
attempt += 1
1231+
logger.info(f'{msg}, attempt {attempt} result missing.')
1232+
continue
13131233

13141234
logger.info(f'{msg} - done.')
13151235
return True
1316-
except (
1317-
ConnectionRefusedError,
1318-
requests.exceptions.ConnectionError,
1319-
) as e:
1320-
attempt += 1
1321-
logger.info(f'{msg}, attempt {attempt}.')
1322-
except Exception as e:
1236+
except (ConnectionRefusedError, requests.exceptions.ConnectionError):
13231237
attempt += 1
1324-
logger.info(f'{msg}, attempt {attempt} failed: {e}')
1238+
logger.info(f'{msg}, attempt {attempt} connection refused.')
13251239
time.sleep(30)
13261240

13271241

13281242
def wait_all_nodes_up(all_nodes):
13291243
pmap(lambda node: wait_node_up(node), all_nodes)
13301244

13311245

1332-
def create_upgrade_schedule(rpc_nodes, validator_nodes, progressive_upgrade,
1333-
increasing_stakes, num_block_producer_seats):
1246+
def create_upgrade_schedule(
1247+
rpc_nodes,
1248+
validator_nodes,
1249+
progressive_upgrade,
1250+
increasing_stakes,
1251+
num_block_producer_seats,
1252+
):
13341253
schedule = {}
13351254
if progressive_upgrade:
13361255
# Re-create stakes assignment.

pytest/tests/mocknet/helpers/genesis_updater.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -70,22 +70,27 @@ def main(argv):
7070
assert all_node_pks
7171
assert node_ips
7272

73-
mocknet.create_genesis_file(validator_keys,
74-
genesis_filename_in,
75-
records_filename_in,
76-
out_dir,
77-
rpc_node_names=rpc_node_names,
78-
chain_id=chain_id,
79-
append=True,
80-
epoch_length=epoch_length,
81-
node_pks=node_pks,
82-
increasing_stakes=increasing_stakes,
83-
num_seats=num_seats,
84-
single_shard=single_shard,
85-
neard=neard)
73+
mocknet.neard_amend_genesis(
74+
neard=neard,
75+
validator_keys=validator_keys,
76+
genesis_filename_in=genesis_filename_in,
77+
records_filename_in=records_filename_in,
78+
out_dir=out_dir,
79+
rpc_node_names=rpc_node_names,
80+
chain_id=chain_id,
81+
epoch_length=epoch_length,
82+
node_pks=node_pks,
83+
increasing_stakes=increasing_stakes,
84+
num_seats=num_seats,
85+
single_shard=single_shard,
86+
)
8687
config_filename_out = os.path.join(out_dir, 'config.json')
87-
mocknet.update_config_file(config_filename_in, config_filename_out,
88-
all_node_pks, node_ips)
88+
mocknet.update_config_file(
89+
config_filename_in,
90+
config_filename_out,
91+
all_node_pks,
92+
node_ips,
93+
)
8994

9095
logger.info(f'done_filename: {done_filename}')
9196
pathlib.Path(done_filename).write_text('DONE')

0 commit comments

Comments
 (0)