|
18 | 18 | from collections import defaultdict |
19 | 19 | from functools import partial |
20 | 20 | from ipaddress import IPv4Address, IPv6Address, IPv4Network, IPv6Network |
21 | | -from typing import Iterable, Optional, TYPE_CHECKING, Sequence |
| 21 | +from typing import Iterable, Optional, TYPE_CHECKING, Sequence, Union |
22 | 22 |
|
23 | 23 | import attr |
24 | 24 | from aiorpcx import (Event, JSONRPCAutoDetect, JSONRPCConnection, |
@@ -1547,43 +1547,36 @@ async def package_broadcast(self, tx_package: Sequence[str], verbose: bool = Fal |
1547 | 1547 |
|
1548 | 1548 | raw_txs: a list of raw transactions as hexadecimal strings""" |
1549 | 1549 | self.bump_cost(0.25 + sum(len(tx) / 5000 for tx in tx_package)) |
1550 | | - try: |
1551 | | - txids = [double_sha256(bytes.fromhex(tx)).hex() for tx in tx_package] |
1552 | | - except ValueError: |
1553 | | - self.logger.info(f"error calculating txids", exc_info=True) |
1554 | | - raise RPCError( |
1555 | | - BAD_REQUEST, |
1556 | | - f'not a valid hex encoded transaction package: {tx_package}') |
1557 | 1550 | try: |
1558 | 1551 | daemon_result = await self.session_mgr.broadcast_package(tx_package) |
1559 | 1552 | except DaemonError as e: |
1560 | 1553 | error, = e.args |
1561 | 1554 | message = error['message'] |
1562 | 1555 | self.logger.info(f"error submitting package: {message}") |
1563 | | - raise RPCError(BAD_REQUEST, 'the tx package was rejected by ' |
1564 | | - f'network rules.\n\n{message}. Package txids: {txids}') |
1565 | | - else: |
1566 | | - self.txs_sent += len(tx_package) |
1567 | | - self.logger.info(f'broadcasted package: {txids}') |
1568 | | - if verbose: |
1569 | | - return daemon_result |
1570 | | - errors = [] |
1571 | | - for tx in daemon_result.get('tx-results', {}).values(): |
1572 | | - if tx.get('error'): |
1573 | | - error_msg = { |
1574 | | - 'txid': tx.get('txid'), |
1575 | | - 'error': tx['error'] |
1576 | | - } |
1577 | | - errors.append(error_msg) |
1578 | | - # check both, package_msg and package-msg due to ongoing discussion to change rpc |
1579 | | - # https://github.com/bitcoin/bitcoin/pull/31900 |
1580 | | - package_msg = daemon_result.get('package_msg', daemon_result.get('package-msg')) |
1581 | | - electrumx_result = { |
1582 | | - 'success': True if package_msg == 'success' else False |
1583 | | - } |
1584 | | - if errors: |
1585 | | - electrumx_result['errors'] = errors |
1586 | | - return electrumx_result |
| 1556 | + raise RPCError( |
| 1557 | + BAD_REQUEST, |
| 1558 | + f'the tx package was rejected by network rules.\n\n{message}.', |
| 1559 | + ) |
| 1560 | + |
| 1561 | + self.txs_sent += len(tx_package) |
| 1562 | + self.logger.info(f'broadcasted package: {len(tx_package)=}') |
| 1563 | + if verbose: |
| 1564 | + return daemon_result |
| 1565 | + |
| 1566 | + response: dict[str, Union[bool, list]] = { |
| 1567 | + 'success': daemon_result['package_msg'] == 'success', |
| 1568 | + } |
| 1569 | + errors = [] |
| 1570 | + for tx in daemon_result.get('tx-results', {}).values(): |
| 1571 | + if tx.get('error'): |
| 1572 | + error_msg = { |
| 1573 | + 'txid': tx.get('txid'), |
| 1574 | + 'error': tx['error'] |
| 1575 | + } |
| 1576 | + errors.append(error_msg) |
| 1577 | + if errors: |
| 1578 | + response['errors'] = errors |
| 1579 | + return response |
1587 | 1580 |
|
1588 | 1581 | async def transaction_get(self, tx_hash, verbose=False): |
1589 | 1582 | '''Return the serialized raw transaction given its hash |
|
0 commit comments