PeerAddress: Remove mostly unused setters to make the class more immutable.

This commit is contained in:
Andreas Schildbach
2017-01-10 11:47:50 +01:00
parent 88fec4e937
commit 380abd0d52
4 changed files with 12 additions and 29 deletions

View File

@@ -71,12 +71,12 @@ public class PeerAddress extends ChildMessage {
/** /**
* Construct a peer address from a memorized or hardcoded address. * Construct a peer address from a memorized or hardcoded address.
*/ */
public PeerAddress(NetworkParameters params, InetAddress addr, int port, int protocolVersion) { public PeerAddress(NetworkParameters params, InetAddress addr, int port, int protocolVersion, BigInteger services) {
super(params); super(params);
this.addr = checkNotNull(addr); this.addr = checkNotNull(addr);
this.port = port; this.port = port;
this.protocolVersion = protocolVersion; this.protocolVersion = protocolVersion;
this.services = BigInteger.ZERO; this.services = services;
length = protocolVersion > 31402 ? MESSAGE_SIZE : MESSAGE_SIZE - 4; length = protocolVersion > 31402 ? MESSAGE_SIZE : MESSAGE_SIZE - 4;
} }
@@ -84,7 +84,8 @@ public class PeerAddress extends ChildMessage {
* Constructs a peer address from the given IP address and port. Version number is default for the given parameters. * Constructs a peer address from the given IP address and port. Version number is default for the given parameters.
*/ */
public PeerAddress(NetworkParameters params, InetAddress addr, int port) { public PeerAddress(NetworkParameters params, InetAddress addr, int port) {
this(params, addr, port, params.getProtocolVersionNum(NetworkParameters.ProtocolVersion.CURRENT)); this(params, addr, port, params.getProtocolVersionNum(NetworkParameters.ProtocolVersion.CURRENT),
BigInteger.ZERO);
} }
/** /**
@@ -178,38 +179,18 @@ public class PeerAddress extends ChildMessage {
return new InetSocketAddress(getAddr(), getPort()); return new InetSocketAddress(getAddr(), getPort());
} }
public void setAddr(InetAddress addr) {
unCache();
this.addr = addr;
}
public int getPort() { public int getPort() {
return port; return port;
} }
public void setPort(int port) {
unCache();
this.port = port;
}
public BigInteger getServices() { public BigInteger getServices() {
return services; return services;
} }
public void setServices(BigInteger services) {
unCache();
this.services = services;
}
public long getTime() { public long getTime() {
return time; return time;
} }
public void setTime(long time) {
unCache();
this.time = time;
}
@Override @Override
public String toString() { public String toString() {
if (hostname != null) { if (hostname != null) {
@@ -224,7 +205,6 @@ public class PeerAddress extends ChildMessage {
if (o == null || getClass() != o.getClass()) return false; if (o == null || getClass() != o.getClass()) return false;
PeerAddress other = (PeerAddress) o; PeerAddress other = (PeerAddress) o;
return other.addr.equals(addr) && other.port == port && other.time == time && other.services.equals(services); return other.addr.equals(addr) && other.port == port && other.time == time && other.services.equals(services);
//TODO: including services and time could cause same peer to be added multiple times in collections
} }
@Override @Override

View File

@@ -22,6 +22,7 @@ import com.google.common.net.InetAddresses;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.math.BigInteger;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.Locale; import java.util.Locale;
@@ -100,8 +101,8 @@ public class VersionMessage extends Message {
// Note that the Bitcoin Core doesn't do anything with these, and finding out your own external IP address // Note that the Bitcoin Core doesn't do anything with these, and finding out your own external IP address
// is kind of tricky anyway, so we just put nonsense here for now. // is kind of tricky anyway, so we just put nonsense here for now.
InetAddress localhost = InetAddresses.forString("127.0.0.1"); InetAddress localhost = InetAddresses.forString("127.0.0.1");
myAddr = new PeerAddress(params, localhost, params.getPort(), 0); myAddr = new PeerAddress(params, localhost, params.getPort(), 0, BigInteger.ZERO);
theirAddr = new PeerAddress(params, localhost, params.getPort(), 0); theirAddr = new PeerAddress(params, localhost, params.getPort(), 0, BigInteger.ZERO);
subVer = LIBRARY_SUBVER; subVer = LIBRARY_SUBVER;
bestHeight = newBestHeight; bestHeight = newBestHeight;
relayTxesBeforeFilter = true; relayTxesBeforeFilter = true;

View File

@@ -800,8 +800,9 @@ public class WalletProtobufSerializer {
throw new UnreadableWalletException("Peer IP address does not have the right length", e); throw new UnreadableWalletException("Peer IP address does not have the right length", e);
} }
int port = proto.getPort(); int port = proto.getPort();
PeerAddress address = new PeerAddress(params, ip, port); int protocolVersion = params.getProtocolVersionNum(NetworkParameters.ProtocolVersion.CURRENT);
address.setServices(BigInteger.valueOf(proto.getServices())); BigInteger services = BigInteger.valueOf(proto.getServices());
PeerAddress address = new PeerAddress(params, ip, port, protocolVersion, services);
confidence.markBroadcastBy(address); confidence.markBroadcastBy(address);
} }
if (confidenceProto.hasLastBroadcastedAt()) if (confidenceProto.hasLastBroadcastedAt())

View File

@@ -20,6 +20,7 @@ package org.bitcoinj.core;
import org.bitcoinj.params.MainNetParams; import org.bitcoinj.params.MainNetParams;
import org.junit.Test; import org.junit.Test;
import java.math.BigInteger;
import java.net.InetAddress; import java.net.InetAddress;
import static org.bitcoinj.core.Utils.HEX; import static org.bitcoinj.core.Utils.HEX;
@@ -39,7 +40,7 @@ public class PeerAddressTest
@Test @Test
public void testBitcoinSerialize() throws Exception { public void testBitcoinSerialize() throws Exception {
PeerAddress pa = new PeerAddress(MainNetParams.get(), InetAddress.getByName(null), 8333, 0); PeerAddress pa = new PeerAddress(MainNetParams.get(), InetAddress.getByName(null), 8333, 0, BigInteger.ZERO);
assertEquals("000000000000000000000000000000000000ffff7f000001208d", assertEquals("000000000000000000000000000000000000ffff7f000001208d",
Utils.HEX.encode(pa.bitcoinSerialize())); Utils.HEX.encode(pa.bitcoinSerialize()));
} }