def broadcast_tx(tx_hex, config_path=CONFIG_PATH, tx_broadcaster=None): """ Send a signed transaction to the blockchain Return {'status': True, 'transaction_hash': ...} on success. Include 'tx': ... if BLOCKSTACK_DRY_RUN is set. Return {'error': ...} on failure. """ from ..config import get_tx_broadcaster from ..utxo import broadcast_transaction if tx_broadcaster is None: tx_broadcaster = get_tx_broadcaster(config_path=config_path) log.debug('Send {}-byte tx {}'.format(len(tx_hex) / 2, tx_hex)) resp = {} try: if BLOCKSTACK_DRY_RUN: # TODO: expand to other blockchains... resp = { 'tx': tx_hex, 'transaction_hash': virtualchain.btc_tx_get_hash(tx_hex), 'status': True } return resp else: resp = broadcast_transaction(tx_hex, tx_broadcaster) if 'tx_hash' not in resp or 'error' in resp: log.error('Failed to send {}'.format(tx_hex)) resp['error'] = 'Failed to broadcast transaction: {}'.format( tx_hex) return resp except Exception as e: log.exception(e) resp['error'] = 'Failed to broadcast transaction: {}'.format(tx_hex) if BLOCKSTACK_TEST is not None: # should NEVER happen in test mode msg = 'FATAL: failed to send transaction:\n{}' log.error(msg.format(json.dumps(resp, indent=4, sort_keys=True))) os.abort() # for compatibility resp['status'] = True resp['transaction_hash'] = resp.pop('tx_hash') return resp
def broadcast_transaction(hex_tx, blockchain_client=BlockchainInfoClient()): """ Dispatch a raw transaction to the network. """ url = BLOCKCHAIN_API_BASE_URL + '/pushtx' payload = {'tx': hex_tx} r = requests.post(url, data=payload, auth=blockchain_client.auth, timeout=blockchain_client.timeout) if 'submitted' in r.text.lower(): return { 'success': True, 'tx_hash': virtualchain.btc_tx_get_hash(hex_tx) } else: raise Exception('Invalid response from blockchain.info.')
def broadcast_tx(tx_hex, config_path=CONFIG_PATH, tx_broadcaster=None): """ Send a signed transaction to the blockchain Return {'status': True, 'transaction_hash': ...} on success. Include 'tx': ... if BLOCKSTACK_DRY_RUN is set. Return {'error': ...} on failure. """ from ..config import get_tx_broadcaster from ..utxo import broadcast_transaction if tx_broadcaster is None: tx_broadcaster = get_tx_broadcaster(config_path=config_path) log.debug('Send {}-byte tx {}'.format(len(tx_hex)/2, tx_hex)) resp = {} try: if BLOCKSTACK_DRY_RUN: # TODO: expand to other blockchains... resp = { 'tx': tx_hex, 'transaction_hash': virtualchain.btc_tx_get_hash(tx_hex), 'status': True } return resp else: resp = broadcast_transaction(tx_hex, tx_broadcaster) if 'tx_hash' not in resp or 'error' in resp: log.error('Failed to send {}'.format(tx_hex)) resp['error'] = 'Failed to broadcast transaction: {}'.format(tx_hex) return resp except Exception as e: log.exception(e) resp['error'] = 'Failed to broadcast transaction: {}'.format(tx_hex) return resp # for compatibility resp['status'] = True resp['transaction_hash'] = resp.pop('tx_hash') return resp