Skip to content

Commit

Permalink
modified flexibleutil so it works with dynamic byte arrays, some logs…
Browse files Browse the repository at this point in the history
… to remove

Signed-off-by: Miguel Rojo <miguelangel.rojofernandez@mastercard.com>
  • Loading branch information
freemanzMrojo committed Aug 19, 2022
1 parent 0464ccc commit 2e2ea1f
Show file tree
Hide file tree
Showing 8 changed files with 904 additions and 577 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
*/
package org.hyperledger.besu.tests.acceptance.dsl.transaction.privacy;

import static com.google.common.base.Preconditions.checkArgument;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hyperledger.besu.ethereum.core.PrivacyParameters.FLEXIBLE_PRIVACY_PROXY;
Expand All @@ -41,6 +40,8 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.tuweni.bytes.Bytes;
import org.web3j.abi.FunctionEncoder;
import org.web3j.abi.datatypes.Type;
import org.web3j.crypto.Credentials;
import org.web3j.protocol.Web3jService;
import org.web3j.protocol.besu.Besu;
Expand Down Expand Up @@ -589,29 +590,19 @@ public String getTransactionHash() {
}

private Bytes encodeAddToGroupFunctionCall(final List<Bytes> participants) {
return Bytes.concatenate(
FlexibleGroupManagement.ADD_PARTICIPANTS_METHOD_SIGNATURE, encodeList(participants));
}

private Bytes encodeList(final List<Bytes> participants) {
final Bytes dynamicParameterOffset = encodeLong(32);
final Bytes length = encodeLong(participants.size());
return Bytes.concatenate(
dynamicParameterOffset,
length,
Bytes.fromHexString(
participants.stream()
.map(Bytes::toUnprefixedHexString)
.collect(Collectors.joining(""))));
}

// long to uint256, 8 bytes big endian, so left padded by 24 bytes
private static Bytes encodeLong(final long l) {
checkArgument(l >= 0, "Unsigned value must be positive");
final byte[] longBytes = new byte[8];
for (int i = 0; i < 8; i++) {
longBytes[i] = (byte) ((l >> ((7 - i) * 8)) & 0xFF);
}
return Bytes.concatenate(Bytes.wrap(new byte[24]), Bytes.wrap(longBytes));
final org.web3j.abi.datatypes.Function function =
new org.web3j.abi.datatypes.Function(
"addParticipants",
Arrays.asList(
new org.web3j.abi.datatypes.DynamicArray<>(
org.web3j.abi.datatypes.DynamicBytes.class,
org.web3j.abi.Utils.typeMap(
participants.stream()
.map(Bytes::toArrayUnsafe)
.collect(Collectors.toList()),
org.web3j.abi.datatypes.DynamicBytes.class))),
Collections.emptyList());

return Bytes.fromHexString(FunctionEncoder.encode(function));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,17 @@ public PrecompileContractResult computePrecompile(
return NO_RESULT;
}

LOG.error(
"PRIVATEFROM ANTES DE EJECUTAR: " + privateTransaction.getPrivateFrom().toBase64String());

final Optional<Bytes> maybeGroupId = privateTransaction.getPrivacyGroupId();
if (maybeGroupId.isEmpty()) {
return NO_RESULT;
}

final Bytes32 privacyGroupId = Bytes32.wrap(maybeGroupId.get());

LOG.debug("Processing private transaction {} in privacy group {}", pmtHash, privacyGroupId);
LOG.error("Processing private transaction {} in privacy group {}", pmtHash, privacyGroupId);

final ProcessableBlockHeader currentBlockHeader =
(ProcessableBlockHeader) messageFrame.getBlockValues();
Expand Down Expand Up @@ -177,6 +180,8 @@ public PrecompileContractResult computePrecompile(
return NO_RESULT;
}

LOG.error("PAYLOAD ANTES DE EJECUTAR: " + privateTransaction.getPayload().toBase64String());

final TransactionProcessingResult result =
processPrivateTransaction(
messageFrame, privateTransaction, privacyGroupId, privateWorldStateUpdater);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,18 @@ public static boolean isGroupAdditionTransaction(final PrivateTransaction privat

public static List<String> getParticipantsFromParameter(final Bytes input) {
final List<String> participants = new ArrayList<>();
final Bytes mungedParticipants = input.slice(4 + 32 + 32);
final int numberOfParticipants = input.slice(32 + 32, 4).toInt();
final int participantSize = mungedParticipants.size() / numberOfParticipants;

for (int i = 0; i <= mungedParticipants.size() - participantSize; i += participantSize) {
participants.add(mungedParticipants.slice(i, participantSize).toBase64String());
final int numberOfParticipants = input.slice(4 + 32, 32).toBigInteger().intValue();
// Method selector + offset + number of participants + (offset * number of participants)
final Bytes mungedParticipants = input.slice(4 + 32 + 32 + (32 * numberOfParticipants));
// 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;
// All the participants have to have the same size, so it is enough to check the first one
final int participantSize = mungedParticipants.slice(0, 32).toBigInteger().intValue();

for (int i = 0; i <= mungedParticipants.size() - sliceSize; i += sliceSize) {
// The size of each participant (as of now, either 32 or 91) is stored in 32 bytes
participants.add(mungedParticipants.slice(i + 32, participantSize).toBase64String());
}

return participants;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,41 +30,51 @@ public class FlexibleUtilTest {
@Test
public void testGetParticipantsFromParameter() {
final String parameterNaCl =
"0xb4926e2500000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000002035695b4cc4b0941e60551d7a19cf30603db5bfc23e5ac43a56f57f25f75486a2a8d9b56a0fe9cd94d60be4413bcb721d3a7be27ed8e28b3a6346df874ee141b";
"llol7wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACADVpW0zEsJQeYFUdehnPMGA9tb/CPlrEOlb1fyX3VIagAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgKo2bVqD+nNlNYL5EE7y3IdOnviftjiizpjRt+HTuFBs=";
final String expectedNaClParticipant1 = "A1aVtMxLCUHmBVHXoZzzBgPbW/wj5axDpW9X8l91SGo=";
final String expectedNaClParticipant2 = "Ko2bVqD+nNlNYL5EE7y3IdOnviftjiizpjRt+HTuFBs=";

List<String> actualParticipants =
FlexibleUtil.getParticipantsFromParameter(Bytes.fromHexString(parameterNaCl));
FlexibleUtil.getParticipantsFromParameter(Bytes.fromBase64String(parameterNaCl));

assertThat(actualParticipants).hasSize(2);
assertThat(actualParticipants.get(0)).isEqualTo(expectedNaClParticipant1);
assertThat(actualParticipants.get(1)).isEqualTo(expectedNaClParticipant2);

final String parameterEC =
"0xb4926e25000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000023059301306072a8648ce3d020106082a8648ce3d030107034200044bc9c2e2a4ff29da00a13485deab3fe3b0d4b038a1c956d68918d9022cafaa9f5e48392a57547394cfb9f283b09e9151c2f58d64cf80c4c5714fe32fc5db2a303059301306072a8648ce3d020106082a8648ce3d030107034200045c8819a91036e55fb79cdf83a3a6f9af48e252e9ee6fab9b8cf86ac07a4fc4fe38b94621f5128240d467b2a089f4f8de4e70bc47789e264ec7580972714d5bba";
"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.fromHexString(parameterEC));
FlexibleUtil.getParticipantsFromParameter(Bytes.fromBase64String(parameterEC));

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

final String parameterEC2 =
"llol7wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABbMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAES8nC4qT/KdoAoTSF3qs/47DUsDihyVbWiRjZAiyvqp9eSDkqV1RzlM+58oOwnpFRwvWNZM+AxMVxT+MvxdsqMAAAAAAA";

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

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

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

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

assertThat(salida).isEqualTo(Arrays.asList(""));
assertThat(actualParticipants).isEqualTo(Arrays.asList(""));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import java.util.stream.Collectors;

import org.apache.tuweni.bytes.Bytes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* A skeletal class for instantiating message processors.
Expand Down Expand Up @@ -69,6 +71,7 @@ public abstract class AbstractMessageProcessor {

// List of addresses to force delete when they are touched but empty
// when the state changes in the message are were not meant to be committed.
private static final Logger LOG = LoggerFactory.getLogger(AbstractMessageProcessor.class);
private final Collection<? super Address> forceDeleteAccountsWhenEmpty;
private final EVM evm;

Expand Down Expand Up @@ -166,17 +169,20 @@ private void codeExecute(final MessageFrame frame, final OperationTracer operati

public void process(final MessageFrame frame, final OperationTracer operationTracer) {
if (frame.getState() == MessageFrame.State.NOT_STARTED) {
LOG.error("PAYLOAD SI NOT_STARTED: " + frame.getInputData().toBase64String());
start(frame, operationTracer);
}

if (frame.getState() == MessageFrame.State.CODE_EXECUTING) {
LOG.error("PAYLOAD SI CODE_EXECUTING: " + frame.getInputData().toBase64String());
codeExecute(frame, operationTracer);

if (frame.getState() == MessageFrame.State.CODE_SUSPENDED) {
return;
}

if (frame.getState() == MessageFrame.State.CODE_SUCCESS) {
LOG.error("PAYLOAD SI CODE_SUCCESS: " + frame.getInputData().toBase64String());
codeSuccess(frame, operationTracer);
}
}
Expand All @@ -186,14 +192,17 @@ public void process(final MessageFrame frame, final OperationTracer operationTra
}

if (frame.getState() == MessageFrame.State.REVERT) {
LOG.error("PAYLOAD SI REVERT: " + frame.getInputData().toBase64String());
revert(frame);
}

if (frame.getState() == MessageFrame.State.COMPLETED_SUCCESS) {
LOG.error("PAYLOAD SI COMPLETED_SUCCESS: " + frame.getInputData().toBase64String());
completedSuccess(frame);
}

if (frame.getState() == MessageFrame.State.COMPLETED_FAILED) {
LOG.error("PAYLOAD SI COMPLETED_FAILED: " + frame.getInputData().toBase64String());
completedFailed(frame);
}
}
Expand Down
Loading

0 comments on commit 2e2ea1f

Please sign in to comment.