def __init__( self, jsonrpc_client, registry_address, ): if not is_binary_address(registry_address): raise InvalidAddress( 'Expected binary address format for token network registry') check_address_has_code(jsonrpc_client, registry_address, CONTRACT_TOKEN_NETWORK_REGISTRY) proxy = jsonrpc_client.new_contract_proxy( CONTRACT_MANAGER.get_contract_abi(CONTRACT_TOKEN_NETWORK_REGISTRY), to_normalized_address(registry_address), ) try: is_valid_version = compare_versions( proxy.contract.functions.contract_version().call(), EXPECTED_CONTRACTS_VERSION, ) except BadFunctionCallOutput: raise AddressWrongContract('') if not is_valid_version: raise ContractVersionMismatch( 'Incompatible ABI for TokenNetworkRegistry') self.address = registry_address self.proxy = proxy self.client = jsonrpc_client self.node_address = privatekey_to_address(self.client.privkey)
def __init__( self, jsonrpc_client, secret_registry_address, ): if not is_binary_address(secret_registry_address): raise InvalidAddress( 'Expected binary address format for secret registry') check_address_has_code(jsonrpc_client, secret_registry_address, CONTRACT_SECRET_REGISTRY) proxy = jsonrpc_client.new_contract_proxy( CONTRACT_MANAGER.get_contract_abi(CONTRACT_SECRET_REGISTRY), to_normalized_address(secret_registry_address), ) try: if not compare_versions( proxy.contract.functions.contract_version().call(), EXPECTED_CONTRACTS_VERSION, ): raise ContractVersionMismatch( 'Incompatible ABI for SecretRegistry') except BadFunctionCallOutput: raise AddressWrongContract('') self.address = secret_registry_address self.proxy = proxy self.client = jsonrpc_client self.node_address = privatekey_to_address(self.client.privkey) self.open_secret_transactions = dict()
def check_contract_version(self, deployed_version, contract_name): """Check if the deployed contract version matches used contract version.""" our_version = CONTRACT_MANAGER.get_version(contract_name) if compare_versions(deployed_version, our_version) is False: raise ContractVersionMismatch( f'Incompatible ABI for {contract_name}. ' f'Expected {our_version} got {deployed_version}', )
def compare_contract_versions( proxy: ContractProxy, expected_version: str, contract_name: str, address: Address, ) -> None: """Compare version strings of a contract. If not matching raise ContractVersionMismatch. Also may raise AddressWrongContract if the contract contains no code.""" assert isinstance(expected_version, str) try: deployed_version = proxy.contract.functions.contract_version().call() except BadFunctionCallOutput: raise AddressWrongContract('') deployed_version = deployed_version.replace('_', '0') expected_version = expected_version.replace('_', '0') deployed = [int(x) for x in deployed_version.split('.')] expected = [int(x) for x in expected_version.split('.')] if deployed != expected: raise ContractVersionMismatch( f'Provided {contract_name} contract ({to_normalized_address(address)}) ' f'version mismatch. Expected: {expected_version} Got: {deployed_version}', )
def __init__( self, jsonrpc_client, manager_address, ): if not is_binary_address(manager_address): raise InvalidAddress( 'Expected binary address format for token nework') check_address_has_code(jsonrpc_client, manager_address, 'Channel Manager') proxy = jsonrpc_client.new_contract_proxy( CONTRACT_MANAGER.get_contract_abi(CONTRACT_TOKEN_NETWORK), to_normalized_address(manager_address), ) if not compare_versions( proxy.contract.functions.contract_version().call(), EXPECTED_CONTRACTS_VERSION, ): raise ContractVersionMismatch('Incompatible ABI for TokenNetwork') self.address = manager_address self.proxy = proxy self.client = jsonrpc_client self.node_address = privatekey_to_address(self.client.privkey) # Prevents concurrent deposit, withdraw, close, or settle operations on the same channel self.channel_operations_lock = dict() self.open_channel_transactions = dict()
def __init__( self, jsonrpc_client, discovery_address, ): contract = jsonrpc_client.new_contract( CONTRACT_MANAGER.get_contract_abi(CONTRACT_ENDPOINT_REGISTRY), to_normalized_address(discovery_address), ) proxy = ContractProxy(jsonrpc_client, contract) if not is_binary_address(discovery_address): raise ValueError('discovery_address must be a valid address') check_address_has_code(jsonrpc_client, discovery_address, 'Discovery') try: is_valid_version = compare_versions( proxy.contract.functions.contract_version().call(), EXPECTED_CONTRACTS_VERSION, ) if not is_valid_version: raise ContractVersionMismatch('Incompatible ABI for Discovery') except BadFunctionCallOutput: raise AddressWrongContract('') self.address = discovery_address self.client = jsonrpc_client self.not_found_address = NULL_ADDRESS self.proxy = proxy
def __init__( self, jsonrpc_client, manager_address, ): if not is_binary_address(manager_address): raise InvalidAddress('Expected binary address format for token nework') check_address_has_code(jsonrpc_client, manager_address, CONTRACT_TOKEN_NETWORK) proxy = jsonrpc_client.new_contract_proxy( CONTRACT_MANAGER.get_contract_abi(CONTRACT_TOKEN_NETWORK), to_normalized_address(manager_address), ) is_good_version = compare_versions( proxy.contract.functions.contract_version().call(), EXPECTED_CONTRACTS_VERSION, ) if not is_good_version: raise ContractVersionMismatch('Incompatible ABI for TokenNetwork') self.address = manager_address self.proxy = proxy self.client = jsonrpc_client self.node_address = privatekey_to_address(self.client.privkey) self.open_channel_transactions = dict() # Forbids concurrent operations on the same channel self.channel_operations_lock = defaultdict(RLock) # Serializes concurent deposits on this token network. This must be an # exclusive lock, since we need to coordinate the approve and # setTotalDeposit calls. self.deposit_lock = Semaphore()