Search in sources :

Example 6 with ActiveNode

use of com.quorum.tessera.discovery.ActiveNode in project tessera by ConsenSys.

the class DiscoveryHelperTest method buildAllNodeInfos.

@Test
public void buildAllNodeInfos() {
    when(runtimeContext.getP2pServerUri()).thenReturn(URI.create("http://own.com"));
    final ActiveNode node1 = ActiveNode.Builder.create().withUri(NodeUri.create("http://node1.com")).withKeys(List.of(PublicKey.from("key1".getBytes()))).withSupportedVersions(List.of("v1")).build();
    final ActiveNode node2 = ActiveNode.Builder.create().withUri(NodeUri.create("http://node2.com")).withKeys(List.of(PublicKey.from("key2".getBytes()))).withSupportedVersions(List.of("v2")).build();
    when(networkStore.getActiveNodes()).thenReturn(Stream.of(node1, node2));
    final Set<NodeInfo> nodeInfos = discoveryHelper.buildRemoteNodeInfos();
    assertThat(nodeInfos).hasSize(2);
    Set<ActiveNode> activeNodes = nodeInfos.stream().map(nodeInfo -> ActiveNode.Builder.create().withUri(NodeUri.create(nodeInfo.getUrl())).withKeys(nodeInfo.getRecipients().stream().map(Recipient::getKey).collect(Collectors.toSet())).withSupportedVersions(nodeInfo.supportedApiVersions()).build()).collect(Collectors.toSet());
    assertThat(activeNodes).containsExactlyInAnyOrder(node1, node2);
    verify(networkStore).getActiveNodes();
    verify(runtimeContext).getP2pServerUri();
    mockedRuntimeContext.verify(RuntimeContext::getInstance);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) IntStream(java.util.stream.IntStream) KeyNotFoundException(com.quorum.tessera.encryption.KeyNotFoundException) PublicKey(com.quorum.tessera.encryption.PublicKey) DiscoveryHelper(com.quorum.tessera.discovery.DiscoveryHelper) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Set(java.util.Set) Test(org.junit.Test) NodeUri(com.quorum.tessera.discovery.NodeUri) Collectors(java.util.stream.Collectors) Recipient(com.quorum.tessera.partyinfo.node.Recipient) Mockito(org.mockito.Mockito) ActiveNode(com.quorum.tessera.discovery.ActiveNode) NetworkStore(com.quorum.tessera.discovery.NetworkStore) List(java.util.List) MockedStatic(org.mockito.MockedStatic) Stream(java.util.stream.Stream) NodeInfo(com.quorum.tessera.partyinfo.node.NodeInfo) After(org.junit.After) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) RuntimeContext(com.quorum.tessera.context.RuntimeContext) Enclave(com.quorum.tessera.enclave.Enclave) URI(java.net.URI) Before(org.junit.Before) NodeInfo(com.quorum.tessera.partyinfo.node.NodeInfo) Recipient(com.quorum.tessera.partyinfo.node.Recipient) ActiveNode(com.quorum.tessera.discovery.ActiveNode) RuntimeContext(com.quorum.tessera.context.RuntimeContext) Test(org.junit.Test)

Example 7 with ActiveNode

use of com.quorum.tessera.discovery.ActiveNode in project tessera by ConsenSys.

the class AutoDiscovery method onUpdate.

@Override
public void onUpdate(final NodeInfo nodeInfo) {
    LOGGER.debug("Processing node info {}", nodeInfo);
    final NodeUri callerNodeUri = NodeUri.create(nodeInfo.getUrl());
    LOGGER.debug("Update node {}", callerNodeUri);
    final Set<PublicKey> keys = nodeInfo.getRecipients().stream().filter(r -> NodeUri.create(r.getUrl()).equals(callerNodeUri)).map(Recipient::getKey).collect(Collectors.toSet());
    final ActiveNode activeNode = ActiveNode.Builder.create().withUri(callerNodeUri).withKeys(keys).withSupportedVersions(nodeInfo.supportedApiVersions()).build();
    networkStore.store(activeNode);
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) NodeUri(com.quorum.tessera.discovery.NodeUri) ActiveNode(com.quorum.tessera.discovery.ActiveNode)

Example 8 with ActiveNode

use of com.quorum.tessera.discovery.ActiveNode in project tessera by ConsenSys.

the class DiscoveryHelperImpl method onCreate.

@Override
public void onCreate() {
    RuntimeContext runtimeContext = RuntimeContext.getInstance();
    final NodeUri nodeUri = Optional.of(runtimeContext).map(RuntimeContext::getP2pServerUri).map(NodeUri::create).get();
    ActiveNode thisNode = ActiveNode.Builder.create().withUri(nodeUri).withKeys(enclave.getPublicKeys()).withSupportedVersions(ApiVersion.versions()).build();
    networkStore.store(thisNode);
}
Also used : NodeUri(com.quorum.tessera.discovery.NodeUri) ActiveNode(com.quorum.tessera.discovery.ActiveNode) RuntimeContext(com.quorum.tessera.context.RuntimeContext)

Example 9 with ActiveNode

use of com.quorum.tessera.discovery.ActiveNode in project tessera by ConsenSys.

the class DiscoveryHelperImpl method buildRemoteNodeInfo.

@Override
public NodeInfo buildRemoteNodeInfo(PublicKey recipientKey) {
    final ActiveNode activeNode = networkStore.getActiveNodes().filter(node -> node.getKeys().contains(recipientKey)).findAny().orElseThrow(() -> new KeyNotFoundException("Recipient not found for key: " + recipientKey.encodeToBase64()));
    final String nodeUrl = activeNode.getUri().asString();
    final Set<Recipient> recipients = activeNode.getKeys().stream().map(k -> Recipient.of(k, nodeUrl)).collect(Collectors.toSet());
    final NodeInfo nodeInfo = NodeInfo.Builder.create().withUrl(nodeUrl).withRecipients(recipients).withSupportedApiVersions(activeNode.getSupportedVersions()).build();
    return nodeInfo;
}
Also used : KeyNotFoundException(com.quorum.tessera.encryption.KeyNotFoundException) PublicKey(com.quorum.tessera.encryption.PublicKey) Logger(org.slf4j.Logger) DiscoveryHelper(com.quorum.tessera.discovery.DiscoveryHelper) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) ApiVersion(com.quorum.tessera.version.ApiVersion) NodeUri(com.quorum.tessera.discovery.NodeUri) Collectors(java.util.stream.Collectors) Recipient(com.quorum.tessera.partyinfo.node.Recipient) ActiveNode(com.quorum.tessera.discovery.ActiveNode) NetworkStore(com.quorum.tessera.discovery.NetworkStore) List(java.util.List) NodeInfo(com.quorum.tessera.partyinfo.node.NodeInfo) RuntimeContext(com.quorum.tessera.context.RuntimeContext) Enclave(com.quorum.tessera.enclave.Enclave) Optional(java.util.Optional) URI(java.net.URI) NodeInfo(com.quorum.tessera.partyinfo.node.NodeInfo) Recipient(com.quorum.tessera.partyinfo.node.Recipient) ActiveNode(com.quorum.tessera.discovery.ActiveNode) KeyNotFoundException(com.quorum.tessera.encryption.KeyNotFoundException)

Example 10 with ActiveNode

use of com.quorum.tessera.discovery.ActiveNode in project tessera by ConsenSys.

the class AutoDiscoveryTest method onUpdateIgnoresKeysThatAreNotOwnedBySender.

@Test
public void onUpdateIgnoresKeysThatAreNotOwnedBySender() {
    String uri = "http://mynode.com";
    PublicKey key = mock(PublicKey.class);
    Recipient recipient = Recipient.of(key, uri);
    Recipient other = Recipient.of(mock(PublicKey.class), "http://othernode.com");
    List<Recipient> recipients = List.of(recipient, other);
    NodeInfo nodeInfo = NodeInfo.Builder.create().withUrl(uri).withRecipients(recipients).withSupportedApiVersions(List.of("Two", "Fifty")).build();
    List<ActiveNode> storedNodes = new ArrayList<>();
    doAnswer(invocation -> {
        storedNodes.add(invocation.getArgument(0));
        return null;
    }).when(networkStore).store(any(ActiveNode.class));
    discovery.onUpdate(nodeInfo);
    assertThat(storedNodes).hasSize(1);
    ActiveNode result = storedNodes.iterator().next();
    assertThat(result.getUri()).isEqualTo(NodeUri.create(uri));
    assertThat(result.getKeys()).containsExactly(key);
    assertThat(result.getSupportedVersions()).containsExactlyInAnyOrder("Two", "Fifty");
    verify(networkStore).store(any(ActiveNode.class));
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) NodeInfo(com.quorum.tessera.partyinfo.node.NodeInfo) ArrayList(java.util.ArrayList) Recipient(com.quorum.tessera.partyinfo.node.Recipient) ActiveNode(com.quorum.tessera.discovery.ActiveNode) Test(org.junit.Test)

Aggregations

ActiveNode (com.quorum.tessera.discovery.ActiveNode)18 PublicKey (com.quorum.tessera.encryption.PublicKey)17 Test (org.junit.Test)12 NodeUri (com.quorum.tessera.discovery.NodeUri)11 NodeInfo (com.quorum.tessera.partyinfo.node.NodeInfo)11 RuntimeContext (com.quorum.tessera.context.RuntimeContext)10 Recipient (com.quorum.tessera.partyinfo.node.Recipient)10 URI (java.net.URI)8 NetworkStore (com.quorum.tessera.discovery.NetworkStore)6 Enclave (com.quorum.tessera.enclave.Enclave)6 KeyNotFoundException (com.quorum.tessera.encryption.KeyNotFoundException)6 List (java.util.List)6 Set (java.util.Set)6 Collectors (java.util.stream.Collectors)6 DiscoveryHelper (com.quorum.tessera.discovery.DiscoveryHelper)5 ArrayList (java.util.ArrayList)4 Stream (java.util.stream.Stream)4 Optional (java.util.Optional)3 IntStream (java.util.stream.IntStream)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3