Skip to content
This repository has been archived by the owner on Dec 22, 2021. It is now read-only.

Change CreatePrivacyGroup and FindPrivacyGroup to return the same type #257

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import net.consensys.orion.http.handler.privacy.DeletePrivacyGroupRequest;
import net.consensys.orion.http.handler.privacy.FindPrivacyGroupRequest;
import net.consensys.orion.http.handler.privacy.FindPrivacyGroupResponse;
import net.consensys.orion.http.handler.privacy.PrivacyGroup;
import net.consensys.orion.http.handler.privacy.PrivacyGroupRequest;
import net.consensys.orion.http.handler.receive.ReceiveRequest;
Expand Down Expand Up @@ -152,12 +151,12 @@ public Optional<PrivacyGroup> createPrivacyGroup(String[] addresses, String from
return Optional.ofNullable(keyFuture.join());
}

public Optional<FindPrivacyGroupResponse[]> findPrivacyGroup(String[] addresses) {
public Optional<PrivacyGroup[]> findPrivacyGroup(String[] addresses) {
FindPrivacyGroupRequest findGroupRequest = new FindPrivacyGroupRequest(addresses);
CompletableFuture<FindPrivacyGroupResponse[]> keyFuture = new CompletableFuture<>();
CompletableFuture<PrivacyGroup[]> keyFuture = new CompletableFuture<>();
httpClient.post(clientPort, "localhost", "/findPrivacyGroup").handler(resp -> {
if (resp.statusCode() == 200) {
resp.bodyHandler(body -> keyFuture.complete(deserialize(body, FindPrivacyGroupResponse[].class)));
resp.bodyHandler(body -> keyFuture.complete(deserialize(body, PrivacyGroup[].class)));
} else {
keyFuture.complete(null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import net.consensys.orion.cmd.Orion;
import net.consensys.orion.config.Config;
import net.consensys.orion.http.handler.privacy.FindPrivacyGroupResponse;
import net.consensys.orion.http.handler.privacy.PrivacyGroup;
import net.consensys.orion.http.handler.receive.ReceiveResponse;

Expand Down Expand Up @@ -140,7 +139,7 @@ public static PrivacyGroup createPrivacyGroupTransaction(
return sender.createPrivacyGroup(addresses, from, name, description).orElseThrow(AssertionFailedError::new);
}

public static FindPrivacyGroupResponse[] findPrivacyGroupTransaction(EthClientStub sender, String[] addresses) {
public static PrivacyGroup[] findPrivacyGroupTransaction(EthClientStub sender, String[] addresses) {
return sender.findPrivacyGroup(addresses).orElseThrow(AssertionFailedError::new);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import net.consensys.orion.acceptance.NodeUtils;
import net.consensys.orion.cmd.Orion;
import net.consensys.orion.config.Config;
import net.consensys.orion.http.handler.privacy.FindPrivacyGroupResponse;
import net.consensys.orion.http.handler.privacy.PrivacyGroup;
import net.consensys.orion.http.server.HttpContentType;
import net.consensys.orion.network.ConcurrentNetworkNodes;
Expand Down Expand Up @@ -174,16 +173,16 @@ void createAndFind() {
assertEquals(privacyGroup.getDescription(), description);

// find the created privacy group in first node
final FindPrivacyGroupResponse[] firstNodePrivacyGroups = findPrivacyGroupTransaction(firstNode, addresses);
final PrivacyGroup[] firstNodePrivacyGroups = findPrivacyGroupTransaction(firstNode, addresses);

assertEquals(firstNodePrivacyGroups.length, 1);
assertEquals(firstNodePrivacyGroups[0].privacyGroupId(), privacyGroupId);
assertEquals(firstNodePrivacyGroups[0].getPrivacyGroupId(), privacyGroupId);

// find the created privacy group in second node
final FindPrivacyGroupResponse[] secondNodePrivacyGroups = findPrivacyGroupTransaction(secondNode, addresses);
final PrivacyGroup[] secondNodePrivacyGroups = findPrivacyGroupTransaction(secondNode, addresses);

assertEquals(secondNodePrivacyGroups.length, firstNodePrivacyGroups.length);
assertEquals(secondNodePrivacyGroups[0].privacyGroupId(), firstNodePrivacyGroups[0].privacyGroupId());
assertEquals(secondNodePrivacyGroups[0].getPrivacyGroupId(), firstNodePrivacyGroups[0].getPrivacyGroupId());
}

@Test
Expand All @@ -205,19 +204,17 @@ void createDeleteFind() {
String privacyGroupDeleted = deletePrivacyGroupTransaction(firstNode, privacyGroupId, PK_1_B_64);

// find the created privacy group deleted in first node
final FindPrivacyGroupResponse[] deleteNodeFirstPrivacyGroups = findPrivacyGroupTransaction(firstNode, addresses);
final PrivacyGroup[] deleteNodeFirstPrivacyGroups = findPrivacyGroupTransaction(firstNode, addresses);

List<String> listFirst =
Arrays.stream(deleteNodeFirstPrivacyGroups).map(FindPrivacyGroupResponse::privacyGroupId).collect(
Collectors.toList());
Arrays.stream(deleteNodeFirstPrivacyGroups).map(PrivacyGroup::getPrivacyGroupId).collect(Collectors.toList());
assertFalse(listFirst.contains(privacyGroupDeleted));

// find the deleted privacy group in second node
final FindPrivacyGroupResponse[] deleteNodeSecondPrivacyGroups = findPrivacyGroupTransaction(secondNode, addresses);
final PrivacyGroup[] deleteNodeSecondPrivacyGroups = findPrivacyGroupTransaction(secondNode, addresses);

List<String> listSecond =
Arrays.stream(deleteNodeSecondPrivacyGroups).map(FindPrivacyGroupResponse::privacyGroupId).collect(
Collectors.toList());
Arrays.stream(deleteNodeSecondPrivacyGroups).map(PrivacyGroup::getPrivacyGroupId).collect(Collectors.toList());
assertFalse(listSecond.contains(privacyGroupDeleted));
}

Expand All @@ -239,9 +236,9 @@ void createTwiceDeleteOnce() {
assertEquals(firstPrivacyGroup.getDescription(), description);

// find the created privacy group in first node
FindPrivacyGroupResponse[] initialPrivacyGroups = findPrivacyGroupTransaction(firstNode, addresses);
PrivacyGroup[] initialPrivacyGroups = findPrivacyGroupTransaction(firstNode, addresses);
assertEquals(initialPrivacyGroups.length, 1);
assertEquals(initialPrivacyGroups[0].privacyGroupId(), firstPrivacyGroupId);
assertEquals(initialPrivacyGroups[0].getPrivacyGroupId(), firstPrivacyGroupId);

//create another privacy group
PrivacyGroup secondPrivacyGroup = createPrivacyGroupTransaction(firstNode, addresses, PK_1_B_64, name, description);
Expand All @@ -251,20 +248,19 @@ void createTwiceDeleteOnce() {
assertEquals(firstPrivacyGroup.getDescription(), description);

// find the created privacy group in second node
FindPrivacyGroupResponse[] updatedPrivacyGroups = findPrivacyGroupTransaction(secondNode, addresses);
PrivacyGroup[] updatedPrivacyGroups = findPrivacyGroupTransaction(secondNode, addresses);
assertEquals(updatedPrivacyGroups.length, 2);
assertEquals(updatedPrivacyGroups[0].privacyGroupId(), firstPrivacyGroupId);
assertEquals(updatedPrivacyGroups[1].privacyGroupId(), secondPrivacyGroupId);
assertEquals(updatedPrivacyGroups[0].getPrivacyGroupId(), firstPrivacyGroupId);
assertEquals(updatedPrivacyGroups[1].getPrivacyGroupId(), secondPrivacyGroupId);

// delete the first privacy group
deletePrivacyGroupTransaction(firstNode, firstPrivacyGroupId, PK_1_B_64);

// find the deleted privacy group in second node
final FindPrivacyGroupResponse[] deleteNodeSecondPrivacyGroups = findPrivacyGroupTransaction(secondNode, addresses);
final PrivacyGroup[] deleteNodeSecondPrivacyGroups = findPrivacyGroupTransaction(secondNode, addresses);

List<String> listSecond =
Arrays.stream(deleteNodeSecondPrivacyGroups).map(FindPrivacyGroupResponse::privacyGroupId).collect(
Collectors.toList());
Arrays.stream(deleteNodeSecondPrivacyGroups).map(PrivacyGroup::getPrivacyGroupId).collect(Collectors.toList());
assertFalse(listSecond.contains(firstPrivacyGroupId));
assertTrue(listSecond.contains(secondPrivacyGroupId));
}
Expand All @@ -285,9 +281,9 @@ void createAndDeleteTwice() {
assertEquals(privacyGroup.getDescription(), description);

// find the created privacy group in first node
FindPrivacyGroupResponse[] initialPrivacyGroups = findPrivacyGroupTransaction(firstNode, addresses);
PrivacyGroup[] initialPrivacyGroups = findPrivacyGroupTransaction(firstNode, addresses);
assertEquals(initialPrivacyGroups.length, 1);
assertEquals(initialPrivacyGroups[0].privacyGroupId(), privacyGroupId);
assertEquals(initialPrivacyGroups[0].getPrivacyGroupId(), privacyGroupId);

// delete the privacy group
deletePrivacyGroupTransaction(firstNode, privacyGroupId, PK_1_B_64);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,34 +74,34 @@ public CreatePrivacyGroupHandler(
public void handle(RoutingContext routingContext) {

byte[] request = routingContext.getBody().getBytes();
PrivacyGroupRequest privacyGroup = Serializer.deserialize(JSON, PrivacyGroupRequest.class, request);
PrivacyGroupRequest privacyGroupRequest = Serializer.deserialize(JSON, PrivacyGroupRequest.class, request);

if (!Arrays.asList(privacyGroup.addresses()).contains(privacyGroup.from())) {
if (!Arrays.asList(privacyGroupRequest.addresses()).contains(privacyGroupRequest.from())) {
routingContext.fail(
new OrionException(OrionErrorCode.CREATE_GROUP_INCLUDE_SELF, "the list of addresses should include self "));
return;
}

byte[] bytes = new byte[20];
if (privacyGroup.getSeed().isPresent()) {
bytes = privacyGroup.getSeed().get();
if (privacyGroupRequest.getSeed().isPresent()) {
bytes = privacyGroupRequest.getSeed().get();
} else {
SecureRandom random = new SecureRandom();
random.nextBytes(bytes);
}

PrivacyGroupPayload privacyGroupPayload = new PrivacyGroupPayload(
privacyGroup.addresses(),
privacyGroup.name(),
privacyGroup.description(),
privacyGroupRequest.addresses(),
privacyGroupRequest.name(),
privacyGroupRequest.description(),
PrivacyGroupPayload.State.ACTIVE,
PrivacyGroupPayload.Type.PANTHEON,
bytes);
final String privacyGroupId = privacyGroupStorage.generateDigest(privacyGroupPayload);

List<Box.PublicKey> addressListToForward = Arrays
.stream(privacyGroup.addresses())
.filter(key -> !key.equals(privacyGroup.from())) // don't forward to self
.stream(privacyGroupRequest.addresses())
.filter(key -> !key.equals(privacyGroupRequest.from())) // don't forward to self
.distinct()
.map(enclave::readKey)
.collect(Collectors.toList());
Expand Down Expand Up @@ -157,8 +157,9 @@ public void handle(RoutingContext routingContext) {
PrivacyGroup group = new PrivacyGroup(
privacyGroupId,
PrivacyGroupPayload.Type.PANTHEON,
privacyGroup.name(),
privacyGroup.description());
privacyGroupRequest.name(),
privacyGroupRequest.description(),
privacyGroupRequest.addresses());

Buffer toReturn = Buffer.buffer(Serializer.serialize(JSON, group));
routingContext.response().end(toReturn);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,19 +75,21 @@ public void handle(RoutingContext routingContext) {

CompletableFuture[] cfs = privacyGroupIds.stream().map(pKey -> {

CompletableFuture<FindPrivacyGroupResponse> responseFuture = new CompletableFuture<>();
CompletableFuture<PrivacyGroup> responseFuture = new CompletableFuture<>();
privacyGroupStorage.get(pKey).thenAccept((res) -> {
if (res.isPresent()) {
PrivacyGroupPayload privacyGroupPayload = res.get();
if (privacyGroupPayload.state().equals(PrivacyGroupPayload.State.ACTIVE)) {
FindPrivacyGroupResponse response = new FindPrivacyGroupResponse(
PrivacyGroup response = new PrivacyGroup(
pKey,
privacyGroupPayload.type(),
privacyGroupPayload.name(),
privacyGroupPayload.description(),
privacyGroupPayload.addresses());

responseFuture.complete(response);
} else {
responseFuture.complete(new FindPrivacyGroupResponse());
responseFuture.complete(new PrivacyGroup());
}
} else {
responseFuture.completeExceptionally(new OrionException(OrionErrorCode.ENCLAVE_PRIVACY_GROUP_MISSING));
Expand All @@ -102,11 +104,11 @@ public void handle(RoutingContext routingContext) {
return;
}

List<FindPrivacyGroupResponse> listPrivacyGroups = new ArrayList<>();
List<PrivacyGroup> listPrivacyGroups = new ArrayList<>();
for (CompletableFuture c : cfs) {
try {
FindPrivacyGroupResponse privacyGroup = (FindPrivacyGroupResponse) c.get();
if (privacyGroup.privacyGroupId() != null) {
PrivacyGroup privacyGroup = (PrivacyGroup) c.get();
if (privacyGroup.getPrivacyGroupId() != null) {
listPrivacyGroups.add(privacyGroup);
}
} catch (InterruptedException | ExecutionException e) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class PrivacyGroup implements Serializable {
private String name;
private String description;
private PrivacyGroupPayload.Type type;
private String[] members;

public String getPrivacyGroupId() {
return privacyGroupId;
Expand Down Expand Up @@ -55,12 +56,30 @@ public void setType(PrivacyGroupPayload.Type type) {
this.type = type;
}

public String[] getMembers() {
return members;
}

public void setMembers(String[] members) {
this.members = members;
}

public PrivacyGroup() {}

public PrivacyGroup(String privacyGroupId, PrivacyGroupPayload.Type type, String name, String description) {


public PrivacyGroup(
String privacyGroupId,
PrivacyGroupPayload.Type type,
String name,
String description,
String[] members) {
this.privacyGroupId = privacyGroupId;
this.type = type;
this.name = name;
this.description = description;
this.members = members;
}


}

This file was deleted.

Loading