Skip to content
This repository has been archived by the owner on Jan 13, 2023. It is now read-only.

Commit

Permalink
Merge pull request #124 from scottbelden/is_promotable
Browse files Browse the repository at this point in the history
add is_promotable api
  • Loading branch information
todofixthis committed Jan 6, 2018
2 parents 56cf926 + d15e2be commit 3a09bb9
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
2 changes: 2 additions & 0 deletions iota/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from iota.adapter import BaseAdapter, resolve_adapter
from iota.commands import BaseCommand, CustomCommand, core, \
discover_commands, extended
from iota.commands.extended.helpers import Helpers
from iota.crypto.addresses import AddressGenerator
from iota.crypto.types import Seed
from six import with_metaclass
Expand Down Expand Up @@ -440,6 +441,7 @@ def __init__(self, adapter, seed=None, testnet=False):
super(Iota, self).__init__(adapter, testnet)

self.seed = Seed(seed) if seed else Seed.random()
self.helpers = Helpers(self)

def broadcast_and_store(self, trytes):
# type: (Iterable[TransactionTrytes]) -> dict
Expand Down
18 changes: 18 additions & 0 deletions iota/commands/extended/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Helpers(object):
"""
Adds additional helper functions that aren't part of the core or extended
API.
"""

def __init__(self, api):
self.api = api

def is_promotable(self, tail):
# type: (TransactionHash) -> bool
"""
Determines if a tail transaction is promotable.
:param tail:
Transaction hash. Must be a tail transaction.
"""
return self.api.check_consistency(tails=[tail])['state']
45 changes: 45 additions & 0 deletions test/commands/extended/helpers_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# coding=utf-8
from __future__ import absolute_import, division, print_function, \
unicode_literals

from unittest import TestCase

from iota import Iota, TransactionHash
from iota.adapter import MockAdapter


class HelpersTestCase(TestCase):
def setUp(self):
super(HelpersTestCase, self).setUp()

self.api = api = Iota('mock://')
self.api.adapter = MockAdapter()

# noinspection SpellCheckingInspection
self.transaction = (
'TESTVALUE9DONTUSEINPRODUCTION99999KPZOTR'
'VDB9GZDJGZSSDCBIX9QOK9PAV9RMDBGDXLDTIZTWQ'
)

def test_positive_is_promotable(self):
"""
Transaction is promotable
"""

self.api.adapter.seed_response('checkConsistency', {
'state': True,
})

self.assertTrue(self.api.helpers.is_promotable(tail=self.transaction))

def test_negative_is_promotable(self):
"""
Transaction is not promotable
"""

self.api.adapter.seed_response('checkConsistency', {
'state': False,
'info': 'Inconsistent state',
})

self.assertFalse(self.api.helpers.is_promotable(tail=self.transaction))

0 comments on commit 3a09bb9

Please sign in to comment.