Search in sources :

Example 6 with PartyInfo

use of com.quorum.tessera.partyinfo.model.PartyInfo in project tessera by ConsenSys.

the class PartyInfoResourceTest method validationDisabledPassesAllKeysToStore.

@Test
public void validationDisabledPassesAllKeysToStore() {
    this.partyInfoResource = new PartyInfoResource(discovery, partyInfoParser, restClient, enclave, payloadEncoder, false, partyStore);
    final byte[] payload = "Test message".getBytes();
    final String url = "http://www.bogus.com";
    final String otherurl = "http://www.randomaddress.com";
    final PublicKey recipientKey = PublicKey.from("recipientKey".getBytes());
    final Set<Recipient> recipientList = new HashSet<>(Arrays.asList(Recipient.of(recipientKey, url), Recipient.of(recipientKey, otherurl)));
    final PartyInfo partyInfo = new PartyInfo(url, recipientList, Collections.emptySet());
    final NodeInfo nodeInfo = NodeInfoUtil.from(partyInfo, null);
    final ArgumentCaptor<PartyInfo> captor = ArgumentCaptor.forClass(PartyInfo.class);
    final byte[] serialisedData = "SERIALISED".getBytes();
    when(partyInfoParser.from(payload)).thenReturn(partyInfo);
    when(discovery.getCurrent()).thenReturn(nodeInfo);
    when(partyInfoParser.to(captor.capture())).thenReturn(serialisedData);
    final Response callResponse = partyInfoResource.partyInfo(payload, null);
    final byte[] data = (byte[]) callResponse.getEntity();
    assertThat(captor.getValue().getUrl()).isEqualTo(url);
    assertThat(captor.getValue().getRecipients()).isEmpty();
    assertThat(captor.getValue().getParties()).isEmpty();
    assertThat(new String(data)).isEqualTo("SERIALISED");
    verify(partyInfoParser).from(payload);
    verify(partyInfoParser).to(any(PartyInfo.class));
    final ArgumentCaptor<NodeInfo> modifiedPartyInfoCaptor = ArgumentCaptor.forClass(NodeInfo.class);
    verify(discovery).onUpdate(modifiedPartyInfoCaptor.capture());
    final NodeInfo modified = modifiedPartyInfoCaptor.getValue();
    assertThat(modified.getUrl()).isEqualTo(url);
    Set<com.quorum.tessera.partyinfo.node.Recipient> updatedRecipients = modified.getRecipients();
    assertThat(updatedRecipients).containsExactlyInAnyOrder(com.quorum.tessera.partyinfo.node.Recipient.of(recipientKey, url), com.quorum.tessera.partyinfo.node.Recipient.of(recipientKey, otherurl));
    verify(discovery).getCurrent();
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) Recipient(com.quorum.tessera.partyinfo.model.Recipient) PartyInfo(com.quorum.tessera.partyinfo.model.PartyInfo) Response(jakarta.ws.rs.core.Response) NodeInfo(com.quorum.tessera.partyinfo.node.NodeInfo) Test(org.junit.Test)

Example 7 with PartyInfo

use of com.quorum.tessera.partyinfo.model.PartyInfo in project tessera by ConsenSys.

the class PartyInfoBroadcasterTest method testWhenURLIsOwn.

@Test
public void testWhenURLIsOwn() {
    final NodeInfo partyInfo = NodeInfo.Builder.create().withUrl(OWN_URL).build();
    when(partyStore.getParties()).thenReturn(Set.of(URI.create(OWN_URL)));
    when(discovery.getCurrent()).thenReturn(partyInfo);
    when(partyInfoParser.to(any(PartyInfo.class))).thenReturn(DATA);
    when(p2pClient.sendPartyInfo(OWN_URL, DATA)).thenReturn(true);
    partyInfoBroadcaster.run();
    verify(partyStore).loadFromConfigIfEmpty();
    verify(partyStore).getParties();
    verify(partyInfoParser).to(any(PartyInfo.class));
    verify(discovery).getCurrent();
}
Also used : NodeInfo(com.quorum.tessera.partyinfo.node.NodeInfo) PartyInfo(com.quorum.tessera.partyinfo.model.PartyInfo) Test(org.junit.Test)

Example 8 with PartyInfo

use of com.quorum.tessera.partyinfo.model.PartyInfo in project tessera by ConsenSys.

the class PartyInfoBroadcasterTest method run.

@Test
public void run() {
    final NodeInfo partyInfo = NodeInfo.Builder.create().withUrl(OWN_URL).build();
    when(partyStore.getParties()).thenReturn(Set.of(URI.create(OWN_URL), URI.create(TARGET_URL)));
    when(discovery.getCurrent()).thenReturn(partyInfo);
    when(p2pClient.sendPartyInfo(TARGET_URL, DATA)).thenReturn(true);
    partyInfoBroadcaster.run();
    verify(partyStore).loadFromConfigIfEmpty();
    verify(partyStore).getParties();
    verify(discovery).getCurrent();
    verify(partyInfoParser).to(any(PartyInfo.class));
    verify(p2pClient).sendPartyInfo(TARGET_URL, DATA);
}
Also used : NodeInfo(com.quorum.tessera.partyinfo.node.NodeInfo) PartyInfo(com.quorum.tessera.partyinfo.model.PartyInfo) Test(org.junit.Test)

Example 9 with PartyInfo

use of com.quorum.tessera.partyinfo.model.PartyInfo in project tessera by ConsenSys.

the class PartyInfoResource method getPartyInfo.

@Operation(summary = "/partyinfo", description = "fetch network/peer information")
@ApiResponse(responseCode = "200", description = "server's partyinfo data", content = @Content(schema = @Schema(implementation = GetPartyInfoResponse.class)))
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getPartyInfo() {
    final NodeInfo current = this.discovery.getCurrent();
    final JsonArrayBuilder peersBuilder = Json.createArrayBuilder();
    partyStore.getParties().stream().map(party -> Json.createObjectBuilder().add("url", party.toString()).build()).forEach(peersBuilder::add);
    final JsonArrayBuilder recipientBuilder = Json.createArrayBuilder();
    current.getRecipients().stream().map(recipient -> Json.createObjectBuilder().add("key", recipient.getKey().encodeToBase64()).add("url", recipient.getUrl()).build()).forEach(recipientBuilder::add);
    final String output = Json.createObjectBuilder().add("url", current.getUrl()).add("peers", peersBuilder.build()).add("keys", recipientBuilder.build()).build().toString();
    LOGGER.debug("Sending json {} from {}", output, current);
    return Response.status(Response.Status.OK).entity(output).build();
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) java.util(java.util) NodeInfoUtil(com.quorum.tessera.partyinfo.model.NodeInfoUtil) LoggerFactory(org.slf4j.LoggerFactory) Party(com.quorum.tessera.partyinfo.model.Party) GetPartyInfoResponse(com.quorum.tessera.p2p.model.GetPartyInfoResponse) Content(io.swagger.v3.oas.annotations.media.Content) Discovery(com.quorum.tessera.discovery.Discovery) Operation(io.swagger.v3.oas.annotations.Operation) Response(jakarta.ws.rs.core.Response) NodeInfo(com.quorum.tessera.partyinfo.node.NodeInfo) Objects.requireNonNull(java.util.Objects.requireNonNull) RequestBody(io.swagger.v3.oas.annotations.parameters.RequestBody) ApiResponse(io.swagger.v3.oas.annotations.responses.ApiResponse) Constants(com.quorum.tessera.shared.Constants) Schema(io.swagger.v3.oas.annotations.media.Schema) PartyInfoParser(com.quorum.tessera.p2p.partyinfo.PartyInfoParser) Client(jakarta.ws.rs.client.Client) Logger(org.slf4j.Logger) Collections.emptySet(java.util.Collections.emptySet) Collections.emptyList(java.util.Collections.emptyList) Predicate(java.util.function.Predicate) com.quorum.tessera.enclave(com.quorum.tessera.enclave) JsonArrayBuilder(jakarta.json.JsonArrayBuilder) NodeUri(com.quorum.tessera.discovery.NodeUri) jakarta.ws.rs(jakarta.ws.rs) Collectors(java.util.stream.Collectors) PartyStore(com.quorum.tessera.p2p.partyinfo.PartyStore) Json(jakarta.json.Json) Entity(jakarta.ws.rs.client.Entity) Parameter(io.swagger.v3.oas.annotations.Parameter) ArraySchema(io.swagger.v3.oas.annotations.media.ArraySchema) PartyInfo(com.quorum.tessera.partyinfo.model.PartyInfo) MediaType(jakarta.ws.rs.core.MediaType) Tag(io.swagger.v3.oas.annotations.tags.Tag) Recipient(com.quorum.tessera.partyinfo.model.Recipient) NodeInfo(com.quorum.tessera.partyinfo.node.NodeInfo) JsonArrayBuilder(jakarta.json.JsonArrayBuilder) Operation(io.swagger.v3.oas.annotations.Operation) ApiResponse(io.swagger.v3.oas.annotations.responses.ApiResponse)

Example 10 with PartyInfo

use of com.quorum.tessera.partyinfo.model.PartyInfo in project tessera by ConsenSys.

the class PartyInfoParser method from.

/**
 * Decodes a set of PartyInfo to the format that is shared between nodes
 *
 * @param encoded the encoded information that needs to be read
 * @return the decoded {@link PartyInfo} which contains the other nodes information
 */
default PartyInfo from(final byte[] encoded) {
    final ByteBuffer byteBuffer = ByteBuffer.wrap(encoded);
    final int urlLength = toIntExact(byteBuffer.getLong());
    checkLength(urlLength);
    final byte[] urlBytes = new byte[urlLength];
    byteBuffer.get(urlBytes);
    final String url = new String(urlBytes, UTF_8);
    final int numberOfRecipients = toIntExact(byteBuffer.getLong());
    checkLength(numberOfRecipients);
    final Set<Recipient> recipients = new HashSet<>();
    for (int i = 0; i < numberOfRecipients; i++) {
        final int recipientKeyLength = toIntExact(byteBuffer.getLong());
        checkLength(recipientKeyLength);
        final byte[] recipientKeyBytes = new byte[recipientKeyLength];
        byteBuffer.get(recipientKeyBytes);
        final int recipientUrlValueLength = toIntExact(byteBuffer.getLong());
        checkLength(recipientUrlValueLength);
        final byte[] urlValueData = new byte[recipientUrlValueLength];
        byteBuffer.get(urlValueData);
        final String recipientUrl = new String(urlValueData, UTF_8);
        recipients.add(Recipient.of(PublicKey.from(recipientKeyBytes), recipientUrl));
    }
    final int partyCount = toIntExact(byteBuffer.getLong());
    checkLength(partyCount);
    final Set<Party> parties = new HashSet<>();
    for (int i = 0; i < partyCount; i++) {
        long partyElementLength = byteBuffer.getLong();
        checkLength(partyElementLength);
        byte[] ptyData = new byte[toIntExact(partyElementLength)];
        byteBuffer.get(ptyData);
        parties.add(new Party(new String(ptyData, UTF_8)));
    }
    return new PartyInfo(url, recipients, parties);
}
Also used : Party(com.quorum.tessera.partyinfo.model.Party) Recipient(com.quorum.tessera.partyinfo.model.Recipient) ByteBuffer(java.nio.ByteBuffer) PartyInfo(com.quorum.tessera.partyinfo.model.PartyInfo) HashSet(java.util.HashSet)

Aggregations

PartyInfo (com.quorum.tessera.partyinfo.model.PartyInfo)24 Test (org.junit.Test)18 Recipient (com.quorum.tessera.partyinfo.model.Recipient)16 PublicKey (com.quorum.tessera.encryption.PublicKey)14 Entity (jakarta.ws.rs.client.Entity)13 Response (jakarta.ws.rs.core.Response)13 NodeInfo (com.quorum.tessera.partyinfo.node.NodeInfo)11 PartyInfoParser (com.quorum.tessera.p2p.partyinfo.PartyInfoParser)9 Collectors (java.util.stream.Collectors)9 Client (jakarta.ws.rs.client.Client)8 MediaType (jakarta.ws.rs.core.MediaType)8 java.util (java.util)8 Party (com.quorum.tessera.partyinfo.model.Party)7 JsonObject (jakarta.json.JsonObject)7 Config (com.quorum.tessera.config.Config)6 EncryptorConfig (com.quorum.tessera.config.EncryptorConfig)6 ServerConfig (com.quorum.tessera.config.ServerConfig)6 ConfigKeyPair (com.quorum.tessera.config.keypairs.ConfigKeyPair)6 KeyEncryptor (com.quorum.tessera.config.keys.KeyEncryptor)6 KeyEncryptorFactory (com.quorum.tessera.config.keys.KeyEncryptorFactory)6