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