introduce a Context object vended by AbstractBlockChain

have it hold the confidencePool
This commit is contained in:
Devrandom 2014-11-30 14:05:58 -08:00 committed by Mike Hearn
parent beb6f0873f
commit bc3a5cd845
6 changed files with 42 additions and 18 deletions

View File

@ -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.<p>
@ -149,6 +150,11 @@ public abstract class AbstractBlockChain {
this.params = params;
this.listeners = new CopyOnWriteArrayList<ListenerRegistration<BlockChainListener>>();
for (BlockChainListener l : listeners) addListener(l, Threading.SAME_THREAD);
context = new Context();
}
public Context getContext() {
return context;
}
/**

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -161,7 +161,7 @@ public class Peer extends PeerSocketHandler {
* used to keep track of which peers relayed transactions and offer more descriptive logging.</p>
*/
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.</p>
*/
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.</p>
*/
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<PendingPing>();
this.wallets = new CopyOnWriteArrayList<Wallet>();
this.confidencePool = mempool;
this.confidencePool = chain.getContext().getConfidencePool();
}
/**

View File

@ -335,7 +335,7 @@ public class PeerGroup implements TransactionBroadcaster {
downloadTxDependencies = true;
confidencePool = params.getConfidencePool();
confidencePool = chain.getContext().getConfidencePool();
inactives = new PriorityQueue<PeerAddress>(1, new Comparator<PeerAddress>() {
@SuppressWarnings("FieldAccessNotGuarded") // only called when inactives is accessed, and lock is held then.
@ -515,7 +515,7 @@ public class PeerGroup implements TransactionBroadcaster {
Iterator<InventoryItem> 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;
}

View File

@ -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;