From 25926d5fc03b035272b1109736c1379d925e2ce1 Mon Sep 17 00:00:00 2001 From: Holger <holger@nahrstaedt.de> Date: Tue, 25 Sep 2018 00:34:41 +0200 Subject: [PATCH] Add unit test for witness_set_properties RawString added to types Some improvements and fixes --- beem/account.py | 2 +- beembase/objects.py | 5 ++++- beembase/operationids.py | 2 +- beembase/operations.py | 11 +++++------ beemgraphenebase/types.py | 15 +++++++++++++++ tests/beembase/test_transactions.py | 22 ++++++++++++++++++++++ 6 files changed, 48 insertions(+), 9 deletions(-) diff --git a/beem/account.py b/beem/account.py index ee0dd4fa..e607552d 100644 --- a/beem/account.py +++ b/beem/account.py @@ -355,7 +355,7 @@ class Account(BlockchainObject): if "voting_power" in self: total_vp = (self["voting_power"] / 100 + regenerated_vp) elif "voting_manabar" in self: - total_vp = (self["voting_manabar"]["current_mana"] / 100 + regenerated_vp) + total_vp = int(self["voting_manabar"]["current_mana"]) / 100 + regenerated_vp if total_vp > 100: return 100 if total_vp < 0: diff --git a/beembase/objects.py b/beembase/objects.py index 8de03909..40008657 100644 --- a/beembase/objects.py +++ b/beembase/objects.py @@ -79,7 +79,10 @@ class Amount(object): def __bytes__(self): # padding # asset = self.asset + "\x00" * (7 - len(self.asset)) - symbol = self.symbol + "\x00" * (7 - len(self.symbol)) + try: + symbol = self.symbol + "\x00" * (7 - len(self.symbol)) + except: + symbol = self.asset + "\x00" * (7 - len(self.asset)) return (struct.pack("<q", int(self.amount)) + struct.pack("<b", self.precision) + py23_bytes(symbol, "ascii")) diff --git a/beembase/operationids.py b/beembase/operationids.py index 92726da6..f609c523 100644 --- a/beembase/operationids.py +++ b/beembase/operationids.py @@ -45,8 +45,8 @@ ops = [ 'set_reset_account', 'claim_reward_balance', 'delegate_vesting_shares', - 'witness_set_properties', 'account_create_with_delegation', + 'witness_set_properties', 'fill_convert_request', 'author_reward', 'curation_reward', diff --git a/beembase/operations.py b/beembase/operations.py index 9ecf8c83..d6a2ff63 100644 --- a/beembase/operations.py +++ b/beembase/operations.py @@ -13,7 +13,7 @@ from beemgraphenebase.types import ( Varint32, Int64, String, Bytes, Void, Array, PointInTime, Signature, Bool, Set, Fixed_array, Optional, Static_variant, - Map, Id + Map, Id, RawString ) from .objects import GrapheneObject, isArgsThisClass from beemgraphenebase.account import PublicKey @@ -297,8 +297,10 @@ class Witness_set_properties(GrapheneObject): is_hex = re.fullmatch(r'[0-9a-fA-F]+', k[1] or '') is not None else: is_hex = False - if isinstance(k[1], int) and k[0] in ["account_subsidy_budget", "account_subsidy_decay", "maximum_block_size", "sbd_interest_rate"]: + if isinstance(k[1], int) and k[0] in ["account_subsidy_budget", "account_subsidy_decay", "maximum_block_size"]: props[k[0]] = (hexlify(Uint32(k[1]).__bytes__())).decode() + elif isinstance(k[1], int) and k[0] in ["sbd_interest_rate"]: + props[k[0]] = (hexlify(Uint16(k[1]).__bytes__())).decode() elif not isinstance(k[1], str) and k[0] in ["account_creation_fee"]: props[k[0]] = (hexlify(Amount(k[1]).__bytes__())).decode() elif not is_hex and isinstance(k[1], str) and k[0] in ["account_creation_fee"]: @@ -311,10 +313,7 @@ class Witness_set_properties(GrapheneObject): props[k[0]] = (k[1]) props_list = [] for k in props: - if k == "key": - props_list.append(([String(k), String(props[k])])) - else: - props_list.append(([String(k), String(props[k])])) + props_list.append(([String(k), RawString(props[k])])) map_props = Map(props_list) super(Witness_set_properties, self).__init__(OrderedDict([ diff --git a/beemgraphenebase/types.py b/beemgraphenebase/types.py index 8fc72b75..af44b132 100644 --- a/beemgraphenebase/types.py +++ b/beemgraphenebase/types.py @@ -160,6 +160,21 @@ class Int64(object): return '%d' % self.data +@python_2_unicode_compatible +class RawString(object): + def __init__(self, d): + self.data = d + + def __bytes__(self): + """Returns bytes representation.""" + d = bytes(self.data, "utf-8") + return varint(len(d)) + d + + def __str__(self): + """Returns data as string.""" + return '%s' % str(self.data) + + @python_2_unicode_compatible class String(object): def __init__(self, d): diff --git a/tests/beembase/test_transactions.py b/tests/beembase/test_transactions.py index bbe95835..365a1bf1 100644 --- a/tests/beembase/test_transactions.py +++ b/tests/beembase/test_transactions.py @@ -653,6 +653,28 @@ class Testcases(unittest.TestCase): "b9f2405478badadb4c") self.doit() + def test_witness_set_properties(self): + self.op = operations.Witness_set_properties( + **{ + "owner": "init-1", + "props": [ + ["key", "032d2a4af3e23294e0a1d9dbc46e0272d8e1977ce2ae3349527cc90fe1cc9c5db9"], + ["account_creation_fee", "d0070000000000000354455354530000"] + ], + "prefix": default_prefix + }) + + self.cm = ("f68585abf4dce7c80457012a06696e69742d3102036b65794230" + "3332643261346166336532333239346530613164396462633436" + "6530323732643865313937376365326165333334393532376363" + "39306665316363396335646239146163636f756e745f63726561" + "74696f6e5f666565206430303730303030303030303030303030" + "3335343435353335343533303030300000011f3ed64264c74203" + "7955e9eb71f7de8a6bd2635251ad569629f849f23d53f2874d0a" + "879f2579bee31f34bbc7c67456e5be08ee8436f0fd5e0e4cc030" + "3030000001") + self.doit() + def test_witness_vote(self): self.op = operations.Account_witness_vote(**{ "account": "xeroc", -- GitLab