Skip to content

Commit

Permalink
Merge branch 'main' of github.com:hyperledger/besu into json-rpc-plugin
Browse files Browse the repository at this point in the history
Signed-off-by: Antony Denyer <[email protected]>
  • Loading branch information
antonydenyer committed Sep 30, 2021
2 parents 7553651 + bb11e21 commit 05479e4
Show file tree
Hide file tree
Showing 24 changed files with 392 additions and 78 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
- Set an idle timeout for metrics connections, to clean up ports when no longer used [\#2748](https://github.com/hyperledger/besu/pull/2748)
- Onchain privacy groups can be unlocked after being locked without having to add a participant [\#2693](https://github.com/hyperledger/besu/pull/2693)
- Update Gas Schedule for Ethereum Classic [#2746](https://github.com/hyperledger/besu/pull/2746)
- Fix bug with private contracts not able to call public contracts that call public contracts [#2816](https://github.com/hyperledger/besu/pull/2816)

### Early Access Features
- \[EXPERIMENTAL\] Added support for QBFT with PKI-backed Block Creation. [#2647](https://github.com/hyperledger/besu/issues/2647)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import org.hyperledger.besu.tests.acceptance.dsl.privacy.PrivacyNode;
import org.hyperledger.besu.tests.web3j.generated.CrossContractReader;
import org.hyperledger.besu.tests.web3j.generated.EventEmitter;
import org.hyperledger.besu.tests.web3j.generated.RemoteSimpleStorage;
import org.hyperledger.besu.tests.web3j.generated.SimpleStorage;
import org.hyperledger.enclave.testutil.EnclaveType;

import java.io.IOException;
Expand Down Expand Up @@ -169,4 +171,43 @@ public void privateContractMustNotBeAbleToCallSelfDestructOnPublicContract() thr
.returns(
"0x", e -> ((PrivateTransactionReceipt) e.getTransactionReceipt().get()).getOutput());
}

@Test
public void privateContractCanCallPublicContractThatCallsPublicContract() throws Exception {
final SimpleStorage simpleStorage =
transactionNode
.getBesu()
.execute(contractTransactions.createSmartContract(SimpleStorage.class));

final RemoteSimpleStorage remoteSimpleStorage =
transactionNode
.getBesu()
.execute(contractTransactions.createSmartContract(RemoteSimpleStorage.class));

remoteSimpleStorage.setRemote(simpleStorage.getContractAddress()).send();

final RemoteSimpleStorage reallyRemoteSimpleStorage =
transactionNode
.getBesu()
.execute(contractTransactions.createSmartContract(RemoteSimpleStorage.class));

reallyRemoteSimpleStorage.setRemote(remoteSimpleStorage.getContractAddress()).send();

simpleStorage.set(BigInteger.valueOf(42)).send();

assertThat(simpleStorage.get().send()).isEqualTo(BigInteger.valueOf(42));
assertThat(remoteSimpleStorage.get().send()).isEqualTo(BigInteger.valueOf(42));
assertThat(reallyRemoteSimpleStorage.get().send()).isEqualTo(BigInteger.valueOf(42));

final RemoteSimpleStorage privateRemoteSimpleStorage =
transactionNode.execute(
privateContractTransactions.createSmartContract(
RemoteSimpleStorage.class,
transactionNode.getTransactionSigningKey(),
transactionNode.getEnclaveKey()));

privateRemoteSimpleStorage.setRemote(reallyRemoteSimpleStorage.getContractAddress()).send();

assertThat(privateRemoteSimpleStorage.get().send()).isEqualTo(BigInteger.valueOf(42));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright contributors to Hyperledger Besu
*
* 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
*/
pragma solidity >=0.4.0 <0.6.0;

import "./SimpleStorage.sol";

// compile with:
// solc RemoteSimpleStorage.sol --bin --abi --optimize --overwrite -o .
// then create web3j wrappers with:
// web3j solidity generate -b ./generated/RemoteSimpleStorage.bin -a ./generated/RemoteSimpleStorage.abi -o ../../../../../ -p org.hyperledger.besu.tests.web3j.generated
contract RemoteSimpleStorage {
SimpleStorage public simpleStorage;

function setRemote(SimpleStorage _simpleStorage) public {
simpleStorage = _simpleStorage;
}

function set(uint value) public {
simpleStorage.set(value);
}

function get() public view returns (uint) {
return simpleStorage.get();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
/*
* Copyright contributors to Hyperledger Besu
*
* 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.generated;

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

import org.web3j.abi.TypeReference;
import org.web3j.abi.datatypes.Address;
import org.web3j.abi.datatypes.Function;
import org.web3j.abi.datatypes.Type;
import org.web3j.abi.datatypes.generated.Uint256;
import org.web3j.crypto.Credentials;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.RemoteCall;
import org.web3j.protocol.core.RemoteFunctionCall;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.tx.Contract;
import org.web3j.tx.TransactionManager;
import org.web3j.tx.gas.ContractGasProvider;

/**
* Auto generated code.
*
* <p><strong>Do not modify!</strong>
*
* <p>Please use the <a href="https://docs.web3j.io/command_line.html">web3j command line tools</a>,
* or the org.web3j.codegen.SolidityFunctionWrapperGenerator in the <a
* href="https://github.com/web3j/web3j/tree/master/codegen">codegen module</a> to update.
*
* <p>Generated with web3j version 4.5.0.
*/
@SuppressWarnings("rawtypes")
public class RemoteSimpleStorage extends Contract {
private static final String BINARY =
"608060405234801561001057600080fd5b5061034c806100206000396000f3fe608060405234801561001057600080fd5b5060043610610069576000357c01000000000000000000000000000000000000000000000000000000009004806360fe47b11461006e5780636be0d6bf1461009c5780636d4ce63c146100e6578063f1efe24c14610104575b600080fd5b61009a6004803603602081101561008457600080fd5b8101908080359060200190929190505050610148565b005b6100a46101f3565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b6100ee610218565b6040518082815260200191505060405180910390f35b6101466004803603602081101561011a57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff1690602001909291905050506102dd565b005b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166360fe47b1826040518263ffffffff167c010000000000000000000000000000000000000000000000000000000002815260040180828152602001915050600060405180830381600087803b1580156101d857600080fd5b505af11580156101ec573d6000803e3d6000fd5b5050505050565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16636d4ce63c6040518163ffffffff167c010000000000000000000000000000000000000000000000000000000002815260040160206040518083038186803b15801561029d57600080fd5b505afa1580156102b1573d6000803e3d6000fd5b505050506040513d60208110156102c757600080fd5b8101908080519060200190929190505050905090565b806000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055505056fea165627a7a723058209de47b3e814c56fb80861a580d360af8738753c53cc6e71163f687ed3eed570f0029";

public static final String FUNC_SET = "set";

public static final String FUNC_SIMPLESTORAGE = "simpleStorage";

public static final String FUNC_GET = "get";

public static final String FUNC_SETREMOTE = "setRemote";

@Deprecated
protected RemoteSimpleStorage(
String contractAddress,
Web3j web3j,
Credentials credentials,
BigInteger gasPrice,
BigInteger gasLimit) {
super(BINARY, contractAddress, web3j, credentials, gasPrice, gasLimit);
}

protected RemoteSimpleStorage(
String contractAddress,
Web3j web3j,
Credentials credentials,
ContractGasProvider contractGasProvider) {
super(BINARY, contractAddress, web3j, credentials, contractGasProvider);
}

@Deprecated
protected RemoteSimpleStorage(
String contractAddress,
Web3j web3j,
TransactionManager transactionManager,
BigInteger gasPrice,
BigInteger gasLimit) {
super(BINARY, contractAddress, web3j, transactionManager, gasPrice, gasLimit);
}

protected RemoteSimpleStorage(
String contractAddress,
Web3j web3j,
TransactionManager transactionManager,
ContractGasProvider contractGasProvider) {
super(BINARY, contractAddress, web3j, transactionManager, contractGasProvider);
}

public RemoteFunctionCall<TransactionReceipt> set(BigInteger value) {
final Function function =
new Function(
FUNC_SET,
Arrays.<Type>asList(new org.web3j.abi.datatypes.generated.Uint256(value)),
Collections.<TypeReference<?>>emptyList());
return executeRemoteCallTransaction(function);
}

public RemoteFunctionCall<String> simpleStorage() {
final Function function =
new Function(
FUNC_SIMPLESTORAGE,
Arrays.<Type>asList(),
Arrays.<TypeReference<?>>asList(new TypeReference<Address>() {}));
return executeRemoteCallSingleValueReturn(function, String.class);
}

public RemoteFunctionCall<BigInteger> get() {
final Function function =
new Function(
FUNC_GET,
Arrays.<Type>asList(),
Arrays.<TypeReference<?>>asList(new TypeReference<Uint256>() {}));
return executeRemoteCallSingleValueReturn(function, BigInteger.class);
}

public RemoteFunctionCall<TransactionReceipt> setRemote(String _simpleStorage) {
final Function function =
new Function(
FUNC_SETREMOTE,
Arrays.<Type>asList(new org.web3j.abi.datatypes.Address(160, _simpleStorage)),
Collections.<TypeReference<?>>emptyList());
return executeRemoteCallTransaction(function);
}

@Deprecated
public static RemoteSimpleStorage load(
String contractAddress,
Web3j web3j,
Credentials credentials,
BigInteger gasPrice,
BigInteger gasLimit) {
return new RemoteSimpleStorage(contractAddress, web3j, credentials, gasPrice, gasLimit);
}

@Deprecated
public static RemoteSimpleStorage load(
String contractAddress,
Web3j web3j,
TransactionManager transactionManager,
BigInteger gasPrice,
BigInteger gasLimit) {
return new RemoteSimpleStorage(contractAddress, web3j, transactionManager, gasPrice, gasLimit);
}

public static RemoteSimpleStorage load(
String contractAddress,
Web3j web3j,
Credentials credentials,
ContractGasProvider contractGasProvider) {
return new RemoteSimpleStorage(contractAddress, web3j, credentials, contractGasProvider);
}

public static RemoteSimpleStorage load(
String contractAddress,
Web3j web3j,
TransactionManager transactionManager,
ContractGasProvider contractGasProvider) {
return new RemoteSimpleStorage(contractAddress, web3j, transactionManager, contractGasProvider);
}

public static RemoteCall<RemoteSimpleStorage> deploy(
Web3j web3j, Credentials credentials, ContractGasProvider contractGasProvider) {
return deployRemoteCall(
RemoteSimpleStorage.class, web3j, credentials, contractGasProvider, BINARY, "");
}

@Deprecated
public static RemoteCall<RemoteSimpleStorage> deploy(
Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {
return deployRemoteCall(
RemoteSimpleStorage.class, web3j, credentials, gasPrice, gasLimit, BINARY, "");
}

public static RemoteCall<RemoteSimpleStorage> deploy(
Web3j web3j, TransactionManager transactionManager, ContractGasProvider contractGasProvider) {
return deployRemoteCall(
RemoteSimpleStorage.class, web3j, transactionManager, contractGasProvider, BINARY, "");
}

@Deprecated
public static RemoteCall<RemoteSimpleStorage> deploy(
Web3j web3j,
TransactionManager transactionManager,
BigInteger gasPrice,
BigInteger gasLimit) {
return deployRemoteCall(
RemoteSimpleStorage.class, web3j, transactionManager, gasPrice, gasLimit, BINARY, "");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public void setUp() throws IOException {
.build();
privacyParameters.setPrivacyUserId(ENCLAVE_PUBLIC_KEY.toBase64String());
privacyController = mock(RestrictedDefaultPrivacyController.class);
when(privacyController.findOffChainPrivacyGroupByGroupId(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 @@ -62,7 +62,7 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
try {
response =
Arrays.asList(
privacyController.findOffChainPrivacyGroupByMembers(
privacyController.findOffchainPrivacyGroupByMembers(
Arrays.asList(addresses),
privacyIdProvider.getPrivacyUserId(requestContext.getUser())));
} catch (final MultiTenancyValidationException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void validPantheonPrivacyGroupTransactionIsSentToTransactionPool() {
new PrivacyGroup(
"", PrivacyGroup.Type.PANTHEON, "", "", singletonList(ENCLAVE_PUBLIC_KEY)));

when(privacyController.findOffChainPrivacyGroupByGroupId(any(), any()))
when(privacyController.findOffchainPrivacyGroupByGroupId(any(), any()))
.thenReturn(pantheonPrivacyGroup);
when(transactionPool.addLocalTransaction(any())).thenReturn(ValidationResult.valid());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public void transactionFailsForLegacyPrivateTransaction() {
}

@Test
public void offChainPrivacyGroupTransactionFailsWhenOnchainPrivacyGroupFeatureIsEnabled() {
public void offchainPrivacyGroupTransactionFailsWhenOnchainPrivacyGroupFeatureIsEnabled() {
when(privacyController.validatePrivateTransaction(any(), any()))
.thenReturn(ValidationResult.valid());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void shouldThrowInvalidJsonRpcParametersExceptionWhenNoPrivacyGroup() {
public void shouldReturnErrorIfInvalidGroupId() {
when(privacyController.findPrivacyGroupByGroupId(anyString(), anyString()))
.thenCallRealMethod();
when(privacyController.findOffChainPrivacyGroupByGroupId(anyString(), anyString()))
when(privacyController.findOffchainPrivacyGroupByGroupId(anyString(), anyString()))
.thenReturn(Optional.empty());
final JsonRpcResponse response = method.response(request("not_base64", "latest"));
assertThat(response.getType()).isEqualByComparingTo(JsonRpcResponseType.ERROR);
Expand Down Expand Up @@ -123,7 +123,7 @@ public void shouldReturnErrorIfUnableToFindStateRoot() {
}

@Test
public void shouldReturnSuccessWhenOffChainGroupExists() {
public void shouldReturnSuccessWhenOffchainGroupExists() {
final Hash hash = Hash.EMPTY_LIST_HASH;
when(privacyController.findPrivacyGroupByGroupId(anyString(), anyString()))
.thenReturn(Optional.of(PRIVACY_GROUP));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void setUp() {
@SuppressWarnings("unchecked")
@Test
public void findsPrivacyGroupWithValidAddresses() {
when(privacyController.findOffChainPrivacyGroupByMembers(ADDRESSES, ENCLAVE_PUBLIC_KEY))
when(privacyController.findOffchainPrivacyGroupByMembers(ADDRESSES, ENCLAVE_PUBLIC_KEY))
.thenReturn(new PrivacyGroup[] {privacyGroup});

final PrivFindPrivacyGroup privFindPrivacyGroup =
Expand All @@ -88,25 +88,25 @@ public void findsPrivacyGroupWithValidAddresses() {
final List<PrivacyGroup> result = (List<PrivacyGroup>) response.getResult();
assertThat(result).hasSize(1);
assertThat(result.get(0)).isEqualToComparingFieldByField(privacyGroup);
verify(privacyController).findOffChainPrivacyGroupByMembers(ADDRESSES, ENCLAVE_PUBLIC_KEY);
verify(privacyController).findOffchainPrivacyGroupByMembers(ADDRESSES, ENCLAVE_PUBLIC_KEY);
}

@Test
public void failsWithFindPrivacyGroupErrorIfEnclaveFails() {
when(privacyController.findOffChainPrivacyGroupByMembers(ADDRESSES, ENCLAVE_PUBLIC_KEY))
when(privacyController.findOffchainPrivacyGroupByMembers(ADDRESSES, ENCLAVE_PUBLIC_KEY))
.thenThrow(new EnclaveClientException(500, "some failure"));
final PrivFindPrivacyGroup privFindPrivacyGroup =
new PrivFindPrivacyGroup(privacyController, privacyIdProvider);

final JsonRpcErrorResponse response =
(JsonRpcErrorResponse) privFindPrivacyGroup.response(request);
assertThat(response.getError()).isEqualTo(JsonRpcError.FIND_PRIVACY_GROUP_ERROR);
verify(privacyController).findOffChainPrivacyGroupByMembers(ADDRESSES, ENCLAVE_PUBLIC_KEY);
verify(privacyController).findOffchainPrivacyGroupByMembers(ADDRESSES, ENCLAVE_PUBLIC_KEY);
}

@Test
public void failsWithUnauthorizedErrorIfMultiTenancyValidationFails() {
when(privacyController.findOffChainPrivacyGroupByMembers(ADDRESSES, ENCLAVE_PUBLIC_KEY))
when(privacyController.findOffchainPrivacyGroupByMembers(ADDRESSES, ENCLAVE_PUBLIC_KEY))
.thenThrow(new MultiTenancyValidationException("validation failed"));
final PrivFindPrivacyGroup privFindPrivacyGroup =
new PrivFindPrivacyGroup(privacyController, privacyIdProvider);
Expand All @@ -116,6 +116,6 @@ public void failsWithUnauthorizedErrorIfMultiTenancyValidationFails() {
request.getRequest().getId(), JsonRpcError.FIND_PRIVACY_GROUP_ERROR);
final JsonRpcResponse response = privFindPrivacyGroup.response(request);
assertThat(response).isEqualTo(expectedResponse);
verify(privacyController).findOffChainPrivacyGroupByMembers(ADDRESSES, ENCLAVE_PUBLIC_KEY);
verify(privacyController).findOffchainPrivacyGroupByMembers(ADDRESSES, ENCLAVE_PUBLIC_KEY);
}
}
Loading

0 comments on commit 05479e4

Please sign in to comment.