def __init__( self, web3: Web3, private_key: str, gas_limit: int, gas_price: int, wait: int = 10, contracts_version: Optional[str] = None, ): # pylint: disable=E1101 super(ContractDeployer, self).__init__(web3=web3, contracts_version=contracts_version) self.wait = wait self.owner = private_key_to_address(private_key) self.transaction = {"from": self.owner, "gas": gas_limit} self.transaction["gasPrice"] = gas_price * int(units["gwei"]) self.web3.middleware_onion.add( construct_sign_and_send_raw_middleware(private_key)) # Check that the precompiled data matches the source code # Only for current version, because this is the only one with source code if self.contracts_version in [None, CONTRACTS_VERSION]: contract_manager_source = ContractSourceManager( contracts_source_path(contracts_version=contracts_version)) contract_manager_source.verify_precompiled_checksums( self.precompiled_path) else: LOG.info( "Skipped checks against the source code because it is not available." )
def run(self) -> None: # pylint: disable=no-self-use from raiden_contracts.contract_manager import contracts_precompiled_path from raiden_contracts.contract_source_manager import ( ContractSourceManager, contracts_source_path, ) contract_manager = ContractSourceManager(contracts_source_path(contracts_version=None)) contract_manager.compile_contracts(contracts_precompiled_path())
def run(self): from raiden_contracts.contract_manager import contracts_precompiled_path from raiden_contracts.contract_source_manager import ( ContractSourceManager, contracts_source_path, ) contract_manager = ContractSourceManager(contracts_source_path()) contract_manager.compile_contracts(contracts_precompiled_path())
def run(self): from raiden_contracts.contract_manager import ( contracts_precompiled_path, ) from raiden_contracts.contract_source_manager import ( ContractSourceManager, contracts_source_path, ) manager = ContractSourceManager(contracts_source_path()) manager.verify_precompiled_checksums(contracts_precompiled_path())
def test_contract_manager_json(tmpdir): """ Check the ABI in contracts.json """ precompiled_path = Path(str(tmpdir)).joinpath('contracts.json') ContractSourceManager( contracts_source_path()).compile_contracts(precompiled_path) # try to load contracts from a precompiled file contract_manager_meta(precompiled_path, source=False)
def test_contract_manager_json(tmpdir: LocalPath) -> None: """ Check the ABI in contracts.json """ precompiled_path = Path(tmpdir).joinpath("contracts.json") ContractSourceManager( contracts_source_path()).compile_contracts(precompiled_path) # try to load contracts from a precompiled file contract_manager_meta(precompiled_path)
def contract_source_manager_meta(contracts_path: Dict[str, Path]) -> None: """ See failures in looking up non-existent ABI entries of TokenNetwork and CLOSED """ with NamedTemporaryFile() as tmpfile: manager = ContractSourceManager(contracts_path).compile_contracts(Path(tmpfile.name)) abi = manager.get_contract_abi(CONTRACT_TOKEN_NETWORK) assert isinstance(abi, list) with pytest.raises(KeyError): manager.get_contract_abi("SomeName") with pytest.raises(KeyError): manager.get_contract("SomeName") abi2 = manager.get_event_abi(CONTRACT_TOKEN_NETWORK, ChannelEvent.CLOSED) assert isinstance(abi2, dict) with pytest.raises(ValueError): manager.get_event_abi(CONTRACT_TOKEN_NETWORK, "NonExistant")
def contract_manager_meta(contracts_path, source: bool): """ See failures in looking up non-existent ABI entries of TokenNetwork and CLOSED """ with NamedTemporaryFile() as tmpfile: if source: manager = ContractSourceManager(contracts_path).compile_contracts( Path(tmpfile.name)) else: manager = ContractManager(contracts_path) abi = manager.get_contract_abi(CONTRACT_TOKEN_NETWORK) assert isinstance(abi, list) with pytest.raises(KeyError): manager.get_contract_abi('SomeName') abi = manager.get_event_abi(CONTRACT_TOKEN_NETWORK, ChannelEvent.CLOSED) assert isinstance(abi, dict) with pytest.raises(ValueError): manager.get_event_abi(CONTRACT_TOKEN_NETWORK, 'NonExistant')
def __init__( self, web3: Web3, private_key: str, gas_limit: int, gas_price: int = 1, wait: int = 10, contracts_version: Optional[str] = None, ): # pylint: disable=E1101 self.web3 = web3 self.wait = wait self.owner = private_key_to_address(private_key) self.transaction = {'from': self.owner, 'gas': gas_limit} if gas_price != 0: self.transaction['gasPrice'] = gas_price * denoms.gwei self.contracts_version = contracts_version self.precompiled_path = contracts_precompiled_path( self.contracts_version) self.contract_manager = ContractManager(self.precompiled_path) self.web3.middleware_stack.add( construct_sign_and_send_raw_middleware(private_key), ) # Check that the precompiled data matches the source code # Only for current version, because this is the only one with source code if self.contracts_version in [None, CONTRACTS_VERSION]: contract_manager_source = ContractSourceManager( contracts_source_path()) contract_manager_source.checksum_contracts() contract_manager_source.verify_precompiled_checksums( self.precompiled_path) else: LOG.info( 'Skipped checks against the source code because it is not available.' )
def test_verification_contracts_checksums(): """ Tamper with the contract checksums and see failures in verify_precompiled_checksums() """ manager = ContractSourceManager(contracts_source_path()) manager.checksum_contracts() manager.verify_precompiled_checksums(contracts_precompiled_path()) assert manager.contracts_checksums for contract, checksum in manager.contracts_checksums.items(): manager.contracts_checksums[contract] += '2' with pytest.raises(ContractSourceManagerVerificationError): manager.verify_precompiled_checksums(contracts_precompiled_path()) manager.contracts_checksums[contract] = None # type: ignore with pytest.raises(ContractSourceManagerVerificationError): manager.verify_precompiled_checksums(contracts_precompiled_path()) manager.contracts_checksums[contract] = '' with pytest.raises(ContractSourceManagerVerificationError): manager.verify_precompiled_checksums(contracts_precompiled_path()) checksum_fail = list(checksum) # Replace the first char with a different one checksum_fail[0] = 'a' if checksum_fail[0] == '2' else '2' manager.contracts_checksums[contract] = ''.join(checksum_fail) with pytest.raises(ContractSourceManagerVerificationError): manager.verify_precompiled_checksums(contracts_precompiled_path()) manager.contracts_checksums[contract] = checksum manager.verify_precompiled_checksums(contracts_precompiled_path())
def test_verification_overall_checksum(): """ Tamper with the overall checksum and see failures in verify_precompiled_checksums() """ manager = ContractSourceManager(contracts_source_path()) manager.checksum_contracts() manager.verify_precompiled_checksums(contracts_precompiled_path()) assert manager.overall_checksum original_checksum = manager.overall_checksum # We change the source code overall checksum manager.overall_checksum += '2' # Now the verification should fail with pytest.raises(ContractSourceManagerVerificationError): manager.verify_precompiled_checksums(contracts_precompiled_path()) manager.overall_checksum = None # type: ignore with pytest.raises(ContractSourceManagerVerificationError): manager.verify_precompiled_checksums(contracts_precompiled_path()) manager.overall_checksum = '' with pytest.raises(ContractSourceManagerVerificationError): manager.verify_precompiled_checksums(contracts_precompiled_path()) checksum_fail = list(original_checksum) # Replace the first char with a different one checksum_fail[0] = 'a' if checksum_fail[0] == '2' else '2' manager.overall_checksum = ''.join(checksum_fail) with pytest.raises(ContractSourceManagerVerificationError): manager.verify_precompiled_checksums(contracts_precompiled_path()) manager.overall_checksum = original_checksum manager.verify_precompiled_checksums(contracts_precompiled_path())
def test_contract_source_manager_constructor_with_wrong_type(): """ ConstructSourceManager's constructor raises TypeError on a wrong kind of argument """ with pytest.raises(TypeError): ContractSourceManager(None) # type: ignore
def test_verification_without_checksum() -> None: """ Call ContractSourceManager.verify_precompiled_checksums() before computing the checksum """ manager = ContractSourceManager(contracts_source_path()) with pytest.raises(AttributeError): manager.verify_precompiled_checksums(contracts_precompiled_path())
def contracts_manager( contract_source_manager: ContractSourceManager) -> Generator: with NamedTemporaryFile() as target_path: yield contract_source_manager.compile_contracts(Path(target_path.name))
def contract_source_manager() -> ContractSourceManager: return ContractSourceManager(contracts_source_path())
def contract_source_manager() -> ContractSourceManager: return ContractSourceManager(contracts_source_path(contracts_version=None))