Search in sources :

Example 1 with NodeRecord

use of org.ethereum.beacon.discovery.schema.NodeRecord in project teku by ConsenSys.

the class NodeRecordConverterTest method shouldConvertRealEnrToDiscoveryPeer.

@Test
public void shouldConvertRealEnrToDiscoveryPeer() throws Exception {
    final String enr = "-Iu4QMmfe-EkDnVX6k5i2LFTiDQ-q4-Cb1I01iRI-wbCD_r4Z8eujNCgZDmZXb1ZOPi1LfJaNx3Bd0QUK9wqBjwUXJQBgmlkgnY0gmlwhH8AAAGJc2VjcDI1NmsxoQO4btn3R6f6mZY_OeOxdrRenoYxCKLRReo6TnbY0JNRlIN0Y3CCIyiDdWRwgiMo";
    final NodeRecord nodeRecord = NodeRecordFactory.DEFAULT.fromBase64(enr);
    final DiscoveryPeer expectedPeer = new DiscoveryPeer(Bytes.fromHexString("0x03B86ED9F747A7FA99963F39E3B176B45E9E863108A2D145EA3A4E76D8D0935194"), new InetSocketAddress(InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 }), 9000), Optional.empty(), ATTNETS, SYNCNETS);
    assertThat(CONVERTER.convertToDiscoveryPeer(nodeRecord, SCHEMA_DEFINITIONS)).contains(expectedPeer);
}
Also used : NodeRecord(org.ethereum.beacon.discovery.schema.NodeRecord) DiscoveryPeer(tech.pegasys.teku.networking.p2p.discovery.DiscoveryPeer) InetSocketAddress(java.net.InetSocketAddress) Test(org.junit.jupiter.api.Test)

Example 2 with NodeRecord

use of org.ethereum.beacon.discovery.schema.NodeRecord in project besu by hyperledger.

the class PeerDiscoveryAgentTest method testNodeRecordCreated.

@Test
public void testNodeRecordCreated() {
    KeyPair keyPair = SIGNATURE_ALGORITHM.get().createKeyPair(SIGNATURE_ALGORITHM.get().createPrivateKey(Bytes32.fromHexString("0xb71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")));
    final MockPeerDiscoveryAgent agent = helper.startDiscoveryAgent(helper.agentBuilder().nodeKey(NodeKeyUtils.createFrom(keyPair)).advertisedHost("127.0.0.1").bindPort(30303));
    assertThat(agent.getAdvertisedPeer().isPresent()).isTrue();
    assertThat(agent.getAdvertisedPeer().get().getNodeRecord().isPresent()).isTrue();
    final NodeRecord nodeRecord = agent.getAdvertisedPeer().get().getNodeRecord().get();
    assertThat(nodeRecord.getNodeId()).isNotNull();
    assertThat(nodeRecord.getIdentityScheme()).isNotNull();
    assertThat(nodeRecord.getSignature()).isNotNull();
    assertThat(nodeRecord.getSeq()).isNotNull();
    assertThat(nodeRecord.get("eth")).isNotNull();
    assertThat(nodeRecord.get("eth")).isEqualTo(Collections.singletonList(Collections.singletonList(Bytes.EMPTY)));
    assertThat(nodeRecord.asEnr()).isEqualTo("enr:-JC4QOfroMOa1sB6ajxcBKdWn3s9S4Ojl33pbRm72S5FnCwyZfskmjkJvZznQaWNTrOHrnKxw1R9xMm9rl" + "EGOcsOyscBg2V0aMLBgIJpZIJ2NIJpcIR_AAABiXNlY3AyNTZrMaEDymNMrg1JrLQB2KTGtv6MVbcNEV" + "v0AHacwUAPMljNMTiDdGNwAoN1ZHCCdl8");
}
Also used : NodeRecord(org.ethereum.beacon.discovery.schema.NodeRecord) KeyPair(org.hyperledger.besu.crypto.KeyPair) MockPeerDiscoveryAgent(org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent) Test(org.junit.Test)

Example 3 with NodeRecord

use of org.ethereum.beacon.discovery.schema.NodeRecord in project besu by hyperledger.

the class ENRResponsePacketDataTest method readFrom.

@Test
public void readFrom() {
    final Bytes requestHash = Bytes.fromHexStringLenient("0x1234");
    final Bytes nodeId = Bytes.fromHexString("a448f24c6d18e575453db13171562b71999873db5b286df957af199ec94617f7");
    final Bytes privateKey = Bytes.fromHexString("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291");
    NodeRecord nodeRecord = NodeRecordFactory.DEFAULT.createFromValues(UInt64.ONE, new EnrField(EnrField.ID, IdentitySchema.V4), new EnrField(EnrField.IP_V4, Bytes.fromHexString("0x7F000001")), new EnrField(EnrField.UDP, 30303), new EnrField(EnrField.PKEY_SECP256K1, Functions.derivePublicKeyFromPrivate(privateKey)));
    nodeRecord.sign(privateKey);
    assertThat(nodeRecord.getNodeId()).isEqualTo(nodeId);
    assertThat(nodeRecord.asEnr()).isEqualTo("enr:-IS4QHCYrYZbAKWCBRlAy5zzaDZXJBGkcnh4MHcBFZntXNFrdvJjX04jRzjzCBOonrkTfj499SZuOh8R33L" + "s8RRcy5wBgmlkgnY0gmlwhH8AAAGJc2VjcDI1NmsxoQPKY0yuDUmstAHYpMa2_oxVtw0RW_QAdpzBQA8y" + "WM0xOIN1ZHCCdl8");
    BytesValueRLPOutput out = new BytesValueRLPOutput();
    out.startList();
    out.writeBytes(requestHash);
    out.writeRLPBytes(nodeRecord.serialize());
    out.endList();
    final Bytes encoded = out.encoded();
    final ENRResponsePacketData deserialized = ENRResponsePacketData.readFrom(RLP.input(encoded));
    assertThat(deserialized.getRequestHash()).isEqualTo(requestHash);
    assertThat(deserialized.getEnr()).isEqualTo(nodeRecord);
}
Also used : Bytes(org.apache.tuweni.bytes.Bytes) NodeRecord(org.ethereum.beacon.discovery.schema.NodeRecord) EnrField(org.ethereum.beacon.discovery.schema.EnrField) BytesValueRLPOutput(org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput) Test(org.junit.Test)

Example 4 with NodeRecord

use of org.ethereum.beacon.discovery.schema.NodeRecord in project besu by hyperledger.

the class ENRResponsePacketDataTest method readFrom_invalidSignature.

@Test
public void readFrom_invalidSignature() {
    final Bytes requestHash = Bytes.fromHexStringLenient("0x1234");
    final Bytes privateKey = Bytes.fromHexString("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f292");
    NodeRecord nodeRecord = NodeRecordFactory.DEFAULT.createFromValues(UInt64.ONE, new EnrField(EnrField.ID, IdentitySchema.V4), new EnrField(EnrField.IP_V4, Bytes.fromHexString("0x7F000001")), new EnrField(EnrField.UDP, 30303), new EnrField(EnrField.PKEY_SECP256K1, Functions.derivePublicKeyFromPrivate(privateKey)));
    nodeRecord.sign(privateKey);
    nodeRecord.set(EnrField.UDP, 1234);
    BytesValueRLPOutput out = new BytesValueRLPOutput();
    out.startList();
    out.writeBytes(requestHash);
    out.writeRLPBytes(nodeRecord.serialize());
    out.endList();
    final Bytes encoded = out.encoded();
    final ENRResponsePacketData deserialized = ENRResponsePacketData.readFrom(RLP.input(encoded));
    assertThat(deserialized.getRequestHash()).isEqualTo(requestHash);
    assertThat(deserialized.getEnr()).isEqualTo(nodeRecord);
    assertThat(deserialized.getEnr().isValid()).isFalse();
}
Also used : Bytes(org.apache.tuweni.bytes.Bytes) NodeRecord(org.ethereum.beacon.discovery.schema.NodeRecord) EnrField(org.ethereum.beacon.discovery.schema.EnrField) BytesValueRLPOutput(org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput) Test(org.junit.Test)

Example 5 with NodeRecord

use of org.ethereum.beacon.discovery.schema.NodeRecord in project besu by hyperledger.

the class ENRResponsePacketDataTest method writeTo.

@Test
public void writeTo() {
    final Bytes requestHash = Bytes.fromHexStringLenient("0x1234");
    final Bytes nodeId = Bytes.fromHexString("a448f24c6d18e575453db13171562b71999873db5b286df957af199ec94617f7");
    final Bytes privateKey = Bytes.fromHexString("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291");
    NodeRecord nodeRecord = NodeRecordFactory.DEFAULT.createFromValues(UInt64.ONE, new EnrField(EnrField.ID, IdentitySchema.V4), new EnrField(EnrField.IP_V4, Bytes.fromHexString("0x7F000001")), new EnrField(EnrField.UDP, 30303), new EnrField(EnrField.PKEY_SECP256K1, Functions.derivePublicKeyFromPrivate(privateKey)));
    nodeRecord.sign(privateKey);
    assertThat(nodeRecord.getNodeId()).isEqualTo(nodeId);
    assertThat(nodeRecord.asEnr()).isEqualTo("enr:-IS4QHCYrYZbAKWCBRlAy5zzaDZXJBGkcnh4MHcBFZntXNFrdvJjX04jRzjzCBOonrkTfj499SZuOh8R33L" + "s8RRcy5wBgmlkgnY0gmlwhH8AAAGJc2VjcDI1NmsxoQPKY0yuDUmstAHYpMa2_oxVtw0RW_QAdpzBQA8y" + "WM0xOIN1ZHCCdl8");
    BytesValueRLPOutput out = new BytesValueRLPOutput();
    final ENRResponsePacketData packet = ENRResponsePacketData.create(requestHash, nodeRecord);
    packet.writeTo(out);
    final Bytes encoded = out.encoded();
    final ENRResponsePacketData deserialized = ENRResponsePacketData.readFrom(RLP.input(encoded));
    assertThat(deserialized.getRequestHash()).isEqualTo(requestHash);
    assertThat(deserialized.getEnr()).isEqualTo(nodeRecord);
}
Also used : Bytes(org.apache.tuweni.bytes.Bytes) NodeRecord(org.ethereum.beacon.discovery.schema.NodeRecord) EnrField(org.ethereum.beacon.discovery.schema.EnrField) BytesValueRLPOutput(org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput) Test(org.junit.Test)

Aggregations

NodeRecord (org.ethereum.beacon.discovery.schema.NodeRecord)15 Bytes (org.apache.tuweni.bytes.Bytes)10 Test (org.junit.Test)9 EnrField (org.ethereum.beacon.discovery.schema.EnrField)7 BytesValueRLPOutput (org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput)4 InetSocketAddress (java.net.InetSocketAddress)3 List (java.util.List)3 Optional (java.util.Optional)3 Stream (java.util.stream.Stream)3 MetricsSystem (org.hyperledger.besu.plugin.services.MetricsSystem)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)2 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)2 Collection (java.util.Collection)2 Collections (java.util.Collections)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 Supplier (java.util.function.Supplier)2 Collectors (java.util.stream.Collectors)2 NodeRecordFactory (org.ethereum.beacon.discovery.schema.NodeRecordFactory)2 NodeKey (org.hyperledger.besu.crypto.NodeKey)2