PeerAddress: make direct subclass of Message

It doesn't need a parent.
This commit is contained in:
Andreas Schildbach 2023-03-30 00:40:35 +02:00
parent 32ee59f7bb
commit 1a007c1182
8 changed files with 16 additions and 29 deletions

View File

@ -48,7 +48,6 @@ public abstract class AddressMessage extends Message {
public void removeAddress(int index) {
unCache();
PeerAddress address = addresses.remove(index);
address.setParent(null);
}
/**

View File

@ -57,7 +57,7 @@ public class AddressV1Message extends AddressMessage {
addresses = new ArrayList<>(numAddresses);
MessageSerializer serializer = this.serializer.withProtocolVersion(1);
for (int i = 0; i < numAddresses; i++) {
PeerAddress addr = new PeerAddress(params, payload, this, serializer);
PeerAddress addr = new PeerAddress(params, payload, serializer);
addresses.add(addr);
}
}
@ -67,8 +67,6 @@ public class AddressV1Message extends AddressMessage {
if (protocolVersion != 1)
throw new IllegalStateException("invalid protocolVersion: " + protocolVersion);
unCache();
address.setParent(this);
addresses.add(address);
}

View File

@ -57,7 +57,7 @@ public class AddressV2Message extends AddressMessage {
addresses = new ArrayList<>(numAddresses);
MessageSerializer serializer = this.serializer.withProtocolVersion(2);
for (int i = 0; i < numAddresses; i++) {
PeerAddress addr = new PeerAddress(params, payload, this, serializer);
PeerAddress addr = new PeerAddress(params, payload, serializer);
addresses.add(addr);
}
}
@ -67,8 +67,6 @@ public class AddressV2Message extends AddressMessage {
if (protocolVersion != 2)
throw new IllegalStateException("invalid protocolVersion: " + protocolVersion);
unCache();
address.setParent(this);
addresses.add(address);
}

View File

@ -50,7 +50,7 @@ import java.util.stream.Stream;
*
* <p>Instances of this class are not safe for use by multiple threads.</p>
*/
public class PeerAddress extends ChildMessage {
public class PeerAddress extends Message {
private InetAddress addr; // Used for IPV4, IPV6, null otherwise or if not-yet-parsed
private String hostname; // Used for (.onion addresses) TORV2, TORV3, null otherwise or if not-yet-parsed
private int port;
@ -89,8 +89,8 @@ public class PeerAddress extends ChildMessage {
* @param serializer the serializer to use for this message.
* @throws ProtocolException
*/
public PeerAddress(NetworkParameters params, ByteBuffer payload, Message parent, MessageSerializer serializer) throws ProtocolException {
super(params, payload, parent, serializer);
public PeerAddress(NetworkParameters params, ByteBuffer payload, MessageSerializer serializer) throws ProtocolException {
super(params, payload, serializer);
}
/**

View File

@ -1493,7 +1493,6 @@ public class PeerGroup implements TransactionBroadcaster {
ver.bestHeight = chain == null ? 0 : chain.getBestChainHeight();
ver.time = TimeUtils.currentTime().truncatedTo(ChronoUnit.SECONDS);
ver.receivingAddr = address;
ver.receivingAddr.setParent(ver);
Peer peer = createPeer(address, ver);
peer.addConnectedEventListener(Threading.SAME_THREAD, startupListener);

View File

@ -105,9 +105,7 @@ public class VersionMessage extends Message {
InetAddress localhost = InetAddresses.forString("127.0.0.1");
MessageSerializer serializer = this.serializer.withProtocolVersion(0);
receivingAddr = new PeerAddress(params, localhost, params.getPort(), Services.none(), serializer);
receivingAddr.setParent(this);
fromAddr = new PeerAddress(params, localhost, params.getPort(), Services.none(), serializer);
fromAddr.setParent(this);
subVer = LIBRARY_SUBVER;
bestHeight = newBestHeight;
relayTxesBeforeFilter = true;
@ -118,9 +116,9 @@ public class VersionMessage extends Message {
clientVersion = (int) ByteUtils.readUint32(payload);
localServices = Services.read(payload);
time = Instant.ofEpochSecond(ByteUtils.readInt64(payload));
receivingAddr = new PeerAddress(params, payload, this, serializer.withProtocolVersion(0));
receivingAddr = new PeerAddress(params, payload, serializer.withProtocolVersion(0));
if (clientVersion >= 106) {
fromAddr = new PeerAddress(params, payload, this, serializer.withProtocolVersion(0));
fromAddr = new PeerAddress(params, payload, serializer.withProtocolVersion(0));
// uint64 localHostNonce (random data)
// We don't care about the localhost nonce. It's used to detect connecting back to yourself in cases where
// there are NATs and proxies in the way. However we don't listen for inbound connections so it's

View File

@ -45,7 +45,7 @@ public class PeerAddressTest {
public void equalsContract() {
EqualsVerifier.forClass(PeerAddress.class)
.suppress(Warning.NONFINAL_FIELDS)
.withIgnoredFields("time", "parent", "params", "payload", "serializer")
.withIgnoredFields("time", "params", "payload", "serializer")
.usingGetClass()
.verify();
}
@ -55,7 +55,7 @@ public class PeerAddressTest {
MessageSerializer serializer = MAINNET.getDefaultSerializer().withProtocolVersion(0);
// copied from https://en.bitcoin.it/wiki/Protocol_documentation#Network_address
String hex = "010000000000000000000000000000000000ffff0a000001208d";
PeerAddress pa = new PeerAddress(MAINNET, ByteBuffer.wrap(ByteUtils.parseHex(hex)), null,
PeerAddress pa = new PeerAddress(MAINNET, ByteBuffer.wrap(ByteUtils.parseHex(hex)),
serializer);
assertEquals(Services.NODE_NETWORK, pa.getServices().bits());
assertEquals("10.0.0.1", pa.getAddr().getHostAddress());
@ -77,7 +77,7 @@ public class PeerAddressTest {
PeerAddress pa = new PeerAddress(MAINNET, InetAddress.getByName("1.2.3.4"), 1234, Services.none(),
serializer);
byte[] serialized = pa.bitcoinSerialize();
PeerAddress pa2 = new PeerAddress(MAINNET, ByteBuffer.wrap(serialized), null, serializer);
PeerAddress pa2 = new PeerAddress(MAINNET, ByteBuffer.wrap(serialized), serializer);
assertEquals("1.2.3.4", pa2.getAddr().getHostAddress());
assertEquals(1234, pa2.getPort());
assertEquals(Services.none(), pa2.getServices());
@ -91,7 +91,7 @@ public class PeerAddressTest {
PeerAddress pa = new PeerAddress(MAINNET, InetAddress.getByName("1.2.3.4"), 1234, Services.none(),
serializer);
byte[] serialized = pa.bitcoinSerialize();
PeerAddress pa2 = new PeerAddress(MAINNET, ByteBuffer.wrap(serialized), null, serializer);
PeerAddress pa2 = new PeerAddress(MAINNET, ByteBuffer.wrap(serialized), serializer);
assertEquals("1.2.3.4", pa2.getAddr().getHostAddress());
assertEquals(1234, pa2.getPort());
assertEquals(Services.none(), pa2.getServices());
@ -104,7 +104,7 @@ public class PeerAddressTest {
PeerAddress pa = new PeerAddress(MAINNET, InetAddress.getByName("1.2.3.4"), 1234, Services.none(),
serializer);
byte[] serialized = pa.bitcoinSerialize();
PeerAddress pa2 = new PeerAddress(MAINNET, ByteBuffer.wrap(serialized), null, serializer);
PeerAddress pa2 = new PeerAddress(MAINNET, ByteBuffer.wrap(serialized), serializer);
assertEquals("1.2.3.4", pa2.getAddr().getHostAddress());
assertEquals(1234, pa2.getPort());
assertEquals(Services.none(), pa2.getServices());
@ -118,7 +118,7 @@ public class PeerAddressTest {
PeerAddress pa = new PeerAddress(MAINNET, InetAddress.getByName("2001:db8:85a3:0:0:8a2e:370:7334"), 1234,
Services.none(), serializer);
byte[] serialized = pa.bitcoinSerialize();
PeerAddress pa2 = new PeerAddress(MAINNET, ByteBuffer.wrap(serialized), null, serializer);
PeerAddress pa2 = new PeerAddress(MAINNET, ByteBuffer.wrap(serialized), serializer);
assertEquals("2001:db8:85a3:0:0:8a2e:370:7334", pa2.getAddr().getHostAddress());
assertEquals(1234, pa2.getPort());
assertEquals(Services.none(), pa2.getServices());
@ -132,7 +132,7 @@ public class PeerAddressTest {
PeerAddress pa = new PeerAddress(MAINNET, InetAddress.getByName("2001:db8:85a3:0:0:8a2e:370:7334"), 1234,
Services.none(), serializer);
byte[] serialized = pa.bitcoinSerialize();
PeerAddress pa2 = new PeerAddress(MAINNET, ByteBuffer.wrap(serialized), null, serializer);
PeerAddress pa2 = new PeerAddress(MAINNET, ByteBuffer.wrap(serialized), serializer);
assertEquals("2001:db8:85a3:0:0:8a2e:370:7334", pa2.getAddr().getHostAddress());
assertEquals(1234, pa2.getPort());
assertEquals(Services.none(), pa2.getServices());
@ -145,8 +145,7 @@ public class PeerAddressTest {
PeerAddress pa = new PeerAddress(MAINNET, InetAddress.getByName("2001:db8:85a3:0:0:8a2e:370:7334"), 1234,
Services.none(), serializer);
byte[] serialized = pa.bitcoinSerialize();
PeerAddress pa2 = new PeerAddress(MAINNET, ByteBuffer.wrap(serialized), null,
serializer);
PeerAddress pa2 = new PeerAddress(MAINNET, ByteBuffer.wrap(serialized), serializer);
assertEquals("2001:db8:85a3:0:0:8a2e:370:7334", pa2.getAddr().getHostAddress());
assertEquals(1234, pa2.getPort());
assertEquals(Services.none(), pa2.getServices());
@ -157,7 +156,7 @@ public class PeerAddressTest {
@Parameters(method = "deserializeToStringValues")
public void deserializeToString(int version, String expectedToString, String hex) {
MessageSerializer serializer = MAINNET.getDefaultSerializer().withProtocolVersion(version);
PeerAddress pa = new PeerAddress(MAINNET, ByteBuffer.wrap(ByteUtils.parseHex(hex)), null, serializer);
PeerAddress pa = new PeerAddress(MAINNET, ByteBuffer.wrap(ByteUtils.parseHex(hex)), serializer);
assertEquals(expectedToString, pa.toString());
}

View File

@ -77,9 +77,7 @@ public class VersionMessageTest {
ver.clientVersion = NetworkParameters.ProtocolVersion.CURRENT.getBitcoinProtocolVersion();
ver.localServices = Services.of(1);
ver.fromAddr = new PeerAddress(TESTNET, InetAddress.getByName("1.2.3.4"), 3888);
ver.fromAddr.setParent(ver);
ver.receivingAddr = new PeerAddress(TESTNET, InetAddress.getByName("4.3.2.1"), 8333);
ver.receivingAddr.setParent(ver);
byte[] serialized = ver.bitcoinSerialize();
VersionMessage ver2 = new VersionMessage(TESTNET, ByteBuffer.wrap(serialized));
assertEquals(1234, ver2.bestHeight);
@ -101,9 +99,7 @@ public class VersionMessageTest {
ver.clientVersion = NetworkParameters.ProtocolVersion.CURRENT.getBitcoinProtocolVersion();
ver.localServices = Services.of(1);
ver.fromAddr = new PeerAddress(TESTNET, InetAddress.getByName("2001:db8:85a3:0:0:8a2e:370:7334"), 3888);
ver.fromAddr.setParent(ver);
ver.receivingAddr = new PeerAddress(TESTNET, InetAddress.getByName("2002:db8:85a3:0:0:8a2e:370:7335"), 8333);
ver.receivingAddr.setParent(ver);
byte[] serialized = ver.bitcoinSerialize();
VersionMessage ver2 = new VersionMessage(TESTNET, ByteBuffer.wrap(serialized));
assertEquals(1234, ver2.bestHeight);