-
Notifications
You must be signed in to change notification settings - Fork 867
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reimplement EthereumNodeRecord for DNS discovery (#7989)
* 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
1 parent
ffd593d
commit 01126c0
Showing
14 changed files
with
244 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
154 changes: 154 additions & 0 deletions
154
...p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/discovery/dns/EthereumNodeRecord.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
...src/test/java/org/hyperledger/besu/ethereum/p2p/discovery/dns/EthereumNodeRecordTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters