Moves tx_in_chain to Carrier

Also integrates it properly so it uses self.get_transaction() instead of bitcoin_cli straightaway. Error messages have also been merged / modified
This commit is contained in:
Sergi Delgado Segura
2019-11-20 13:01:27 +00:00
parent f91413ebd8
commit 05961f1632
2 changed files with 19 additions and 31 deletions

View File

@@ -80,10 +80,28 @@ class Carrier:
# reorged while we were querying bitcoind to get the confirmation count. In such a case we just
# restart the job
if e.error.get("code") == RPC_INVALID_ADDRESS_OR_KEY:
logger.info("Transaction got reorged before obtaining information", txid=txid)
logger.info("Transaction not found in mempool nor blockchain", txid=txid)
else:
# If something else happens (unlikely but possible) log it so we can treat it in future releases
logger.error("JSONRPCException.", method="Carrier.get_transaction", error=e.error)
return tx_info
def check_tx_in_chain(self, txid):
tx_in_chain = False
confirmations = None
tx_info = self.get_transaction(txid)
if tx_info is not None:
confirmations = int(tx_info.get("confirmations")) if tx_info.get("confirmations") is not None else None
if confirmations is not None:
tx_in_chain = True
logger.error("Transaction found in the blockchain", txid=txid)
else:
logger.error("Transaction found in mempool", txid=txid)
return tx_in_chain, confirmations

View File

@@ -2,8 +2,6 @@ import re
from http.client import HTTPException
import pisa.conf as conf
from pisa.logger import Logger
from pisa.rpc_errors import RPC_INVALID_ADDRESS_OR_KEY
from pisa.utils.auth_proxy import AuthServiceProxy, JSONRPCException
@@ -14,34 +12,6 @@ def bitcoin_cli():
)
# TODO: currently only used in the Responder; might move there or in the BlockProcessor
# NOTCOVERED
def check_tx_in_chain(tx_id, logger=Logger(), tx_label="Transaction"):
tx_in_chain = False
confirmations = 0
try:
tx_info = bitcoin_cli().getrawtransaction(tx_id, 1)
if tx_info.get("confirmations"):
confirmations = int(tx_info.get("confirmations"))
tx_in_chain = True
logger.error("{} found in the blockchain".format(tx_label), txid=tx_id)
else:
logger.error("{} found in mempool".format(tx_label), txid=tx_id)
except JSONRPCException as e:
if e.error.get("code") == RPC_INVALID_ADDRESS_OR_KEY:
logger.error("{} not found in mempool nor blockchain".format(tx_label), txid=tx_id)
else:
# ToDO: Unhandled errors, check this properly
logger.error("JSONRPCException.", method="tools.check_tx_in_chain", error=e.error)
return tx_in_chain, confirmations
# NOTCOVERED
def can_connect_to_bitcoind():
can_connect = True