From df5c86daaa9ad0845615b34500128148f68aa1c5 Mon Sep 17 00:00:00 2001 From: Matthew Date: Wed, 15 Jan 2020 23:37:30 -0600 Subject: [PATCH] Hostname, port to client binary call; fix prior Athens issues (#168) * pass hostname and port to client binary call * remove debugging line * fix unit tests * Fixes #160 * Corrected remaining client-http calls to use correct -client parameters * fixes issue with calling RPC-API twice for same URL; corrected try-except logic flow * revert previous logic; remove extra request * revert http calls due to requests.get usage --- src/NetworkConfiguration.py | 2 +- src/cli/simple_client_manager.py | 16 ++++++++++---- src/cli/test_walletClientManager.py | 8 +++---- src/cli/wallet_client_manager.py | 4 ++-- src/config/test_BakingYamlConfParser.py | 6 +++--- src/main.py | 4 ++-- src/pay/batch_payer.py | 28 ++++++++++++------------- src/rpc/rpc_reward_api.py | 10 ++++----- 8 files changed, 43 insertions(+), 35 deletions(-) diff --git a/src/NetworkConfiguration.py b/src/NetworkConfiguration.py index 133c8085..547f98a8 100644 --- a/src/NetworkConfiguration.py +++ b/src/NetworkConfiguration.py @@ -15,7 +15,7 @@ 'BLOCKS_PER_ROLL_SNAPSHOT': 8}, } -CONSTANTS_COMM = " rpc get http://{}/chains/main/blocks/head/context/constants" +CONSTANTS_COMM = " rpc get /chains/main/blocks/head/context/constants" URL = "https://{}.tzbeta.net/chains/main/blocks/head/context/constants" url_prefix = {"MAINNET": "rpc", "ALPHANET": "rpcalpha", "ZERONET": "rpczero"} diff --git a/src/cli/simple_client_manager.py b/src/cli/simple_client_manager.py index bbc72b92..2d4a5ec6 100644 --- a/src/cli/simple_client_manager.py +++ b/src/cli/simple_client_manager.py @@ -3,15 +3,23 @@ class SimpleClientManager: - def __init__(self, client_path, verbose=None) -> None: + def __init__(self, client_path, node_addr, verbose=None) -> None: super().__init__() self.verbose = verbose self.client_path = client_path self.cmd_manager = CommandManager(verbose) + self.node_hostname = "127.0.0.1" + self.node_port = 8732 - def send_request(self, cmd, verbose_override=None, timeout=None): - whole_cmd = self.client_path + cmd + # Need to split host:port, default port to 8732 if not specified + if node_addr is not None: + parts = node_addr.split(":") + self.node_hostname = parts[0] + self.node_port = 8732 if len(parts) == 1 else parts[1] + def send_request(self, cmd, verbose_override=None, timeout=None): + # Build command with flags + whole_cmd = "{} -A {} -P {} {}".format(self.client_path, self.node_hostname, self.node_port, cmd) return self.cmd_manager.execute(whole_cmd, verbose_override, timeout=timeout) def sign(self, bytes, key_name, verbose_override=None): @@ -24,4 +32,4 @@ def sign(self, bytes, key_name, verbose_override=None): if "Signature" in line: return line.replace("Signature:","").strip() - raise ClientException("Signature not found in response '{}'. Signed with key '{}'".format(response, key_name)) \ No newline at end of file + raise ClientException("Signature not found in response '{}'. Signed with key '{}'".format(response, key_name)) diff --git a/src/cli/test_walletClientManager.py b/src/cli/test_walletClientManager.py index c6fbd16e..8200684e 100644 --- a/src/cli/test_walletClientManager.py +++ b/src/cli/test_walletClientManager.py @@ -16,7 +16,7 @@ def test_parse_get_manager_for_contract_response(self): tz1PJnnddwa1P5jg5sChddigxfMccK8nwLiV (known as habanoz) """ - clientManager = WalletClientManager(None) + clientManager = WalletClientManager(None, node_addr=None) manager = clientManager.parse_get_manager_for_contract_response(response) self.assertEqual('tz1PJnnddwa1P5jg5sChddigxfMccK8nwLiV', manager) @@ -34,7 +34,7 @@ def test_parse_client_list_known_contracts_response(self): habanoz: tz1fyvFH2pd3V9UEq5psqVokVBYkt7rHTKio mainnetme: tz1a5GGJeyqeQ4ihZqbiRVcvj5rY5kMAt3Xa """ - clientManager = WalletClientManager(None) + clientManager = WalletClientManager(None, node_addr=None) dict = clientManager.parse_list_known_contracts_response(response) self.assertTrue(dict['newcontr'] == 'KT1XqEHigP5XumZy9i76QyVd6u93VD4HTqJK') @@ -58,7 +58,7 @@ def test_parse_list_known_addresses_response(self): baker: tz1XXXXXXXX (unix sk known) """ - clientManager = WalletClientManager(None) + clientManager = WalletClientManager(None, node_addr=None) dict = clientManager.parse_list_known_addresses_response(response) habanoz = dict['tz1fyvFH2pd3V9UEq5psqVokVBYkt7rHTKio'] @@ -84,4 +84,4 @@ def test_parse_list_known_addresses_response(self): baker = dict['tz1XXXXXXXX'] self.assertEqual(baker['alias'], 'baker') - self.assertEqual(baker['sk'], True) \ No newline at end of file + self.assertEqual(baker['sk'], True) diff --git a/src/cli/wallet_client_manager.py b/src/cli/wallet_client_manager.py index e707c4ef..ccffee4f 100644 --- a/src/cli/wallet_client_manager.py +++ b/src/cli/wallet_client_manager.py @@ -9,9 +9,9 @@ class WalletClientManager(SimpleClientManager): - def __init__(self, client_path, contr_dict_by_alias=None, + def __init__(self, client_path, node_addr, contr_dict_by_alias=None, addr_dict_by_pkh=None, managers=None, verbose=None) -> None: - super().__init__(client_path, verbose) + super().__init__(client_path, node_addr, verbose) self.managers = managers if self.managers is None: diff --git a/src/config/test_BakingYamlConfParser.py b/src/config/test_BakingYamlConfParser.py index 3cf5cebd..6868be1f 100644 --- a/src/config/test_BakingYamlConfParser.py +++ b/src/config/test_BakingYamlConfParser.py @@ -32,7 +32,7 @@ def test_validate(self): "manager": "tz1Z1tMai15JWUWeN2PKL9faXXVPMuWamzJj"} } - wallet_client_manager = WalletClientManager(client_path=None, addr_dict_by_pkh=addr_dict_by_pkh, contr_dict_by_alias=contr_dict_by_alias, managers=managers) + wallet_client_manager = WalletClientManager(client_path=None, node_addr=None, addr_dict_by_pkh=addr_dict_by_pkh, contr_dict_by_alias=contr_dict_by_alias, managers=managers) block_api = RpcBlockApiImpl(network, mainnet_public_node_url) cnf_prsr = BakingYamlConfParser(data_fine, wallet_client_manager, provider_factory=None, network_config=network,node_url=mainnet_public_node_url,block_api=block_api) @@ -66,7 +66,7 @@ def test_validate_no_founders_map(self): "alias": "main1", "sk": True, "manager": "tz1Z1tMai15JWUWeN2PKL9faXXVPMuWamzJj"}} - wallet_client_manager = WalletClientManager(client_path=None, addr_dict_by_pkh=addr_dict_by_pkh, + wallet_client_manager = WalletClientManager(client_path=None, node_addr=None, addr_dict_by_pkh=addr_dict_by_pkh, contr_dict_by_alias=contr_dict_by_alias, managers=managers_map) block_api = RpcBlockApiImpl(network, mainnet_public_node_url) @@ -109,7 +109,7 @@ def test_validate_pymnt_alias(self): "manager": "tz1Z1tMai15JWUWeN2PKL9faXXVPMuWamzJj"} } - wallet_client_manager = WalletClientManager(client_path=None, addr_dict_by_pkh=addr_dict_by_pkh, contr_dict_by_alias=contr_dict_by_alias, managers=managers_map) + wallet_client_manager = WalletClientManager(client_path=None, node_addr=None, addr_dict_by_pkh=addr_dict_by_pkh, contr_dict_by_alias=contr_dict_by_alias, managers=managers_map) block_api = RpcBlockApiImpl(network, mainnet_public_node_url) cnf_prsr = BakingYamlConfParser(data_no_founders, wallet_client_manager, provider_factory=None, network_config=network, node_url=mainnet_public_node_url, block_api=block_api) diff --git a/src/main.py b/src/main.py index 7a2381fd..ca9d8a46 100644 --- a/src/main.py +++ b/src/main.py @@ -74,7 +74,7 @@ def main(args): logger.debug("Tezos client path is {}".format(client_path)) # 4. get network config - config_client_manager = SimpleClientManager(client_path) + config_client_manager = SimpleClientManager(client_path, args.node_addr) network_config_map = init_network_config(args.network, config_client_manager, args.node_addr) network_config = network_config_map[args.network] @@ -85,7 +85,7 @@ def main(args): logger.info("Loading baking configuration file {}".format(config_file_path)) - wllt_clnt_mngr = WalletClientManager(client_path, contracts_by_alias, addresses_by_pkh, managers, + wllt_clnt_mngr = WalletClientManager(client_path, args.node_addr, contracts_by_alias, addresses_by_pkh, managers, verbose=args.verbose) provider_factory = ProviderFactory(args.reward_data_provider, verbose=args.verbose) diff --git a/src/pay/batch_payer.py b/src/pay/batch_payer.py index 10bf636e..4475999b 100644 --- a/src/pay/batch_payer.py +++ b/src/pay/batch_payer.py @@ -18,18 +18,18 @@ CONFIRMATIONS = 1 PATIENCE = 10 -COMM_HEAD = " rpc get http://{}/chains/main/blocks/head" -COMM_COUNTER = " rpc get http://{}/chains/main/blocks/head/context/contracts/{}/counter" +COMM_HEAD = "rpc get /chains/main/blocks/head" +COMM_COUNTER = "rpc get /chains/main/blocks/head/context/contracts/{}/counter" CONTENT = '{"kind":"transaction","source":"%SOURCE%","destination":"%DESTINATION%","fee":"%fee%","counter":"%COUNTER%","gas_limit": "%gas_limit%", "storage_limit": "%storage_limit%","amount":"%AMOUNT%"}' FORGE_JSON = '{"branch": "%BRANCH%","contents":[%CONTENT%]}' RUNOPS_JSON = '{"branch": "%BRANCH%","contents":[%CONTENT%], "signature":"edsigtXomBKi5CTRf5cjATJWSyaRvhfYNHqSUGrn4SdbYRcGwQrUGjzEfQDTuqHhuA8b2d8NarZjz8TRf65WkpQmo423BtomS8Q"}' PREAPPLY_JSON = '[{"protocol":"%PROTOCOL%","branch":"%BRANCH%","contents":[%CONTENT%],"signature":"%SIGNATURE%"}]' JSON_WRAP='{"operation": %JSON%,"chain_id":"%chain_id%"}' -COMM_FORGE = " rpc post http://%NODE%/chains/main/blocks/head/helpers/forge/operations with '%JSON%'" -COMM_RUNOPS = " rpc post http://%NODE%/chains/main/blocks/head/helpers/scripts/run_operation with '%JSON%'" -COMM_PREAPPLY = " rpc post http://%NODE%/chains/main/blocks/head/helpers/preapply/operations with '%JSON%'" -COMM_INJECT = " rpc post http://%NODE%/injection/operation with '\"%OPERATION_HASH%\"'" -COMM_WAIT = " wait for %OPERATION% to be included --confirmations {}".format(CONFIRMATIONS) +COMM_FORGE = "rpc post /chains/main/blocks/head/helpers/forge/operations with '%JSON%'" +COMM_RUNOPS = "rpc post /chains/main/blocks/head/helpers/scripts/run_operation with '%JSON%'" +COMM_PREAPPLY = "rpc post /chains/main/blocks/head/helpers/preapply/operations with '%JSON%'" +COMM_INJECT = "rpc post /injection/operation with '\"%OPERATION_HASH%\"'" +COMM_WAIT = "wait for %OPERATION% to be included --confirmations {}".format(CONFIRMATIONS) FEE_INI = 'fee.ini' MUTEZ = 1e6 @@ -90,13 +90,13 @@ def __init__(self, node_url, pymnt_addr, wllt_clnt_mngr, delegator_pays_xfer_fee logger.debug("Payment address is {}".format(self.source)) logger.debug("Signing address is {}, manager alias is {}".format(self.manager, self.manager_alias)) - self.comm_head = COMM_HEAD.format(self.node_url) - self.comm_counter = COMM_COUNTER.format(self.node_url, self.source) - self.comm_runops = COMM_RUNOPS.format().replace("%NODE%", self.node_url) - self.comm_forge = COMM_FORGE.format().replace("%NODE%", self.node_url) - self.comm_preapply = COMM_PREAPPLY.format().replace("%NODE%", self.node_url) - self.comm_inject = COMM_INJECT.format().replace("%NODE%", self.node_url) - self.comm_wait = COMM_WAIT.format() + self.comm_head = COMM_HEAD + self.comm_counter = COMM_COUNTER.format(self.source) + self.comm_runops = COMM_RUNOPS + self.comm_forge = COMM_FORGE + self.comm_preapply = COMM_PREAPPLY + self.comm_inject = COMM_INJECT + self.comm_wait = COMM_WAIT def pay(self, payment_items_in, verbose=None, dry_run=None): logger.info("{} payment items to process".format(len(payment_items_in))) diff --git a/src/rpc/rpc_reward_api.py b/src/rpc/rpc_reward_api.py index ed1092f3..2085fee1 100644 --- a/src/rpc/rpc_reward_api.py +++ b/src/rpc/rpc_reward_api.py @@ -14,7 +14,7 @@ class RpcRewardApiImpl(RewardApi): COMM_DELEGATES = "{}/chains/main/blocks/{}/context/delegates/{}" COMM_BLOCK = "{}/chains/main/blocks/{}" COMM_SNAPSHOT = COMM_BLOCK + "/context/raw/json/cycle/{}/roll_snapshot" - COMM_DELEGATE_BALANCE = "{}/chains/main/blocks/{}/context/contracts/{}" + COMM_DELEGATE_BALANCE = "{}/chains/main/blocks/{}/context/contracts/{}/balance" def __init__(self, nw, baking_address, node_url, verbose=True): super(RpcRewardApiImpl, self).__init__() @@ -85,7 +85,8 @@ def __get_unfrozen_rewards(self, level_of_last_block_in_unfreeze_cycle, cycle): balance_update = balance_updates[i] if balance_update["kind"] == "freezer": if balance_update["delegate"] == self.baking_address: - if int(balance_update["cycle"]) == cycle and int(balance_update["change"]) < 0: + # Protocols < Athens (004) mistakenly used 'level' + if (("level" in balance_update and int(balance_update["level"]) == cycle) or ("cycle" in balance_update and int(balance_update["cycle"]) == cycle)) and int(balance_update["change"]) < 0: if balance_update["category"] == "rewards": unfrozen_rewards = -int(balance_update["change"]) logger.debug( @@ -145,7 +146,6 @@ def __get_delegators_and_delgators_balance(self, cycle, current_level): delegators_addresses = response["delegated_contracts"] for idx, delegator in enumerate(delegators_addresses): request = self.COMM_DELEGATE_BALANCE.format(self.node_url, hash_snapshot_block, delegator) - response = self.do_rpc_request(request) sleep(0.5) # be nice to public node service @@ -155,9 +155,9 @@ def __get_delegators_and_delgators_balance(self, cycle, current_level): try: response = self.do_rpc_request(request, time_out=5) except: - logger.debug("Fetching delegator info failed {}, will retry", delegator) + logger.error("Fetching delegator info failed {}, will retry", delegator) - delegators[delegator] = int(response["balance"]) + delegators[delegator] = int(response) logger.debug( "Delegator info ({}/{}) fetched: address {}, balance {}".format(idx, len(delegators_addresses),