This repository has been archived by the owner on Jan 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #234 from pdecol/99-find-transaction-objects
Implement find transaction objects command
- Loading branch information
Showing
8 changed files
with
261 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# coding=utf-8 | ||
from __future__ import absolute_import, division, print_function, \ | ||
unicode_literals | ||
|
||
from typing import Iterable, List, Optional | ||
|
||
from iota import Address, BundleHash, Tag, Transaction, TransactionHash | ||
from iota.commands.core import GetTrytesCommand, FindTransactionsCommand | ||
|
||
__all__ = [ | ||
'FindTransactionObjectsCommand', | ||
] | ||
|
||
|
||
class FindTransactionObjectsCommand(FindTransactionsCommand): | ||
""" | ||
Executes `FindTransactionObjects` command. | ||
See :py:meth:`iota.api.StrictIota.find_transaction_objects`. | ||
""" | ||
command = 'findTransactionObjects' | ||
|
||
def get_response_filter(self): | ||
pass | ||
|
||
def _execute(self, request): | ||
bundles = request\ | ||
.get('bundles') # type: Optional[Iterable[BundleHash]] | ||
addresses = request\ | ||
.get('addresses') # type: Optional[Iterable[Address]] | ||
tags = request\ | ||
.get('tags') # type: Optional[Iterable[Tag]] | ||
approvees = request\ | ||
.get('approvees') # type: Optional[Iterable[TransactionHash]] | ||
|
||
ft_response = FindTransactionsCommand(adapter=self.adapter)( | ||
bundles=bundles, | ||
addresses=addresses, | ||
tags=tags, | ||
approvees=approvees, | ||
) | ||
|
||
hashes = ft_response['hashes'] | ||
transactions = [] | ||
if hashes: | ||
gt_response = GetTrytesCommand(adapter=self.adapter)(hashes=hashes) | ||
|
||
transactions = list(map( | ||
Transaction.from_tryte_string, | ||
gt_response.get('trytes') or [], | ||
)) # type: List[Transaction] | ||
|
||
return { | ||
'transactions': transactions, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
# coding=utf-8 | ||
from __future__ import absolute_import, division, print_function, \ | ||
unicode_literals | ||
|
||
from unittest import TestCase | ||
|
||
import mock | ||
|
||
from iota import Iota, MockAdapter, Transaction | ||
from iota.commands.extended import FindTransactionObjectsCommand | ||
|
||
|
||
class FindTransactionObjectsCommandTestCase(TestCase): | ||
# noinspection SpellCheckingInspection | ||
def setUp(self): | ||
super(FindTransactionObjectsCommandTestCase, self).setUp() | ||
|
||
self.adapter = MockAdapter() | ||
self.command = FindTransactionObjectsCommand(self.adapter) | ||
|
||
# Define values that we can reuse across tests. | ||
self.address = 'A' * 81 | ||
self.transaction_hash = \ | ||
b'BROTOVRCAEMFLRWGPVWDPDTBRAMLHVCHQDEHXLCWH' \ | ||
b'KKXLVDFCPIJEUZTPPFMPQQ9KOHAEUAMMVJN99999' | ||
self.trytes = \ | ||
b'99999999999999999999999999999999999999999999999999999999999999' \ | ||
b'99999999999999999999999999999999999999999999999999999999999999' \ | ||
b'99999999999999999999999999999999999999999999999999999999999999' \ | ||
b'99999999999999999999999999999999999999999999999999999999999999' \ | ||
b'99999999999999999999999999999999999999999999999999999999999999' \ | ||
b'99999999999999999999999999999999999999999999999999999999999999' \ | ||
b'99999999999999999999999999999999999999999999999999999999999999' \ | ||
b'99999999999999999999999999999999999999999999999999999999999999' \ | ||
b'99999999999999999999999999999999999999999999999999999999999999' \ | ||
b'99999999999999999999999999999999999999999999999999999999999999' \ | ||
b'99999999999999999999999999999999999999999999999999999999999999' \ | ||
b'99999999999999999999999999999999999999999999999999999999999999' \ | ||
b'99999999999999999999999999999999999999999999999999999999999999' \ | ||
b'99999999999999999999999999999999999999999999999999999999999999' \ | ||
b'99999999999999999999999999999999999999999999999999999999999999' \ | ||
b'99999999999999999999999999999999999999999999999999999999999999' \ | ||
b'99999999999999999999999999999999999999999999999999999999999999' \ | ||
b'99999999999999999999999999999999999999999999999999999999999999' \ | ||
b'99999999999999999999999999999999999999999999999999999999999999' \ | ||
b'99999999999999999999999999999999999999999999999999999999999999' \ | ||
b'99999999999999999999999999999999999999999999999999999999999999' \ | ||
b'99999999999999999999999999999999999999999999999999999999999999' \ | ||
b'99999999999999999999999999999999999999999999999999999999999999' \ | ||
b'99999999999999999999999999999999999999999999999999999999999999' \ | ||
b'99999999999999999999999999999999999999999999999999999999999999' \ | ||
b'99999999999999999999999999999999999999999999999999999999999999' \ | ||
b'99999999999999999999999999999999999999999999999999999999999999' \ | ||
b'99999999999999999999999999999999999999999999999999999999999999' \ | ||
b'99999999999999999999999999999999999999999999999999999999999999' \ | ||
b'99999999999999999999999999999999999999999999999999999999999999' \ | ||
b'99999999999999999999999999999999999999999999999999999999999999' \ | ||
b'99999999999999999999999999999999999999999999999999999999999999' \ | ||
b'99999999999999999999999999999999999999999999999999999999999999' \ | ||
b'99999999999999999999999999999999999999999999999999999999999999' \ | ||
b'99999999999999999999999999999999999999999999999999999999999999' \ | ||
b'99999999999999999AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' \ | ||
b'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA99999999999999999999999999' \ | ||
b'9QC9999999999999999999999999PQYJHAD99999999999999999999WHIUDFV' \ | ||
b'IFXNBJVEHYPLDADIDINGAWMHYIJNPYUDWXCAWL9GSKTUIZLJGGFIXEIYTJEDQZ' \ | ||
b'TIYRXHC9PBWBDSOTEJTQTYYSZLVTFLDQMZSGLHKLYVJOLMXIJJRTGS9RYBXLAT' \ | ||
b'ZJXBVBCPUGWRUKZJYLBGPKRKWIA9999FPYHMFFWMMKOHTSAPMMATZQLWXJSPMT' \ | ||
b'JSRQIPMDCQXFFMXMHCYDKVJCFSRECAVALCOFIYCJLNRZZZ9999999999999999' \ | ||
b'999999999999999KITCXNZOF999999999MMMMMMMMMEA9999F9999999999999' \ | ||
b'9999999' | ||
|
||
def test_wireup(self): | ||
""" | ||
Verify that the command is wired up correctly. | ||
""" | ||
self.assertIsInstance( | ||
Iota(self.adapter).findTransactionObjects, | ||
FindTransactionObjectsCommand, | ||
) | ||
|
||
def test_transaction_found(self): | ||
""" | ||
A transaction is found with the inputs. A transaction object is | ||
returned | ||
""" | ||
with mock.patch( | ||
'iota.commands.core.find_transactions.FindTransactionsCommand.' | ||
'_execute', | ||
mock.Mock(return_value={'hashes': [self.transaction_hash, ]}), | ||
): | ||
with mock.patch( | ||
'iota.commands.core.get_trytes.GetTrytesCommand._execute', | ||
mock.Mock(return_value={'trytes': [self.trytes, ]}), | ||
): | ||
response = self.command(addresses=[self.address]) | ||
|
||
self.assertEqual(len(response['transactions']), 1) | ||
transaction = response['transactions'][0] | ||
self.assertIsInstance(transaction, Transaction) | ||
self.assertEqual(transaction.address, self.address) | ||
|
||
def test_no_transactions_fround(self): | ||
""" | ||
No transaction is found with the inputs. An empty list is returned | ||
""" | ||
with mock.patch( | ||
'iota.commands.core.find_transactions.FindTransactionsCommand.' | ||
'_execute', | ||
mock.Mock(return_value={'hashes': []}), | ||
): | ||
response = self.command(addresses=[self.address]) | ||
|
||
self.assertDictEqual( | ||
response, | ||
{ | ||
'transactions': [], | ||
}, | ||
) |