Skip to content

Commit

Permalink
Hostname, port to client binary call; fix prior Athens issues (tezos-…
Browse files Browse the repository at this point in the history
…reward-distributor-organization#168)

* pass hostname and port to client binary call

* remove debugging line

* fix unit tests

* Fixes tezos-reward-distributor-organization#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
  • Loading branch information
utdrmac authored and jdsika committed Jan 16, 2020
1 parent 9252a86 commit df5c86d
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 35 deletions.
2 changes: 1 addition & 1 deletion src/NetworkConfiguration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}

Expand Down
16 changes: 12 additions & 4 deletions src/cli/simple_client_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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))
raise ClientException("Signature not found in response '{}'. Signed with key '{}'".format(response, key_name))
8 changes: 4 additions & 4 deletions src/cli/test_walletClientManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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')
Expand All @@ -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']
Expand All @@ -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)
self.assertEqual(baker['sk'], True)
4 changes: 2 additions & 2 deletions src/cli/wallet_client_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions src/config/test_BakingYamlConfParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand All @@ -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)
Expand Down
28 changes: 14 additions & 14 deletions src/pay/batch_payer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)))
Expand Down
10 changes: 5 additions & 5 deletions src/rpc/rpc_reward_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__()
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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

Expand All @@ -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),
Expand Down

0 comments on commit df5c86d

Please sign in to comment.