Skip to content

Commit

Permalink
Allow to get operation name more generally #79
Browse files Browse the repository at this point in the history
  • Loading branch information
xeroc committed Mar 27, 2019
1 parent 7fa32cc commit 78ac7fb
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 5 deletions.
19 changes: 18 additions & 1 deletion graphenebase/operationids.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
#: Operation ids
ops = [
"demooepration",
Expand All @@ -10,10 +11,26 @@
operations = {o: ops.index(o) for o in ops}


def getOperationNameForId(i):
def getOperationNameForId(i: int):
""" Convert an operation id into the corresponding string
"""
assert isinstance(i, (int)), "This method expects an integer argument"
for key in operations:
if int(operations[key]) is int(i):
return key
raise ValueError("Unknown Operation ID %d" % i)


def getOperationName(id: str):
""" This method returns the name representation of an operation given
its value as used in the API
"""
if isinstance(id, str):
# Some graphene chains (e.g. steem) do not encode the
# operation_type as id but in its string form
assert id in operations.keys(), "Unknown operation {}".format(id)
return id
elif isinstance(id, int):
return getOperationNameForId(id)
else:
raise ValueError
2 changes: 1 addition & 1 deletion graphenecommon/blockchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ def ops(self, start=None, stop=None, **kwargs):
for tx in block["transactions"]:
for op in tx["operations"]:
# Replace opid by op name
op[0] = self.operationids.getOperationNameForId(op[0])
op[0] = self.operationids.getOperationName(op[0])
yield {
"block_num": block["block_num"],
"op": op,
Expand Down
17 changes: 15 additions & 2 deletions tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,13 @@
Demooepration,
Account_create,
)
from graphenebase import operations as operations_module
from graphenebase.operationids import ops, getOperationNameForId, operations
from graphenebase import operations as operations_module, operationids
from graphenebase.operationids import (
ops,
getOperationNameForId,
getOperationName,
operations,
)

from graphenebase import bip38
from graphenebase.bip38 import encrypt, decrypt
Expand Down Expand Up @@ -76,6 +81,7 @@
from graphenecommon.asset import Asset as GAsset
from graphenecommon.committee import Committee as GCommittee
from graphenecommon.block import Block as GBlock, BlockHeader as GBlockHeader
from graphenecommon.blockchain import Blockchain as GBLockchain
from graphenecommon.message import Message as GMessage
from graphenecommon.blockchainobject import ObjectCache, BlockchainObject
from graphenecommon.price import Price as GPrice
Expand Down Expand Up @@ -259,6 +265,13 @@ def define_classes(self):
self.amount_class = Amount


@BlockchainInstance.inject
class Blockchain(GBLockchain):
def define_classes(self):
self.block_class = Block
self.operationids = operationids


@BlockchainInstance.inject
class Wallet(GWallet):
def define_classes(self):
Expand Down
8 changes: 7 additions & 1 deletion tests/test_operationids.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
import unittest
from .fixtures import ops, operations, getOperationNameForId
from .fixtures import ops, operations, getOperationNameForId, getOperationName


class Testcases(unittest.TestCase):
Expand All @@ -17,3 +17,9 @@ def test_getOperationNameForId(self):

with self.assertRaises(ValueError):
getOperationNameForId(20)

def test_operation_type_decode(self):
self.assertEqual(getOperationName(0), "demooepration")
self.assertEqual(getOperationName("account_create"), "account_create")
with self.assertRaises(AssertionError):
self.assertEqual(getOperationName("-not-exist-"), "account_create")

0 comments on commit 78ac7fb

Please sign in to comment.