VersionMessage: move isBloomFilteringSupported() to Peer

This commit is contained in:
Andreas Schildbach 2023-04-03 12:28:26 +02:00
parent f2fa39ef3e
commit fb9b578542
3 changed files with 26 additions and 18 deletions

View File

@ -1225,7 +1225,7 @@ public class Peer extends PeerSocketHandler {
// the duplicate check in blockChainDownloadLocked(). But Bitcoin Core may change in future so
// it's better to be safe here.
if (!pendingBlockDownloads.contains(item.hash)) {
if (vPeerVersionMessage.isBloomFilteringSupported() && useFilteredBlocks) {
if (isBloomFilteringSupported(vPeerVersionMessage) && useFilteredBlocks) {
getdata.addFilteredBlock(item.hash);
pingAfterGetData = true;
} else {
@ -1728,7 +1728,7 @@ public class Peer extends PeerSocketHandler {
Objects.requireNonNull(filter, "Clearing filters is not currently supported");
final VersionMessage version = vPeerVersionMessage;
Objects.requireNonNull(version, "Cannot set filter before version handshake is complete");
if (version.isBloomFilteringSupported()) {
if (isBloomFilteringSupported(version)) {
vBloomFilter = filter;
log.info("{}: Sending Bloom filter{}", this, andQueryMemPool ? " and querying mempool" : "");
sendMessage(filter);
@ -1808,4 +1808,17 @@ public class Peer extends PeerSocketHandler {
public void setDownloadTxDependencies(int depth) {
vDownloadTxDependencyDepth = depth;
}
/**
* Returns true if the peer supports bloom filtering according to BIP37 and BIP111.
*/
private boolean isBloomFilteringSupported(VersionMessage version) {
int clientVersion = version.clientVersion();
if (clientVersion >= params.getProtocolVersionNum(NetworkParameters.ProtocolVersion.BLOOM_FILTER)
&& clientVersion < params.getProtocolVersionNum(NetworkParameters.ProtocolVersion.BLOOM_FILTER_BIP111))
return true;
if (version.services().has(Services.NODE_BLOOM))
return true;
return false;
}
}

View File

@ -113,6 +113,15 @@ public class VersionMessage extends Message {
relayTxesBeforeFilter = true;
}
/**
* Gets the client version.
*
* @return client version
*/
public int clientVersion() {
return clientVersion;
}
/**
* Get the service bitfield that represents the node services being provided.
*
@ -257,16 +266,4 @@ public class VersionMessage extends Message {
public boolean isPingPongSupported() {
return true;
}
/**
* Returns true if the peer supports bloom filtering according to BIP37 and BIP111.
*/
public boolean isBloomFilteringSupported() {
if (clientVersion >= params.getProtocolVersionNum(NetworkParameters.ProtocolVersion.BLOOM_FILTER)
&& clientVersion < params.getProtocolVersionNum(NetworkParameters.ProtocolVersion.BLOOM_FILTER_BIP111))
return true;
if (localServices.has(Services.NODE_BLOOM))
return true;
return false;
}
}

View File

@ -175,9 +175,7 @@ public class TestWithPeerGroup extends TestWithNetworkConnections {
checkState(writeTarget.nextMessageBlocking() instanceof VersionMessage);
checkState(writeTarget.nextMessageBlocking() instanceof SendAddrV2Message);
checkState(writeTarget.nextMessageBlocking() instanceof VersionAck);
if (versionMessage.isBloomFilteringSupported()) {
checkState(writeTarget.nextMessageBlocking() instanceof BloomFilter);
checkState(writeTarget.nextMessageBlocking() instanceof MemoryPoolMessage);
}
checkState(writeTarget.nextMessageBlocking() instanceof BloomFilter);
checkState(writeTarget.nextMessageBlocking() instanceof MemoryPoolMessage);
}
}