Skip to content

Commit

Permalink
fixed issue with decodelist and no elements
Browse files Browse the repository at this point in the history
Signed-off-by: Miguel Rojo <miguelangel.rojofernandez@mastercard.com>
  • Loading branch information
freemanzMrojo committed Aug 19, 2022
1 parent 84881d7 commit 2188c66
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ protected PrivateTransactionReceipt buildExpectedAddMemberTransactionReceipt(
output.append("0000000000000000000000000000000000000000000000000000000000000020");
// Length of the array (with padded zeros to the left)
output.append(Quantity.longToPaddedHex(members.length, 32).substring(2));
// Offset of the elements of the array
output.append(Quantity.longToPaddedHex(members.length, members.length * 32).substring(2));
// TODO continue here
// Each member enclave key converted from Base64 to bytes
for (final String member : members) {
output.append(Bytes.fromBase64String(member).toUnprefixedHexString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.hyperledger.besu.ethereum.core.PrivacyParameters.FLEXIBLE_PRIVACY_PROXY;
import static org.hyperledger.besu.ethereum.privacy.FlexiblePrivacyGroupContract.decodeList;
import static org.hyperledger.besu.ethereum.privacy.group.FlexibleGroupManagement.GET_PARTICIPANTS_METHOD_SIGNATURE;
import static org.hyperledger.besu.ethereum.privacy.group.FlexibleGroupManagement.GET_VERSION_METHOD_SIGNATURE;

Expand Down Expand Up @@ -123,18 +122,18 @@ public Optional<PrivacyGroup> findPrivacyGroupByGroupId(
privateTransactionSimulator.process(
privacyGroupId, buildCallParams(GET_PARTICIPANTS_METHOD_SIGNATURE));

LOG.error("LLEGA AQUI 1: " + privateTransactionSimulatorResultOptional.isPresent());

if (privateTransactionSimulatorResultOptional.isPresent()
&& privateTransactionSimulatorResultOptional.get().isSuccessful()) {
LOG.error("LLEGA AQUI 2: " + privateTransactionSimulatorResultOptional.get().getOutput());
final RLPInput rlpInput =
RLP.input(privateTransactionSimulatorResultOptional.get().getOutput());
if (rlpInput.nextSize() > 0) {
LOG.error("LLEGA AQUI 3: " + rlpInput.raw().toBase64String());
return Optional.of(
new PrivacyGroup(
privacyGroupId, PrivacyGroup.Type.FLEXIBLE, "", "", decodeList(rlpInput.raw())));
privacyGroupId,
PrivacyGroup.Type.FLEXIBLE,
"",
"",
FlexibleUtil.decodeList(rlpInput.raw())));
}
}
return Optional.empty();
Expand Down Expand Up @@ -276,9 +275,7 @@ private Optional<String> buildAndSendAddPayload(
if (FlexibleUtil.isGroupAdditionTransaction(privateTransaction)) {
final List<PrivateTransactionMetadata> privateTransactionMetadataList =
buildTransactionMetadataList(privacyGroupId);
LOG.error("buildAndSendAddPayload ENTRA AQUI 1");
if (!privateTransactionMetadataList.isEmpty()) {
LOG.error("buildAndSendAddPayload ENTRA AQUI 2");
final List<PrivateTransactionWithMetadata> privateTransactionWithMetadataList =
retrievePrivateTransactions(
privacyGroupId, privateTransactionMetadataList, privacyUserId);
Expand All @@ -289,7 +286,6 @@ private Optional<String> buildAndSendAddPayload(
enclave.send(bytes.toBase64String(), privacyUserId, privateFor).getKey());
}
}
LOG.error("buildAndSendAddPayload ESTA VACIO");

return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,12 @@
import org.hyperledger.besu.evm.worldstate.WorldUpdater;
import org.hyperledger.besu.plugin.data.Restriction;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
import org.apache.tuweni.units.bigints.UInt256;

/*
This class is an abstraction on top of the privacy group management smart contract.
Expand All @@ -67,6 +64,7 @@
that is not on a block boundary. Used this way, the object's lifetime is intended to be short.
*/
public class FlexiblePrivacyGroupContract {

@FunctionalInterface
public interface TransactionSimulator {
Optional<TransactionProcessingResult> simulate(
Expand Down Expand Up @@ -167,7 +165,11 @@ private Optional<PrivacyGroup> readPrivacyGroupFromResult(
if (rlpInput.nextSize() > 0) {
final PrivacyGroup privacyGroup =
new PrivacyGroup(
privacyGroupId, PrivacyGroup.Type.FLEXIBLE, "", "", decodeList(rlpInput.raw()));
privacyGroupId,
PrivacyGroup.Type.FLEXIBLE,
"",
"",
FlexibleUtil.decodeList(rlpInput.raw()));
return Optional.of(privacyGroup);
} else {
return Optional.empty();
Expand Down Expand Up @@ -227,20 +229,4 @@ private CallParameter buildCallParams(final Bytes methodCall) {
return new CallParameter(
Address.ZERO, FLEXIBLE_PRIVACY_PROXY, 3000000, Wei.of(1000), Wei.ZERO, methodCall);
}

public static List<String> decodeList(final Bytes rlpEncodedList) {
final ArrayList<String> decodedElements = new ArrayList<>();
// first 32 bytes is dynamic list offset
if (rlpEncodedList.size() < 64) return decodedElements;
// Bytes uses a byte[] for the content which can only have up to Integer.MAX_VALUE-5 elements
final int lengthOfList =
UInt256.fromBytes(rlpEncodedList.slice(32, 32)).toInt(); // length of list
if (rlpEncodedList.size() < 64 + lengthOfList * 32) return decodedElements;

for (int i = 0; i < lengthOfList; ++i) {
decodedElements.add(
Bytes.wrap(rlpEncodedList.slice(64 + (32 * i), 32)).toBase64String()); // participant
}
return decodedElements;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@
import org.hyperledger.besu.datatypes.Address;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

import com.google.common.annotations.VisibleForTesting;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.units.bigints.UInt256;

public class FlexibleUtil {

Expand All @@ -40,10 +43,32 @@ public static boolean isGroupAdditionTransaction(final PrivateTransaction privat
}

public static List<String> getParticipantsFromParameter(final Bytes input) {
final List<String> participants = new ArrayList<>();
final int numberOfParticipants = input.slice(4 + 32, 32).toBigInteger().intValue();
if (numberOfParticipants == 0) return Collections.emptyList();
// Method selector + offset + number of participants + (offset * number of participants)
final Bytes mungedParticipants = input.slice(4 + 32 + 32 + (32 * numberOfParticipants));

return getParticipantsFromMungedParticipants(mungedParticipants, numberOfParticipants);
}

public static List<String> decodeList(final Bytes rlpEncodedList) {
// first 32 bytes is dynamic list offset
if (rlpEncodedList.size() < 64) return Collections.emptyList();
// Bytes uses a byte[] for the content which can only have up to Integer.MAX_VALUE-5 elements
final int lengthOfList =
UInt256.fromBytes(rlpEncodedList.slice(32, 32)).toInt(); // length of list
if (lengthOfList == 0 || rlpEncodedList.size() < 64 + lengthOfList * 32)
return Collections.emptyList();

final Bytes mungedParticipants = rlpEncodedList.slice(32 + 32 + (32 * lengthOfList));

return getParticipantsFromMungedParticipants(mungedParticipants, lengthOfList);
}

@VisibleForTesting
public static List<String> getParticipantsFromMungedParticipants(
final Bytes mungedParticipants, final int numberOfParticipants) {
final List<String> participants = new ArrayList<>();
// The participant value is enclosed in the closest multiple of 32 (for instance, 91 would be
// enclosed in 96)
final int sliceSize = mungedParticipants.size() / numberOfParticipants;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@
@RunWith(MockitoJUnitRunner.class)
public class FlexibleUtilTest {

private static final String EXPECTED_EC_PARTICIPANT_1 =
"MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAES8nC4qT/KdoAoTSF3qs/47DUsDihyVbWiRjZAiyvqp9eSDkqV1RzlM+58oOwnpFRwvWNZM+AxMVxT+MvxdsqMA==";

private static final String EXPECTED_EC_PARTICIPANT_2 =
"MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEXIgZqRA25V+3nN+Do6b5r0jiUunub6ubjPhqwHpPxP44uUYh9RKCQNRnsqCJ9PjeTnC8R3ieJk7HWAlycU1bug==";

@Test
public void testGetParticipantsFromParameter() {
final String parameterNaCl =
Expand All @@ -43,18 +49,13 @@ public void testGetParticipantsFromParameter() {

final String parameterEC =
"llol7wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFswWTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAARLycLipP8p2gChNIXeqz/jsNSwOKHJVtaJGNkCLK+qn15IOSpXVHOUz7nyg7CekVHC9Y1kz4DExXFP4y/F2yowAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAWzBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABFyIGakQNuVft5zfg6Om+a9I4lLp7m+rm4z4asB6T8T+OLlGIfUSgkDUZ7KgifT43k5wvEd4niZOx1gJcnFNW7oAAAAAAA==";
final String expectedECParticipant1 =
"MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAES8nC4qT/KdoAoTSF3qs/47DUsDihyVbWiRjZAiyvqp9eSDkqV1RzlM+58oOwnpFRwvWNZM+AxMVxT+MvxdsqMA==";

final String expectedECParticipant2 =
"MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEXIgZqRA25V+3nN+Do6b5r0jiUunub6ubjPhqwHpPxP44uUYh9RKCQNRnsqCJ9PjeTnC8R3ieJk7HWAlycU1bug==";

actualParticipants =
FlexibleUtil.getParticipantsFromParameter(Bytes.fromBase64String(parameterEC));

assertThat(actualParticipants).hasSize(2);
assertThat(actualParticipants.get(0)).isEqualTo(expectedECParticipant1);
assertThat(actualParticipants.get(1)).isEqualTo(expectedECParticipant2);
assertThat(actualParticipants.get(0)).isEqualTo(EXPECTED_EC_PARTICIPANT_1);
assertThat(actualParticipants.get(1)).isEqualTo(EXPECTED_EC_PARTICIPANT_2);

final String parameterEC2 =
"llol7wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABbMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAES8nC4qT/KdoAoTSF3qs/47DUsDihyVbWiRjZAiyvqp9eSDkqV1RzlM+58oOwnpFRwvWNZM+AxMVxT+MvxdsqMAAAAAAA";
Expand All @@ -63,18 +64,24 @@ public void testGetParticipantsFromParameter() {
FlexibleUtil.getParticipantsFromParameter(Bytes.fromBase64String(parameterEC2));

assertThat(actualParticipants).hasSize(1);
assertThat(actualParticipants.get(0)).isEqualTo(expectedECParticipant1);
assertThat(actualParticipants.get(0)).isEqualTo(EXPECTED_EC_PARTICIPANT_1);
}

@Test
public void testDecodeList() {
// FIXME review this test when the encoded list is correct for EC enclave pub keys
Bytes bytes =
final Bytes rlpEncodedOneParticipant =
Bytes.fromBase64String(
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFswWTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAARLycLipP8p2gChNIXeqz/jsNSwOKHJVtaJGNkCLK+qn15IOSpXVHOUz7nyg7CekVHC9Y1kz4DExXFP4y/F2yowAAAAAAA=");

List<String> actualParticipants = FlexiblePrivacyGroupContract.decodeList(bytes);
List<String> actualParticipants = FlexibleUtil.decodeList(rlpEncodedOneParticipant);

assertThat(actualParticipants).isEqualTo(Arrays.asList(EXPECTED_EC_PARTICIPANT_1));

final Bytes wrongBytes =
Bytes.fromBase64String(
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==");

assertThat(actualParticipants).isEqualTo(Arrays.asList(""));
actualParticipants = FlexibleUtil.decodeList(wrongBytes);
assertThat(actualParticipants).isEmpty();
}
}

0 comments on commit 2188c66

Please sign in to comment.