3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-02-13 02:35:52 +00:00

Don't run event listener twice if a peer announces twice.

This commit is contained in:
Mike Hearn 2015-03-01 20:22:09 +01:00
parent 750f469bd3
commit 95dec1a910
3 changed files with 22 additions and 3 deletions

View File

@ -270,9 +270,10 @@ public class TransactionConfidence implements Serializable {
/**
* Called by a {@link Peer} when a transaction is pending and announced by a peer. The more peers announce the
* transaction, the more peers have validated it (assuming your internet connection is not being intercepted).
* If confidence is currently unknown, sets it to {@link ConfidenceType#PENDING}.
* If confidence is currently unknown, sets it to {@link ConfidenceType#PENDING}. Does not run listeners.
*
* @param address IP address of the peer, used as a proxy for identity.
* @return true if marked, false if this address was already seen (no-op)
*/
public synchronized boolean markBroadcastBy(PeerAddress address) {
if (!broadcastBy.addIfAbsent(address))

View File

@ -139,14 +139,16 @@ public class TxConfidenceTable {
*/
public TransactionConfidence seen(Sha256Hash hash, PeerAddress byPeer) {
TransactionConfidence confidence;
boolean fresh = false;
lock.lock();
{
cleanTable();
confidence = getOrCreate(hash);
confidence.markBroadcastBy(byPeer);
fresh = confidence.markBroadcastBy(byPeer);
}
lock.unlock();
confidence.queueListeners(TransactionConfidence.Listener.ChangeReason.SEEN_PEERS);
if (fresh)
confidence.queueListeners(TransactionConfidence.Listener.ChangeReason.SEEN_PEERS);
return confidence;
}

View File

@ -69,6 +69,22 @@ public class TxConfidenceTableTest {
assertEquals(2, seen[0]);
}
@Test
public void events() throws Exception {
final TransactionConfidence.Listener.ChangeReason[] run = new TransactionConfidence.Listener.ChangeReason[1];
tx1.getConfidence().addEventListener(new TransactionConfidence.Listener() {
@Override
public void onConfidenceChanged(TransactionConfidence confidence, ChangeReason reason) {
run[0] = reason;
}
}, Threading.SAME_THREAD);
table.seen(tx1.getHash(), address1);
assertEquals(TransactionConfidence.Listener.ChangeReason.SEEN_PEERS, run[0]);
run[0] = null;
table.seen(tx1.getHash(), address1);
assertNull(run[0]);
}
@Test
public void invAndDownload() throws Exception {
// Base case: we see a transaction announced twice and then download it. The count is in the confidence object.