2023-02-01 16:53:02 +00:00
|
|
|
import os
|
2023-04-14 15:25:54 +00:00
|
|
|
from collections import OrderedDict
|
|
|
|
from unittest.mock import Mock
|
|
|
|
|
2023-02-01 16:53:02 +00:00
|
|
|
import pytest
|
|
|
|
from eth_account import Account
|
|
|
|
from eth_utils import keccak
|
2023-04-17 23:44:26 +00:00
|
|
|
from ferveo_py import Keypair as FerveoKeypair
|
2023-02-01 16:53:02 +00:00
|
|
|
|
2023-04-13 15:41:33 +00:00
|
|
|
from tests.integration.blockchain.test_ritualist import FAKE_TRANSCRIPT
|
2023-04-16 19:59:28 +00:00
|
|
|
from tests.mock.coordinator import MockCoordinatorAgent
|
2023-04-17 23:44:26 +00:00
|
|
|
from tests.mock.interfaces import MockBlockchain
|
2023-02-01 16:53:02 +00:00
|
|
|
|
2023-04-13 15:41:33 +00:00
|
|
|
DKG_SIZE = 4
|
|
|
|
|
2023-02-01 16:53:02 +00:00
|
|
|
|
|
|
|
@pytest.fixture(scope='module')
|
2023-04-14 15:25:54 +00:00
|
|
|
def nodes_transacting_powers():
|
|
|
|
accounts = OrderedDict()
|
2023-04-13 15:41:33 +00:00
|
|
|
for _ in range(DKG_SIZE):
|
2023-02-01 16:53:02 +00:00
|
|
|
account = Account.create()
|
2023-04-14 15:25:54 +00:00
|
|
|
mock_transacting_power = Mock()
|
|
|
|
mock_transacting_power.account = account.address
|
|
|
|
accounts[account.address] = mock_transacting_power
|
2023-02-01 16:53:02 +00:00
|
|
|
return accounts
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='module')
|
2023-04-16 19:59:28 +00:00
|
|
|
def coordinator():
|
|
|
|
return MockCoordinatorAgent(blockchain=MockBlockchain())
|
2023-02-01 16:53:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_mock_coordinator_creation(coordinator):
|
|
|
|
assert len(coordinator.rituals) == 0
|
|
|
|
|
|
|
|
|
2023-04-14 15:25:54 +00:00
|
|
|
def test_mock_coordinator_initiation(mocker, nodes_transacting_powers, coordinator, random_address):
|
2023-02-01 16:53:02 +00:00
|
|
|
assert len(coordinator.rituals) == 0
|
2023-04-14 15:25:54 +00:00
|
|
|
mock_transacting_power = mocker.Mock()
|
|
|
|
mock_transacting_power.account = random_address
|
|
|
|
coordinator.initiate_ritual(nodes=list(nodes_transacting_powers.keys()), transacting_power=mock_transacting_power)
|
2023-02-01 16:53:02 +00:00
|
|
|
assert len(coordinator.rituals) == 1
|
|
|
|
|
|
|
|
assert coordinator.number_of_rituals() == 1
|
|
|
|
|
|
|
|
ritual = coordinator.rituals[0]
|
2023-04-13 15:41:33 +00:00
|
|
|
assert len(ritual.participants) == DKG_SIZE
|
2023-04-03 17:09:35 +00:00
|
|
|
for p in ritual.participants:
|
2023-02-01 16:53:02 +00:00
|
|
|
assert p.transcript == bytes()
|
|
|
|
|
2023-04-16 19:59:28 +00:00
|
|
|
assert len(coordinator.EVENTS) == 1
|
2023-04-03 17:09:35 +00:00
|
|
|
|
2023-04-16 19:59:28 +00:00
|
|
|
timestamp, signal = list(coordinator.EVENTS.items())[0]
|
2023-02-01 16:53:02 +00:00
|
|
|
signal_type, signal_data = signal
|
2023-04-16 19:59:28 +00:00
|
|
|
assert signal_type == MockCoordinatorAgent.Events.START_TRANSCRIPT_ROUND
|
2023-02-01 16:53:02 +00:00
|
|
|
assert signal_data['ritual_id'] == 0
|
2023-04-14 15:25:54 +00:00
|
|
|
assert set(signal_data['nodes']) == nodes_transacting_powers.keys()
|
2023-02-01 16:53:02 +00:00
|
|
|
|
|
|
|
|
2023-04-14 15:25:54 +00:00
|
|
|
def test_mock_coordinator_round_1(nodes_transacting_powers, coordinator):
|
2023-02-01 16:53:02 +00:00
|
|
|
ritual = coordinator.rituals[0]
|
2023-04-16 19:59:28 +00:00
|
|
|
assert coordinator.get_ritual_status(ritual.id) == MockCoordinatorAgent.RitualStatus.AWAITING_TRANSCRIPTS
|
2023-02-01 16:53:02 +00:00
|
|
|
|
2023-04-03 17:09:35 +00:00
|
|
|
for p in ritual.participants:
|
2023-02-01 16:53:02 +00:00
|
|
|
assert p.transcript == bytes()
|
|
|
|
|
2023-04-14 15:25:54 +00:00
|
|
|
for index, node_address in enumerate(nodes_transacting_powers):
|
2023-04-13 15:41:33 +00:00
|
|
|
transcript = FAKE_TRANSCRIPT
|
2023-04-14 15:25:54 +00:00
|
|
|
|
2023-02-01 16:53:02 +00:00
|
|
|
coordinator.post_transcript(
|
|
|
|
ritual_id=0,
|
|
|
|
node_index=index,
|
2023-04-14 15:25:54 +00:00
|
|
|
transcript=transcript,
|
|
|
|
transacting_power=nodes_transacting_powers[node_address]
|
2023-02-01 16:53:02 +00:00
|
|
|
)
|
|
|
|
|
2023-04-03 17:09:35 +00:00
|
|
|
performance = ritual.participants[index]
|
2023-04-14 15:25:54 +00:00
|
|
|
assert performance.transcript == transcript
|
2023-02-01 16:53:02 +00:00
|
|
|
|
2023-04-14 15:25:54 +00:00
|
|
|
if index == len(nodes_transacting_powers) - 1:
|
2023-04-16 19:59:28 +00:00
|
|
|
assert len(coordinator.EVENTS) == 2
|
2023-02-01 16:53:02 +00:00
|
|
|
|
2023-04-16 19:59:28 +00:00
|
|
|
timestamp, signal = list(coordinator.EVENTS.items())[1]
|
2023-02-01 16:53:02 +00:00
|
|
|
signal_type, signal_data = signal
|
2023-04-16 19:59:28 +00:00
|
|
|
assert signal_type == MockCoordinatorAgent.Events.START_AGGREGATION_ROUND
|
2023-02-01 16:53:02 +00:00
|
|
|
assert signal_data['ritual_id'] == ritual.id
|
2023-04-14 15:25:54 +00:00
|
|
|
assert set(signal_data['nodes']) == nodes_transacting_powers.keys()
|
2023-02-01 16:53:02 +00:00
|
|
|
|
|
|
|
|
2023-04-14 15:25:54 +00:00
|
|
|
def test_mock_coordinator_round_2(nodes_transacting_powers, coordinator):
|
2023-02-01 16:53:02 +00:00
|
|
|
ritual = coordinator.rituals[0]
|
2023-04-16 19:59:28 +00:00
|
|
|
assert coordinator.get_ritual_status(ritual.id) == MockCoordinatorAgent.RitualStatus.AWAITING_AGGREGATIONS
|
2023-02-01 16:53:02 +00:00
|
|
|
|
2023-04-03 17:09:35 +00:00
|
|
|
for p in ritual.participants:
|
2023-04-13 15:41:33 +00:00
|
|
|
assert p.transcript == FAKE_TRANSCRIPT
|
2023-02-01 16:53:02 +00:00
|
|
|
|
2023-04-14 15:25:54 +00:00
|
|
|
aggregated_transcript = os.urandom(len(FAKE_TRANSCRIPT))
|
|
|
|
aggregated_transcript_hash = keccak(aggregated_transcript)
|
2023-04-17 23:44:26 +00:00
|
|
|
public_key = FerveoKeypair.random().public_key
|
2023-04-14 15:25:54 +00:00
|
|
|
|
|
|
|
for index, node_address in enumerate(nodes_transacting_powers):
|
2023-04-03 17:09:35 +00:00
|
|
|
coordinator.post_aggregation(
|
2023-02-01 16:53:02 +00:00
|
|
|
ritual_id=0,
|
|
|
|
node_index=index,
|
2023-04-14 15:25:54 +00:00
|
|
|
aggregated_transcript=aggregated_transcript,
|
2023-04-17 23:44:26 +00:00
|
|
|
public_key=public_key,
|
2023-04-14 15:25:54 +00:00
|
|
|
transacting_power=nodes_transacting_powers[node_address]
|
2023-02-01 16:53:02 +00:00
|
|
|
)
|
2023-04-14 15:25:54 +00:00
|
|
|
if index == len(nodes_transacting_powers) - 1:
|
2023-04-16 19:59:28 +00:00
|
|
|
assert len(coordinator.EVENTS) == 2
|
|
|
|
|
|
|
|
for p in ritual.participants:
|
|
|
|
assert p.aggregated_transcript != FAKE_TRANSCRIPT
|
2023-02-01 16:53:02 +00:00
|
|
|
|
2023-04-16 19:59:28 +00:00
|
|
|
assert len(coordinator.EVENTS) == 2 # no additional event emitted here?
|
2023-04-03 17:09:35 +00:00
|
|
|
for p in ritual.participants:
|
2023-04-14 15:25:54 +00:00
|
|
|
assert p.transcript == FAKE_TRANSCRIPT
|
2023-04-13 15:41:33 +00:00
|
|
|
assert p.aggregated_transcript != FAKE_TRANSCRIPT
|
2023-04-14 15:25:54 +00:00
|
|
|
assert p.aggregated_transcript == aggregated_transcript
|
|
|
|
assert p.aggregated_transcript_hash == aggregated_transcript_hash
|
2023-04-13 15:41:33 +00:00
|
|
|
|
2023-04-16 19:59:28 +00:00
|
|
|
assert coordinator.get_ritual_status(ritual.id) == MockCoordinatorAgent.RitualStatus.FINALIZED
|