From ee5f881c514361f25f803103156abed18d1ad1f1 Mon Sep 17 00:00:00 2001 From: Mike Hearn Date: Mon, 7 Jan 2013 11:13:33 +0100 Subject: [PATCH] Run TransactionConfidence listeners with the confidence object unlocked when markBroadcastBy is called. Resolves issue 270. --- .../bitcoin/core/TransactionConfidence.java | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/core/src/main/java/com/google/bitcoin/core/TransactionConfidence.java b/core/src/main/java/com/google/bitcoin/core/TransactionConfidence.java index ce52f6d4..146b02f1 100644 --- a/core/src/main/java/com/google/bitcoin/core/TransactionConfidence.java +++ b/core/src/main/java/com/google/bitcoin/core/TransactionConfidence.java @@ -79,15 +79,14 @@ public class TransactionConfidence implements Serializable { */ private BigInteger workDone = BigInteger.ZERO; - // TODO: The advice below is a mess. There should be block chain listeners, see issue 94. /** *

Adds an event listener that will be run when this confidence object is updated. The listener will be locked and * is likely to be invoked on a peer thread.

* *

Note that this is NOT called when every block arrives. Instead it is called when the transaction * transitions between confidence states, ie, from not being seen in the chain to being seen (not necessarily in - * the best chain). If you want to know when the transaction gets buried under another block, listen for new block - * events using {@link PeerEventListener#onBlocksDownloaded(Peer, Block, int)} and then use the getters on the + * the best chain). If you want to know when the transaction gets buried under another block, implement a + * {@link BlockChainListener}, attach it to a {@link BlockChain} and then use the getters on the * confidence object to determine the new depth.

*/ public synchronized void addEventListener(Listener listener) { @@ -234,14 +233,14 @@ public class TransactionConfidence implements Serializable { * * @param address IP address of the peer, used as a proxy for identity. */ - public synchronized void markBroadcastBy(PeerAddress address) { - broadcastBy.add(address); - if (getConfidenceType() == ConfidenceType.UNKNOWN) { - setConfidenceType(ConfidenceType.NOT_SEEN_IN_CHAIN); - // Listeners are already run by setConfidenceType. - } else { - runListeners(); + public void markBroadcastBy(PeerAddress address) { + synchronized (this) { + broadcastBy.add(address); + if (getConfidenceType() == ConfidenceType.UNKNOWN) { + this.confidenceType = ConfidenceType.NOT_SEEN_IN_CHAIN; + } } + runListeners(); } /**