Skip to content

Commit 1dbb153

Browse files
committed
Add assert_operation
1 parent e0a8f3a commit 1dbb153

File tree

3 files changed

+130
-9
lines changed

3 files changed

+130
-9
lines changed

bitsharesbase/objects.py

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
Varint32,
2323
Void,
2424
VoteId,
25+
Ripemd160,
2526
)
2627

2728
from .account import PublicKey
@@ -31,6 +32,8 @@
3132

3233
default_prefix = "BTS"
3334

35+
BlockId = Ripemd160
36+
3437

3538
class Operation(GrapheneOperation):
3639
""" Need to overwrite a few attributes to load proper operations from
@@ -319,7 +322,7 @@ def __init__(self, *args, **kwargs):
319322
elif id == 2:
320323
data = Burn_worker_initializer(o[1])
321324
else:
322-
raise Exception("Unknown Worker_initializer")
325+
raise ValueError("Unknown {}".format(self.__class__.name))
323326
super().__init__(data, id)
324327

325328

@@ -415,3 +418,46 @@ def targetCollateralRatio(value):
415418
return None
416419

417420
sorted_options = [("target_collateral_ratio", targetCollateralRatio)]
421+
422+
423+
class AssertPredicate(Static_variant):
424+
def __init__(self, o):
425+
class Account_name_eq_lit_predicate(GrapheneObject):
426+
def __init__(self, *args, **kwargs):
427+
kwargs.update(args[0])
428+
super().__init__(
429+
OrderedDict(
430+
[
431+
("account_id", ObjectId(kwargs["account_id"], "account")),
432+
("name", String(kwargs["name"])),
433+
]
434+
)
435+
)
436+
437+
class Asset_symbol_eq_lit_predicate(GrapheneObject):
438+
def __init__(self, *args, **kwargs):
439+
kwargs.update(args[0])
440+
super().__init__(
441+
OrderedDict(
442+
[
443+
("asset_id", ObjectId(kwargs["asset_id"], "asset")),
444+
("symbol", String(kwargs["symbol"])),
445+
]
446+
)
447+
)
448+
449+
class Block_id_predicate(GrapheneObject):
450+
def __init__(self, *args, **kwargs):
451+
kwargs.update(args[0])
452+
super().__init__(OrderedDict([("id", BlockId(kwargs["id"]))]))
453+
454+
id = o[0]
455+
if id == 0:
456+
data = Account_name_eq_lit_predicate(o[1])
457+
elif id == 1:
458+
data = Asset_symbol_eq_lit_predicate(o[1])
459+
elif id == 2:
460+
data = Block_id_predicate(o[1])
461+
else:
462+
raise ValueError("Unknown {}".format(self.__class__.name))
463+
super().__init__(data, id)

bitsharesbase/operations.py

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
# -*- coding: utf-8 -*-
2-
import json
3-
42
from collections import OrderedDict
5-
from binascii import hexlify, unhexlify
63

74
from graphenebase.types import (
85
Array,
@@ -16,7 +13,6 @@
1613
Optional,
1714
PointInTime,
1815
Set,
19-
Signature,
2016
Static_variant,
2117
String,
2218
Uint8,
@@ -25,7 +21,6 @@
2521
Uint64,
2622
Varint32,
2723
Void,
28-
VoteId,
2924
Ripemd160,
3025
Sha1,
3126
Sha256,
@@ -46,9 +41,9 @@
4641
Permission,
4742
Price,
4843
PriceFeed,
49-
SpecialAuthority,
5044
Worker_initializer,
5145
isArgsThisClass,
46+
AssertPredicate,
5247
)
5348
from .operationids import operations
5449

@@ -918,9 +913,15 @@ def detail(self, *args, **kwargs):
918913
return OrderedDict(
919914
[
920915
("fee", Asset(kwargs["fee"])),
921-
("deposit_to_account", ObjectId(kwargs["deposit_to_account"], "account")),
916+
(
917+
"deposit_to_account",
918+
ObjectId(kwargs["deposit_to_account"], "account"),
919+
),
922920
("balance_to_claim", ObjectId(kwargs["balance_to_claim"], "balance")),
923-
("balance_owner_key", PublicKey(kwargs["balance_owner_key"], prefix=prefix)),
921+
(
922+
"balance_owner_key",
923+
PublicKey(kwargs["balance_owner_key"], prefix=prefix),
924+
),
924925
("total_claimed", Asset(kwargs["total_claimed"])),
925926
]
926927
)
@@ -1013,4 +1014,36 @@ def __init__(self, *args, **kwargs):
10131014
)
10141015

10151016

1017+
class Assert(GrapheneObject):
1018+
def __init__(self, *args, **kwargs):
1019+
if isArgsThisClass(self, args):
1020+
self.data = args[0].data
1021+
else:
1022+
super().__init__(
1023+
OrderedDict(
1024+
[
1025+
("fee", Asset(kwargs["fee"])),
1026+
(
1027+
"fee_paying_account",
1028+
ObjectId(kwargs["fee_paying_account"], "account"),
1029+
),
1030+
(
1031+
"predicates",
1032+
Array([AssertPredicate(o) for o in kwargs["predicates"]]),
1033+
),
1034+
(
1035+
"required_auths",
1036+
Array(
1037+
[
1038+
ObjectId(o, "account")
1039+
for o in kwargs["required_auths"]
1040+
]
1041+
),
1042+
),
1043+
("extensions", Set([])),
1044+
]
1045+
)
1046+
)
1047+
1048+
10161049
fill_classmaps()

tests/test_transactions.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -926,6 +926,48 @@ def test_htlc_extend(self):
926926
)
927927
self.doit(0)
928928

929+
def test_assert_a(self):
930+
self.op = operations.Assert(
931+
**{
932+
"fee": {"amount": 0, "asset_id": "1.3.0"},
933+
"fee_paying_account": "1.2.0",
934+
"predicates": [],
935+
"required_auths": [],
936+
"extensions": [],
937+
}
938+
)
939+
self.cm = (
940+
"f68585abf4dce7c80457012400000000000000000000000000"
941+
"00011f2ee18d66da4314d7fa4d4cf5ad327e7c0504ace99e5d"
942+
"757b214107a955e363fe7a371aaa16101741163ce64d0c0ee0"
943+
"5c799b94467dbf15322d0f6bd7c5ed29a4"
944+
)
945+
self.doit(0)
946+
947+
def test_assert_b(self):
948+
self.op = operations.Assert(
949+
**{
950+
"fee": {"amount": 0, "asset_id": "1.3.0"},
951+
"fee_paying_account": "1.2.4",
952+
"predicates": [
953+
[0, {"account_id": "1.2.2414", "name": "foobar"}],
954+
[1, {"asset_id": "1.3.2424", "symbol": "USD"}],
955+
[2, {"id": "0260c042666dd23b6c380f55be2eaae0086f643f"}],
956+
],
957+
"required_auths": ["1.2.124124"],
958+
"extensions": [],
959+
}
960+
)
961+
self.cm = (
962+
"f68585abf4dce7c804570124000000000000000000040300ee"
963+
"1206666f6f62617201f81203555344020260c042666dd23b6c"
964+
"380f55be2eaae0086f643f01dcc9070000011f3820288751dd"
965+
"38b422c3930cb5b703e8f3af06aa50fde004eb46a480846dd4"
966+
"4e01b0d4893934225befa42e1077ab2d71fc13fff5c6e0697c"
967+
"f103aec7dc8e7496"
968+
)
969+
self.doit(0)
970+
929971
def compareConstructedTX(self):
930972
self.maxDiff = None
931973
self.op = operations.Call_order_update(

0 commit comments

Comments
 (0)