Search in sources :

Example 6 with UInt64

use of org.apache.tuweni.units.bigints.UInt64 in project besu by hyperledger.

the class PingPacketDataTest method serializeDeserialize.

@Test
public void serializeDeserialize() {
    final long currentTimeSec = Instant.now().getEpochSecond();
    final Endpoint from = new Endpoint("127.0.0.1", 30303, Optional.of(30303));
    final Endpoint to = new Endpoint("127.0.0.2", 30303, Optional.empty());
    final UInt64 enrSeq = UInt64.ONE;
    final PingPacketData packet = PingPacketData.create(Optional.of(from), to, enrSeq);
    final Bytes serialized = RLP.encode(packet::writeTo);
    final PingPacketData deserialized = PingPacketData.readFrom(RLP.input(serialized));
    assertThat(deserialized.getFrom()).contains(from);
    assertThat(deserialized.getTo()).isEqualTo(to);
    assertThat(deserialized.getExpiration()).isGreaterThan(currentTimeSec);
    assertThat(deserialized.getEnrSeq().isPresent()).isTrue();
    assertThat(deserialized.getEnrSeq().get()).isEqualTo(enrSeq);
}
Also used : Bytes(org.apache.tuweni.bytes.Bytes) Endpoint(org.hyperledger.besu.ethereum.p2p.discovery.Endpoint) UInt64(org.apache.tuweni.units.bigints.UInt64) Test(org.junit.Test)

Example 7 with UInt64

use of org.apache.tuweni.units.bigints.UInt64 in project besu by hyperledger.

the class PongPacketDataTest method serializeDeserialize.

@Test
public void serializeDeserialize() {
    final long currentTimeSec = Instant.now().getEpochSecond();
    final Endpoint to = new Endpoint("127.0.0.2", 30303, Optional.empty());
    final Bytes32 hash = Bytes32.fromHexStringLenient("0x1234");
    final UInt64 enrSeq = UInt64.ONE;
    final PongPacketData packet = PongPacketData.create(to, hash, enrSeq);
    final Bytes serialized = RLP.encode(packet::writeTo);
    final PongPacketData deserialized = PongPacketData.readFrom(RLP.input(serialized));
    assertThat(deserialized.getTo()).isEqualTo(to);
    assertThat(deserialized.getPingHash()).isEqualTo(hash);
    assertThat(deserialized.getExpiration()).isGreaterThan(currentTimeSec);
    assertThat(deserialized.getEnrSeq().isPresent()).isTrue();
    assertThat(deserialized.getEnrSeq().get()).isEqualTo(enrSeq);
}
Also used : Bytes(org.apache.tuweni.bytes.Bytes) Endpoint(org.hyperledger.besu.ethereum.p2p.discovery.Endpoint) UInt64(org.apache.tuweni.units.bigints.UInt64) Bytes32(org.apache.tuweni.bytes.Bytes32) Test(org.junit.Test)

Example 8 with UInt64

use of org.apache.tuweni.units.bigints.UInt64 in project besu by hyperledger.

the class PongPacketDataTest method legacyHandlesScalar.

@Test
public void legacyHandlesScalar() {
    final Endpoint to = new Endpoint("127.0.0.2", 30303, Optional.empty());
    final UInt64 enrSeq = UInt64.MAX_VALUE;
    final Bytes32 hash = Bytes32.fromHexStringLenient("0x1234");
    final PongPacketData pong = PongPacketData.create(to, hash, enrSeq);
    final BytesValueRLPOutput out = new BytesValueRLPOutput();
    pong.writeTo(out);
    final PongPacketData legacyPong = PongPacketData.legacyReadFrom(RLP.input(out.encoded()));
    assertThat(legacyPong.getTo()).isEqualTo(to);
    assertThat(legacyPong.getPingHash()).isEqualTo(hash);
    assertThat(legacyPong.getEnrSeq().isPresent()).isTrue();
    assertThat(legacyPong.getEnrSeq().get()).isEqualTo(enrSeq);
}
Also used : Endpoint(org.hyperledger.besu.ethereum.p2p.discovery.Endpoint) UInt64(org.apache.tuweni.units.bigints.UInt64) Bytes32(org.apache.tuweni.bytes.Bytes32) BytesValueRLPOutput(org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput) Test(org.junit.Test)

Example 9 with UInt64

use of org.apache.tuweni.units.bigints.UInt64 in project besu by hyperledger.

the class PeerDiscoveryAgent method updateNodeRecord.

public void updateNodeRecord() {
    if (!config.isActive()) {
        return;
    }
    final KeyValueStorage keyValueStorage = storageProvider.getStorageBySegmentIdentifier(KeyValueSegmentIdentifier.BLOCKCHAIN);
    final NodeRecordFactory nodeRecordFactory = NodeRecordFactory.DEFAULT;
    final Optional<NodeRecord> existingNodeRecord = keyValueStorage.get(Bytes.of(SEQ_NO_STORE_KEY.getBytes(UTF_8)).toArray()).map(Bytes::of).map(nodeRecordFactory::fromBytes);
    final Bytes addressBytes = Bytes.of(InetAddresses.forString(advertisedAddress).getAddress());
    final Optional<EnodeURL> maybeEnodeURL = localNode.map(DiscoveryPeer::getEnodeURL);
    final Integer discoveryPort = maybeEnodeURL.flatMap(EnodeURL::getDiscoveryPort).orElse(0);
    final Integer listeningPort = maybeEnodeURL.flatMap(EnodeURL::getListeningPort).orElse(0);
    final String forkIdEnrField = "eth";
    final NodeRecord newNodeRecord = existingNodeRecord.filter(nodeRecord -> id.equals(nodeRecord.get(EnrField.PKEY_SECP256K1)) && addressBytes.equals(nodeRecord.get(EnrField.IP_V4)) && discoveryPort.equals(nodeRecord.get(EnrField.UDP)) && listeningPort.equals(nodeRecord.get(EnrField.TCP)) && forkIdSupplier.get().equals(nodeRecord.get(forkIdEnrField))).orElseGet(() -> {
        final UInt64 sequenceNumber = existingNodeRecord.map(NodeRecord::getSeq).orElse(UInt64.ZERO).add(1);
        final NodeRecord nodeRecord = nodeRecordFactory.createFromValues(sequenceNumber, new EnrField(EnrField.ID, IdentitySchema.V4), new EnrField(SIGNATURE_ALGORITHM.get().getCurveName(), SIGNATURE_ALGORITHM.get().compressPublicKey(SIGNATURE_ALGORITHM.get().createPublicKey(id))), new EnrField(EnrField.IP_V4, addressBytes), new EnrField(EnrField.TCP, listeningPort), new EnrField(EnrField.UDP, discoveryPort), new EnrField(forkIdEnrField, Collections.singletonList(forkIdSupplier.get())));
        nodeRecord.setSignature(nodeKey.sign(Hash.keccak256(nodeRecord.serializeNoSignature())).encodedBytes().slice(0, 64));
        LOG.info("Writing node record to disk. {}", nodeRecord);
        final KeyValueStorageTransaction keyValueStorageTransaction = keyValueStorage.startTransaction();
        keyValueStorageTransaction.put(Bytes.wrap(SEQ_NO_STORE_KEY.getBytes(UTF_8)).toArray(), nodeRecord.serialize().toArray());
        keyValueStorageTransaction.commit();
        return nodeRecord;
    });
    localNode.orElseThrow(() -> new IllegalStateException("Local node should be set here")).setNodeRecord(newNodeRecord);
}
Also used : EnodeURLImpl(org.hyperledger.besu.ethereum.p2p.peers.EnodeURLImpl) LoggerFactory(org.slf4j.LoggerFactory) Peer(org.hyperledger.besu.ethereum.p2p.peers.Peer) CompletableFuture(java.util.concurrent.CompletableFuture) Bytes(org.apache.tuweni.bytes.Bytes) Subscribers(org.hyperledger.besu.util.Subscribers) Supplier(java.util.function.Supplier) Hash(org.hyperledger.besu.crypto.Hash) UInt64(org.apache.tuweni.units.bigints.UInt64) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) StorageProvider(org.hyperledger.besu.ethereum.storage.StorageProvider) NodeRecordFactory(org.ethereum.beacon.discovery.schema.NodeRecordFactory) IdentitySchema(org.ethereum.beacon.discovery.schema.IdentitySchema) DiscoveryConfiguration(org.hyperledger.besu.ethereum.p2p.config.DiscoveryConfiguration) Suppliers(com.google.common.base.Suppliers) PingPacketData(org.hyperledger.besu.ethereum.p2p.discovery.internal.PingPacketData) NetworkUtility(org.hyperledger.besu.util.NetworkUtility) EnrField(org.ethereum.beacon.discovery.schema.EnrField) Logger(org.slf4j.Logger) AsyncExecutor(org.hyperledger.besu.ethereum.p2p.discovery.internal.PeerDiscoveryController.AsyncExecutor) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Packet(org.hyperledger.besu.ethereum.p2p.discovery.internal.Packet) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) KeyValueSegmentIdentifier(org.hyperledger.besu.ethereum.storage.keyvalue.KeyValueSegmentIdentifier) SignatureAlgorithmFactory(org.hyperledger.besu.crypto.SignatureAlgorithmFactory) InetSocketAddress(java.net.InetSocketAddress) Collectors(java.util.stream.Collectors) EnodeURL(org.hyperledger.besu.plugin.data.EnodeURL) List(java.util.List) Stream(java.util.stream.Stream) SignatureAlgorithm(org.hyperledger.besu.crypto.SignatureAlgorithm) KeyValueStorage(org.hyperledger.besu.plugin.services.storage.KeyValueStorage) NodeRecord(org.ethereum.beacon.discovery.schema.NodeRecord) PeerId(org.hyperledger.besu.ethereum.p2p.peers.PeerId) KeyValueStorageTransaction(org.hyperledger.besu.plugin.services.storage.KeyValueStorageTransaction) Optional(java.util.Optional) TimerUtil(org.hyperledger.besu.ethereum.p2p.discovery.internal.TimerUtil) PeerPermissions(org.hyperledger.besu.ethereum.p2p.permissions.PeerPermissions) MetricsSystem(org.hyperledger.besu.plugin.services.MetricsSystem) VisibleForTesting(com.google.common.annotations.VisibleForTesting) InetAddresses(com.google.common.net.InetAddresses) PeerRequirement(org.hyperledger.besu.ethereum.p2p.discovery.internal.PeerRequirement) Collections(java.util.Collections) NatService(org.hyperledger.besu.nat.NatService) NodeKey(org.hyperledger.besu.crypto.NodeKey) PeerDiscoveryController(org.hyperledger.besu.ethereum.p2p.discovery.internal.PeerDiscoveryController) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) EnrField(org.ethereum.beacon.discovery.schema.EnrField) KeyValueStorage(org.hyperledger.besu.plugin.services.storage.KeyValueStorage) KeyValueStorageTransaction(org.hyperledger.besu.plugin.services.storage.KeyValueStorageTransaction) EnodeURL(org.hyperledger.besu.plugin.data.EnodeURL) NodeRecord(org.ethereum.beacon.discovery.schema.NodeRecord) Bytes(org.apache.tuweni.bytes.Bytes) NodeRecordFactory(org.ethereum.beacon.discovery.schema.NodeRecordFactory) UInt64(org.apache.tuweni.units.bigints.UInt64)

Example 10 with UInt64

use of org.apache.tuweni.units.bigints.UInt64 in project besu by hyperledger.

the class PongPacketData method readFrom.

public static PongPacketData readFrom(final RLPInput in) {
    in.enterList();
    final Endpoint to = Endpoint.decodeStandalone(in);
    final Bytes hash = in.readBytes();
    final long expiration = in.readLongScalar();
    UInt64 enrSeq = null;
    if (!in.isEndOfCurrentList()) {
        try {
            enrSeq = UInt64.valueOf(in.readBigIntegerScalar());
            LOG.debug("read PONG enr from scalar");
        } catch (MalformedRLPInputException malformed) {
            LOG.debug("failed to read PONG enr from scalar, trying as byte array");
            enrSeq = UInt64.fromBytes(in.readBytes());
        }
    }
    in.leaveListLenient();
    return new PongPacketData(to, hash, expiration, enrSeq);
}
Also used : Bytes(org.apache.tuweni.bytes.Bytes) Endpoint(org.hyperledger.besu.ethereum.p2p.discovery.Endpoint) MalformedRLPInputException(org.hyperledger.besu.ethereum.rlp.MalformedRLPInputException) UInt64(org.apache.tuweni.units.bigints.UInt64)

Aggregations

UInt64 (org.apache.tuweni.units.bigints.UInt64)27 Bytes (org.apache.tuweni.bytes.Bytes)20 Endpoint (org.hyperledger.besu.ethereum.p2p.discovery.Endpoint)19 Test (org.junit.Test)16 BytesValueRLPOutput (org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput)13 Bytes32 (org.apache.tuweni.bytes.Bytes32)6 Test (org.junit.jupiter.api.Test)5 InterchangeV5Format (dsl.InterchangeV5Format)3 List (java.util.List)2 Optional (java.util.Optional)2 Collectors (java.util.stream.Collectors)2 NodeKey (org.hyperledger.besu.crypto.NodeKey)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 Suppliers (com.google.common.base.Suppliers)1 InetAddresses (com.google.common.net.InetAddresses)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 InetSocketAddress (java.net.InetSocketAddress)1