extract ferveo key mismatch detection on startup to a method for improved mocking capability

pull/3529/head
KPrasch 2024-07-29 22:23:50 +07:00
parent 7263001db6
commit 270d6b9f40
No known key found for this signature in database
3 changed files with 34 additions and 32 deletions

View File

@ -573,18 +573,10 @@ class Operator(BaseActor):
to be handled by the EventActuator.
"""
local_ferveo_key = self.ritual_power.public_key()
onchain_ferveo_key = self.coordinator_agent.get_provider_public_key(
ritual_id=ritual_id,
provider=self.staking_provider_address,
)
if bytes(local_ferveo_key) != bytes(onchain_ferveo_key):
self.log.critical(
render_ferveo_key_mismatch_warning(
local_key=local_ferveo_key, onchain_key=onchain_ferveo_key
)
)
try:
self.check_ferveo_public_key_match()
except FerveoKeyMismatch:
# crash this node
self.stop()
exit()
@ -1017,27 +1009,28 @@ class Operator(BaseActor):
)
else:
# this node's ferveo public key is already set
local_ferveo_key = self.ritual_power.public_key()
onchain_ferveo_key = self.coordinator_agent.get_provider_public_key(
provider=self.staking_provider_address
# this node's ferveo public key is already published
self.check_ferveo_public_key_match()
emitter.message(
f"✓ Provider's DKG participation public key already set for "
f"{self.staking_provider_address} on Coordinator {self.coordinator_agent.contract_address}",
color="green",
)
if bytes(local_ferveo_key) != bytes(onchain_ferveo_key):
message = render_ferveo_key_mismatch_warning(
local_key=local_ferveo_key,
onchain_key=onchain_ferveo_key,
)
self.log.critical(message)
raise FerveoKeyMismatch(message)
def check_ferveo_public_key_match(self):
latest_ritual_id = self.coordinator_agent.number_of_rituals()
local_ferveo_key = self.ritual_power.public_key()
onchain_ferveo_key = self.coordinator_agent.get_provider_public_key(
ritual_id=latest_ritual_id, provider=self.staking_provider_address
)
else:
emitter.message(
f"✓ Provider's DKG participation public key already set for "
f"{self.staking_provider_address} on {taco_child_pretty_chain_name} at Coordinator {coordinator_address}",
color="green",
)
if bytes(local_ferveo_key) != bytes(onchain_ferveo_key):
message = render_ferveo_key_mismatch_warning(
local_key=local_ferveo_key,
onchain_key=onchain_ferveo_key,
)
self.log.critical(message)
raise FerveoKeyMismatch(message)
class PolicyAuthor(NucypherTokenActor):

View File

@ -123,9 +123,13 @@ def test_dkg_failure_with_ferveo_key_mismatch(
yield clock.advance(interval)
yield testerchain.time_travel(seconds=1)
assert log_messages[0] == render_ferveo_key_mismatch_warning(
bytes(new_public_key), bytes(onchain_public_key)
assert (
render_ferveo_key_mismatch_warning(
bytes(new_public_key), bytes(onchain_public_key)
)
in log_messages
)
testerchain.tx_machine.stop()
assert not testerchain.tx_machine.running
globalLogPublisher.removeObserver(log_trapper)

View File

@ -6,6 +6,7 @@ import pytest_twisted as pt
from eth_account import Account
from twisted.internet import threads
from nucypher.blockchain.eth.actors import Operator
from nucypher.characters.base import Learner
from nucypher.cli.literature import NO_CONFIGURATIONS_ON_DISK
from nucypher.cli.main import nucypher_cli
@ -35,6 +36,10 @@ def test_missing_configuration_file(_default_filepath_mock, click_runner):
def test_run_lone_default_development_ursula(click_runner, mocker, ursulas, accounts):
deploy_port = select_test_port()
operator_address = ursulas[0].operator_address
# mock key mismatch detection
mocker.patch.object(Operator, "check_ferveo_public_key_match", return_value=None)
args = (
"ursula",
"run", # Stat Ursula Command