Skip to content

Commit

Permalink
[Feature] Add/Remove invlunerables (#113)
Browse files Browse the repository at this point in the history
* Add new endpoint for managing invulnerable, while deprecated the old endpoints
  • Loading branch information
lazam authored Nov 7, 2023
1 parent ab570e3 commit 21ef6a9
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 8 deletions.
40 changes: 40 additions & 0 deletions app/lib/collator_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,43 @@ async def set_collator_selection_invulnerables(para_id, invulnerables):
else:
log.error(f'Failed to run xcm call para_id {para_id}, message: {encoded_call}, err: {invulnerables}')
return None

async def add_collator_selection_invulnerable(para_id, invulnerable):
log.info(f'Add the collator as Invulnerable for for para #{para_id}')
node_client = get_parachain_rpc_client(para_id)
call = node_client.compose_call(
call_module='CollatorSelection',
call_function='add_invulnerable',
call_params={
'who': invulnerable
}
)
encoded_call = call.encode()
weight = get_query_weight(node_client, call)
log.info("call: {}, weight: {}".format(call, weight))
receipt = substrate_sudo_relay_xcm_call(para_id, encoded_call, weight)
if receipt and receipt.is_success:
log.info(f'✅ Success: Add a new invulnerable via XCM to para #{para_id}')
else:
log.error(f'Failed to run xcm call para_id {para_id}, message: {encoded_call}, err: {invulnerable}')
return None

async def remove_collator_selection_invulnerable(para_id, invulnerable):
log.info(f'Remove the collator as Invulnerable for para #{para_id}')
node_client = get_parachain_rpc_client(para_id)
call = node_client.compose_call(
call_module='CollatorSelection',
call_function='remove_invulnerable',
call_params={
'who': invulnerable
}
)
encoded_call = call.encode()
weight = get_query_weight(node_client, call)
log.info("call: {}, weight: {}".format(call, weight))
receipt = substrate_sudo_relay_xcm_call(para_id, encoded_call, weight)
if receipt and receipt.is_success:
log.info(f'✅ Success: Remove invulnerable via XCM to para #{para_id}')
else:
log.error(f'Failed to run xcm call para_id {para_id}, message: {encoded_call}, err: {invulnerable}')
return None
24 changes: 21 additions & 3 deletions app/lib/network_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
get_derived_collator_session_keys
from app.lib.collator_manager import get_collator_status, \
collator_register, collator_deregister, get_moon_collator_status, \
set_collator_selection_invulnerables, get_collator_selection_invulnerables
add_collator_selection_invulnerable, remove_collator_selection_invulnerable, \
get_collator_selection_invulnerables, set_collator_selection_invulnerables
from app.lib.collator_mint import collator_set_keys
from app.lib.kubernetes_client import list_validator_pods, get_external_validators_from_configmap, \
list_collator_pods, get_pod, list_substrate_node_pods
Expand Down Expand Up @@ -651,7 +652,7 @@ async def deregister_statefulset_collators(para_id: str, stateful_set_name: str)
collator_node_names = list(map(lambda pod: pod.metadata.name, collators_pods))
await deregister_collator_nodes(chain, collator_node_names, ss58_format)


# To be deprecated next version
async def add_invulnerable_collators(para_id, nodes=[], addresses=[]):
log.info(f'Adding invulnerables collators to parachain #{para_id}; nodes={nodes} and addresses={addresses}')
current_invulnerables = get_collator_selection_invulnerables(para_id)
Expand All @@ -665,7 +666,7 @@ async def add_invulnerable_collators(para_id, nodes=[], addresses=[]):
invulnerables = list(set(current_invulnerables).union(set(invulnerables_to_add)))
await set_collator_selection_invulnerables(para_id, invulnerables)


# To be deprecated next version
async def remove_invulnerable_collators(para_id, nodes=[], addresses=[]):
log.info(f'Removing invulnerables collators to parachain #{para_id}; nodes={nodes} and addresses={addresses}')
current_invulnerables = get_collator_selection_invulnerables(para_id)
Expand All @@ -679,6 +680,23 @@ async def remove_invulnerable_collators(para_id, nodes=[], addresses=[]):
invulnerables = list(set(current_invulnerables).difference(set(invulnerables_to_remove)))
await set_collator_selection_invulnerables(para_id, invulnerables)

async def add_invulnerable_collator(para_id, node, address):
log.info(f'Adding invulnerable collator of parachain #{para_id}; node={node} and address={address}')
if node:
invulnerable = get_substrate_node(node).get("collator_account")
elif address:
invulnerable = address
await add_collator_selection_invulnerable(para_id, invulnerable)


async def remove_invulnerable_collator(para_id, node, address):
log.info(f'Removing invulnerable collator of parachain #{para_id}; node={node} and address={address}')
if node:
invulnerable = get_substrate_node(node).get("collator_account")
elif address:
invulnerable = address
await remove_collator_selection_invulnerable(para_id, invulnerable)


async def set_collator_nodes_keys_on_chain(para_id, nodes=[], statefulset=''):
if statefulset:
Expand Down
30 changes: 26 additions & 4 deletions app/routers/apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
rotate_nodes_session_keys, register_statefulset_collators, onboard_parachain_by_id, \
offboard_parachain_by_id, deregister_statefulset_collators, get_substrate_node, \
register_validator_nodes, register_validator_addresses, deregister_validator_nodes, register_collator_nodes, \
deregister_collator_nodes, add_invulnerable_collators, remove_invulnerable_collators, \
set_collator_nodes_keys_on_chain
deregister_collator_nodes, add_invulnerable_collator, remove_invulnerable_collator, \
set_collator_nodes_keys_on_chain, add_invulnerable_collators, remove_invulnerable_collators
from app.lib.parachain_manager import parachain_runtime_upgrade
from app.lib.runtime_utils import get_relay_runtime, get_relay_active_configuration, update_relay_configuration, \
get_parachain_runtime, runtime_upgrade, get_relaychain_metadata, get_parachain_metadata
Expand Down Expand Up @@ -244,7 +244,7 @@ async def deregister_collators(
return PlainTextResponse('OK')


@router.post("/collators/{para_id}/add_invulnerables")
@router.post("/collators/{para_id}/add_invulnerables", deprecated=True)
async def add_invulnerables(
para_id: str = Path(description="Parachain ID"),
address: list[str] = Query(default=[], description="Address(es) to be added as invulnerables"),
Expand All @@ -255,7 +255,7 @@ async def add_invulnerables(
return PlainTextResponse('OK')


@router.post("/collators/{para_id}/remove_invulnerables")
@router.post("/collators/{para_id}/remove_invulnerables", deprecated=True)
async def remove_invulnerables(
para_id: str = Path(description="Parachain ID"),
address: list[str] = Query(default=[], description="Address(es) to removed from invulnerables"),
Expand All @@ -266,6 +266,28 @@ async def remove_invulnerables(
return PlainTextResponse('OK')


@router.post("/collators/{para_id}/invulnerable/add")
async def add_invulnerable(
para_id: str = Path(description="Parachain ID"),
address: str = Query(default="", description="Address to be added as invulnerable"),
node: str = Query(default="", description="Collator node to be added as invulnerable on the parachain")
):
if node or address:
asyncio.create_task(add_invulnerable_collator(para_id, node, address))
return PlainTextResponse('OK')


@router.post("/collators/{para_id}/invulnerable/remove")
async def remove_invulnerable(
para_id: str = Path(description="Parachain ID"),
address:str = Query(default="", description="Address to removed from invulnerable"),
node: str = Query(default="", description="Collator node to be removed as invulnerable on the parachain")
):
if node or address:
asyncio.create_task(remove_invulnerable_collator(para_id, node, address))
return PlainTextResponse('OK')


@router.post("/collators/set_on_chain_keys")
async def set_on_chain_keys(
para_id: str = Query(description="Parachain ID"),
Expand Down
2 changes: 1 addition & 1 deletion local-kubernetes/charts/helmfile-rococo.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ releases:
- name: local-rococo-bootnode
namespace: rococo
chart: parity/node
version: &node_version 5.1.7
version: &node_version 5.4.0
values:
- ./values-local-rococo-bootnode.yaml
- node:
Expand Down

0 comments on commit 21ef6a9

Please sign in to comment.