PeerGroup: migrate constructors argument from NetworkParameters to Network

* Add 3 new constructors which take Network
* Deprecate (2 of 3) constructors that take NetworkParameters
* Mark 3-arg NetworkParameters constructor as @VisibleForTesting
This commit is contained in:
Sean Gilligan 2022-08-16 12:42:13 -07:00 committed by Andreas Schildbach
parent 81ddf4f27e
commit 0acfcb10ec
11 changed files with 53 additions and 15 deletions

View file

@ -26,6 +26,7 @@ import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.Runnables;
import com.google.common.util.concurrent.Uninterruptibles;
import net.jcip.annotations.GuardedBy;
import org.bitcoinj.base.Network;
import org.bitcoinj.core.listeners.AddressEventListener;
import org.bitcoinj.core.listeners.BlockchainDownloadEventListener;
import org.bitcoinj.core.listeners.BlocksDownloadedEventListener;
@ -356,27 +357,64 @@ public class PeerGroup implements TransactionBroadcaster {
/** Whether bloom filter support is enabled when using a non FullPrunedBlockchain*/
private volatile boolean vBloomFilteringEnabled = true;
/**
* Creates a PeerGroup for the given network. No chain is provided so this node will report its chain height
* as zero to other peers. This constructor is useful if you just want to explore the network but aren't interested
* in downloading block data.
* @param network the P2P network to connect to
*/
public PeerGroup(Network network) {
this(network, null);
}
/**
* Creates a PeerGroup with the given network. No chain is provided so this node will report its chain height
* as zero to other peers. This constructor is useful if you just want to explore the network but aren't interested
* in downloading block data.
* @deprecated Use {@link #PeerGroup(Network)}
*/
@Deprecated
public PeerGroup(NetworkParameters params) {
this(params, null);
this(params.network());
}
/**
* Creates a PeerGroup for the given network and chain. Blocks will be passed to the chain as they are broadcast
* and downloaded. This is probably the constructor you want to use.
* @param network the P2P network to connect to
* @param chain used to process blocks
*/
public PeerGroup(NetworkParameters params, @Nullable AbstractBlockChain chain) {
this(params, chain, new NioClientManager());
public PeerGroup(Network network, @Nullable AbstractBlockChain chain) {
this(network, chain, new NioClientManager());
}
/**
* Creates a new PeerGroup allowing you to specify the {@link ClientConnectionManager} which is used to create new
* connections and keep track of existing ones.
* Creates a PeerGroup for the given network and chain. Blocks will be passed to the chain as they are broadcast
* and downloaded.
* @deprecated Use {@link PeerGroup#PeerGroup(Network, AbstractBlockChain)}
*/
@Deprecated
public PeerGroup(NetworkParameters params, @Nullable AbstractBlockChain chain) {
this(params.network(), chain);
}
/**
* Create a PeerGroup for the given network, chain and connection manager.
* @param network the P2P network to connect to
* @param chain used to process blocks
* @param connectionManager used to create new connections and keep track of existing ones.
*/
protected PeerGroup(Network network, @Nullable AbstractBlockChain chain, ClientConnectionManager connectionManager) {
this(NetworkParameters.of(checkNotNull(network)), chain, connectionManager);
}
/**
* Create a PeerGroup for the given network, chain and connection manager.
* @param params the P2P network to connect to
* @param chain used to process blocks
* @param connectionManager used to create new connections and keep track of existing ones.
*/
@VisibleForTesting
protected PeerGroup(NetworkParameters params, @Nullable AbstractBlockChain chain, ClientConnectionManager connectionManager) {
checkNotNull(params);
Context.getOrCreate(); // create a context for convenience

View file

@ -465,7 +465,7 @@ public class WalletAppKit extends AbstractIdleService {
}
protected PeerGroup createPeerGroup() {
return new PeerGroup(params, vChain);
return new PeerGroup(network, vChain);
}
private void installShutdownHook() {

View file

@ -58,7 +58,7 @@ public class FetchBlock implements Callable<Integer> {
final NetworkParameters params = TestNet3Params.get();
BlockStore blockStore = new MemoryBlockStore(params);
BlockChain chain = new BlockChain(params, blockStore);
PeerGroup peerGroup = new PeerGroup(params, chain);
PeerGroup peerGroup = new PeerGroup(params.network(), chain);
if (localhost) {
peerGroup.addPeerDiscovery(new DnsDiscovery(params));
} else {

View file

@ -39,7 +39,7 @@ public class FetchTransactions {
BlockStore blockStore = new MemoryBlockStore(params);
BlockChain chain = new BlockChain(params, blockStore);
PeerGroup peerGroup = new PeerGroup(params, chain);
PeerGroup peerGroup = new PeerGroup(params.network(), chain);
peerGroup.start();
peerGroup.addAddress(new PeerAddress(params, InetAddress.getLocalHost()));
peerGroup.waitForPeers(1).get();

View file

@ -68,7 +68,7 @@ public class PeerMonitor {
private void setupNetwork() {
params = MainNetParams.get();
peerGroup = new PeerGroup(params, null /* no chain */);
peerGroup = new PeerGroup(params.network(), null /* no chain */);
peerGroup.setUserAgent("PeerMonitor", "1.0");
peerGroup.setMaxConnections(4);
peerGroup.addPeerDiscovery(new DnsDiscovery(params));

View file

@ -71,7 +71,7 @@ public class PrivateKeys {
final MemoryBlockStore blockStore = new MemoryBlockStore(params);
BlockChain chain = new BlockChain(params, wallet, blockStore);
final PeerGroup peerGroup = new PeerGroup(params, chain);
final PeerGroup peerGroup = new PeerGroup(params.network(), chain);
peerGroup.addAddress(new PeerAddress(params, InetAddress.getLocalHost()));
peerGroup.startAsync();
peerGroup.downloadBlockChain();

View file

@ -42,7 +42,7 @@ public class RefreshWallet {
BlockStore blockStore = new MemoryBlockStore(params);
BlockChain chain = new BlockChain(params, wallet, blockStore);
final PeerGroup peerGroup = new PeerGroup(params, chain);
final PeerGroup peerGroup = new PeerGroup(params.network(), chain);
peerGroup.startAsync();
wallet.addCoinsReceivedEventListener(new WalletCoinsReceivedEventListener() {

View file

@ -63,7 +63,7 @@ public class RestoreFromSeed {
// Setting up the BlochChain, the BlocksStore and connecting to the network.
SPVBlockStore chainStore = new SPVBlockStore(params, chainFile);
BlockChain chain = new BlockChain(params, chainStore);
PeerGroup peerGroup = new PeerGroup(params, chain);
PeerGroup peerGroup = new PeerGroup(params.network(), chain);
peerGroup.addPeerDiscovery(new DnsDiscovery(params));
// Now we need to hook the wallet up to the blockchain and the peers. This registers event listeners that notify our wallet about new transactions.

View file

@ -98,7 +98,7 @@ public class BuildCheckpoints implements Callable<Integer> {
// node and to save block headers that are on interval boundaries, as long as they are <1 month old.
final BlockStore store = new MemoryBlockStore(params);
final BlockChain chain = new BlockChain(params, store);
final PeerGroup peerGroup = new PeerGroup(params, chain);
final PeerGroup peerGroup = new PeerGroup(net, chain);
final InetAddress ipAddress;

View file

@ -46,7 +46,7 @@ public class WatchMempool {
public static void main(String[] args) throws InterruptedException {
BriefLogFormatter.init();
PeerGroup peerGroup = new PeerGroup(PARAMS);
PeerGroup peerGroup = new PeerGroup(PARAMS.network());
peerGroup.setMaxConnections(32);
peerGroup.addPeerDiscovery(new DnsDiscovery(PARAMS));
peerGroup.addOnTransactionBroadcastListener((peer, tx) -> {

View file

@ -1007,7 +1007,7 @@ public class WalletTool implements Callable<Integer> {
// This will ensure the wallet is saved when it changes.
wallet.autosaveToFile(walletFile, 5, TimeUnit.SECONDS, null);
if (peerGroup == null) {
peerGroup = new PeerGroup(params, chain);
peerGroup = new PeerGroup(net, chain);
}
peerGroup.setUserAgent("WalletTool", "1.0");
if (params == RegTestParams.get())