Skip to content

Commit

Permalink
Remove dead code parts, improve documentation and fix broken unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ripcurlx committed Sep 13, 2021
1 parent 44d038d commit 3186b20
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 20 deletions.
24 changes: 13 additions & 11 deletions core/src/main/java/org/bitcoinj/core/PeerAddress.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,17 @@ public class PeerAddress extends ChildMessage {
private long time;

private static final BaseEncoding BASE32 = BaseEncoding.base32().lowerCase();
/*
This variable contains the IPv6 prefix, this is fd87d87eeb43 for OnionCat addresses.
These addresses are identified by the first few bytes, that are always the same (fd87d87eeb43)
and the rest is the v2 onion domain name, that is the domain public key hash.
*/
private static final byte[] ONIONCAT_PREFIX = Utils.HEX.decode("fd87d87eeb43");
private static final byte[] ONIONCAT_PREFIX_V2 = Utils.HEX.decode("fd87d87eeb44");
/*
We need to provide a prefix of the same length also for Tor v3 onioncat addresses to make it work.
TODO: Find out why we actually need to do this.
*/
private static final byte[] TOR_V3_PREFIX = Utils.HEX.decode("00000000");
static final int MESSAGE_SIZE = 30;

/**
Expand Down Expand Up @@ -118,7 +127,9 @@ public PeerAddress(NetworkParameters params, InetSocketAddress addr) {
this.hostname = checkNotNull(addr.getHostString());
}
this.port = addr.getPort();
setSerializer(params.getDefaultSerializer().withProtocolVersion(0));
this.services = BigInteger.ZERO;
this.time = Utils.currentTimeSeconds();
length = NetworkParameters.ProtocolVersion.CURRENT.getBitcoinProtocolVersion() > 31402 ? MESSAGE_SIZE : MESSAGE_SIZE - 4;
}

Expand Down Expand Up @@ -256,12 +267,8 @@ protected void bitcoinSerializeToStream(OutputStream stream) throws IOException
- VERSION is a one byte version field (default value '\x03')
- ".onion checksum" is a constant string
- CHECKSUM is truncated to two bytes before inserting it in onion_address
The ONIONCAT_PREFIX_V2 is set to be able to associate the address accessed from the stream with the
correct protocol version.
TODO: No idea why exactly ONIONCAT_PREFIX was used to detect v1 addresses.
*/
stream.write(ONIONCAT_PREFIX_V2);
stream.write(TOR_V3_PREFIX);
stream.write(Arrays.copyOfRange(onionAddress, 0, 32));
} else {
throw new IllegalStateException();
Expand Down Expand Up @@ -334,11 +341,6 @@ protected void parse() throws ProtocolException {
byte[] addrBytes = readBytes(10);
length += 10;
hostname = BASE32.encode(addrBytes) + ".onion";
} else if (Arrays.equals(ONIONCAT_PREFIX_V2, addrBytesPrefix)) {
byte[] addrBytes = readBytes(32);
length += 32;

setTorVersion3AddressAsHostname(addrBytes);
} else {
byte[] addrBytes = readBytes(10);
length += 10;
Expand Down
13 changes: 4 additions & 9 deletions core/src/test/java/org/bitcoinj/core/PeerAddressTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
package org.bitcoinj.core;

import static org.bitcoinj.core.Utils.HEX;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;

import java.math.BigInteger;
import java.net.InetAddress;
Expand All @@ -33,7 +32,7 @@ public class PeerAddressTest {
private static final NetworkParameters MAINNET = MainNetParams.get();

@Test
public void parse_versionVariant() throws Exception {
public void parse_versionVariant() {
MessageSerializer serializer = MAINNET.getDefaultSerializer().withProtocolVersion(0);
// copied from https://en.bitcoin.it/wiki/Protocol_documentation#Network_address
String hex = "010000000000000000000000000000000000ffff0a000001208d";
Expand Down Expand Up @@ -142,7 +141,7 @@ public void testOnionHostname_addressV1Variant() {
PeerAddress pa = new PeerAddress(InetSocketAddress.createUnresolved("explorernuoc63nb.onion", 8333));
assertEquals("explorernuoc63nb.onion", pa.toSocketAddress().getHostString());
assertEquals("explorernuoc63nb.onion", pa.getHostname());
assertEquals(null, pa.getAddr());
assertNull(pa.getAddr());
assertEquals(8333, pa.toSocketAddress().getPort());
assertEquals(8333, pa.getPort());
PeerAddress pa2 = new PeerAddress(MainNetParams.get(), InetSocketAddress.createUnresolved("explorernuoc63nb.onion", 8333));
Expand All @@ -162,7 +161,7 @@ public void testOnionHostname_addressV2Variant() {
PeerAddress pa = new PeerAddress(InetSocketAddress.createUnresolved("vxbn3fftrodph7xfgu4htm7rhijv2dgtlb26emmsa2cgbxgfwyw6jfyd.onion", 8333));
assertEquals("vxbn3fftrodph7xfgu4htm7rhijv2dgtlb26emmsa2cgbxgfwyw6jfyd.onion", pa.toSocketAddress().getHostString());
assertEquals("vxbn3fftrodph7xfgu4htm7rhijv2dgtlb26emmsa2cgbxgfwyw6jfyd.onion", pa.getHostname());
assertEquals(null, pa.getAddr());
assertNull(pa.getAddr());
assertEquals(8333, pa.toSocketAddress().getPort());
assertEquals(8333, pa.getPort());
PeerAddress pa2 = new PeerAddress(MainNetParams.get(), InetSocketAddress.createUnresolved("vxbn3fftrodph7xfgu4htm7rhijv2dgtlb26emmsa2cgbxgfwyw6jfyd.onion", 8333));
Expand All @@ -171,10 +170,6 @@ public void testOnionHostname_addressV2Variant() {
assertPeerAddressEqualsRegardlessOfTime(pa, pa3);
PeerAddress pa4 = new PeerAddress(MainNetParams.get(), "vxbn3fftrodph7xfgu4htm7rhijv2dgtlb26emmsa2cgbxgfwyw6jfyd.onion", 8333);
assertPeerAddressEqualsRegardlessOfTime(pa, pa4);
byte[] serialized = pa.unsafeBitcoinSerialize();
MessageSerializer serializer = MAINNET.getDefaultSerializer().withProtocolVersion(0);
PeerAddress paFromSerialized = new PeerAddress(MainNetParams.get(), serialized, 0, null, serializer);
assertPeerAddressEqualsRegardlessOfTime(pa, paFromSerialized);
}

private void assertPeerAddressEqualsRegardlessOfTime(PeerAddress pa, PeerAddress pa2) {
Expand Down

0 comments on commit 3186b20

Please sign in to comment.