mirror of
https://github.com/bitcoinj/bitcoinj.git
synced 2025-02-24 06:47:54 +01:00
VersionMessage: Add support for BIP111 (NODE_BLOOM service bit).
This commit is contained in:
parent
d35583236a
commit
dceb2a4261
4 changed files with 22 additions and 12 deletions
|
@ -516,7 +516,8 @@ public abstract class NetworkParameters {
|
|||
public static enum ProtocolVersion {
|
||||
MINIMUM(70000),
|
||||
PONG(60001),
|
||||
BLOOM_FILTER(70000),
|
||||
BLOOM_FILTER(70000), // BIP37
|
||||
BLOOM_FILTER_BIP111(70011), // BIP111
|
||||
WITNESS_VERSION(70012),
|
||||
CURRENT(70012);
|
||||
|
||||
|
|
|
@ -420,6 +420,10 @@ public class Peer extends PeerSocketHandler {
|
|||
a.add("GETUTXOS");
|
||||
services &= ~VersionMessage.NODE_GETUTXOS;
|
||||
}
|
||||
if ((services & VersionMessage.NODE_BLOOM) == VersionMessage.NODE_BLOOM) {
|
||||
a.add("BLOOM");
|
||||
services &= ~VersionMessage.NODE_BLOOM;
|
||||
}
|
||||
if ((services & VersionMessage.NODE_WITNESS) == VersionMessage.NODE_WITNESS) {
|
||||
a.add("WITNESS");
|
||||
services &= ~VersionMessage.NODE_WITNESS;
|
||||
|
|
|
@ -49,6 +49,8 @@ public class VersionMessage extends Message {
|
|||
public static final int NODE_NETWORK = 1 << 0;
|
||||
/** A service bit that denotes whether the peer supports the getutxos message or not. */
|
||||
public static final int NODE_GETUTXOS = 1 << 1;
|
||||
/** A service bit that denotes whether the peer supports BIP37 bloom filters or not. The service bit is defined in BIP111. */
|
||||
public static final int NODE_BLOOM = 1 << 2;
|
||||
/** Indicates that a node can be asked for blocks and transactions including witness data. */
|
||||
public static final int NODE_WITNESS = 1 << 3;
|
||||
/** A service bit that denotes whether the peer has at least the last two days worth of blockchain (BIP159). */
|
||||
|
@ -273,12 +275,15 @@ public class VersionMessage extends Message {
|
|||
}
|
||||
|
||||
/**
|
||||
* Returns true if the clientVersion field is {@link NetworkParameters.ProtocolVersion#BLOOM_FILTER} or higher.
|
||||
* If it is then Bloom filtering
|
||||
* is available and the memory pool of the remote peer will be queried when the downloadData property is true.
|
||||
* Returns true if the peer supports bloom filtering according to BIP37 and BIP111.
|
||||
*/
|
||||
public boolean isBloomFilteringSupported() {
|
||||
return clientVersion >= params.getProtocolVersionNum(NetworkParameters.ProtocolVersion.BLOOM_FILTER);
|
||||
if (clientVersion >= params.getProtocolVersionNum(NetworkParameters.ProtocolVersion.BLOOM_FILTER)
|
||||
&& clientVersion < params.getProtocolVersionNum(NetworkParameters.ProtocolVersion.BLOOM_FILTER_BIP111))
|
||||
return true;
|
||||
if ((localServices & NODE_BLOOM) == NODE_BLOOM)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
/** Returns true if the protocol version and service bits both indicate support for the getutxos message. */
|
||||
|
|
|
@ -688,24 +688,24 @@ public class PeerGroupTest extends TestWithPeerGroup {
|
|||
|
||||
@Test
|
||||
public void waitForPeersOfVersion() throws Exception {
|
||||
final int baseVer = peerGroup.getMinRequiredProtocolVersion() + 3000;
|
||||
final int newVer = baseVer + 1000;
|
||||
final int bip37ver = UNITTEST.getProtocolVersionNum(NetworkParameters.ProtocolVersion.BLOOM_FILTER);
|
||||
final int bip111ver = UNITTEST.getProtocolVersionNum(NetworkParameters.ProtocolVersion.BLOOM_FILTER_BIP111);
|
||||
|
||||
ListenableFuture<List<Peer>> future = peerGroup.waitForPeersOfVersion(2, newVer);
|
||||
ListenableFuture<List<Peer>> future = peerGroup.waitForPeersOfVersion(2, bip111ver);
|
||||
|
||||
VersionMessage ver1 = new VersionMessage(UNITTEST, 10);
|
||||
ver1.clientVersion = baseVer;
|
||||
ver1.clientVersion = bip37ver;
|
||||
ver1.localServices = VersionMessage.NODE_NETWORK;
|
||||
VersionMessage ver2 = new VersionMessage(UNITTEST, 10);
|
||||
ver2.clientVersion = newVer;
|
||||
ver2.localServices = VersionMessage.NODE_NETWORK;
|
||||
ver2.clientVersion = bip111ver;
|
||||
ver2.localServices = VersionMessage.NODE_NETWORK | VersionMessage.NODE_BLOOM;
|
||||
peerGroup.start();
|
||||
assertFalse(future.isDone());
|
||||
connectPeer(1, ver1);
|
||||
assertFalse(future.isDone());
|
||||
connectPeer(2, ver2);
|
||||
assertFalse(future.isDone());
|
||||
assertTrue(peerGroup.waitForPeersOfVersion(1, newVer).isDone()); // Immediate completion.
|
||||
assertTrue(peerGroup.waitForPeersOfVersion(1, bip111ver).isDone()); // Immediate completion.
|
||||
connectPeer(3, ver2);
|
||||
future.get();
|
||||
assertTrue(future.isDone());
|
||||
|
|
Loading…
Add table
Reference in a new issue