From bc3a5cd845485d7e41055be676e1a5b6cba520b7 Mon Sep 17 00:00:00 2001
From: Devrandom
Date: Sun, 30 Nov 2014 14:05:58 -0800
Subject: [PATCH] introduce a Context object vended by AbstractBlockChain
have it hold the confidencePool
---
.../org/bitcoinj/core/AbstractBlockChain.java | 6 +++++
.../main/java/org/bitcoinj/core/Context.java | 23 +++++++++++++++++++
.../org/bitcoinj/core/NetworkParameters.java | 6 -----
.../src/main/java/org/bitcoinj/core/Peer.java | 10 ++++----
.../java/org/bitcoinj/core/PeerGroup.java | 9 ++++----
.../test/java/org/bitcoinj/core/PeerTest.java | 6 ++---
6 files changed, 42 insertions(+), 18 deletions(-)
create mode 100644 core/src/main/java/org/bitcoinj/core/Context.java
diff --git a/core/src/main/java/org/bitcoinj/core/AbstractBlockChain.java b/core/src/main/java/org/bitcoinj/core/AbstractBlockChain.java
index 7b28eff41..6fc6687a4 100644
--- a/core/src/main/java/org/bitcoinj/core/AbstractBlockChain.java
+++ b/core/src/main/java/org/bitcoinj/core/AbstractBlockChain.java
@@ -86,6 +86,7 @@ public abstract class AbstractBlockChain {
/** Keeps a map of block hashes to StoredBlocks. */
private final BlockStore blockStore;
+ private final Context context;
/**
* Tracks the top of the best known chain.
@@ -149,6 +150,11 @@ public abstract class AbstractBlockChain {
this.params = params;
this.listeners = new CopyOnWriteArrayList>();
for (BlockChainListener l : listeners) addListener(l, Threading.SAME_THREAD);
+ context = new Context();
+ }
+
+ public Context getContext() {
+ return context;
}
/**
diff --git a/core/src/main/java/org/bitcoinj/core/Context.java b/core/src/main/java/org/bitcoinj/core/Context.java
new file mode 100644
index 000000000..98842e399
--- /dev/null
+++ b/core/src/main/java/org/bitcoinj/core/Context.java
@@ -0,0 +1,23 @@
+package org.bitcoinj.core;
+
+/**
+ * The Context object holds various objects that are relevant to the global state of our
+ * view of the Bitcoin network.
+ */
+public class Context {
+ protected TxConfidencePool confidencePool;
+
+ protected Context() {
+ confidencePool = new TxConfidencePool();
+ }
+
+ /**
+ * Returns the {@link TxConfidencePool} created by this context. The pool tracks advertised
+ * and downloaded transactions so their confidence can be measured as a proportion of how many peers announced it.
+ * With an un-tampered with internet connection, the more peers announce a transaction the more confidence you can
+ * have that it's really valid.
+ */
+ public TxConfidencePool getConfidencePool() {
+ return confidencePool;
+ }
+}
diff --git a/core/src/main/java/org/bitcoinj/core/NetworkParameters.java b/core/src/main/java/org/bitcoinj/core/NetworkParameters.java
index 535e98fd8..a1486d0ea 100644
--- a/core/src/main/java/org/bitcoinj/core/NetworkParameters.java
+++ b/core/src/main/java/org/bitcoinj/core/NetworkParameters.java
@@ -77,7 +77,6 @@ public abstract class NetworkParameters implements Serializable {
protected byte[] alertSigningKey;
protected int bip32HeaderPub;
protected int bip32HeaderPriv;
- transient protected TxConfidencePool confidencePool;
/**
* See getId(). This may be null for old deserialized wallets. In that case we derive it heuristically
@@ -98,7 +97,6 @@ public abstract class NetworkParameters implements Serializable {
protected NetworkParameters() {
alertSigningKey = SATOSHI_KEY;
genesisBlock = createGenesis(this);
- confidencePool = new TxConfidencePool();
}
private static Block createGenesis(NetworkParameters n) {
@@ -358,8 +356,4 @@ public abstract class NetworkParameters implements Serializable {
public int getBip32HeaderPriv() {
return bip32HeaderPriv;
}
-
- public TxConfidencePool getConfidencePool() {
- return confidencePool;
- }
}
diff --git a/core/src/main/java/org/bitcoinj/core/Peer.java b/core/src/main/java/org/bitcoinj/core/Peer.java
index 7329e7632..04dab7af7 100644
--- a/core/src/main/java/org/bitcoinj/core/Peer.java
+++ b/core/src/main/java/org/bitcoinj/core/Peer.java
@@ -161,7 +161,7 @@ public class Peer extends PeerSocketHandler {
* used to keep track of which peers relayed transactions and offer more descriptive logging.
*/
public Peer(NetworkParameters params, VersionMessage ver, @Nullable AbstractBlockChain chain, PeerAddress remoteAddress) {
- this(params, ver, remoteAddress, chain, null);
+ this(params, ver, remoteAddress, chain);
}
/**
@@ -179,8 +179,8 @@ public class Peer extends PeerSocketHandler {
* used to keep track of which peers relayed transactions and offer more descriptive logging.
*/
public Peer(NetworkParameters params, VersionMessage ver, PeerAddress remoteAddress,
- @Nullable AbstractBlockChain chain, @Nullable TxConfidencePool mempool) {
- this(params, ver, remoteAddress, chain, mempool, true);
+ @Nullable AbstractBlockChain chain) {
+ this(params, ver, remoteAddress, chain, true);
}
/**
@@ -198,7 +198,7 @@ public class Peer extends PeerSocketHandler {
* used to keep track of which peers relayed transactions and offer more descriptive logging.
*/
public Peer(NetworkParameters params, VersionMessage ver, PeerAddress remoteAddress,
- @Nullable AbstractBlockChain chain, @Nullable TxConfidencePool mempool, boolean downloadTxDependencies) {
+ @Nullable AbstractBlockChain chain, boolean downloadTxDependencies) {
super(params, remoteAddress);
this.params = Preconditions.checkNotNull(params);
this.versionMessage = Preconditions.checkNotNull(ver);
@@ -211,7 +211,7 @@ public class Peer extends PeerSocketHandler {
this.isAcked = false;
this.pendingPings = new CopyOnWriteArrayList();
this.wallets = new CopyOnWriteArrayList();
- this.confidencePool = mempool;
+ this.confidencePool = chain.getContext().getConfidencePool();
}
/**
diff --git a/core/src/main/java/org/bitcoinj/core/PeerGroup.java b/core/src/main/java/org/bitcoinj/core/PeerGroup.java
index 345d5c3fb..72ce6d71d 100644
--- a/core/src/main/java/org/bitcoinj/core/PeerGroup.java
+++ b/core/src/main/java/org/bitcoinj/core/PeerGroup.java
@@ -335,7 +335,7 @@ public class PeerGroup implements TransactionBroadcaster {
downloadTxDependencies = true;
- confidencePool = params.getConfidencePool();
+ confidencePool = chain.getContext().getConfidencePool();
inactives = new PriorityQueue(1, new Comparator() {
@SuppressWarnings("FieldAccessNotGuarded") // only called when inactives is accessed, and lock is held then.
@@ -515,7 +515,7 @@ public class PeerGroup implements TransactionBroadcaster {
Iterator it = items.iterator();
while (it.hasNext()) {
InventoryItem item = it.next();
- // Check the mempool first.
+ // Check the confidence pool first.
Transaction tx = confidencePool.get(item.hash);
if (tx != null) {
transactions.add(tx);
@@ -1137,7 +1137,7 @@ public class PeerGroup implements TransactionBroadcaster {
ver.bestHeight = chain == null ? 0 : chain.getBestChainHeight();
ver.time = Utils.currentTimeSeconds();
- Peer peer = new Peer(params, ver, address, chain, confidencePool, downloadTxDependencies);
+ Peer peer = new Peer(params, ver, address, chain, downloadTxDependencies);
peer.addEventListener(startupListener, Threading.SAME_THREAD);
peer.setMinProtocolVersion(vMinRequiredProtocolVersion);
pendingPeers.add(peer);
@@ -1321,11 +1321,12 @@ public class PeerGroup implements TransactionBroadcaster {
}
/**
- * Returns the {@link TxConfidencePool} created by this peer group to synchronize its peers. The pool tracks advertised
+ * Returns the {@link TxConfidencePool} used by this peer group to synchronize its peers. The pool tracks advertised
* and downloaded transactions so their confidence can be measured as a proportion of how many peers announced it.
* With an un-tampered with internet connection, the more peers announce a transaction the more confidence you can
* have that it's really valid.
*/
+ @Deprecated
public TxConfidencePool getConfidencePool() {
return confidencePool;
}
diff --git a/core/src/test/java/org/bitcoinj/core/PeerTest.java b/core/src/test/java/org/bitcoinj/core/PeerTest.java
index 0d579548a..9980c1ec9 100644
--- a/core/src/test/java/org/bitcoinj/core/PeerTest.java
+++ b/core/src/test/java/org/bitcoinj/core/PeerTest.java
@@ -77,10 +77,10 @@ public class PeerTest extends TestWithNetworkConnections {
public void setUp() throws Exception {
super.setUp();
- confidencePool = new TxConfidencePool();
+ confidencePool = blockChain.getContext().getConfidencePool();
VersionMessage ver = new VersionMessage(unitTestParams, 100);
InetSocketAddress address = new InetSocketAddress("127.0.0.1", 4000);
- peer = new Peer(unitTestParams, ver, new PeerAddress(address), blockChain, confidencePool);
+ peer = new Peer(unitTestParams, ver, new PeerAddress(address), blockChain);
peer.addWallet(wallet);
}
@@ -269,7 +269,7 @@ public class PeerTest extends TestWithNetworkConnections {
// Check co-ordination of which peer to download via the memory pool.
VersionMessage ver = new VersionMessage(unitTestParams, 100);
InetSocketAddress address = new InetSocketAddress("127.0.0.1", 4242);
- Peer peer2 = new Peer(unitTestParams, ver, new PeerAddress(address), blockChain, confidencePool);
+ Peer peer2 = new Peer(unitTestParams, ver, new PeerAddress(address), blockChain);
peer2.addWallet(wallet);
VersionMessage peerVersion = new VersionMessage(unitTestParams, OTHER_PEER_CHAIN_HEIGHT);
peerVersion.clientVersion = 70001;