VersionMessage: Move toStringServices() from Peer to here.

This commit is contained in:
Andreas Schildbach 2020-03-04 15:09:35 +01:00
parent 16fda8ffcb
commit b4bd43d428
2 changed files with 33 additions and 26 deletions

View File

@ -28,7 +28,6 @@ import org.bitcoinj.utils.Threading;
import org.bitcoinj.wallet.Wallet;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.MoreObjects;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
@ -371,7 +370,7 @@ public class Peer extends PeerSocketHandler {
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;
@ -380,31 +379,9 @@ public class Peer extends PeerSocketHandler {
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

View File

@ -16,6 +16,7 @@
package org.bitcoinj.core;
import com.google.common.base.Joiner;
import com.google.common.net.InetAddresses;
import javax.annotation.Nullable;
@ -24,6 +25,8 @@ import java.io.OutputStream;
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;
import java.util.Objects;
@ -309,4 +312,31 @@ public class VersionMessage extends Message {
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);
}
}