2020-08-19 00:40:26 +00:00
|
|
|
import pytest
|
2023-06-01 15:55:48 +00:00
|
|
|
from nucypher_core.ferveo import Keypair, Validator
|
2020-08-19 00:40:26 +00:00
|
|
|
|
2023-05-04 19:26:01 +00:00
|
|
|
from nucypher.blockchain.eth.actors import Operator
|
2023-04-14 15:25:54 +00:00
|
|
|
from nucypher.blockchain.eth.agents import ContractAgency
|
2023-05-04 19:26:01 +00:00
|
|
|
from nucypher.blockchain.eth.interfaces import BlockchainInterfaceFactory
|
2023-10-01 18:52:48 +00:00
|
|
|
from nucypher.blockchain.eth.registry import ContractRegistry
|
2023-10-28 13:59:19 +00:00
|
|
|
from nucypher.config.constants import TEMPORARY_DOMAIN_NAME
|
2023-05-19 17:35:05 +00:00
|
|
|
from nucypher.crypto.ferveo import dkg
|
2022-12-07 11:14:59 +00:00
|
|
|
from nucypher.crypto.powers import TransactingPower
|
|
|
|
from nucypher.network.nodes import Teacher
|
2023-10-29 18:42:02 +00:00
|
|
|
from tests.constants import TEMPORARY_DOMAIN
|
2023-05-04 19:26:01 +00:00
|
|
|
from tests.mock.interfaces import MockBlockchain, MockEthereumClient
|
2023-10-01 18:52:48 +00:00
|
|
|
from tests.utils.registry import MockRegistrySource, mock_registry_sources
|
2020-08-19 00:40:26 +00:00
|
|
|
|
|
|
|
|
2023-04-28 05:59:40 +00:00
|
|
|
def pytest_addhooks(pluginmanager):
|
|
|
|
pluginmanager.set_blocked('ape_test')
|
|
|
|
|
|
|
|
|
2023-10-28 13:58:38 +00:00
|
|
|
|
2023-04-28 06:04:01 +00:00
|
|
|
@pytest.fixture(scope='module')
|
2023-10-29 18:42:02 +00:00
|
|
|
def test_registry(module_mocker):
|
2023-10-03 20:06:10 +00:00
|
|
|
with mock_registry_sources(mocker=module_mocker):
|
2023-10-29 18:42:02 +00:00
|
|
|
source = MockRegistrySource(domain=TEMPORARY_DOMAIN)
|
2023-10-01 18:52:48 +00:00
|
|
|
yield ContractRegistry(source=source)
|
2023-04-28 06:04:01 +00:00
|
|
|
|
|
|
|
|
2020-08-19 00:40:26 +00:00
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
def mock_ethereum_client(mocker):
|
|
|
|
web3_mock = mocker.Mock()
|
|
|
|
mock_client = MockEthereumClient(w3=web3_mock)
|
|
|
|
return mock_client
|
2022-12-07 11:14:59 +00:00
|
|
|
|
|
|
|
|
2022-12-01 10:19:22 +00:00
|
|
|
@pytest.fixture(scope='module', autouse=True)
|
2022-12-07 11:14:59 +00:00
|
|
|
def mock_transacting_power(module_mocker):
|
2022-12-01 10:19:22 +00:00
|
|
|
module_mocker.patch.object(TransactingPower, 'unlock')
|
2022-12-07 11:14:59 +00:00
|
|
|
|
|
|
|
|
2022-12-01 10:19:22 +00:00
|
|
|
@pytest.fixture(scope='module', autouse=True)
|
2023-09-21 15:07:20 +00:00
|
|
|
def mock_contract_agency():
|
2023-04-26 19:59:33 +00:00
|
|
|
from tests.mock.agents import MockContractAgency
|
|
|
|
|
2022-12-07 11:14:59 +00:00
|
|
|
# Monkeypatch # TODO: Use better tooling for this monkeypatch?
|
|
|
|
get_agent = ContractAgency.get_agent
|
|
|
|
get_agent_by_name = ContractAgency.get_agent_by_contract_name
|
|
|
|
ContractAgency.get_agent = MockContractAgency.get_agent
|
2022-12-01 10:19:22 +00:00
|
|
|
ContractAgency.get_agent_by_contract_name = MockContractAgency.get_agent_by_contract_name
|
2022-12-07 11:14:59 +00:00
|
|
|
|
|
|
|
# Test
|
|
|
|
yield MockContractAgency()
|
|
|
|
|
|
|
|
# Restore the monkey patching
|
|
|
|
ContractAgency.get_agent = get_agent
|
|
|
|
ContractAgency.get_agent_by_contract_name = get_agent_by_name
|
|
|
|
|
|
|
|
|
2022-12-01 10:19:22 +00:00
|
|
|
@pytest.fixture(scope='session', autouse=True)
|
2022-12-07 11:14:59 +00:00
|
|
|
def mock_operator_bonding(session_mocker):
|
2022-12-01 10:19:22 +00:00
|
|
|
session_mocker.patch.object(Teacher, '_operator_is_bonded', autospec=True)
|
2023-05-04 19:26:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
|
|
def testerchain(mock_testerchain, module_mocker) -> MockBlockchain:
|
|
|
|
def always_use_mock(*a, **k):
|
|
|
|
return mock_testerchain
|
|
|
|
|
|
|
|
module_mocker.patch.object(
|
|
|
|
BlockchainInterfaceFactory, "get_interface", always_use_mock
|
|
|
|
)
|
|
|
|
return mock_testerchain
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="module", autouse=True)
|
|
|
|
def staking_providers(testerchain, test_registry, monkeymodule):
|
|
|
|
def faked(self, *args, **kwargs):
|
|
|
|
return testerchain.stake_providers_accounts[
|
|
|
|
testerchain.ursulas_accounts.index(self.transacting_power.account)
|
|
|
|
]
|
|
|
|
|
|
|
|
Operator.get_staking_provider_address = faked
|
|
|
|
return testerchain.stake_providers_accounts
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="module", autouse=True)
|
|
|
|
def mock_substantiate_stamp(module_mocker, monkeymodule):
|
|
|
|
fake_signature = b"\xb1W5?\x9b\xbaix>'\xfe`\x1b\x9f\xeb*9l\xc0\xa7\xb9V\x9a\x83\x84\x04\x97\x0c\xad\x99\x86\x81W\x93l\xc3\xbde\x03\xcd\"Y\xce\xcb\xf7\x02z\xf6\x9c\xac\x84\x05R\x9a\x9f\x97\xf7\xa02\xb2\xda\xa1Gv\x01"
|
|
|
|
from nucypher.characters.lawful import Ursula
|
|
|
|
|
|
|
|
module_mocker.patch.object(Ursula, "_substantiate_stamp", autospec=True)
|
|
|
|
module_mocker.patch.object(Ursula, "operator_signature", fake_signature)
|
|
|
|
module_mocker.patch.object(Teacher, "validate_operator")
|
2023-05-19 17:35:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
|
|
def random_transcript(get_random_checksum_address):
|
|
|
|
ritual_id = 0
|
|
|
|
num_shares = 4
|
|
|
|
threshold = 3
|
|
|
|
validators = []
|
|
|
|
for i in range(0, num_shares):
|
|
|
|
validators.append(
|
|
|
|
Validator(
|
|
|
|
address=get_random_checksum_address(),
|
2023-06-01 15:55:48 +00:00
|
|
|
public_key=Keypair.random().public_key(),
|
2023-05-19 17:35:05 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
validators.sort(key=lambda x: x.address) # must be sorte
|
|
|
|
|
|
|
|
transcript = dkg.generate_transcript(
|
|
|
|
ritual_id=ritual_id,
|
|
|
|
me=validators[0],
|
|
|
|
shares=num_shares,
|
|
|
|
threshold=threshold,
|
|
|
|
nodes=validators,
|
|
|
|
)
|
|
|
|
|
|
|
|
return transcript
|