2018-11-04 19:23:11 +00:00
|
|
|
"""
|
|
|
|
This file is part of nucypher.
|
|
|
|
|
|
|
|
nucypher is free software: you can redistribute it and/or modify
|
2019-03-05 02:50:11 +00:00
|
|
|
it under the terms of the GNU Affero General Public License as published by
|
2018-11-04 19:23:11 +00:00
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
nucypher is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2019-03-05 02:50:11 +00:00
|
|
|
GNU Affero General Public License for more details.
|
2018-11-04 19:23:11 +00:00
|
|
|
|
2019-03-05 02:50:11 +00:00
|
|
|
You should have received a copy of the GNU Affero General Public License
|
2018-11-04 19:23:11 +00:00
|
|
|
along with nucypher. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
"""
|
2019-10-13 21:55:43 +00:00
|
|
|
|
|
|
|
import json
|
|
|
|
import os
|
2018-04-13 00:24:55 +00:00
|
|
|
import pytest
|
|
|
|
|
2019-10-13 21:55:43 +00:00
|
|
|
from nucypher.blockchain.eth.deployers import PreallocationEscrowDeployer
|
2019-08-14 02:17:08 +00:00
|
|
|
from nucypher.blockchain.eth.interfaces import BaseContractRegistry
|
2019-10-13 21:55:43 +00:00
|
|
|
from nucypher.blockchain.eth.registry import LocalContractRegistry, IndividualAllocationRegistry
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True, scope='module')
|
|
|
|
def patch_individual_allocation_registry_fetch_latest_publication(agency, test_registry):
|
|
|
|
empty_allocation_escrow_deployer = PreallocationEscrowDeployer(registry=test_registry)
|
|
|
|
allocation_contract_abi = empty_allocation_escrow_deployer.get_contract_abi()
|
|
|
|
allocation_template = {
|
|
|
|
"BENEFICIARY_ADDRESS": ["ALLOCATION_CONTRACT_ADDRESS", allocation_contract_abi]
|
|
|
|
}
|
|
|
|
new_fetch_result = json.dumps(allocation_template).encode()
|
|
|
|
|
|
|
|
original_fetch = IndividualAllocationRegistry.fetch_latest_publication
|
|
|
|
|
|
|
|
def new_fetch(*args, **kwargs):
|
|
|
|
return new_fetch_result
|
|
|
|
|
|
|
|
IndividualAllocationRegistry.fetch_latest_publication = new_fetch
|
|
|
|
yield
|
|
|
|
IndividualAllocationRegistry.fetch_latest_publication = original_fetch
|
2018-04-13 00:24:55 +00:00
|
|
|
|
|
|
|
|
2018-06-25 19:21:01 +00:00
|
|
|
def test_contract_registry(tempfile_path):
|
2018-06-20 20:57:16 +00:00
|
|
|
|
2019-08-15 03:29:24 +00:00
|
|
|
# ABC
|
|
|
|
with pytest.raises(TypeError):
|
|
|
|
BaseContractRegistry(filepath='test')
|
|
|
|
|
2019-08-14 02:17:08 +00:00
|
|
|
with pytest.raises(BaseContractRegistry.RegistryError):
|
2019-08-15 03:29:24 +00:00
|
|
|
bad_registry = LocalContractRegistry(filepath='/fake/file/path/registry.json')
|
2018-06-25 19:21:01 +00:00
|
|
|
bad_registry.search(contract_address='0xdeadbeef')
|
2018-06-20 20:57:16 +00:00
|
|
|
|
2018-04-13 00:24:55 +00:00
|
|
|
# Tests everything is as it should be when initially created
|
2019-08-15 03:29:24 +00:00
|
|
|
test_registry = LocalContractRegistry(filepath=tempfile_path)
|
2018-04-13 00:24:55 +00:00
|
|
|
|
2018-09-28 17:26:42 +00:00
|
|
|
assert test_registry.read() == list()
|
2018-04-13 00:24:55 +00:00
|
|
|
|
2018-04-16 07:19:05 +00:00
|
|
|
# Test contract enrollment and dump_chain
|
2018-06-20 20:57:16 +00:00
|
|
|
test_name = 'TestContract'
|
2018-04-13 00:24:55 +00:00
|
|
|
test_addr = '0xDEADBEEF'
|
|
|
|
test_abi = ['fake', 'data']
|
|
|
|
|
2018-06-25 19:21:01 +00:00
|
|
|
test_registry.enroll(contract_name=test_name,
|
|
|
|
contract_address=test_addr,
|
|
|
|
contract_abi=test_abi)
|
2018-04-13 00:24:55 +00:00
|
|
|
|
2018-06-20 20:57:16 +00:00
|
|
|
# Search by name...
|
2018-06-25 19:21:01 +00:00
|
|
|
contract_records = test_registry.search(contract_name=test_name)
|
2018-06-20 20:57:16 +00:00
|
|
|
assert len(contract_records) == 1, 'More than one record for {}'.format(test_name)
|
|
|
|
assert len(contract_records[0]) == 3, 'Registry record is the wrong length'
|
|
|
|
name, address, abi = contract_records[0]
|
2018-04-13 00:24:55 +00:00
|
|
|
|
2018-06-20 20:57:16 +00:00
|
|
|
assert name == test_name
|
|
|
|
assert address == test_addr
|
|
|
|
assert abi == test_abi
|
2018-04-13 00:24:55 +00:00
|
|
|
|
2019-08-21 12:11:39 +00:00
|
|
|
# ...or by address
|
2018-06-25 19:21:01 +00:00
|
|
|
contract_record = test_registry.search(contract_address=test_addr)
|
2018-06-20 20:57:16 +00:00
|
|
|
name, address, abi = contract_record
|
2018-04-13 00:24:55 +00:00
|
|
|
|
2018-06-20 20:57:16 +00:00
|
|
|
assert name == test_name
|
|
|
|
assert address == test_addr
|
|
|
|
assert abi == test_abi
|
2018-04-13 00:24:55 +00:00
|
|
|
|
2018-06-20 20:57:16 +00:00
|
|
|
# Check that searching for an unknown contract raises
|
2019-08-14 02:17:08 +00:00
|
|
|
with pytest.raises(BaseContractRegistry.UnknownContract):
|
2018-06-25 19:21:01 +00:00
|
|
|
test_registry.search(contract_name='this does not exist')
|
2018-04-13 00:24:55 +00:00
|
|
|
|
2018-06-25 19:21:01 +00:00
|
|
|
current_dataset = test_registry.read()
|
2019-08-21 12:11:39 +00:00
|
|
|
# Corrupt the registry with a duplicate address
|
2018-06-20 20:57:16 +00:00
|
|
|
current_dataset.append([test_name, test_addr, test_abi])
|
2018-10-17 16:33:20 +00:00
|
|
|
test_registry.write(current_dataset)
|
2018-04-13 00:24:55 +00:00
|
|
|
|
2018-06-20 20:57:16 +00:00
|
|
|
# Check that searching for an unknown contract raises
|
2019-08-14 02:17:08 +00:00
|
|
|
with pytest.raises(BaseContractRegistry.IllegalRegistry):
|
2018-06-25 19:21:01 +00:00
|
|
|
test_registry.search(contract_address=test_addr)
|
2019-10-13 21:55:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_individual_allocation_registry(get_random_checksum_address, test_registry, tempfile_path):
|
|
|
|
empty_allocation_escrow_deployer = PreallocationEscrowDeployer(registry=test_registry)
|
|
|
|
allocation_contract_abi = empty_allocation_escrow_deployer.get_contract_abi()
|
|
|
|
|
|
|
|
beneficiary = get_random_checksum_address()
|
|
|
|
contract_address = get_random_checksum_address()
|
|
|
|
allocation_registry = IndividualAllocationRegistry(beneficiary_address=beneficiary,
|
|
|
|
contract_address=contract_address)
|
|
|
|
|
|
|
|
registry_data = allocation_registry.read()
|
|
|
|
assert len(registry_data) == 1
|
|
|
|
|
|
|
|
assert allocation_registry.search(beneficiary_address=beneficiary) == [contract_address, allocation_contract_abi]
|
|
|
|
assert allocation_registry.search(contract_address=contract_address) == [beneficiary, allocation_contract_abi]
|
|
|
|
|
|
|
|
# Check that searching for an unknown beneficiary or unknown contract raises
|
|
|
|
with pytest.raises(IndividualAllocationRegistry.UnknownBeneficiary):
|
|
|
|
allocation_registry.search(beneficiary_address=get_random_checksum_address())
|
|
|
|
|
|
|
|
with pytest.raises(IndividualAllocationRegistry.UnknownContract):
|
|
|
|
allocation_registry.search(contract_address=get_random_checksum_address())
|
|
|
|
|
|
|
|
# Check that it gets the same data if using a file to create the allocation registry
|
|
|
|
individual_allocation_file_data = {
|
|
|
|
'beneficiary_address': beneficiary,
|
|
|
|
'contract_address': contract_address
|
|
|
|
}
|
|
|
|
with open(tempfile_path, 'w') as outfile:
|
|
|
|
json.dump(individual_allocation_file_data, outfile)
|
|
|
|
|
|
|
|
allocation_registry = IndividualAllocationRegistry.from_allocation_file(filepath=tempfile_path)
|
|
|
|
assert registry_data == allocation_registry.read()
|
|
|
|
|