Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix setup_pos_validator function #16

Merged
merged 5 commits into from
Oct 13, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/lib/network_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ async def register_validator_pods(pods):
node_session_key = rotate_node_session_keys(node_http_endpoint(node))

log.info(f'Registering PoS Validator: {node}')
status = setup_pos_validator(ws_endpoint, node_session_key, validator_stash_mnemonic)
status = setup_pos_validator(ws_endpoint, validator_stash_mnemonic, node_session_key)
if status:
log.info(f'Successfully set up PoS Validator: {node}')
else:
Expand Down
60 changes: 38 additions & 22 deletions app/lib/validator_manager.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging
from substrateinterface import Keypair
from app.lib.substrate import substrate_query_url, substrate_sudo_call, get_substrate_client, substrate_call,\
from app.lib.substrate import substrate_query_url, substrate_sudo_call, get_substrate_client, substrate_call, \
substrate_batchall_call
from app.config.network_configuration import network_consensus
from app.lib.session_keys import decode_session_key
Expand Down Expand Up @@ -56,34 +56,44 @@ def get_validators_pending_deletion(ws_endpoint):
return list(set(active_validators) - set(staking_validators))


def setup_pos_validator(ws_endpoint, session_key, stash_seed, controller_seed=None):
def setup_pos_validator(ws_endpoint, stash_seed, session_key=None, controller_seed=None):
batch_call = []
substrate_client = get_substrate_client(ws_endpoint)
stash_keypair = Keypair.create_from_uri(stash_seed)

batch_call.append(substrate_client.compose_call(
call_module='Staking',
call_function='bond',
call_params={
'controller': stash_keypair.ss58_address,
'value': 1 * 10 ** 11,
'payee': "Staked"
}
# 1. Bond
bonded = substrate_client.query('Staking', 'Bonded', params=[stash_keypair.ss58_address]).value
# If we already bond, skip this step
if not bonded or not (bonded in [stash_keypair.ss58_address, controller_seed]):
batch_call.append(
substrate_client.compose_call(
call_module='Staking',
call_function='bond',
call_params={
'controller': stash_keypair.ss58_address,
'value': 1 * 10 ** 11,
'payee': "Staked"
}
)
)
)

if type(session_key) == str:
session_key = decode_session_key(substrate_client,session_key)
batch_call.append(substrate_client.compose_call(
call_module='Session',
call_function='set_keys',
call_params={
'keys': session_key,
'proof': ''
}
# 2. Set session key. If a session key is not provided we assume what session key is already set ( Example: we are
# registering deregistered validator)
if session_key:
if type(session_key) == str:
PierreBesson marked this conversation as resolved.
Show resolved Hide resolved
session_key = decode_session_key(substrate_client, session_key)
batch_call.append(
substrate_client.compose_call(
call_module='Session',
call_function='set_keys',
call_params={
'keys': session_key,
'proof': ''
}
)
)
)

# 3. Validate
batch_call.append(substrate_client.compose_call(
call_module='Staking',
call_function='validate',
Expand All @@ -96,6 +106,7 @@ def setup_pos_validator(ws_endpoint, session_key, stash_seed, controller_seed=N
)
)

# 4. Set collator account.
if controller_seed:
controller_keypair = Keypair.create_from_uri(controller_seed)
batch_call.append(substrate_client.compose_call(
Expand All @@ -109,7 +120,12 @@ def setup_pos_validator(ws_endpoint, session_key, stash_seed, controller_seed=N

result = substrate_batchall_call(substrate_client, stash_keypair, batch_call, wait=True)
if result and result.is_success:
return True
if any(e.value['event_id'] == "BatchInterrupted" for e in result.triggered_events):
log.error("Batch call failed: BatchInterrupted for stash {}, extrinsic_hash: {}, block_hash: {}".format(
stash_keypair.ss58_address, result.extrinsic_hash, result.block_hash))
return False
else:
return True
else:
return False

Expand Down