Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Priv call #250

Merged
merged 23 commits into from
Jan 7, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -72,7 +72,7 @@ public String execute(final NodeRequests node) {
encodedFunction,
null)
.getTransactionHash();
} catch (IOException e) {
} catch (final IOException e) {
throw new RuntimeException(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class LoadPrivateSmartContractTransaction<T extends Contract> implements
private final long chainId;
private final Base64String privateFrom;
private final List<Base64String> privateFor;
private String contractAddress;
private final String contractAddress;

public LoadPrivateSmartContractTransaction(
final String contractAddress,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import java.io.IOException;

public class PrivDistributeTransactionTransaction implements Transaction<String> {
private String signedPrivateTransaction;
private final String signedPrivateTransaction;

public PrivDistributeTransactionTransaction(final String signedPrivateTransaction) {
this.signedPrivateTransaction = signedPrivateTransaction;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* 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.web3j.privacy;

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

import org.hyperledger.besu.tests.acceptance.dsl.privacy.PrivacyAcceptanceTestBase;
import org.hyperledger.besu.tests.acceptance.dsl.privacy.PrivacyNode;
import org.hyperledger.besu.tests.web3j.generated.EventEmitter;

import java.math.BigInteger;
import java.util.Arrays;
import java.util.List;

import org.jetbrains.annotations.NotNull;
import org.junit.Before;
import org.junit.Test;
import org.web3j.abi.FunctionEncoder;
import org.web3j.abi.TypeReference;
import org.web3j.abi.datatypes.Function;
import org.web3j.abi.datatypes.generated.Uint256;
import org.web3j.protocol.core.Request;
import org.web3j.protocol.core.methods.request.Transaction;
import org.web3j.protocol.core.methods.response.EthCall;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.protocol.http.HttpService;
import org.web3j.tx.Contract;

public class PrivCallAcceptanceTest extends PrivacyAcceptanceTestBase {

private static final long POW_CHAIN_ID = 2018;
private static final int VALUE = 1024;

private PrivacyNode minerNode;

@Before
public void setUp() throws Exception {
minerNode =
privacyBesu.createPrivateTransactionEnabledMinerNode(
"miner-node", privacyAccountResolver.resolve(0));
privacyCluster.start(minerNode);
}

@Test
public void deployingAndDoPrivCallMust() throws Exception {

final String privacyGroupId =
minerNode.execute(
privacyTransactions.createPrivacyGroup(
"myGroupName", "my group description", minerNode));

final EventEmitter eventEmitter =
minerNode.execute(
privateContractTransactions.createSmartContractWithPrivacyGroupId(
EventEmitter.class,
minerNode.getTransactionSigningKey(),
POW_CHAIN_ID,
minerNode.getEnclaveKey(),
privacyGroupId));

privateContractVerifier
.validPrivateContractDeployed(
eventEmitter.getContractAddress(), minerNode.getAddress().toString())
.verify(eventEmitter);

final Request<Object, EthCall> priv_call = privCall(privacyGroupId, eventEmitter);

EthCall resp = priv_call.send();

String value = resp.getValue();
assertThat(new BigInteger(value.substring(2), 16)).isEqualByComparingTo(BigInteger.ZERO);

final TransactionReceipt receipt = eventEmitter.store(BigInteger.valueOf(VALUE)).send();
assertThat(receipt).isNotNull();

resp = priv_call.send();
value = resp.getValue();
assertThat(new BigInteger(value.substring(2), 16))
.isEqualByComparingTo(BigInteger.valueOf(VALUE));
}

@NotNull
private Request<Object, EthCall> privCall(
final String privacyGroupId, final Contract eventEmitter) {

final Function function =
new Function(
"value", List.of(), Arrays.<TypeReference<?>>asList(new TypeReference<Uint256>() {}));

final String encoded = FunctionEncoder.encode(function);

final HttpService httpService =
new HttpService(
"http://"
+ minerNode.getBesu().getHostName()
+ ":"
+ minerNode.getBesu().getJsonRpcSocketPort().get());

final Transaction transaction =
Transaction.createEthCallTransaction(null, eventEmitter.getContractAddress(), encoded);

return new Request<>(
"priv_call",
Arrays.asList(privacyGroupId, transaction, "latest"),
httpService,
EthCall.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@
import org.hyperledger.besu.metrics.prometheus.MetricsConfiguration;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;

Expand Down Expand Up @@ -98,6 +100,11 @@ public Map<String, JsonRpcMethod> methods() {
final WebSocketConfiguration webSocketConfiguration = mock(WebSocketConfiguration.class);
final MetricsConfiguration metricsConfiguration = mock(MetricsConfiguration.class);

final List<RpcApi> apis = new ArrayList<>();
apis.add(RpcApis.ETH);
apis.add(RpcApis.NET);
apis.add(RpcApis.WEB3);
apis.add(RpcApis.PRIV);
return new JsonRpcMethodsFactory()
.methods(
CLIENT_VERSION,
Expand All @@ -114,7 +121,7 @@ public Map<String, JsonRpcMethod> methods() {
new HashSet<>(),
accountWhitelistController,
nodeWhitelistController,
RpcApis.DEFAULT_JSON_RPC_APIS,
apis,
privacyParameters,
jsonRpcConfiguration,
webSocketConfiguration,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public class PrivGetPrivateTransactionIntegrationTest {

private final Transaction justTransaction = mock(Transaction.class);

private static Vertx vertx = Vertx.vertx();
private static final Vertx vertx = Vertx.vertx();

@BeforeClass
public static void setUpOnce() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public enum RpcMethod {
DEBUG_TRACE_BLOCK_BY_HASH("debug_traceBlockByHash"),
DEBUG_TRACE_BLOCK_BY_NUMBER("debug_traceBlockByNumber"),
DEBUG_TRACE_TRANSACTION("debug_traceTransaction"),
PRIV_CALL("priv_call"),
PRIV_GET_PRIVATE_TRANSACTION("priv_getPrivateTransaction"),
PRIV_GET_TRANSACTION_COUNT("priv_getTransactionCount"),
PRIV_GET_PRIVACY_PRECOMPILE_ADDRESS("priv_getPrivacyPrecompileAddress"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* 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.ethereum.api.jsonrpc.internal.privacy.methods.priv;

import org.hyperledger.besu.ethereum.api.jsonrpc.JsonRpcErrorConverter;
import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.exception.InvalidJsonRpcParameters;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.AbstractBlockParameterMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.BlockParameter;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.JsonCallParameter;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse;
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.PrivateTransactionSimulator;
import org.hyperledger.besu.ethereum.transaction.CallParameter;

public class PrivCall extends AbstractBlockParameterMethod {

private final PrivateTransactionSimulator privateTransactionSimulator;

public PrivCall(
final BlockchainQueries blockchainQueries,
final PrivateTransactionSimulator privateTransactionSimulator) {
super(blockchainQueries);
this.privateTransactionSimulator = privateTransactionSimulator;
}

@Override
public String getName() {
return RpcMethod.PRIV_CALL.getMethodName();
}

@Override
protected BlockParameter blockParameter(final JsonRpcRequestContext request) {
return request.getRequiredParameter(2, BlockParameter.class);
}

@Override
protected Object resultByBlockNumber(
final JsonRpcRequestContext request, final long blockNumber) {
final CallParameter callParams = validateAndGetCallParams(request);
final String privacyGroupId = request.getRequiredParameter(0, String.class);

// TODO: if multi-tenancy is enabled we need to check whether access to the privacyGroup can be
// granted!
return privateTransactionSimulator
.process(privacyGroupId, callParams, blockNumber)
.map(
result ->
result
.getValidationResult()
.either(
(() ->
new JsonRpcSuccessResponse(
request.getRequest().getId(), result.getOutput().toString())),
reason ->
new JsonRpcErrorResponse(
request.getRequest().getId(),
JsonRpcErrorConverter.convertTransactionInvalidReason(reason))))
.orElse(validRequestBlockNotFound(request));
}

private JsonRpcSuccessResponse validRequestBlockNotFound(final JsonRpcRequestContext request) {
return new JsonRpcSuccessResponse(request.getRequest().getId(), null);
}

@Override
public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
return (JsonRpcResponse) findResultByParamType(requestContext);
}

private CallParameter validateAndGetCallParams(final JsonRpcRequestContext request) {
final JsonCallParameter callParams = request.getRequiredParameter(1, JsonCallParameter.class);
if (callParams.getTo() == null) {
throw new InvalidJsonRpcParameters("Missing \"to\" field in call arguments");
}
return callParams;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.hyperledger.besu.ethereum.api.jsonrpc.RpcApi;
import org.hyperledger.besu.ethereum.api.jsonrpc.RpcApis;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.priv.PrivCall;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.priv.PrivCreatePrivacyGroup;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.priv.PrivDeletePrivacyGroup;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.priv.PrivDistributeRawTransaction;
Expand All @@ -30,6 +31,7 @@
import org.hyperledger.besu.ethereum.eth.transactions.TransactionPool;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.ethereum.privacy.PrivateTransactionHandler;
import org.hyperledger.besu.ethereum.privacy.PrivateTransactionSimulator;

import java.util.Map;

Expand Down Expand Up @@ -60,6 +62,13 @@ protected Map<String, JsonRpcMethod> create(
new PrivGetTransactionCount(getPrivacyParameters(), privateTransactionHandler),
new PrivGetPrivateTransaction(getBlockchainQueries(), getPrivacyParameters()),
new PrivDistributeRawTransaction(
getPrivacyParameters(), privateTransactionHandler, getTransactionPool()));
getPrivacyParameters(), privateTransactionHandler, getTransactionPool()),
new PrivCall(
getBlockchainQueries(),
new PrivateTransactionSimulator(
getBlockchainQueries().getBlockchain(),
getBlockchainQueries().getWorldStateArchive(),
getProtocolSchedule(),
getPrivacyParameters())));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public class EthGetTransactionReceiptTest {
null,
null,
null,
null,
BlockHeader::getCoinbase,
null,
false,
Expand All @@ -111,6 +112,7 @@ public class EthGetTransactionReceiptTest {
null,
null,
null,
null,
BlockHeader::getCoinbase,
null,
false,
Expand Down
Loading