Search in sources :

Example 1 with NodeRecordFactory

use of org.ethereum.beacon.discovery.schema.NodeRecordFactory 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)

Aggregations

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 InetSocketAddress (java.net.InetSocketAddress)1 UTF_8 (java.nio.charset.StandardCharsets.UTF_8)1 Collections (java.util.Collections)1 List (java.util.List)1 Optional (java.util.Optional)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 Supplier (java.util.function.Supplier)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 Bytes (org.apache.tuweni.bytes.Bytes)1 UInt64 (org.apache.tuweni.units.bigints.UInt64)1 EnrField (org.ethereum.beacon.discovery.schema.EnrField)1 IdentitySchema (org.ethereum.beacon.discovery.schema.IdentitySchema)1 NodeRecord (org.ethereum.beacon.discovery.schema.NodeRecord)1