Skip to content

Commit

Permalink
Reimplement EthereumNodeRecord for DNS discovery (#7989)
Browse files Browse the repository at this point in the history
* Reimplement EthereumNodeRecord and remove dependency on tuweni-devp2p
* Refactor EthereumNodeRecord for DNSDaemon
* Update EthereumNodeRecord to use Besu RLP
* additional unit tests
* Convert ENR to Java record
* regenerate equals and hashcode for enr record
---------

Signed-off-by: Usman Saleem <[email protected]>
  • Loading branch information
usmansaleem authored Jan 7, 2025
1 parent ffd593d commit 01126c0
Show file tree
Hide file tree
Showing 14 changed files with 244 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,11 @@ public String getProvider() {
return PROVIDER;
}

@Override
public ECDomainParameters getCurve() {
return curve;
}

/**
* Gets K calculator.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
import org.bouncycastle.crypto.params.ECDomainParameters;
import org.bouncycastle.math.ec.ECPoint;

/** The interface Signature algorithm. */
Expand Down Expand Up @@ -124,6 +125,13 @@ SECPSignature normaliseSignature(
*/
String getCurveName();

/**
* Bouncy castle ECDomainParameters representing the curve.
*
* @return instance of ECDomainParameters
*/
ECDomainParameters getCurve();

/**
* Create secp private key.
*
Expand Down
3 changes: 0 additions & 3 deletions ethereum/p2p/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ dependencies {

implementation 'io.tmio:tuweni-bytes'
implementation 'io.tmio:tuweni-crypto'
implementation('io.tmio:tuweni-devp2p') {
exclude group:'ch.qos.logback', module:'logback-classic'
}
implementation 'io.tmio:tuweni-io'
implementation 'io.tmio:tuweni-rlp'
implementation 'io.tmio:tuweni-units'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import java.util.Optional;

import io.vertx.core.AbstractVerticle;
import org.apache.tuweni.devp2p.EthereumNodeRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

import java.util.List;

import org.apache.tuweni.devp2p.EthereumNodeRecord;

// Adapted from https://github.com/tmio/tuweni and licensed under Apache 2.0
/** Callback listening to updates of the DNS records. */
@FunctionalInterface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.crypto.SECP256K1;
import org.apache.tuweni.devp2p.EthereumNodeRecord;
import org.apache.tuweni.io.Base32;
import org.apache.tuweni.io.Base64URLSafe;
import org.bouncycastle.math.ec.ECPoint;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
import org.apache.tuweni.crypto.SECP256K1;
import org.apache.tuweni.devp2p.EthereumNodeRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
*/
package org.hyperledger.besu.ethereum.p2p.discovery.dns;

import org.apache.tuweni.devp2p.EthereumNodeRecord;

// Adapted from https://github.com/tmio/tuweni and licensed under Apache 2.0
/**
* Reads ENR (Ethereum Node Records) entries passed in from DNS. The visitor may decide to stop the
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/*
* Copyright contributors to Hyperledger Besu.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/

// Adapted from https://github.com/tmio/tuweni and licensed under Apache 2.0
package org.hyperledger.besu.ethereum.p2p.discovery.dns;

import org.hyperledger.besu.crypto.SignatureAlgorithmFactory;
import org.hyperledger.besu.ethereum.rlp.BytesValueRLPInput;

import java.net.InetAddress;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

import org.apache.tuweni.bytes.Bytes;

/**
* A modified implementation of Ethereum Node Record (ENR) that is used by DNSResolver. See <a
* href="https://eips.ethereum.org/EIPS/eip-778">EIP-778</a>
*/
public record EthereumNodeRecord(
Bytes rlp, Bytes publicKey, InetAddress ip, Optional<Integer> tcp, Optional<Integer> udp) {

/**
* Creates an ENR from its serialized form as a RLP list
*
* @param rlp the serialized form of the ENR
* @return the ENR
* @throws IllegalArgumentException if the rlp bytes length is longer than 300 bytes
*/
public static EthereumNodeRecord fromRLP(final Bytes rlp) {
if (rlp.size() > 300) {
throw new IllegalArgumentException("Record too long");
}
var data = new HashMap<String, Bytes>();

// rlp: sig, sequence, k1,v1, k2,v2, k3, [v3, vn]...
var input = new BytesValueRLPInput(rlp, false);
input.enterList();

input.skipNext(); // skip signature
input.skipNext(); // skip sequence

// go through rest of the list
while (!input.isEndOfCurrentList()) {
var key = new String(input.readBytes().toArrayUnsafe(), StandardCharsets.UTF_8);
if (input.nextIsList()) {
// skip list as we currently don't need any of these complex structures
input.skipNext();
} else {
data.put(key, input.readBytes());
}
}

input.leaveList();

var publicKey = initPublicKeyBytes(data);

return new EthereumNodeRecord(rlp, publicKey, initIPAddr(data), initTCP(data), initUDP(data));
}

/**
* Returns the public key of the ENR
*
* @return the public key of the ENR
*/
static Bytes initPublicKeyBytes(final Map<String, Bytes> data) {
var keyBytes = data.get("secp256k1");
if (keyBytes == null) {
throw new IllegalArgumentException("Missing secp256k1 entry in ENR");
}
// convert 33 bytes compressed public key to uncompressed using Bouncy Castle
var curve = SignatureAlgorithmFactory.getInstance().getCurve();
var ecPoint = curve.getCurve().decodePoint(keyBytes.toArrayUnsafe());
// uncompressed public key is 65 bytes, first byte is 0x04.
var encodedPubKey = ecPoint.getEncoded(false);
return Bytes.of(Arrays.copyOfRange(encodedPubKey, 1, encodedPubKey.length));
}

/**
* Returns the InetAddress of the ENR
*
* @return The IP address of the ENR
*/
static InetAddress initIPAddr(final Map<String, Bytes> data) {
var ipBytes = data.get("ip");
if (ipBytes != null) {
try {
return InetAddress.getByAddress(ipBytes.toArrayUnsafe());
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
return InetAddress.getLoopbackAddress();
}

/**
* The TCP port of the ENR
*
* @return the TCP port associated with this ENR
*/
static Optional<Integer> initTCP(final Map<String, Bytes> data) {
var tcpBytes = data.get("tcp");
return tcpBytes != null ? Optional.of(tcpBytes.toInt()) : Optional.empty();
}

/**
* The UDP port of the ENR. If the UDP port is not present, the TCP port is used.
*
* @return the UDP port associated with this ENR
*/
static Optional<Integer> initUDP(final Map<String, Bytes> data) {
var udpBytes = data.get("udp");
return udpBytes != null ? Optional.of(udpBytes.toInt()) : initTCP(data);
}

/**
* @return the ENR as a URI
*/
@Override
public String toString() {
return "enr:" + ip() + ":" + tcp() + "?udp=" + udp();
}

/** Override equals method to compare the RLP bytes */
@Override
public boolean equals(final Object o) {
if (!(o instanceof EthereumNodeRecord that)) {
return false;
}
return Objects.equals(rlp, that.rlp);
}

/** Override hashCode method to use hashCode of the RLP bytes */
@Override
public int hashCode() {
return Objects.hashCode(rlp);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.hyperledger.besu.ethereum.p2p.discovery.VertxPeerDiscoveryAgent;
import org.hyperledger.besu.ethereum.p2p.discovery.dns.DNSDaemon;
import org.hyperledger.besu.ethereum.p2p.discovery.dns.DNSDaemonListener;
import org.hyperledger.besu.ethereum.p2p.discovery.dns.EthereumNodeRecord;
import org.hyperledger.besu.ethereum.p2p.discovery.internal.PeerTable;
import org.hyperledger.besu.ethereum.p2p.peers.DefaultPeerPrivileges;
import org.hyperledger.besu.ethereum.p2p.peers.EnodeURLImpl;
Expand Down Expand Up @@ -82,7 +83,6 @@
import io.vertx.core.ThreadingModel;
import io.vertx.core.Vertx;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.devp2p.EthereumNodeRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -366,9 +366,9 @@ DNSDaemonListener createDaemonListener() {
final EnodeURL enodeURL =
EnodeURLImpl.builder()
.ipAddress(enr.ip())
.nodeId(enr.publicKey().bytes())
.discoveryPort(Optional.ofNullable(enr.udp()))
.listeningPort(Optional.ofNullable(enr.tcp()))
.nodeId(enr.publicKey())
.discoveryPort(enr.udp())
.listeningPort(enr.tcp())
.build();
final DiscoveryPeer peer = DiscoveryPeer.fromEnode(enodeURL);
peers.add(peer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
*/
package org.hyperledger.besu.ethereum.p2p.discovery.dns;

import org.hyperledger.besu.ethereum.p2p.peers.EnodeURLImpl;

import java.security.Security;
import java.util.concurrent.atomic.AtomicInteger;

Expand Down Expand Up @@ -67,10 +69,24 @@ void testDNSDaemon(final Vertx vertx, final VertxTestContext testContext) {
testContext.failNow(
"Expecting 115 records in first pass but got: " + records.size());
}
records.forEach(
enr -> {
try {
// make sure enode url can be built from record
EnodeURLImpl.builder()
.ipAddress(enr.ip())
.nodeId(enr.publicKey())
.discoveryPort(enr.udp())
.listeningPort(enr.tcp())
.build();
} catch (final Exception e) {
testContext.failNow(e);
}
});
checkpoint.flag();
},
0,
0,
1L,
0,
"localhost:" + mockDnsServerVerticle.port());

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright contributors to Besu.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.ethereum.p2p.discovery.dns;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import java.net.InetAddress;
import java.util.Random;

import org.apache.tuweni.bytes.Bytes;
import org.junit.jupiter.api.Test;

class EthereumNodeRecordTest {

@Test
void buildFromRLP() throws Exception {
final Bytes rlp =
Bytes.fromHexString(
"0xf8a3b84033b8a07e5c8e19dc8ac2529354b21a6c09e5516335eb57c383924aa0ca73434c0c65d8625eb05236e172fcc00d80e913506bde5446fb5c55ea2035380c97480a86018d56dc241083657468c7c6849b192ad0808269648276348269708441157e4389736563703235366b31a102a48c4c032f4c2e1b4007dd15b0d7046b60774f6bc38e2f52a8e0361c65e4234284736e6170c08374637082765f8375647082765f");
// method under test
final EthereumNodeRecord enr = EthereumNodeRecord.fromRLP(rlp);
// expected values
final InetAddress expectedIPAddr =
InetAddress.getByAddress(Bytes.fromHexString("0x41157e43").toArrayUnsafe());
final Bytes expectedPublicKey =
Bytes.fromHexString(
"0xa48c4c032f4c2e1b4007dd15b0d7046b60774f6bc38e2f52a8e0361c65e423424520b07898c59a8c9e85c440594ca734f23b7f2b906d5da54676eee6a1d64874");

// assertions
assertThat(enr.ip()).isEqualTo(expectedIPAddr);
assertThat(enr.publicKey()).isEqualTo(expectedPublicKey);
assertThat(enr.tcp()).isNotEmpty().contains(30303);
assertThat(enr.udp()).isNotEmpty().contains(30303);
}

@Test
void buildFromRLPWithSizeGreaterThan300() {
final Bytes rlp = Bytes.random(301, new Random(1L));
assertThatThrownBy(() -> EthereumNodeRecord.fromRLP(rlp))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("Record too long");
}
}
8 changes: 0 additions & 8 deletions gradle/verification-metadata.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2883,14 +2883,6 @@
<sha256 value="a08b5eecfb91563c9d1844d062e544a27eda111b327dc8bd887527dee1235cd0" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="io.tmio" name="tuweni-devp2p" version="2.4.2">
<artifact name="tuweni-devp2p-2.4.2.jar">
<sha256 value="1430eb4456aefae120265a31cf2312dfaaecc74b4457ad7cd808de4d14069bf6" origin="Generated by Gradle"/>
</artifact>
<artifact name="tuweni-devp2p-2.4.2.pom">
<sha256 value="681d98c3b6c5e740d96a1779d1589201a7c7e48553dcaa2192d4d00ee973b9f3" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="io.tmio" name="tuweni-io" version="2.4.2">
<artifact name="tuweni-io-2.4.2.jar">
<sha256 value="67effc78467927c6790ca519bd941d55fbee517eb5a1059961d54d0dc3ab626e" origin="Generated by Gradle"/>
Expand Down
1 change: 0 additions & 1 deletion platform/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ dependencies {
api 'io.tmio:tuweni-config:2.4.2'
api 'io.tmio:tuweni-concurrent:2.4.2'
api 'io.tmio:tuweni-crypto:2.4.2'
api 'io.tmio:tuweni-devp2p:2.4.2'
api 'io.tmio:tuweni-io:2.4.2'
api 'io.tmio:tuweni-net:2.4.2'
api 'io.tmio:tuweni-rlp:2.4.2'
Expand Down

0 comments on commit 01126c0

Please sign in to comment.