public class PeerGroup extends Object implements TransactionBroadcaster
Runs a set of connections to the P2P network, brings up connections to replace disconnected nodes and manages the interaction between them all. Most applications will want to use one of these.
PeerGroup tries to maintain a constant number of connections to a set of distinct peers. Each peer runs a network listener in its own thread. When a connection is lost, a new peer will be tried after a delay as long as the number of connections less than the maximum.
Connections are made to addresses from a provided list. When that list is exhausted, we start again from the head of the list.
The PeerGroup can broadcast a transaction to the currently connected set of peers. It can also handle download of the blockchain from peers, restarting the process when peers die.
A PeerGroup won't do anything until you call the start()
method
which will block until peer discovery is completed and some outbound connections
have been initiated (it will return before handshaking is done, however).
You should call stop()
when finished. Note that not all methods
of PeerGroup are safe to call from a UI thread as some may do network IO,
but starting and stopping the service should be fine.
Modifier and Type | Class and Description |
---|---|
static class |
PeerGroup.FilterRecalculateMode |
Modifier and Type | Field and Description |
---|---|
protected AbstractBlockChain |
chain |
static double |
DEFAULT_BLOOM_FILTER_FP_RATE
The default Bloom filter false positive rate, which is selected to be extremely low such that you hardly ever
download false positives.
|
static int |
DEFAULT_CONNECT_TIMEOUT_MILLIS
The default timeout between when a connection attempt begins and version message exchange completes
|
static int |
DEFAULT_CONNECTIONS
The default number of connections to the p2p network the library will try to build.
|
static long |
DEFAULT_PING_INTERVAL_MSEC
How many milliseconds to wait after receiving a pong before sending another ping.
|
protected ListeningScheduledExecutorService |
executor |
protected ReentrantLock |
lock |
static double |
MAX_FP_RATE_INCREASE
Maximum increase in FP rate before forced refresh of the bloom filter
|
protected NetworkParameters |
params |
protected CopyOnWriteArrayList<ListenerRegistration<PeerConnectedEventListener>> |
peerConnectedEventListeners
Callbacks for events related to peers connecting
|
protected CopyOnWriteArrayList<ListenerRegistration<PeerDisconnectedEventListener>> |
peerDisconnectedEventListeners
Callbacks for events related to peers disconnecting
|
protected CopyOnWriteArrayList<ListenerRegistration<PeerDiscoveredEventListener>> |
peerDiscoveredEventListeners
Callbacks for events related to peer connection/disconnection
|
protected CopyOnWriteArrayList<ListenerRegistration<OnTransactionBroadcastListener>> |
peersTransactionBroadastEventListeners |
Constructor and Description |
---|
PeerGroup(Context context)
Creates a PeerGroup with the given context.
|
PeerGroup(Context context,
AbstractBlockChain chain)
Creates a PeerGroup for the given context and chain.
|
PeerGroup(Context context,
AbstractBlockChain chain,
ClientConnectionManager connectionManager)
Creates a new PeerGroup allowing you to specify the
ClientConnectionManager which is used to create new
connections and keep track of existing ones. |
PeerGroup(NetworkParameters params)
|
PeerGroup(NetworkParameters params,
AbstractBlockChain chain)
|
PeerGroup(NetworkParameters params,
AbstractBlockChain chain,
ClientConnectionManager connectionManager)
|
Modifier and Type | Method and Description |
---|---|
void |
addAddress(InetAddress address)
Convenience method for addAddress(new PeerAddress(address, params.port));
|
void |
addAddress(PeerAddress peerAddress)
Add an address to the list of potential peers to connect to.
|
void |
addBlocksDownloadedEventListener(BlocksDownloadedEventListener listener)
|
void |
addBlocksDownloadedEventListener(Executor executor,
BlocksDownloadedEventListener listener)
Adds a listener that will be notified on the given executor when
blocks are downloaded by the download peer.
|
void |
addChainDownloadStartedEventListener(ChainDownloadStartedEventListener listener)
|
void |
addChainDownloadStartedEventListener(Executor executor,
ChainDownloadStartedEventListener listener)
Adds a listener that will be notified on the given executor when
chain download starts.
|
void |
addConnectedEventListener(Executor executor,
PeerConnectedEventListener listener)
Adds a listener that will be notified on the given executor when
new peers are connected to.
|
void |
addConnectedEventListener(PeerConnectedEventListener listener)
|
void |
addDisconnectedEventListener(Executor executor,
PeerDisconnectedEventListener listener)
Adds a listener that will be notified on the given executor when
peers are disconnected from.
|
void |
addDisconnectedEventListener(PeerDisconnectedEventListener listener)
|
void |
addDiscoveredEventListener(Executor executor,
PeerDiscoveredEventListener listener)
Adds a listener that will be notified on the given executor when new
peers are discovered.
|
void |
addDiscoveredEventListener(PeerDiscoveredEventListener listener)
See
Peer#addDiscoveredEventListener(PeerDiscoveredEventListener) |
void |
addEventListener(AbstractPeerEventListener listener)
Deprecated.
|
void |
addEventListener(AbstractPeerEventListener listener,
Executor executor)
Deprecated.
|
void |
addGetDataEventListener(Executor executor,
GetDataEventListener listener)
|
void |
addGetDataEventListener(GetDataEventListener listener)
|
void |
addOnTransactionBroadcastListener(Executor executor,
OnTransactionBroadcastListener listener)
|
void |
addOnTransactionBroadcastListener(OnTransactionBroadcastListener listener)
|
void |
addPeerDiscovery(PeerDiscovery peerDiscovery)
Add addresses from a discovery source to the list of potential peers to connect to.
|
ListenableFuture<BloomFilter> |
addPeerFilterProvider(PeerFilterProvider provider)
Link the given PeerFilterProvider to this PeerGroup.
|
void |
addPreMessageReceivedEventListener(Executor executor,
PreMessageReceivedEventListener listener)
|
void |
addPreMessageReceivedEventListener(PreMessageReceivedEventListener listener)
|
void |
addWallet(Wallet wallet)
Link the given wallet to this PeerGroup.
|
void |
awaitRunning()
Deprecated.
|
void |
awaitTerminated()
Deprecated.
|
TransactionBroadcast |
broadcastTransaction(Transaction tx)
Calls
broadcastTransaction(Transaction,int) with getMinBroadcastConnections() as the number
of connections to wait for before commencing broadcast. |
TransactionBroadcast |
broadcastTransaction(Transaction tx,
int minConnections)
Given a transaction, sends it un-announced to one peer and then waits for it to be received back from other
peers.
|
Peer |
connectTo(InetSocketAddress address)
Connect to a peer by creating a channel to the destination address.
|
protected Peer |
connectTo(PeerAddress address,
boolean incrementMaxConnections,
int connectTimeoutMillis)
Creates a version message to send, constructs a Peer object and attempts to connect it.
|
Peer |
connectToLocalHost()
Helper for forcing a connection to localhost.
|
protected Peer |
createPeer(PeerAddress address,
VersionMessage ver)
You can override this to customise the creation of
Peer objects. |
protected ListeningScheduledExecutorService |
createPrivateExecutor() |
protected int |
discoverPeers()
Returns number of discovered peers.
|
void |
downloadBlockChain()
Download the blockchain from peers.
|
List<Peer> |
findPeersOfAtLeastVersion(long protocolVersion)
Returns an array list of peers that implement the given protocol version or better.
|
List<Peer> |
findPeersWithServiceMask(int mask)
Returns an array list of peers that match the requested service bit mask.
|
List<Peer> |
getConnectedPeers()
Returns a newly allocated list containing the currently connected peers.
|
Peer |
getDownloadPeer()
Returns the currently selected download peer.
|
long |
getFastCatchupTimeSecs()
Returns the current fast catchup time.
|
int |
getMaxConnections()
The maximum number of connections that we will create to peers.
|
int |
getMaxPeersToDiscoverCount()
Returns the maximum number of
Peer s to discover. |
TxConfidenceTable |
getMemoryPool()
Deprecated.
|
int |
getMinBroadcastConnections()
Returns the number of connections that are required before transactions will be broadcast.
|
int |
getMinRequiredProtocolVersion()
The minimum protocol version required: defaults to the version required for Bloom filtering.
|
int |
getMostCommonChainHeight()
Returns our peers most commonly reported chain height.
|
static int |
getMostCommonChainHeight(List<Peer> peers)
Returns most commonly reported chain height from the given list of
Peer s. |
List<Peer> |
getPendingPeers()
Returns a list containing Peers that did not complete connection yet.
|
long |
getPingIntervalMsec()
Returns the period between pings for an individual peer.
|
com.subgraph.orchid.TorClient |
getTorClient()
Returns the
TorClient object for this peer group, if Tor is in use, null otherwise. |
boolean |
getUseLocalhostPeerWhenPossible()
|
VersionMessage |
getVersionMessage()
Returns the version message provided by setVersionMessage or a default if none was given.
|
protected void |
handleNewPeer(Peer peer) |
protected void |
handlePeerDeath(Peer peer,
Throwable exception) |
boolean |
isBloomFilteringEnabled()
Returns whether the Bloom filtering protocol optimisation is in use: defaults to true.
|
boolean |
isRunning() |
static PeerGroup |
newWithTor(Context context,
AbstractBlockChain chain,
com.subgraph.orchid.TorClient torClient)
Creates a PeerGroup that accesses the network via the Tor network.
|
static PeerGroup |
newWithTor(Context context,
AbstractBlockChain chain,
com.subgraph.orchid.TorClient torClient,
boolean doDiscovery)
Creates a PeerGroup that accesses the network via the Tor network.
|
static PeerGroup |
newWithTor(NetworkParameters params,
AbstractBlockChain chain,
com.subgraph.orchid.TorClient torClient)
|
int |
numConnectedPeers()
Returns the number of currently connected peers.
|
ListenableFuture<BloomFilter> |
recalculateFastCatchupAndFilter(PeerGroup.FilterRecalculateMode mode)
Recalculates the bloom filter given to peers as well as the timestamp after which full blocks are downloaded
(instead of only headers).
|
boolean |
removeBlocksDownloadedEventListener(BlocksDownloadedEventListener listener) |
boolean |
removeChainDownloadStartedEventListener(ChainDownloadStartedEventListener listener) |
boolean |
removeConnectedEventListener(PeerConnectedEventListener listener)
The given event listener will no longer be called with events.
|
boolean |
removeDisconnectedEventListener(PeerDisconnectedEventListener listener)
The given event listener will no longer be called with events.
|
boolean |
removeDiscoveredEventListener(PeerDiscoveredEventListener listener)
The given event listener will no longer be called with events.
|
void |
removeEventListener(AbstractPeerEventListener listener)
Deprecated.
|
boolean |
removeGetDataEventListener(GetDataEventListener listener)
The given event listener will no longer be called with events.
|
boolean |
removeOnTransactionBroadcastListener(OnTransactionBroadcastListener listener)
The given event listener will no longer be called with events.
|
void |
removePeerFilterProvider(PeerFilterProvider provider)
Opposite of
addPeerFilterProvider(PeerFilterProvider) . |
boolean |
removePreMessageReceivedEventListener(PreMessageReceivedEventListener listener) |
void |
removeWallet(Wallet wallet)
Unlinks the given wallet so it no longer receives broadcast transactions or has its transactions announced.
|
protected Peer |
selectDownloadPeer(List<Peer> peers)
Given a list of Peers, return a Peer to be used as the download peer.
|
void |
setBloomFilterFalsePositiveRate(double bloomFilterFPRate)
Sets the false positive rate of bloom filters given to peers.
|
void |
setBloomFilteringEnabled(boolean bloomFilteringEnabled)
Can be used to disable Bloom filtering entirely, even in SPV mode.
|
void |
setConnectTimeoutMillis(int connectTimeoutMillis)
Sets the timeout between when a connection attempt to a peer begins and when the version message exchange
completes.
|
void |
setDownloadTxDependencies(int depth)
Configure download of pending transaction dependencies.
|
void |
setFastCatchupTimeSecs(long secondsSinceEpoch)
Tells the PeerGroup to download only block headers before a certain time and bodies after that.
|
void |
setMaxConnections(int maxConnections)
Adjusts the desired number of connections that we will create to peers.
|
void |
setMaxPeersToDiscoverCount(int maxPeersToDiscoverCount)
Sets the maximum number of
Peer s to discover. |
void |
setMinBroadcastConnections(int value)
|
void |
setMinRequiredProtocolVersion(int minRequiredProtocolVersion)
If a peer is connected to that claims to speak a protocol version lower than the given version, it will
be disconnected and another one will be tried instead.
|
void |
setPeerDiscoveryTimeoutMillis(long peerDiscoveryTimeoutMillis)
This is how many milliseconds we wait for peer discoveries to return their results.
|
void |
setPingIntervalMsec(long pingIntervalMsec)
Sets the period between pings for an individual peer.
|
void |
setRequiredServices(long requiredServices)
Convenience for connecting only to peers that can serve specific services.
|
void |
setStallThreshold(int periodSecs,
int bytesPerSecond)
Configures the stall speed: the speed at which a peer is considered to be serving us the block chain
unacceptably slowly.
|
void |
setUseLocalhostPeerWhenPossible(boolean useLocalhostPeerWhenPossible)
When true (the default), PeerGroup will attempt to connect to a Bitcoin node running on localhost before
attempting to use the P2P network.
|
void |
setUserAgent(String name,
String version)
Sets information that identifies this software to remote nodes.
|
void |
setUserAgent(String name,
String version,
String comments)
Sets information that identifies this software to remote nodes.
|
void |
setVersionMessage(VersionMessage ver)
Sets the
VersionMessage that will be announced on newly created connections. |
void |
start()
Does a blocking startup.
|
ListenableFuture |
startAsync()
Starts the PeerGroup and begins network activity.
|
void |
startBlockChainDownload(PeerDataEventListener listener)
Start downloading the blockchain from the first available peer.
|
void |
stop()
Does a blocking stop
|
ListenableFuture |
stopAsync() |
ListenableFuture<List<Peer>> |
waitForPeers(int numPeers)
Returns a future that is triggered when the number of connected peers is equal to the given number of
peers.
|
ListenableFuture<List<Peer>> |
waitForPeersOfVersion(int numPeers,
long protocolVersion)
Returns a future that is triggered when there are at least the requested number of connected peers that support
the given protocol version or higher.
|
ListenableFuture<List<Peer>> |
waitForPeersWithServiceMask(int numPeers,
int mask)
Returns a future that is triggered when there are at least the requested number of connected peers that support
the given protocol version or higher.
|
public static final int DEFAULT_CONNECTIONS
protected final ReentrantLock lock
protected final NetworkParameters params
@Nullable protected final AbstractBlockChain chain
protected final ListeningScheduledExecutorService executor
protected final CopyOnWriteArrayList<ListenerRegistration<PeerConnectedEventListener>> peerConnectedEventListeners
protected final CopyOnWriteArrayList<ListenerRegistration<PeerDiscoveredEventListener>> peerDiscoveredEventListeners
protected final CopyOnWriteArrayList<ListenerRegistration<PeerDisconnectedEventListener>> peerDisconnectedEventListeners
protected final CopyOnWriteArrayList<ListenerRegistration<OnTransactionBroadcastListener>> peersTransactionBroadastEventListeners
public static final long DEFAULT_PING_INTERVAL_MSEC
public static final double DEFAULT_BLOOM_FILTER_FP_RATE
public static final double MAX_FP_RATE_INCREASE
public static final int DEFAULT_CONNECT_TIMEOUT_MILLIS
public PeerGroup(NetworkParameters params)
public PeerGroup(Context context)
public PeerGroup(NetworkParameters params, @Nullable AbstractBlockChain chain)
public PeerGroup(Context context, @Nullable AbstractBlockChain chain)
public PeerGroup(NetworkParameters params, @Nullable AbstractBlockChain chain, ClientConnectionManager connectionManager)
public PeerGroup(Context context, @Nullable AbstractBlockChain chain, ClientConnectionManager connectionManager)
ClientConnectionManager
which is used to create new
connections and keep track of existing ones.public static PeerGroup newWithTor(NetworkParameters params, @Nullable AbstractBlockChain chain, com.subgraph.orchid.TorClient torClient) throws TimeoutException
TimeoutException
public static PeerGroup newWithTor(Context context, @Nullable AbstractBlockChain chain, com.subgraph.orchid.TorClient torClient) throws TimeoutException
Creates a PeerGroup that accesses the network via the Tor network. The provided TorClient is used so you can preconfigure it beforehand. It should not have been already started. You can just use "new TorClient()" if you don't have any particular configuration requirements.
Peer discovery is automatically configured to use DNS seeds resolved via a random selection of exit nodes. If running on the Oracle JDK the unlimited strength jurisdiction checks will also be overridden, as they no longer apply anyway and can cause startup failures due to the requirement for AES-256.
The user does not need any additional software for this: it's all pure Java. As of April 2014 this mode is experimental.
TimeoutException
- if Tor fails to start within 20 seconds.public static PeerGroup newWithTor(Context context, @Nullable AbstractBlockChain chain, com.subgraph.orchid.TorClient torClient, boolean doDiscovery) throws TimeoutException
Creates a PeerGroup that accesses the network via the Tor network. The provided TorClient is used so you can preconfigure it beforehand. It should not have been already started. You can just use "new TorClient()" if you don't have any particular configuration requirements.
If running on the Oracle JDK the unlimited strength jurisdiction checks will also be overridden, as they no longer apply anyway and can cause startup failures due to the requirement for AES-256.
The user does not need any additional software for this: it's all pure Java. As of April 2014 this mode is experimental.
doDiscovery
- if true, DNS or HTTP peer discovery will be performed via Tor: this is almost always what you want.TimeoutException
- if Tor fails to start within 20 seconds.protected ListeningScheduledExecutorService createPrivateExecutor()
public void setPeerDiscoveryTimeoutMillis(long peerDiscoveryTimeoutMillis)
public void setMaxConnections(int maxConnections)
public void setDownloadTxDependencies(int depth)
public int getMaxConnections()
public void setVersionMessage(VersionMessage ver)
VersionMessage
that will be announced on newly created connections. A version message is
primarily interesting because it lets you customize the "subVer" field which is used a bit like the User-Agent
field from HTTP. It means your client tells the other side what it is, see
BIP 14.
The VersionMessage you provide is copied and the best chain height/time filled in for each new connection,
therefore you don't have to worry about setting that. The provided object is really more of a template.public VersionMessage getVersionMessage()
public void setUserAgent(String name, String version, @Nullable String comments)
VersionMessage
, calling VersionMessage.appendToSubVer(String, String, String)
on it,
and then calling setVersionMessage(VersionMessage)
on the result of that. See the docs for
VersionMessage.appendToSubVer(String, String, String)
for information on what the fields should contain.public void setUserAgent(String name, String version)
VersionMessage
, calling VersionMessage.appendToSubVer(String, String, String)
on it,
and then calling setVersionMessage(VersionMessage)
on the result of that. See the docs for
VersionMessage.appendToSubVer(String, String, String)
for information on what the fields should contain.@Deprecated public void addEventListener(AbstractPeerEventListener listener, Executor executor)
@Deprecated public void addEventListener(AbstractPeerEventListener listener)
public void addBlocksDownloadedEventListener(BlocksDownloadedEventListener listener)
public void addBlocksDownloadedEventListener(Executor executor, BlocksDownloadedEventListener listener)
Adds a listener that will be notified on the given executor when blocks are downloaded by the download peer.
public void addChainDownloadStartedEventListener(ChainDownloadStartedEventListener listener)
public void addChainDownloadStartedEventListener(Executor executor, ChainDownloadStartedEventListener listener)
Adds a listener that will be notified on the given executor when chain download starts.
public void addConnectedEventListener(PeerConnectedEventListener listener)
public void addConnectedEventListener(Executor executor, PeerConnectedEventListener listener)
Adds a listener that will be notified on the given executor when new peers are connected to.
public void addDisconnectedEventListener(PeerDisconnectedEventListener listener)
public void addDisconnectedEventListener(Executor executor, PeerDisconnectedEventListener listener)
Adds a listener that will be notified on the given executor when peers are disconnected from.
public void addDiscoveredEventListener(PeerDiscoveredEventListener listener)
Peer#addDiscoveredEventListener(PeerDiscoveredEventListener)
public void addDiscoveredEventListener(Executor executor, PeerDiscoveredEventListener listener)
Adds a listener that will be notified on the given executor when new peers are discovered.
public void addGetDataEventListener(GetDataEventListener listener)
public void addGetDataEventListener(Executor executor, GetDataEventListener listener)
public void addOnTransactionBroadcastListener(OnTransactionBroadcastListener listener)
public void addOnTransactionBroadcastListener(Executor executor, OnTransactionBroadcastListener listener)
public void addPreMessageReceivedEventListener(PreMessageReceivedEventListener listener)
public void addPreMessageReceivedEventListener(Executor executor, PreMessageReceivedEventListener listener)
@Deprecated public void removeEventListener(AbstractPeerEventListener listener)
public boolean removeBlocksDownloadedEventListener(BlocksDownloadedEventListener listener)
public boolean removeChainDownloadStartedEventListener(ChainDownloadStartedEventListener listener)
public boolean removeConnectedEventListener(PeerConnectedEventListener listener)
public boolean removeDisconnectedEventListener(PeerDisconnectedEventListener listener)
public boolean removeDiscoveredEventListener(PeerDiscoveredEventListener listener)
public boolean removeGetDataEventListener(GetDataEventListener listener)
public boolean removeOnTransactionBroadcastListener(OnTransactionBroadcastListener listener)
public boolean removePreMessageReceivedEventListener(PreMessageReceivedEventListener listener)
public List<Peer> getConnectedPeers()
public List<Peer> getPendingPeers()
public void addAddress(PeerAddress peerAddress)
peerAddress
- IP/port to use.public void setRequiredServices(long requiredServices)
requiredServices
- Required services as a bitmask, e.g. VersionMessage.NODE_NETWORK
.public void addAddress(InetAddress address)
public void addPeerDiscovery(PeerDiscovery peerDiscovery)
protected int discoverPeers() throws PeerDiscoveryException
PeerDiscoveryException
public ListenableFuture startAsync()
public void start()
@Deprecated public void awaitRunning()
public ListenableFuture stopAsync()
public void stop()
@Deprecated public void awaitTerminated()
public void addWallet(Wallet wallet)
Link the given wallet to this PeerGroup. This is used for three purposes:
setFastCatchupTimeSecs(long)
, to optimize chain
download.Note that this should be done before chain download commences because if you add a wallet with keys earlier than the current chain head, the relevant parts of the chain won't be redownloaded for you.
The Wallet will have an event listener registered on it, so to avoid leaks remember to use
removeWallet(Wallet)
on it if you wish to keep the Wallet but lose the PeerGroup.
public ListenableFuture<BloomFilter> addPeerFilterProvider(PeerFilterProvider provider)
Link the given PeerFilterProvider to this PeerGroup. DO NOT use this for Wallets, use
addWallet(Wallet)
instead.
Note that this should be done before chain download commences because if you add a listener with keys earlier than the current chain head, the relevant parts of the chain won't be redownloaded for you.
This method invokes recalculateFastCatchupAndFilter(FilterRecalculateMode)
.
The return value of this method is the ListenableFuture
returned by that invocation.
Peer
in this group has had its
BloomFilter
(re)set.public void removePeerFilterProvider(PeerFilterProvider provider)
addPeerFilterProvider(PeerFilterProvider)
. Again, don't use this for wallets. Does not
trigger recalculation of the filter.public void removeWallet(Wallet wallet)
public ListenableFuture<BloomFilter> recalculateFastCatchupAndFilter(PeerGroup.FilterRecalculateMode mode)
mode
- In what situations to send the filter to connected peers.public void setBloomFilterFalsePositiveRate(double bloomFilterFPRate)
Sets the false positive rate of bloom filters given to peers. The default is DEFAULT_BLOOM_FILTER_FP_RATE
.
Be careful regenerating the bloom filter too often, as it decreases anonymity because remote nodes can compare transactions against both the new and old filters to significantly decrease the false positive rate.
See the docs for BloomFilter.BloomFilter(int, double, long, BloomFilter.BloomUpdate)
for a brief
explanation of anonymity when using bloom filters.
public int numConnectedPeers()
PeerConnectionEventListener
and use the onPeerConnected/onPeerDisconnected methods.@Nullable public Peer connectTo(InetSocketAddress address)
start()
address
- destination IP and port.Peer.getConnectionOpenFuture()
if you
want a future which completes when the connection is open.@Nullable public Peer connectToLocalHost()
@Nullable protected Peer connectTo(PeerAddress address, boolean incrementMaxConnections, int connectTimeoutMillis)
address
- Remote network addressincrementMaxConnections
- Whether to consider this connection an attempt to fill our quota, or something
explicitly requested.protected Peer createPeer(PeerAddress address, VersionMessage ver)
Peer
objects.public void setConnectTimeoutMillis(int connectTimeoutMillis)
public void startBlockChainDownload(PeerDataEventListener listener)
Start downloading the blockchain from the first available peer.
If no peers are currently connected, the download will be started once a peer starts. If the peer dies, the download will resume with another peer.
listener
- a listener for chain download events, may not be nullpublic void downloadBlockChain()
DownloadProgressTracker
for you.This method waits until the download is complete. "Complete" is defined as downloading from at least one peer all the blocks that are in that peer's inventory.
protected void handleNewPeer(Peer peer)
@Deprecated @Nullable public TxConfidenceTable getMemoryPool()
public void setFastCatchupTimeSecs(long secondsSinceEpoch)
public long getFastCatchupTimeSecs()
addWallet(Wallet)
this just returns
the min of the wallets earliest key times.public void setStallThreshold(int periodSecs, int bytesPerSecond)
periodSecs
- How many seconds the download speed must be below blocksPerSec, defaults to 10.bytesPerSecond
- Download speed (only blocks/txns count) must be consistently below this for a stall, defaults to the bandwidth required for 20 block headers per second.public ListenableFuture<List<Peer>> waitForPeers(int numPeers)
getMaxConnections()
you can wait until the
network is fully online. To block immediately, just call get() on the result. Just calls
waitForPeersOfVersion(int, long)
with zero as the protocol version.numPeers
- How many peers to wait for.public ListenableFuture<List<Peer>> waitForPeersOfVersion(int numPeers, long protocolVersion)
numPeers
- How many peers to wait for.protocolVersion
- The protocol version the awaited peers must implement (or better).public List<Peer> findPeersOfAtLeastVersion(long protocolVersion)
public ListenableFuture<List<Peer>> waitForPeersWithServiceMask(int numPeers, int mask)
numPeers
- How many peers to wait for.mask
- An integer representing a bit mask that will be ANDed with the peers advertised service masks.public List<Peer> findPeersWithServiceMask(int mask)
public int getMinBroadcastConnections()
broadcastTransaction(Transaction)
will wait until the minimum number is reached so
propagation across the network can be observed. If no value has been set using
setMinBroadcastConnections(int)
a default of 80% of whatever
getMaxConnections()
returns is used.public void setMinBroadcastConnections(int value)
public TransactionBroadcast broadcastTransaction(Transaction tx)
broadcastTransaction(Transaction,int)
with getMinBroadcastConnections() as the number
of connections to wait for before commencing broadcast.broadcastTransaction
in interface TransactionBroadcaster
public TransactionBroadcast broadcastTransaction(Transaction tx, int minConnections)
Given a transaction, sends it un-announced to one peer and then waits for it to be received back from other
peers. Once all connected peers have announced the transaction, the future available via the
TransactionBroadcast.future()
method will be completed. If anything goes
wrong the exception will be thrown when get() is called, or you can receive it via a callback on the
ListenableFuture
. This method returns immediately, so if you want it to block just call get() on the
result.
Note that if the PeerGroup is limited to only one connection (discovery is not activated) then the future will complete as soon as the transaction was successfully written to that peer.
The transaction won't be sent until there are at least minConnections active connections available. A good choice for proportion would be between 0.5 and 0.8 but if you want faster transmission during initial bringup of the peer group you can lower it.
The returned TransactionBroadcast
object can be used to get progress feedback,
which is calculated by watching the transaction propagate across the network and be announced by peers.
public long getPingIntervalMsec()
Peer.getLastPingTime()
but it increases load on the
remote node. It defaults to 5000.public void setPingIntervalMsec(long pingIntervalMsec)
Peer.getLastPingTime()
but it increases load on the
remote node. It defaults to DEFAULT_PING_INTERVAL_MSEC
.
Setting the value to be <= 0 disables pinging entirely, although you can still request one yourself
using Peer.ping()
.public void setMinRequiredProtocolVersion(int minRequiredProtocolVersion)
public int getMinRequiredProtocolVersion()
public int getMostCommonChainHeight()
public static int getMostCommonChainHeight(List<Peer> peers)
Peer
s.
If multiple heights are tied, the highest is returned. If no peers are connected, returns zero.@Nullable protected Peer selectDownloadPeer(List<Peer> peers)
public Peer getDownloadPeer()
@Nullable public com.subgraph.orchid.TorClient getTorClient()
TorClient
object for this peer group, if Tor is in use, null otherwise.public int getMaxPeersToDiscoverCount()
Peer
s to discover. This maximum is checked after
each PeerDiscovery
so this max number can be surpassed.public void setMaxPeersToDiscoverCount(int maxPeersToDiscoverCount)
Peer
s to discover. This maximum is checked after
each PeerDiscovery
so this max number can be surpassed.maxPeersToDiscoverCount
- the maximum number of peers to discoverpublic boolean getUseLocalhostPeerWhenPossible()
public void setUseLocalhostPeerWhenPossible(boolean useLocalhostPeerWhenPossible)
public boolean isRunning()
public void setBloomFilteringEnabled(boolean bloomFilteringEnabled)
public boolean isBloomFilteringEnabled()
Copyright © 2016. All rights reserved.