Skip to content

Commit

Permalink
VersionMessage: Move toStringServices() from Peer to here.
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas Schildbach committed Mar 4, 2020
1 parent 409f574 commit 77898c2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 27 deletions.
36 changes: 9 additions & 27 deletions core/src/main/java/org/bitcoinj/core/Peer.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

package org.bitcoinj.core;

import com.google.common.base.*;
import com.google.common.base.Objects;
import com.google.common.collect.Lists;
import org.bitcoinj.core.listeners.*;
import org.bitcoinj.net.AbstractTimeoutHandler;
import org.bitcoinj.net.NioClient;
Expand All @@ -29,7 +29,11 @@
import org.bitcoinj.utils.Threading;
import org.bitcoinj.wallet.Wallet;

import com.google.common.collect.Lists;
import com.google.common.base.Function;
import com.google.common.base.MoreObjects;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.base.Throwables;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
Expand Down Expand Up @@ -366,7 +370,7 @@ public String toString() {
helper.addValue(getAddress());
helper.add("version", vPeerVersionMessage.clientVersion);
helper.add("subVer", vPeerVersionMessage.subVer);
String servicesStr = Strings.emptyToNull(toStringServices(vPeerVersionMessage.localServices));
String servicesStr = Strings.emptyToNull(VersionMessage.toStringServices(vPeerVersionMessage.localServices));
helper.add("services",
vPeerVersionMessage.localServices + (servicesStr != null ? " (" + servicesStr + ")" : ""));
long peerTime = vPeerVersionMessage.time * 1000;
Expand All @@ -375,31 +379,9 @@ public String toString() {
return helper.toString();
}

@Deprecated
public String toStringServices(long services) {
List<String> a = new LinkedList<>();
if ((services & VersionMessage.NODE_NETWORK) == VersionMessage.NODE_NETWORK) {
a.add("NETWORK");
services &= ~VersionMessage.NODE_NETWORK;
}
if ((services & VersionMessage.NODE_GETUTXOS) == VersionMessage.NODE_GETUTXOS) {
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;
}
if ((services & VersionMessage.NODE_NETWORK_LIMITED) == VersionMessage.NODE_NETWORK_LIMITED) {
a.add("NETWORK_LIMITED");
services &= ~VersionMessage.NODE_NETWORK_LIMITED;
}
if (services != 0)
a.add("remaining: " + Long.toBinaryString(services));
return Joiner.on(", ").join(a);
return VersionMessage.toStringServices(services);
}

@Override
Expand Down
30 changes: 30 additions & 0 deletions core/src/main/java/org/bitcoinj/core/VersionMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.bitcoinj.core;

import com.google.common.base.Joiner;
import com.google.common.base.Objects;
import com.google.common.net.InetAddresses;

Expand All @@ -25,6 +26,8 @@
import java.math.BigInteger;
import java.net.InetAddress;
import java.nio.charset.StandardCharsets;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;

/**
Expand Down Expand Up @@ -309,4 +312,31 @@ public boolean hasBlockChain() {
public boolean hasLimitedBlockChain() {
return hasBlockChain() || (localServices & NODE_NETWORK_LIMITED) == NODE_NETWORK_LIMITED;
}

public static String toStringServices(long services) {
List<String> strings = new LinkedList<>();
if ((services & NODE_NETWORK) == NODE_NETWORK) {
strings.add("NETWORK");
services &= ~NODE_NETWORK;
}
if ((services & NODE_GETUTXOS) == NODE_GETUTXOS) {
strings.add("GETUTXOS");
services &= ~NODE_GETUTXOS;
}
if ((services & NODE_BLOOM) == NODE_BLOOM) {
strings.add("BLOOM");
services &= ~NODE_BLOOM;
}
if ((services & NODE_WITNESS) == NODE_WITNESS) {
strings.add("WITNESS");
services &= ~NODE_WITNESS;
}
if ((services & NODE_NETWORK_LIMITED) == NODE_NETWORK_LIMITED) {
strings.add("NETWORK_LIMITED");
services &= ~NODE_NETWORK_LIMITED;
}
if (services != 0)
strings.add("remaining: " + Long.toBinaryString(services));
return Joiner.on(", ").join(strings);
}
}

0 comments on commit 77898c2

Please sign in to comment.