Skip to content

Commit

Permalink
fix(mocknet): use a different key for each validator (near#8629)
Browse files Browse the repository at this point in the history
the mocknet test used to initialize each validator with the same key, and now after near#8106, this will cause the test to fail, as nodes will no longer connect to multiple validators with the same key. Fix it by calling neard init on each machine to create a new key for each.
  • Loading branch information
marcelo-gonzalez authored Feb 24, 2023
1 parent 81e92c4 commit a0a846e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 25 deletions.
55 changes: 34 additions & 21 deletions pytest/lib/mocknet.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ def create_and_upload_genesis(validator_nodes,
with tempfile.TemporaryDirectory() as tmp_dir:
logger.info(
'Assuming that genesis_updater.py is available on the instances.')
validator_node_names = [node.instance_name for node in validator_nodes]
validator_keys = dict(pmap(get_validator_key, validator_nodes))
rpc_node_names = [node.instance_name for node in rpc_nodes]
assert '-spoon' in chain_id, f'Expecting chain_id like "testnet-spoon" or "mainnet-spoon", got {chain_id}'
chain_id_in = chain_id.split('-spoon')[0]
Expand All @@ -515,15 +515,15 @@ def create_and_upload_genesis(validator_nodes,
lambda node: start_genesis_updater(
node, 'genesis_updater.py', genesis_filename_in,
records_filename_in, config_filename_in, '/home/ubuntu/.near/',
chain_id, validator_node_names, rpc_node_names, done_filename,
chain_id, validator_keys, rpc_node_names, done_filename,
epoch_length, node_pks, increasing_stakes, num_seats,
single_shard, all_node_pks, node_ips, neard),
validator_nodes + rpc_nodes)
pmap(lambda node: wait_genesis_updater_done(node, done_filename),
validator_nodes + rpc_nodes)


def extra_genesis_records(validator_node_names, rpc_node_names, node_pks,
def extra_genesis_records(validator_keys, rpc_node_names, node_pks,
seen_accounts, num_seats, increasing_stakes):
records = []

Expand Down Expand Up @@ -562,8 +562,7 @@ def extra_genesis_records(validator_node_names, rpc_node_names, node_pks,

stakes = []
prev_stake = None
for i, node_name in enumerate(validator_node_names):
account_id = node_account_name(node_name)
for i, account_id in enumerate(validator_keys):
logger.info(f'Adding account {account_id}')
if increasing_stakes:
if i * 5 < num_seats * 3 and i < len(MAINNET_STAKES):
Expand Down Expand Up @@ -658,19 +657,19 @@ def extra_genesis_records(validator_node_names, rpc_node_names, node_pks,
break
validators.append({
'account_id': account_id,
'public_key': PUBLIC_KEY,
'public_key': validator_keys[account_id],
'amount': str(staked),
})
seats_taken += seats

return records, validators


def neard_amend_genesis(neard, validator_node_names, genesis_filename_in,
def neard_amend_genesis(neard, validator_keys, genesis_filename_in,
records_filename_in, out_dir, rpc_node_names, chain_id,
epoch_length, node_pks, increasing_stakes, num_seats,
single_shard):
extra_records, validators = extra_genesis_records(validator_node_names,
extra_records, validators = extra_genesis_records(validator_keys,
rpc_node_names, node_pks,
set(), num_seats,
increasing_stakes)
Expand Down Expand Up @@ -725,7 +724,7 @@ def neard_amend_genesis(neard, validator_node_names, genesis_filename_in,
subprocess.run(cmd, text=True)


def do_create_genesis_file(validator_node_names,
def do_create_genesis_file(validator_keys,
genesis_filename_in,
genesis_filename_out,
records_filename_in,
Expand All @@ -738,8 +737,7 @@ def do_create_genesis_file(validator_node_names,
increasing_stakes=0.0,
num_seats=None,
single_shard=False):
logger.info(
f'create_genesis_file: validator_node_names: {validator_node_names}')
logger.info(f'create_genesis_file: validators: {validator_keys}')
logger.info(f'create_genesis_file: rpc_node_names: {rpc_node_names}')
with open(genesis_filename_in) as f:
genesis_config = json.load(f)
Expand Down Expand Up @@ -777,7 +775,7 @@ def do_create_genesis_file(validator_node_names,
account = account_record.get('account', {})
account['amount'] = str(ACCOUNTS[account_id])

extra_records, validators = extra_genesis_records(validator_node_names,
extra_records, validators = extra_genesis_records(validator_keys,
rpc_node_names, node_pks,
seen_accounts, num_seats,
increasing_stakes)
Expand Down Expand Up @@ -815,7 +813,7 @@ def do_create_genesis_file(validator_node_names,
json.dump(records, f)


def create_genesis_file(validator_node_names,
def create_genesis_file(validator_keys,
genesis_filename_in,
records_filename_in,
out_dir,
Expand All @@ -829,14 +827,14 @@ def create_genesis_file(validator_node_names,
single_shard=False,
neard=None):
if append and neard is not None:
neard_amend_genesis(neard, validator_node_names, genesis_filename_in,
neard_amend_genesis(neard, validator_keys, genesis_filename_in,
records_filename_in, out_dir, rpc_node_names,
chain_id, epoch_length, node_pks, increasing_stakes,
num_seats, single_shard)
else:
genesis_filename_out = os.path.join(out_dir, 'genesis.json')
records_filename_out = os.path.join(out_dir, 'records.json')
do_create_genesis_file(validator_node_names, genesis_filename_in,
do_create_genesis_file(validator_keys, genesis_filename_in,
genesis_filename_out, records_filename_in,
records_filename_out, rpc_node_names, chain_id,
append, epoch_length, node_pks,
Expand Down Expand Up @@ -1031,13 +1029,26 @@ def get_validator_account_id(node):
return node_key_json["account_id"]


def get_validator_key(node):
node_key_json = download_and_read_json(
node, '/home/ubuntu/.near/validator_key.json')
return node_key_json["account_id"], node_key_json["public_key"]


def get_node_keys(node):
logger.info(f'get_node_keys from {node.instance_name}')
node_key_json = download_and_read_json(node,
'/home/ubuntu/.near/node_key.json')
return node_key_json['public_key'], node_key_json['secret_key']


def init_validator_key(node):
account_id = node_account_name(node.instance_name)
node.machine.run(
f'dir=$(mktemp -d) && /home/ubuntu/neard --home $dir init --account-id {account_id} && mv $dir/validator_key.json /home/ubuntu/.near/validator_key.json'
)


def update_config_file(config_filename_in, config_filename_out, all_node_pks,
node_ips):
with open(config_filename_in) as f:
Expand Down Expand Up @@ -1169,17 +1180,19 @@ def reset_data(node, retries=0):

def start_genesis_updater_script(script, genesis_filename_in,
records_filename_in, config_filename_in,
out_dir, chain_id, validator_nodes, rpc_nodes,
out_dir, chain_id, validator_keys, rpc_nodes,
done_filename, epoch_length, node_pks,
increasing_stakes, num_seats, single_shard,
all_node_pks, node_ips, neard):
validators = ','.join(
[f'{account_id}={key}' for (account_id, key) in validator_keys.items()])
cmd = ' '.join([
shlex.quote(str(arg)) for arg in [
'nohup', './venv/bin/python', script, genesis_filename_in,
records_filename_in, config_filename_in, out_dir, chain_id,
','.join(validator_nodes), ','.join(rpc_nodes), done_filename,
epoch_length, ','.join(node_pks), increasing_stakes, num_seats,
single_shard, ','.join(all_node_pks), ','.join(
validators, ','.join(rpc_nodes), done_filename, epoch_length,
','.join(node_pks), increasing_stakes, num_seats, single_shard,
','.join(all_node_pks), ','.join(
node_ips), neard if neard is not None else 'None'
]
])
Expand All @@ -1191,14 +1204,14 @@ def start_genesis_updater_script(script, genesis_filename_in,

def start_genesis_updater(node, script, genesis_filename_in,
records_filename_in, config_filename_in, out_dir,
chain_id, validator_nodes, rpc_nodes, done_filename,
chain_id, validator_keys, rpc_nodes, done_filename,
epoch_length, node_pks, increasing_stakes, num_seats,
single_shard, all_node_pks, node_ips, neard):
logger.info(f'Starting genesis_updater on {node.instance_name}')
node.machine.run('bash',
input=start_genesis_updater_script(
script, genesis_filename_in, records_filename_in,
config_filename_in, out_dir, chain_id, validator_nodes,
config_filename_in, out_dir, chain_id, validator_keys,
rpc_nodes, done_filename, epoch_length, node_pks,
increasing_stakes, num_seats, single_shard,
all_node_pks, node_ips, neard))
Expand Down
8 changes: 4 additions & 4 deletions pytest/tests/mocknet/helpers/genesis_updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ def main(argv):
out_dir = argv[4]

chain_id = argv[5]
validator_node_names = None
validator_keys = None
if argv[6]:
validator_node_names = argv[6].split(',')
validator_keys = dict(map(lambda x: x.split('='), argv[6].split(',')))
rpc_node_names = None
if argv[7]:
rpc_node_names = argv[7].split(',')
Expand Down Expand Up @@ -61,7 +61,7 @@ def main(argv):
assert config_filename_in
assert out_dir
assert chain_id
assert validator_node_names
assert validator_keys
assert done_filename
assert epoch_length
assert node_pks
Expand All @@ -70,7 +70,7 @@ def main(argv):
assert all_node_pks
assert node_ips

mocknet.create_genesis_file(validator_node_names,
mocknet.create_genesis_file(validator_keys,
genesis_filename_in,
records_filename_in,
out_dir,
Expand Down
1 change: 1 addition & 0 deletions pytest/tests/mocknet/load_test_spoon.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ def check_slow_blocks(initial_metrics, final_metrics):
validator_nodes)
all_node_pks = pmap(lambda node: mocknet.get_node_keys(node)[0],
all_nodes)
pmap(lambda node: mocknet.init_validator_key(node), all_nodes)
node_ips = [node.machine.ip for node in all_nodes]
mocknet.create_and_upload_genesis(
validator_nodes,
Expand Down

0 comments on commit a0a846e

Please sign in to comment.