From 1a007c1182a8b742482790f3f6928157addc804f Mon Sep 17 00:00:00 2001 From: Andreas Schildbach Date: Thu, 30 Mar 2023 00:40:35 +0200 Subject: [PATCH] PeerAddress: make direct subclass of `Message` It doesn't need a parent. --- .../org/bitcoinj/core/AddressMessage.java | 1 - .../org/bitcoinj/core/AddressV1Message.java | 4 +--- .../org/bitcoinj/core/AddressV2Message.java | 4 +--- .../java/org/bitcoinj/core/PeerAddress.java | 6 +++--- .../java/org/bitcoinj/core/PeerGroup.java | 1 - .../org/bitcoinj/core/VersionMessage.java | 6 ++---- .../org/bitcoinj/core/PeerAddressTest.java | 19 +++++++++---------- .../org/bitcoinj/core/VersionMessageTest.java | 4 ---- 8 files changed, 16 insertions(+), 29 deletions(-) diff --git a/core/src/main/java/org/bitcoinj/core/AddressMessage.java b/core/src/main/java/org/bitcoinj/core/AddressMessage.java index dae4f759c..4006c2313 100644 --- a/core/src/main/java/org/bitcoinj/core/AddressMessage.java +++ b/core/src/main/java/org/bitcoinj/core/AddressMessage.java @@ -48,7 +48,6 @@ public abstract class AddressMessage extends Message { public void removeAddress(int index) { unCache(); PeerAddress address = addresses.remove(index); - address.setParent(null); } /** diff --git a/core/src/main/java/org/bitcoinj/core/AddressV1Message.java b/core/src/main/java/org/bitcoinj/core/AddressV1Message.java index a4212a8ef..77bec9f9e 100644 --- a/core/src/main/java/org/bitcoinj/core/AddressV1Message.java +++ b/core/src/main/java/org/bitcoinj/core/AddressV1Message.java @@ -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); } diff --git a/core/src/main/java/org/bitcoinj/core/AddressV2Message.java b/core/src/main/java/org/bitcoinj/core/AddressV2Message.java index a64386d12..8bdbb119e 100644 --- a/core/src/main/java/org/bitcoinj/core/AddressV2Message.java +++ b/core/src/main/java/org/bitcoinj/core/AddressV2Message.java @@ -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); } diff --git a/core/src/main/java/org/bitcoinj/core/PeerAddress.java b/core/src/main/java/org/bitcoinj/core/PeerAddress.java index 44d5e9e2f..aa7b6aafd 100644 --- a/core/src/main/java/org/bitcoinj/core/PeerAddress.java +++ b/core/src/main/java/org/bitcoinj/core/PeerAddress.java @@ -50,7 +50,7 @@ import java.util.stream.Stream; * *

Instances of this class are not safe for use by multiple threads.

*/ -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); } /** diff --git a/core/src/main/java/org/bitcoinj/core/PeerGroup.java b/core/src/main/java/org/bitcoinj/core/PeerGroup.java index f4c8f1a19..306c8542b 100644 --- a/core/src/main/java/org/bitcoinj/core/PeerGroup.java +++ b/core/src/main/java/org/bitcoinj/core/PeerGroup.java @@ -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); diff --git a/core/src/main/java/org/bitcoinj/core/VersionMessage.java b/core/src/main/java/org/bitcoinj/core/VersionMessage.java index e3878e16a..c9d2413ae 100644 --- a/core/src/main/java/org/bitcoinj/core/VersionMessage.java +++ b/core/src/main/java/org/bitcoinj/core/VersionMessage.java @@ -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 diff --git a/core/src/test/java/org/bitcoinj/core/PeerAddressTest.java b/core/src/test/java/org/bitcoinj/core/PeerAddressTest.java index c57b8cfb2..1fab679b6 100644 --- a/core/src/test/java/org/bitcoinj/core/PeerAddressTest.java +++ b/core/src/test/java/org/bitcoinj/core/PeerAddressTest.java @@ -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()); } diff --git a/core/src/test/java/org/bitcoinj/core/VersionMessageTest.java b/core/src/test/java/org/bitcoinj/core/VersionMessageTest.java index a733390fa..04e8c31db 100644 --- a/core/src/test/java/org/bitcoinj/core/VersionMessageTest.java +++ b/core/src/test/java/org/bitcoinj/core/VersionMessageTest.java @@ -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);