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();
}
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;
}
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());
}
}
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);
}
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());
}
Aggregations