A P2PInterface which stores a count of how many times each txid has been announced.
| 765 | assert tx.hash not in raw_mempool, "{} tx found in mempool".format(tx.hash) |
| 766 | |
| 767 | class P2PTxInvStore(P2PInterface): |
| 768 | """A P2PInterface which stores a count of how many times each txid has been announced.""" |
| 769 | def __init__(self): |
| 770 | super().__init__() |
| 771 | self.tx_invs_received = defaultdict(int) |
| 772 | |
| 773 | def on_inv(self, message): |
| 774 | super().on_inv(message) # Send getdata in response. |
| 775 | # Store how many times invs have been received for each tx. |
| 776 | for i in message.inv: |
| 777 | if (i.type == MSG_TX) or (i.type == MSG_WTX): |
| 778 | # save txid |
| 779 | self.tx_invs_received[i.hash] += 1 |
| 780 | |
| 781 | def get_invs(self): |
| 782 | with p2p_lock: |
| 783 | return list(self.tx_invs_received.keys()) |
| 784 | |
| 785 | def wait_for_broadcast(self, txns, timeout=60): |
| 786 | """Waits for the txns (list of txids) to complete initial broadcast. |
| 787 | The mempool should mark unbroadcast=False for these transactions. |
| 788 | """ |
| 789 | # Wait until invs have been received (and getdatas sent) for each txid. |
| 790 | self.wait_until(lambda: set(self.tx_invs_received.keys()) == set([int(tx, 16) for tx in txns]), timeout=timeout) |
| 791 | # Flush messages and wait for the getdatas to be processed |
| 792 | self.sync_with_ping() |
no outgoing calls