Search in sources :

Example 1 with PeerId

use of io.libp2p.core.PeerId in project teku by ConsenSys.

the class LibP2PParamsFactory method createPeerScoreParams.

public static GossipPeerScoreParams createPeerScoreParams(final GossipPeerScoringConfig config) {
    final GossipPeerScoreParamsBuilder builder = GossipPeerScoreParams.builder().topicScoreCap(config.getTopicScoreCap()).appSpecificWeight(config.getAppSpecificWeight()).ipColocationFactorWeight(config.getIpColocationFactorWeight()).ipColocationFactorThreshold(config.getIpColocationFactorThreshold()).behaviourPenaltyWeight(config.getBehaviourPenaltyWeight()).behaviourPenaltyDecay(config.getBehaviourPenaltyDecay()).behaviourPenaltyThreshold(config.getBehaviourPenaltyThreshold()).decayInterval(config.getDecayInterval()).decayToZero(config.getDecayToZero()).retainScore(config.getRetainScore());
    // Configure optional params
    config.getAppSpecificScorer().ifPresent(scorer -> {
        final Function1<? super PeerId, Double> appSpecificScore = peerId -> scorer.scorePeer(new LibP2PNodeId(peerId));
        builder.appSpecificScore(appSpecificScore);
    });
    config.getDirectPeerManager().ifPresent(mgr -> {
        final Function1<? super PeerId, Boolean> isDirectPeer = peerId -> mgr.isDirectPeer(new LibP2PNodeId(peerId));
        builder.isDirect(isDirectPeer);
    });
    config.getWhitelistManager().ifPresent(mgr -> {
        // Ip whitelisting
        final Function1<? super String, Boolean> isIpWhitelisted = mgr::isWhitelisted;
        builder.ipWhitelisted(isIpWhitelisted);
    });
    return builder.build();
}
Also used : GossipPeerScoreParams(io.libp2p.pubsub.gossip.GossipPeerScoreParams) GossipScoringConfig(tech.pegasys.teku.networking.p2p.gossip.config.GossipScoringConfig) GossipPeerScoreParamsBuilder(io.libp2p.pubsub.gossip.builders.GossipPeerScoreParamsBuilder) GossipConfig(tech.pegasys.teku.networking.p2p.gossip.config.GossipConfig) GossipParams(io.libp2p.pubsub.gossip.GossipParams) GossipScoreParams(io.libp2p.pubsub.gossip.GossipScoreParams) Function1(kotlin.jvm.functions.Function1) Collectors(java.util.stream.Collectors) GossipPeerScoringConfig(tech.pegasys.teku.networking.p2p.gossip.config.GossipPeerScoringConfig) PeerId(io.libp2p.core.PeerId) GossipTopicScoreParams(io.libp2p.pubsub.gossip.GossipTopicScoreParams) LibP2PNodeId(tech.pegasys.teku.networking.p2p.libp2p.LibP2PNodeId) Map(java.util.Map) GossipTopicsScoreParams(io.libp2p.pubsub.gossip.GossipTopicsScoreParams) GossipTopicScoringConfig(tech.pegasys.teku.networking.p2p.gossip.config.GossipTopicScoringConfig) LibP2PNodeId(tech.pegasys.teku.networking.p2p.libp2p.LibP2PNodeId) GossipPeerScoreParamsBuilder(io.libp2p.pubsub.gossip.builders.GossipPeerScoreParamsBuilder)

Example 2 with PeerId

use of io.libp2p.core.PeerId in project teku by ConsenSys.

the class LibP2PGossipNetwork method getSubscribersByTopic.

@Override
public Map<String, Collection<NodeId>> getSubscribersByTopic() {
    Map<PeerId, Set<Topic>> peerTopics = gossip.getPeerTopics().join();
    final Map<String, Collection<NodeId>> result = new HashMap<>();
    for (Map.Entry<PeerId, Set<Topic>> peerTopic : peerTopics.entrySet()) {
        final LibP2PNodeId nodeId = new LibP2PNodeId(peerTopic.getKey());
        peerTopic.getValue().forEach(topic -> result.computeIfAbsent(topic.getTopic(), __ -> new HashSet<>()).add(nodeId));
    }
    return result;
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) Collection(java.util.Collection) LibP2PNodeId(tech.pegasys.teku.networking.p2p.libp2p.LibP2PNodeId) HashMap(java.util.HashMap) Map(java.util.Map) PeerId(io.libp2p.core.PeerId)

Example 3 with PeerId

use of io.libp2p.core.PeerId in project teku by ConsenSys.

the class PeerCommand method validateParamsAndGenerate.

void validateParamsAndGenerate(String outputFile, int number) throws IOException {
    try {
        File f = new File(outputFile);
        if (f.exists()) {
            throw new InvalidConfigurationException(String.format("Not overwriting existing file %s \nDelete file or use --output-file to point to a file that does not currently exist.", outputFile));
        }
        FileWriter fileWriter = new FileWriter(outputFile, Charset.defaultCharset());
        PrintWriter printWriter = new PrintWriter(fileWriter);
        printWriter.println("Private Key(Hex)\tPublic Key(Hex)\tPeerId(Base58)");
        for (int i = 0; i < number; i++) {
            PrivKey privKey = KeyKt.generateKeyPair(KEY_TYPE.SECP256K1).component1();
            PubKey pubKey = privKey.publicKey();
            PeerId peerId = PeerId.fromPubKey(pubKey);
            printWriter.println(Bytes.wrap(privKey.bytes()).toHexString() + "\t" + Bytes.wrap(pubKey.bytes()).toHexString() + "\t" + peerId.toBase58());
        }
        printWriter.close();
    } catch (final FileNotFoundException ex) {
        throw new InvalidConfigurationException("use --output-file to point to a file in an existing directory " + ex.getMessage());
    }
}
Also used : PubKey(io.libp2p.core.crypto.PubKey) FileWriter(java.io.FileWriter) FileNotFoundException(java.io.FileNotFoundException) PrivKey(io.libp2p.core.crypto.PrivKey) File(java.io.File) InvalidConfigurationException(tech.pegasys.teku.infrastructure.exceptions.InvalidConfigurationException) PrintWriter(java.io.PrintWriter) PeerId(io.libp2p.core.PeerId)

Example 4 with PeerId

use of io.libp2p.core.PeerId in project teku by ConsenSys.

the class MultiaddrPeerAddress method fromMultiaddr.

private static MultiaddrPeerAddress fromMultiaddr(final Multiaddr multiaddr) {
    final PeerId peerId = multiaddr.getPeerId();
    if (peerId == null) {
        throw new IllegalArgumentException("No peer ID present in multiaddr: " + multiaddr);
    }
    final LibP2PNodeId nodeId = new LibP2PNodeId(peerId);
    return new MultiaddrPeerAddress(nodeId, multiaddr);
}
Also used : PeerId(io.libp2p.core.PeerId)

Example 5 with PeerId

use of io.libp2p.core.PeerId in project teku by ConsenSys.

the class NodeIdTest method shouldBeEqualToNodeIdsOfDifferentTypes.

@Test
public void shouldBeEqualToNodeIdsOfDifferentTypes() {
    final PeerId peerId = PeerId.fromHex(ID);
    final NodeId libP2PNodeId = new LibP2PNodeId(peerId);
    final MockNodeId mockNodeId = new MockNodeId(Bytes.fromHexString(ID));
    assertThat(mockNodeId).isEqualTo(libP2PNodeId);
    assertThat(libP2PNodeId).isEqualTo(mockNodeId);
    assertThat(mockNodeId.hashCode()).isEqualTo(libP2PNodeId.hashCode());
}
Also used : MockNodeId(tech.pegasys.teku.networking.p2p.mock.MockNodeId) LibP2PNodeId(tech.pegasys.teku.networking.p2p.libp2p.LibP2PNodeId) MockNodeId(tech.pegasys.teku.networking.p2p.mock.MockNodeId) LibP2PNodeId(tech.pegasys.teku.networking.p2p.libp2p.LibP2PNodeId) PeerId(io.libp2p.core.PeerId) Test(org.junit.jupiter.api.Test)

Aggregations

PeerId (io.libp2p.core.PeerId)5 LibP2PNodeId (tech.pegasys.teku.networking.p2p.libp2p.LibP2PNodeId)3 Map (java.util.Map)2 PrivKey (io.libp2p.core.crypto.PrivKey)1 PubKey (io.libp2p.core.crypto.PubKey)1 GossipParams (io.libp2p.pubsub.gossip.GossipParams)1 GossipPeerScoreParams (io.libp2p.pubsub.gossip.GossipPeerScoreParams)1 GossipScoreParams (io.libp2p.pubsub.gossip.GossipScoreParams)1 GossipTopicScoreParams (io.libp2p.pubsub.gossip.GossipTopicScoreParams)1 GossipTopicsScoreParams (io.libp2p.pubsub.gossip.GossipTopicsScoreParams)1 GossipPeerScoreParamsBuilder (io.libp2p.pubsub.gossip.builders.GossipPeerScoreParamsBuilder)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 FileWriter (java.io.FileWriter)1 PrintWriter (java.io.PrintWriter)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1