|
| 1 | +# coding=utf-8 |
| 2 | +from __future__ import absolute_import, division, print_function, \ |
| 3 | + unicode_literals |
| 4 | + |
| 5 | +import filters as f |
| 6 | +from iota import ( |
| 7 | + Bundle, TransactionHash, Address, ProposedTransaction, BadApiResponse, |
| 8 | +) |
| 9 | +from iota.commands import FilterCommand, RequestFilter |
| 10 | +from iota.commands.core.check_consistency import CheckConsistencyCommand |
| 11 | +from iota.commands.extended.send_transfer import SendTransferCommand |
| 12 | +from iota.filters import Trytes |
| 13 | + |
| 14 | +__all__ = [ |
| 15 | + 'PromoteTransactionCommand', |
| 16 | +] |
| 17 | + |
| 18 | + |
| 19 | +class PromoteTransactionCommand(FilterCommand): |
| 20 | + """ |
| 21 | + Executes ``promoteTransaction`` extended API command. |
| 22 | +
|
| 23 | + See :py:meth:`iota.api.Iota.promote_transaction` for more information. |
| 24 | + """ |
| 25 | + command = 'promoteTransaction' |
| 26 | + |
| 27 | + def get_request_filter(self): |
| 28 | + return PromoteTransactionRequestFilter() |
| 29 | + |
| 30 | + def get_response_filter(self): |
| 31 | + pass |
| 32 | + |
| 33 | + def _execute(self, request): |
| 34 | + depth = request['depth'] # type: int |
| 35 | + min_weight_magnitude = request['minWeightMagnitude'] # type: int |
| 36 | + transaction = request['transaction'] # type: TransactionHash |
| 37 | + |
| 38 | + cc_response = CheckConsistencyCommand(self.adapter)(tails=[transaction]) |
| 39 | + if cc_response['state'] is False: |
| 40 | + raise BadApiResponse( |
| 41 | + 'Transaction {transaction} is not promotable. ' |
| 42 | + 'You should reattach first.'.format(transaction=transaction) |
| 43 | + ) |
| 44 | + |
| 45 | + spam_transfer = ProposedTransaction( |
| 46 | + address=Address(b''), |
| 47 | + value=0, |
| 48 | + ) |
| 49 | + |
| 50 | + return SendTransferCommand(self.adapter)( |
| 51 | + seed=spam_transfer.address, |
| 52 | + depth=depth, |
| 53 | + transfers=[spam_transfer], |
| 54 | + minWeightMagnitude=min_weight_magnitude, |
| 55 | + reference=transaction, |
| 56 | + ) |
| 57 | + |
| 58 | + |
| 59 | +class PromoteTransactionRequestFilter(RequestFilter): |
| 60 | + def __init__(self): |
| 61 | + super(PromoteTransactionRequestFilter, self).__init__({ |
| 62 | + 'depth': f.Required | f.Type(int) | f.Min(1), |
| 63 | + 'transaction': f.Required | Trytes(result_type=TransactionHash), |
| 64 | + |
| 65 | + # Loosely-validated; testnet nodes require a different value than |
| 66 | + # mainnet. |
| 67 | + 'minWeightMagnitude': f.Required | f.Type(int) | f.Min(1), |
| 68 | + }) |
0 commit comments