Adds a dict of sent receipts to act as a cache and avoid sending the same data more than once

The dict is supposed to be reset periodically so it does not grow unbounded
This commit is contained in:
Sergi Delgado Segura
2020-01-31 12:48:11 +01:00
parent b31e24f655
commit 0c4b8eaf4a

View File

@@ -38,8 +38,16 @@ class Carrier:
"""
The :class:`Carrier` is the class in charge of interacting with ``bitcoind`` to send/get transactions. It uses
:obj:`Receipt` objects to report about the sending outcome.
Attributes:
issued_receipts (:obj:`dict`): a dictionary of issued receipts to prevent resending the same transaction over
and over. It should periodically be reset to prevent it from growing unbounded.
"""
def __init__(self):
self.issued_receipts = {}
# NOTCOVERED
def send_transaction(self, rawtx, txid):
"""
@@ -53,6 +61,12 @@ class Carrier:
:obj:`Receipt`: A receipt reporting whether the transaction was successfully delivered or not and why.
"""
if txid in self.issued_receipts:
logger.info("Transaction already sent", txid=txid)
receipt = self.issued_receipts[txid]
return receipt
try:
logger.info("Pushing transaction to the network", txid=txid, rawtx=rawtx)
bitcoin_cli().sendrawtransaction(rawtx)
@@ -101,6 +115,8 @@ class Carrier:
logger.error("JSONRPCException", method="Carrier.send_transaction", error=e.error)
receipt = Receipt(delivered=False, reason=UNKNOWN_JSON_RPC_EXCEPTION)
self.issued_receipts[txid] = receipt
return receipt
@staticmethod