Skip to content

Commit

Permalink
priv_debugGetStateRoot bugfix. (hyperledger#1607)
Browse files Browse the repository at this point in the history
priv_debugGetStateRoot bugfix, refactorings and tests.

Signed-off-by: Mark Terry <mark.terry@consensys.net>
  • Loading branch information
mark-terry authored Dec 8, 2020
1 parent fe2137a commit 96a96ba
Show file tree
Hide file tree
Showing 21 changed files with 395 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
*/
package org.hyperledger.besu.tests.acceptance.dsl.privacy;

import static org.assertj.core.api.Assertions.assertThat;

import org.hyperledger.besu.tests.acceptance.dsl.WaitUtils;
import org.hyperledger.besu.tests.acceptance.dsl.condition.eth.EthConditions;
import org.hyperledger.besu.tests.acceptance.dsl.condition.net.NetConditions;
import org.hyperledger.besu.tests.acceptance.dsl.condition.priv.PrivConditions;
Expand All @@ -27,6 +30,8 @@
import org.hyperledger.besu.tests.acceptance.dsl.transaction.eth.EthTransactions;
import org.hyperledger.besu.tests.acceptance.dsl.transaction.net.NetTransactions;

import java.math.BigInteger;

import io.vertx.core.Vertx;
import org.junit.After;
import org.junit.ClassRule;
Expand Down Expand Up @@ -69,6 +74,14 @@ public PrivacyAcceptanceTestBase() {
eth = new EthConditions(ethTransactions);
}

protected void waitForBlockHeight(final PrivacyNode node, final long blockchainHeight) {
WaitUtils.waitFor(
120,
() ->
assertThat(node.execute(ethTransactions.blockNumber()))
.isGreaterThanOrEqualTo(BigInteger.valueOf(blockchainHeight)));
}

@After
public void tearDownAcceptanceTestBase() {
privacyCluster.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.hyperledger.besu.tests.acceptance.dsl.privacy.util.LogFilterJsonParameter;
import org.hyperledger.besu.tests.acceptance.dsl.transaction.privacy.EeaSendRawTransactionTransaction;
import org.hyperledger.besu.tests.acceptance.dsl.transaction.privacy.PrivCallTransaction;
import org.hyperledger.besu.tests.acceptance.dsl.transaction.privacy.PrivDebugGetStateRoot;
import org.hyperledger.besu.tests.acceptance.dsl.transaction.privacy.PrivGetCodeTransaction;
import org.hyperledger.besu.tests.acceptance.dsl.transaction.privacy.PrivGetLogsTransaction;
import org.hyperledger.besu.tests.acceptance.dsl.transaction.privacy.PrivGetTransaction;
Expand Down Expand Up @@ -136,4 +137,9 @@ public PrivGetFilterChangesTransaction getFilterChanges(
final String privacyGroupId, final String filterId) {
return new PrivGetFilterChangesTransaction(privacyGroupId, filterId);
}

public PrivDebugGetStateRoot debugGetStateRoot(
final String privacyGroupId, final String blockParam) {
return new PrivDebugGetStateRoot(privacyGroupId, blockParam);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright ConsenSys AG.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.tests.acceptance.dsl.transaction.privacy;

import static org.assertj.core.api.Assertions.assertThat;

import org.hyperledger.besu.tests.acceptance.dsl.transaction.NodeRequests;
import org.hyperledger.besu.tests.acceptance.dsl.transaction.Transaction;

import java.io.IOException;

public class PrivDebugGetStateRoot implements Transaction<PrivacyRequestFactory.DebugGetStateRoot> {

private final String privacyGroupId;
private final String blockParam;

public PrivDebugGetStateRoot(final String privacyGroupId, final String blockParam) {
this.privacyGroupId = privacyGroupId;
this.blockParam = blockParam;
}

@Override
public PrivacyRequestFactory.DebugGetStateRoot execute(final NodeRequests node) {
try {
final PrivacyRequestFactory.DebugGetStateRoot response =
node.privacy().privDebugGetStateRoot(privacyGroupId, blockParam).send();
assertThat(response).as("check response is not null").isNotNull();
return response;
} catch (final IOException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ public Integer getCount() {

public static class GetCodeResponse extends Response<String> {}

public static class DebugGetStateRoot extends Response<Hash> {}

public Request<?, PrivDistributeTransactionResponse> privDistributeTransaction(
final String signedPrivateTransaction) {
return new Request<>(
Expand Down Expand Up @@ -419,6 +421,15 @@ public Request<?, EthLog> privGetFilterChanges(
EthLog.class);
}

public Request<?, DebugGetStateRoot> privDebugGetStateRoot(
final String privacyGroupId, final String blockParam) {
return new Request<>(
"priv_debugGetStateRoot",
Arrays.asList(privacyGroupId, blockParam),
web3jService,
DebugGetStateRoot.class);
}

public static class PrivxFindPrivacyGroupResponse extends Response<List<OnChainPrivacyGroup>> {

public List<OnChainPrivacyGroup> getGroups() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright ConsenSys AG.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.tests.acceptance.privacy;

import static org.assertj.core.api.Assertions.assertThat;

import org.hyperledger.besu.ethereum.core.Hash;
import org.hyperledger.besu.tests.acceptance.dsl.privacy.PrivacyAcceptanceTestBase;
import org.hyperledger.besu.tests.acceptance.dsl.privacy.PrivacyNode;
import org.hyperledger.besu.tests.acceptance.dsl.privacy.account.PrivacyAccountResolver;
import org.hyperledger.besu.tests.acceptance.dsl.transaction.privacy.PrivacyRequestFactory;

import java.io.IOException;
import java.net.URISyntaxException;

import org.apache.tuweni.bytes.Bytes32;
import org.junit.Before;
import org.junit.Test;

public class PrivDebugGetStateRootOffchainGroupAcceptanceTest extends PrivacyAcceptanceTestBase {

private PrivacyNode aliceNode;
private PrivacyNode bobNode;

@Before
public void setUp() throws IOException, URISyntaxException {
aliceNode =
privacyBesu.createPrivateTransactionEnabledMinerNode(
"alice-node", PrivacyAccountResolver.ALICE);
bobNode =
privacyBesu.createPrivateTransactionEnabledMinerNode(
"bob-node", PrivacyAccountResolver.BOB);
privacyCluster.start(aliceNode, bobNode);
}

@Test
public void nodesInGroupShouldHaveSameStateRoot() {
final String privacyGroupId =
aliceNode.execute(
privacyTransactions.createPrivacyGroup(
"testGroup", "A group for everyone", aliceNode, bobNode));

final Hash aliceStateRootId =
aliceNode
.execute(privacyTransactions.debugGetStateRoot(privacyGroupId, "latest"))
.getResult();

final Hash bobStateRootId =
bobNode
.execute(privacyTransactions.debugGetStateRoot(privacyGroupId, "latest"))
.getResult();

assertThat(aliceStateRootId).isEqualTo(bobStateRootId);
}

@Test
public void unknownGroupShouldReturnError() {
final PrivacyRequestFactory.DebugGetStateRoot aliceResult =
aliceNode.execute(
privacyTransactions.debugGetStateRoot(
Hash.wrap(Bytes32.random()).toBase64String(), "latest"));

assertThat(aliceResult.getResult()).isNull();
assertThat(aliceResult.hasError()).isTrue();
assertThat(aliceResult.getError()).isNotNull();
assertThat(aliceResult.getError().getMessage()).contains("Error finding privacy group");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Copyright ConsenSys AG.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.tests.acceptance.privacy;

import static org.assertj.core.api.Assertions.assertThat;

import org.hyperledger.besu.ethereum.core.Address;
import org.hyperledger.besu.ethereum.core.Hash;
import org.hyperledger.besu.tests.acceptance.dsl.privacy.PrivacyNode;
import org.hyperledger.besu.tests.acceptance.dsl.privacy.account.PrivacyAccountResolver;
import org.hyperledger.besu.tests.acceptance.dsl.transaction.privacy.PrivacyRequestFactory;
import org.hyperledger.besu.tests.web3j.privacy.OnChainPrivacyAcceptanceTestBase;

import java.io.IOException;
import java.net.URISyntaxException;

import org.apache.tuweni.bytes.Bytes32;
import org.junit.Before;
import org.junit.Test;

public class PrivDebugGetStateRootOnchainGroupAcceptanceTest
extends OnChainPrivacyAcceptanceTestBase {

private PrivacyNode aliceNode;
private PrivacyNode bobNode;

@Before
public void setUp() throws IOException, URISyntaxException {
aliceNode =
privacyBesu.createOnChainPrivacyGroupEnabledMinerNode(
"alice-node", PrivacyAccountResolver.ALICE, Address.PRIVACY, false);
bobNode =
privacyBesu.createOnChainPrivacyGroupEnabledMinerNode(
"bob-node", PrivacyAccountResolver.BOB, Address.PRIVACY, false);
privacyCluster.start(aliceNode, bobNode);
}

@Test
public void nodesInGroupShouldHaveSameStateRoot() {
final String privacyGroupId = createOnChainPrivacyGroup(aliceNode, bobNode);

final Hash aliceStateRootId =
aliceNode
.execute(privacyTransactions.debugGetStateRoot(privacyGroupId, "latest"))
.getResult();

final Hash bobStateRootId =
bobNode
.execute(privacyTransactions.debugGetStateRoot(privacyGroupId, "latest"))
.getResult();

assertThat(aliceStateRootId).isEqualTo(bobStateRootId);
}

@Test
public void unknownGroupShouldReturnError() {
final PrivacyRequestFactory.DebugGetStateRoot aliceResult =
aliceNode.execute(
privacyTransactions.debugGetStateRoot(
Hash.wrap(Bytes32.random()).toBase64String(), "latest"));

assertThat(aliceResult.getResult()).isNull();
assertThat(aliceResult.hasError()).isTrue();
assertThat(aliceResult.getError()).isNotNull();
assertThat(aliceResult.getError().getMessage()).contains("Error finding privacy group");
}

@Test
public void blockParamShouldBeApplied() {
waitForBlockHeight(aliceNode, 2);
waitForBlockHeight(bobNode, 2);

final String privacyGroupId = createOnChainPrivacyGroup(aliceNode, bobNode);

waitForBlockHeight(aliceNode, 10);
waitForBlockHeight(bobNode, 10);

final Hash aliceResult1 =
aliceNode.execute(privacyTransactions.debugGetStateRoot(privacyGroupId, "1")).getResult();
final Hash bobResultInt1 =
bobNode.execute(privacyTransactions.debugGetStateRoot(privacyGroupId, "1")).getResult();

assertThat(aliceResult1).isEqualTo(bobResultInt1);

final Hash aliceResultLatest =
aliceNode
.execute(privacyTransactions.debugGetStateRoot(privacyGroupId, "latest"))
.getResult();

final Hash bobResultLatest =
bobNode
.execute(privacyTransactions.debugGetStateRoot(privacyGroupId, "latest"))
.getResult();

assertThat(aliceResultLatest).isEqualTo(bobResultLatest);
assertThat(aliceResult1).isNotEqualTo(aliceResultLatest);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public void setUp() throws IOException {
.build();
privacyParameters.setEnclavePublicKey(ENCLAVE_PUBLIC_KEY.toBase64String());
privacyController = mock(DefaultPrivacyController.class);
when(privacyController.retrieveOffChainPrivacyGroup(any(), any()))
when(privacyController.findOffChainPrivacyGroupByGroupId(any(), any()))
.thenReturn(Optional.of(new PrivacyGroup()));

privateStateRootResolver =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse;
import org.hyperledger.besu.ethereum.api.query.BlockchainQueries;
import org.hyperledger.besu.ethereum.privacy.MultiTenancyValidationException;
import org.hyperledger.besu.ethereum.privacy.PrivacyController;

import java.util.Collections;
import java.util.Optional;

import org.apache.logging.log4j.Logger;

Expand Down Expand Up @@ -68,13 +69,19 @@ protected Object resultByBlockNumber(
enclavePublicKeyProvider.getEnclaveKey(requestContext.getUser());
LOG.trace("Executing {}", this::getName);

final PrivacyGroup[] privacyGroups =
privacyController.findPrivacyGroup(
Collections.singletonList(privacyGroupId),
enclavePublicKeyProvider.getEnclaveKey(requestContext.getUser()));
final Optional<PrivacyGroup> privacyGroup;
try {
privacyGroup = privacyController.findPrivacyGroupByGroupId(privacyGroupId, enclavePublicKey);
} catch (final MultiTenancyValidationException e) {
return new JsonRpcErrorResponse(
requestContext.getRequest().getId(), FIND_PRIVACY_GROUP_ERROR);
} catch (final Exception e) {
return new JsonRpcErrorResponse(
requestContext.getRequest().getId(), JsonRpcError.INVALID_PARAMS);
}

if (privacyGroups.length == 0) {
LOG.error("Failed to fetch privacy group");
if (privacyGroup.isEmpty()) {
LOG.error("Failed to retrieve privacy group");
return new JsonRpcErrorResponse(
requestContext.getRequest().getId(), FIND_PRIVACY_GROUP_ERROR);
}
Expand All @@ -83,10 +90,11 @@ protected Object resultByBlockNumber(
.getStateRootByBlockNumber(privacyGroupId, enclavePublicKey, blockNumber)
.<JsonRpcResponse>map(
stateRootHash ->
new JsonRpcSuccessResponse(requestContext.getRequest().getId(), stateRootHash))
new JsonRpcSuccessResponse(
requestContext.getRequest().getId(), stateRootHash.toString()))
.orElse(
new JsonRpcErrorResponse(
requestContext.getRequest().getId(), JsonRpcError.INVALID_PARAMS));
requestContext.getRequest().getId(), JsonRpcError.INTERNAL_ERROR));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
try {
response =
Arrays.asList(
privacyController.findPrivacyGroup(
privacyController.findOffChainPrivacyGroupByMembers(
Arrays.asList(addresses),
enclavePublicKeyProvider.getEnclaveKey(requestContext.getUser())));
} catch (final MultiTenancyValidationException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
final List<PrivacyGroup> response;
try {
response =
privacyController.findOnChainPrivacyGroup(
privacyController.findOnChainPrivacyGroupByMembers(
Arrays.asList(addresses),
enclavePublicKeyProvider.getEnclaveKey(requestContext.getUser()));
} catch (final MultiTenancyValidationException e) {
Expand Down
Loading

0 comments on commit 96a96ba

Please sign in to comment.