|
1 | 1 | # coding=utf-8 |
2 | 2 | from __future__ import absolute_import, division, print_function, \ |
3 | | - unicode_literals |
| 3 | + unicode_literals |
4 | 4 |
|
5 | 5 | from operator import attrgetter |
6 | 6 | from typing import List, Optional |
7 | 7 |
|
8 | 8 | import filters as f |
| 9 | + |
9 | 10 | from iota import Address, TransactionHash |
10 | 11 | from iota.commands import FilterCommand, RequestFilter |
11 | 12 | from iota.commands.core.find_transactions import FindTransactionsCommand |
12 | 13 | from iota.commands.core.get_balances import GetBalancesCommand |
13 | 14 | from iota.commands.extended.utils import get_bundles_from_transaction_hashes, \ |
14 | | - iter_used_addresses |
| 15 | + iter_used_addresses |
15 | 16 | from iota.crypto.addresses import AddressGenerator |
16 | 17 | from iota.crypto.types import Seed |
17 | 18 | from iota.filters import Trytes |
18 | 19 |
|
19 | 20 | __all__ = [ |
20 | | - 'GetAccountDataCommand', |
| 21 | + 'GetAccountDataCommand', |
21 | 22 | ] |
22 | 23 |
|
23 | 24 |
|
24 | 25 | class GetAccountDataCommand(FilterCommand): |
25 | | - """ |
26 | | - Executes ``getAccountData`` extended API command. |
27 | | -
|
28 | | - See :py:meth:`iota.api.Iota.get_account_data` for more info. |
29 | | - """ |
30 | | - command = 'getAccountData' |
31 | | - |
32 | | - def get_request_filter(self): |
33 | | - return GetAccountDataRequestFilter() |
34 | | - |
35 | | - def get_response_filter(self): |
36 | | - pass |
37 | | - |
38 | | - def _execute(self, request): |
39 | | - inclusion_states = request['inclusionStates'] # type: bool |
40 | | - seed = request['seed'] # type: Seed |
41 | | - start = request['start'] # type: int |
42 | | - stop = request['stop'] # type: Optional[int] |
43 | | - |
44 | | - if stop is None: |
45 | | - my_addresses = [] # type: List[Address] |
46 | | - my_hashes = [] # type: List[TransactionHash] |
47 | | - |
48 | | - for addy, hashes in iter_used_addresses(self.adapter, seed, start): |
49 | | - my_addresses.append(addy) |
50 | | - my_hashes.extend(hashes) |
51 | | - else: |
52 | | - ft_command = FindTransactionsCommand(self.adapter) |
53 | | - |
54 | | - my_addresses = AddressGenerator(seed).get_addresses(start, stop - start) |
55 | | - my_hashes = ft_command(addresses=my_addresses).get('hashes') or [] |
56 | | - |
57 | | - account_balance = 0 |
58 | | - if my_hashes: |
59 | | - # Load balances for the addresses that we generated. |
60 | | - gb_response = GetBalancesCommand(self.adapter)(addresses=my_addresses) |
61 | | - |
62 | | - for i, balance in enumerate(gb_response['balances']): |
63 | | - my_addresses[i].balance = balance |
64 | | - account_balance += balance |
65 | | - |
66 | | - return { |
67 | | - 'addresses': list(sorted(my_addresses, key=attrgetter('key_index'))), |
68 | | - 'balance': account_balance, |
69 | | - |
70 | | - 'bundles': |
71 | | - get_bundles_from_transaction_hashes( |
72 | | - adapter = self.adapter, |
73 | | - transaction_hashes = my_hashes, |
74 | | - inclusion_states = inclusion_states, |
75 | | - ), |
76 | | - } |
| 26 | + """ |
| 27 | + Executes ``getAccountData`` extended API command. |
| 28 | +
|
| 29 | + See :py:meth:`iota.api.Iota.get_account_data` for more info. |
| 30 | + """ |
| 31 | + command = 'getAccountData' |
| 32 | + |
| 33 | + def get_request_filter(self): |
| 34 | + return GetAccountDataRequestFilter() |
| 35 | + |
| 36 | + def get_response_filter(self): |
| 37 | + pass |
| 38 | + |
| 39 | + def _execute(self, request): |
| 40 | + inclusion_states = request['inclusionStates'] # type: bool |
| 41 | + seed = request['seed'] # type: Seed |
| 42 | + start = request['start'] # type: int |
| 43 | + stop = request['stop'] # type: Optional[int] |
| 44 | + |
| 45 | + if stop is None: |
| 46 | + my_addresses = [] # type: List[Address] |
| 47 | + my_hashes = [] # type: List[TransactionHash] |
| 48 | + |
| 49 | + for addy, hashes in iter_used_addresses(self.adapter, seed, start): |
| 50 | + my_addresses.append(addy) |
| 51 | + my_hashes.extend(hashes) |
| 52 | + else: |
| 53 | + ft_command = FindTransactionsCommand(self.adapter) |
| 54 | + |
| 55 | + my_addresses = ( |
| 56 | + AddressGenerator(seed).get_addresses(start, stop - start) |
| 57 | + ) |
| 58 | + my_hashes = ft_command(addresses=my_addresses).get('hashes') or [] |
| 59 | + |
| 60 | + account_balance = 0 |
| 61 | + if my_hashes: |
| 62 | + # Load balances for the addresses that we generated. |
| 63 | + gb_response = ( |
| 64 | + GetBalancesCommand(self.adapter)(addresses=my_addresses) |
| 65 | + ) |
| 66 | + |
| 67 | + for i, balance in enumerate(gb_response['balances']): |
| 68 | + my_addresses[i].balance = balance |
| 69 | + account_balance += balance |
| 70 | + |
| 71 | + return { |
| 72 | + 'addresses': |
| 73 | + list(sorted(my_addresses, key=attrgetter('key_index'))), |
| 74 | + |
| 75 | + 'balance': account_balance, |
| 76 | + |
| 77 | + 'bundles': |
| 78 | + get_bundles_from_transaction_hashes( |
| 79 | + adapter=self.adapter, |
| 80 | + transaction_hashes=my_hashes, |
| 81 | + inclusion_states=inclusion_states, |
| 82 | + ), |
| 83 | + } |
77 | 84 |
|
78 | 85 |
|
79 | 86 | class GetAccountDataRequestFilter(RequestFilter): |
80 | | - MAX_INTERVAL = 500 |
81 | | - |
82 | | - CODE_INTERVAL_INVALID = 'interval_invalid' |
83 | | - CODE_INTERVAL_TOO_BIG = 'interval_too_big' |
84 | | - |
85 | | - templates = { |
86 | | - CODE_INTERVAL_INVALID: '``start`` must be <= ``stop``', |
87 | | - CODE_INTERVAL_TOO_BIG: '``stop`` - ``start`` must be <= {max_interval}', |
88 | | - } |
89 | | - |
90 | | - def __init__(self): |
91 | | - super(GetAccountDataRequestFilter, self).__init__( |
92 | | - { |
93 | | - # Required parameters. |
94 | | - 'seed': f.Required | Trytes(result_type=Seed), |
95 | | - |
96 | | - # Optional parameters. |
97 | | - 'stop': f.Type(int) | f.Min(0), |
98 | | - 'start': f.Type(int) | f.Min(0) | f.Optional(0), |
99 | | - |
100 | | - 'inclusionStates': f.Type(bool) | f.Optional(False), |
101 | | - }, |
102 | | - |
103 | | - allow_missing_keys = { |
104 | | - 'stop', |
105 | | - 'inclusionStates', |
106 | | - 'start', |
107 | | - }, |
108 | | - ) |
109 | | - |
110 | | - def _apply(self, value): |
111 | | - # noinspection PyProtectedMember |
112 | | - filtered = super(GetAccountDataRequestFilter, self)._apply(value) |
113 | | - |
114 | | - if self._has_errors: |
115 | | - return filtered |
116 | | - |
117 | | - if filtered['stop'] is not None: |
118 | | - if filtered['start'] > filtered['stop']: |
119 | | - filtered['start'] = self._invalid_value( |
120 | | - value = filtered['start'], |
121 | | - reason = self.CODE_INTERVAL_INVALID, |
122 | | - sub_key = 'start', |
123 | | - |
124 | | - context = { |
125 | | - 'start': filtered['start'], |
126 | | - 'stop': filtered['stop'], |
127 | | - }, |
128 | | - ) |
129 | | - elif (filtered['stop'] - filtered['start']) > self.MAX_INTERVAL: |
130 | | - filtered['stop'] = self._invalid_value( |
131 | | - value = filtered['stop'], |
132 | | - reason = self.CODE_INTERVAL_TOO_BIG, |
133 | | - sub_key = 'stop', |
134 | | - |
135 | | - context = { |
136 | | - 'start': filtered['start'], |
137 | | - 'stop': filtered['stop'], |
138 | | - }, |
139 | | - |
140 | | - template_vars = { |
141 | | - 'max_interval': self.MAX_INTERVAL, |
142 | | - }, |
| 87 | + MAX_INTERVAL = 500 |
| 88 | + |
| 89 | + CODE_INTERVAL_INVALID = 'interval_invalid' |
| 90 | + CODE_INTERVAL_TOO_BIG = 'interval_too_big' |
| 91 | + |
| 92 | + templates = { |
| 93 | + CODE_INTERVAL_INVALID: '``start`` must be <= ``stop``', |
| 94 | + CODE_INTERVAL_TOO_BIG: '``stop`` - ``start`` must be <= {max_interval}', |
| 95 | + } |
| 96 | + |
| 97 | + def __init__(self): |
| 98 | + super(GetAccountDataRequestFilter, self).__init__( |
| 99 | + { |
| 100 | + # Required parameters. |
| 101 | + 'seed': f.Required | Trytes(Seed), |
| 102 | + |
| 103 | + # Optional parameters. |
| 104 | + 'stop': f.Type(int) | f.Min(0), |
| 105 | + 'start': f.Type(int) | f.Min(0) | f.Optional(0), |
| 106 | + |
| 107 | + 'inclusionStates': f.Type(bool) | f.Optional(False), |
| 108 | + }, |
| 109 | + |
| 110 | + allow_missing_keys={ |
| 111 | + 'stop', |
| 112 | + 'inclusionStates', |
| 113 | + 'start', |
| 114 | + }, |
143 | 115 | ) |
144 | 116 |
|
145 | | - return filtered |
| 117 | + def _apply(self, value): |
| 118 | + # noinspection PyProtectedMember |
| 119 | + filtered = super(GetAccountDataRequestFilter, self)._apply(value) |
| 120 | + |
| 121 | + if self._has_errors: |
| 122 | + return filtered |
| 123 | + |
| 124 | + if filtered['stop'] is not None: |
| 125 | + if filtered['start'] > filtered['stop']: |
| 126 | + filtered['start'] = self._invalid_value( |
| 127 | + value=filtered['start'], |
| 128 | + reason=self.CODE_INTERVAL_INVALID, |
| 129 | + sub_key='start', |
| 130 | + |
| 131 | + context={ |
| 132 | + 'start': filtered['start'], |
| 133 | + 'stop': filtered['stop'], |
| 134 | + }, |
| 135 | + ) |
| 136 | + |
| 137 | + elif (filtered['stop'] - filtered['start']) > self.MAX_INTERVAL: |
| 138 | + filtered['stop'] = self._invalid_value( |
| 139 | + value=filtered['stop'], |
| 140 | + reason=self.CODE_INTERVAL_TOO_BIG, |
| 141 | + sub_key='stop', |
| 142 | + |
| 143 | + context={ |
| 144 | + 'start': filtered['start'], |
| 145 | + 'stop': filtered['stop'], |
| 146 | + }, |
| 147 | + |
| 148 | + template_vars={ |
| 149 | + 'max_interval': self.MAX_INTERVAL, |
| 150 | + }, |
| 151 | + ) |
| 152 | + |
| 153 | + return filtered |
0 commit comments