mirror of https://github.com/hyperledger/besu
[PAN-3122] Allow private contracts to access public state (#9)
* Allow private contracts to access public state Signed-off-by: Joshua Richardson <joshua@richardson.tech>pull/62/head
parent
c09145e490
commit
1ca84871e4
@ -0,0 +1,60 @@ |
||||
/* |
||||
* Copyright 2019 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. |
||||
*/ |
||||
pragma solidity >=0.4.0 <0.6.0; |
||||
|
||||
import "./EventEmitter.sol"; |
||||
|
||||
// compile with: |
||||
// solc CrossContractReader.sol --bin --abi --optimize --overwrite -o . |
||||
// then create web3j wrappers with: |
||||
// web3j solidity generate -b ./generated/CrossContractReader.bin -a ./generated/CrossContractReader.abi -o ../../../../../ -p org.hyperledger.besu.tests.web3j.generated |
||||
contract CrossContractReader { |
||||
uint counter; |
||||
|
||||
event NewEventEmitter( |
||||
address contractAddress |
||||
); |
||||
|
||||
function read(address emitter_address) view public returns (uint) { |
||||
EventEmitter em = EventEmitter(emitter_address); |
||||
return em.value(); |
||||
} |
||||
|
||||
function deploy() public { |
||||
EventEmitter em = new EventEmitter(); |
||||
emit NewEventEmitter(address(em)); |
||||
} |
||||
|
||||
function deployRemote(address crossAddress) public { |
||||
CrossContractReader cross = CrossContractReader(crossAddress); |
||||
cross.deploy(); |
||||
} |
||||
|
||||
function increment() public { |
||||
counter++; |
||||
} |
||||
|
||||
function incrementRemote(address crossAddress) public { |
||||
CrossContractReader cross = CrossContractReader(crossAddress); |
||||
cross.increment(); |
||||
} |
||||
|
||||
function destroy() public { |
||||
selfdestruct(msg.sender); |
||||
} |
||||
|
||||
function remoteDestroy(address crossAddress) public { |
||||
CrossContractReader cross = CrossContractReader(crossAddress); |
||||
cross.destroy(); |
||||
} |
||||
} |
@ -0,0 +1 @@ |
||||
[{"constant":false,"inputs":[{"name":"crossAddress","type":"address"}],"name":"remoteDestroy","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"crossAddress","type":"address"}],"name":"deployRemote","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[],"name":"deploy","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[],"name":"destroy","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"emitter_address","type":"address"}],"name":"read","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"increment","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"crossAddress","type":"address"}],"name":"incrementRemote","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"anonymous":false,"inputs":[{"indexed":false,"name":"contractAddress","type":"address"}],"name":"NewEventEmitter","type":"event"}] |
@ -0,0 +1 @@ |
||||
608060405234801561001057600080fd5b506104b7806100206000396000f3fe608060405234801561001057600080fd5b506004361061007d5760003560e01c806383197ef01161005b57806383197ef0146100d8578063a087a87e146100e0578063d09de08a14610118578063e689ef8a146101205761007d565b8063305155f9146100825780635374ded2146100aa578063775c300c146100d0575b600080fd5b6100a86004803603602081101561009857600080fd5b50356001600160a01b0316610146565b005b6100a8600480360360208110156100c057600080fd5b50356001600160a01b03166101a2565b6100a86101e2565b6100a8610250565b610106600480360360208110156100f657600080fd5b50356001600160a01b0316610253565b60408051918252519081900360200190f35b6100a86102c5565b6100a86004803603602081101561013657600080fd5b50356001600160a01b03166102d0565b6000819050806001600160a01b03166383197ef06040518163ffffffff1660e01b8152600401600060405180830381600087803b15801561018657600080fd5b505af115801561019a573d6000803e3d6000fd5b505050505050565b6000819050806001600160a01b031663775c300c6040518163ffffffff1660e01b8152600401600060405180830381600087803b15801561018657600080fd5b60006040516101f090610310565b604051809103906000f08015801561020c573d6000803e3d6000fd5b50604080516001600160a01b038316815290519192507f9ac6876e0aa40667ffeaa9b359b5ed924f4cdd0e029eb6e9c369e78c68f711fb919081900360200190a150565b33ff5b600080829050806001600160a01b0316633fa4f2456040518163ffffffff1660e01b815260040160206040518083038186803b15801561029257600080fd5b505afa1580156102a6573d6000803e3d6000fd5b505050506040513d60208110156102bc57600080fd5b50519392505050565b600080546001019055565b6000819050806001600160a01b031663d09de08a6040518163ffffffff1660e01b8152600401600060405180830381600087803b15801561018657600080fd5b6101658061031e8339019056fe608060405234801561001057600080fd5b50600080546001600160a01b03191633179055610133806100326000396000f3fe6080604052348015600f57600080fd5b5060043610603c5760003560e01c80633fa4f2451460415780636057361d14605957806367e404ce146075575b600080fd5b60476097565b60408051918252519081900360200190f35b607360048036036020811015606d57600080fd5b5035609d565b005b607b60ef565b604080516001600160a01b039092168252519081900360200190f35b60025490565b604080513381526020810183905281517fc9db20adedc6cf2b5d25252b101ab03e124902a73fcb12b753f3d1aaa2d8f9f5929181900390910190a1600255600180546001600160a01b03191633179055565b6001546001600160a01b03169056fea265627a7a72305820dc1ce4d08260105d146ec5efa5274950ee9e66f81ff18994d44a40fbd33e45c064736f6c634300050a0032a265627a7a72305820d71e5a225a48fdeb043aaba4264138353b3443a28658bacec7570e108659ad2864736f6c634300050a0032 |
@ -0,0 +1,276 @@ |
||||
/* |
||||
* 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.generated; |
||||
|
||||
import java.math.BigInteger; |
||||
import java.util.ArrayList; |
||||
import java.util.Arrays; |
||||
import java.util.Collections; |
||||
import java.util.List; |
||||
|
||||
import io.reactivex.Flowable; |
||||
import org.web3j.abi.EventEncoder; |
||||
import org.web3j.abi.TypeReference; |
||||
import org.web3j.abi.datatypes.Address; |
||||
import org.web3j.abi.datatypes.Event; |
||||
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.DefaultBlockParameter; |
||||
import org.web3j.protocol.core.RemoteCall; |
||||
import org.web3j.protocol.core.RemoteFunctionCall; |
||||
import org.web3j.protocol.core.methods.request.EthFilter; |
||||
import org.web3j.protocol.core.methods.response.BaseEventResponse; |
||||
import org.web3j.protocol.core.methods.response.Log; |
||||
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 CrossContractReader extends Contract { |
||||
private static final String BINARY = |
||||
"608060405234801561001057600080fd5b506104b7806100206000396000f3fe608060405234801561001057600080fd5b506004361061007d5760003560e01c806383197ef01161005b57806383197ef0146100d8578063a087a87e146100e0578063d09de08a14610118578063e689ef8a146101205761007d565b8063305155f9146100825780635374ded2146100aa578063775c300c146100d0575b600080fd5b6100a86004803603602081101561009857600080fd5b50356001600160a01b0316610146565b005b6100a8600480360360208110156100c057600080fd5b50356001600160a01b03166101a2565b6100a86101e2565b6100a8610250565b610106600480360360208110156100f657600080fd5b50356001600160a01b0316610253565b60408051918252519081900360200190f35b6100a86102c5565b6100a86004803603602081101561013657600080fd5b50356001600160a01b03166102d0565b6000819050806001600160a01b03166383197ef06040518163ffffffff1660e01b8152600401600060405180830381600087803b15801561018657600080fd5b505af115801561019a573d6000803e3d6000fd5b505050505050565b6000819050806001600160a01b031663775c300c6040518163ffffffff1660e01b8152600401600060405180830381600087803b15801561018657600080fd5b60006040516101f090610310565b604051809103906000f08015801561020c573d6000803e3d6000fd5b50604080516001600160a01b038316815290519192507f9ac6876e0aa40667ffeaa9b359b5ed924f4cdd0e029eb6e9c369e78c68f711fb919081900360200190a150565b33ff5b600080829050806001600160a01b0316633fa4f2456040518163ffffffff1660e01b815260040160206040518083038186803b15801561029257600080fd5b505afa1580156102a6573d6000803e3d6000fd5b505050506040513d60208110156102bc57600080fd5b50519392505050565b600080546001019055565b6000819050806001600160a01b031663d09de08a6040518163ffffffff1660e01b8152600401600060405180830381600087803b15801561018657600080fd5b6101658061031e8339019056fe608060405234801561001057600080fd5b50600080546001600160a01b03191633179055610133806100326000396000f3fe6080604052348015600f57600080fd5b5060043610603c5760003560e01c80633fa4f2451460415780636057361d14605957806367e404ce146075575b600080fd5b60476097565b60408051918252519081900360200190f35b607360048036036020811015606d57600080fd5b5035609d565b005b607b60ef565b604080516001600160a01b039092168252519081900360200190f35b60025490565b604080513381526020810183905281517fc9db20adedc6cf2b5d25252b101ab03e124902a73fcb12b753f3d1aaa2d8f9f5929181900390910190a1600255600180546001600160a01b03191633179055565b6001546001600160a01b03169056fea265627a7a72305820dc1ce4d08260105d146ec5efa5274950ee9e66f81ff18994d44a40fbd33e45c064736f6c634300050a0032a265627a7a72305820d71e5a225a48fdeb043aaba4264138353b3443a28658bacec7570e108659ad2864736f6c634300050a0032"; |
||||
|
||||
public static final String FUNC_REMOTEDESTROY = "remoteDestroy"; |
||||
|
||||
public static final String FUNC_DEPLOYREMOTE = "deployRemote"; |
||||
|
||||
public static final String FUNC_DESTROY = "destroy"; |
||||
|
||||
public static final String FUNC_READ = "read"; |
||||
|
||||
public static final String FUNC_INCREMENT = "increment"; |
||||
|
||||
public static final String FUNC_INCREMENTREMOTE = "incrementRemote"; |
||||
|
||||
public static final Event NEWEVENTEMITTER_EVENT = |
||||
new Event( |
||||
"NewEventEmitter", Arrays.<TypeReference<?>>asList(new TypeReference<Address>() {}));; |
||||
|
||||
@Deprecated |
||||
protected CrossContractReader( |
||||
String contractAddress, |
||||
Web3j web3j, |
||||
Credentials credentials, |
||||
BigInteger gasPrice, |
||||
BigInteger gasLimit) { |
||||
super(BINARY, contractAddress, web3j, credentials, gasPrice, gasLimit); |
||||
} |
||||
|
||||
protected CrossContractReader( |
||||
String contractAddress, |
||||
Web3j web3j, |
||||
Credentials credentials, |
||||
ContractGasProvider contractGasProvider) { |
||||
super(BINARY, contractAddress, web3j, credentials, contractGasProvider); |
||||
} |
||||
|
||||
@Deprecated |
||||
protected CrossContractReader( |
||||
String contractAddress, |
||||
Web3j web3j, |
||||
TransactionManager transactionManager, |
||||
BigInteger gasPrice, |
||||
BigInteger gasLimit) { |
||||
super(BINARY, contractAddress, web3j, transactionManager, gasPrice, gasLimit); |
||||
} |
||||
|
||||
protected CrossContractReader( |
||||
String contractAddress, |
||||
Web3j web3j, |
||||
TransactionManager transactionManager, |
||||
ContractGasProvider contractGasProvider) { |
||||
super(BINARY, contractAddress, web3j, transactionManager, contractGasProvider); |
||||
} |
||||
|
||||
public RemoteFunctionCall<TransactionReceipt> remoteDestroy(String crossAddress) { |
||||
final Function function = |
||||
new Function( |
||||
FUNC_REMOTEDESTROY, |
||||
Arrays.<Type>asList(new org.web3j.abi.datatypes.Address(160, crossAddress)), |
||||
Collections.<TypeReference<?>>emptyList()); |
||||
return executeRemoteCallTransaction(function); |
||||
} |
||||
|
||||
public RemoteFunctionCall<TransactionReceipt> deployRemote(String crossAddress) { |
||||
final Function function = |
||||
new Function( |
||||
FUNC_DEPLOYREMOTE, |
||||
Arrays.<Type>asList(new org.web3j.abi.datatypes.Address(160, crossAddress)), |
||||
Collections.<TypeReference<?>>emptyList()); |
||||
return executeRemoteCallTransaction(function); |
||||
} |
||||
|
||||
public RemoteFunctionCall<TransactionReceipt> deploy() { |
||||
final Function function = |
||||
new Function(FUNC_DEPLOY, Arrays.<Type>asList(), Collections.<TypeReference<?>>emptyList()); |
||||
return executeRemoteCallTransaction(function); |
||||
} |
||||
|
||||
public RemoteFunctionCall<TransactionReceipt> destroy() { |
||||
final Function function = |
||||
new Function( |
||||
FUNC_DESTROY, Arrays.<Type>asList(), Collections.<TypeReference<?>>emptyList()); |
||||
return executeRemoteCallTransaction(function); |
||||
} |
||||
|
||||
public RemoteFunctionCall<BigInteger> read(String emitter_address) { |
||||
final Function function = |
||||
new Function( |
||||
FUNC_READ, |
||||
Arrays.<Type>asList(new org.web3j.abi.datatypes.Address(160, emitter_address)), |
||||
Arrays.<TypeReference<?>>asList(new TypeReference<Uint256>() {})); |
||||
return executeRemoteCallSingleValueReturn(function, BigInteger.class); |
||||
} |
||||
|
||||
public RemoteFunctionCall<TransactionReceipt> increment() { |
||||
final Function function = |
||||
new Function( |
||||
FUNC_INCREMENT, Arrays.<Type>asList(), Collections.<TypeReference<?>>emptyList()); |
||||
return executeRemoteCallTransaction(function); |
||||
} |
||||
|
||||
public RemoteFunctionCall<TransactionReceipt> incrementRemote(String crossAddress) { |
||||
final Function function = |
||||
new Function( |
||||
FUNC_INCREMENTREMOTE, |
||||
Arrays.<Type>asList(new org.web3j.abi.datatypes.Address(160, crossAddress)), |
||||
Collections.<TypeReference<?>>emptyList()); |
||||
return executeRemoteCallTransaction(function); |
||||
} |
||||
|
||||
public List<NewEventEmitterEventResponse> getNewEventEmitterEvents( |
||||
TransactionReceipt transactionReceipt) { |
||||
List<Contract.EventValuesWithLog> valueList = |
||||
extractEventParametersWithLog(NEWEVENTEMITTER_EVENT, transactionReceipt); |
||||
ArrayList<NewEventEmitterEventResponse> responses = |
||||
new ArrayList<NewEventEmitterEventResponse>(valueList.size()); |
||||
for (Contract.EventValuesWithLog eventValues : valueList) { |
||||
NewEventEmitterEventResponse typedResponse = new NewEventEmitterEventResponse(); |
||||
typedResponse.log = eventValues.getLog(); |
||||
typedResponse.contractAddress = (String) eventValues.getNonIndexedValues().get(0).getValue(); |
||||
responses.add(typedResponse); |
||||
} |
||||
return responses; |
||||
} |
||||
|
||||
public Flowable<NewEventEmitterEventResponse> newEventEmitterEventFlowable(EthFilter filter) { |
||||
return web3j |
||||
.ethLogFlowable(filter) |
||||
.map( |
||||
new io.reactivex.functions.Function<Log, NewEventEmitterEventResponse>() { |
||||
@Override |
||||
public NewEventEmitterEventResponse apply(Log log) { |
||||
Contract.EventValuesWithLog eventValues = |
||||
extractEventParametersWithLog(NEWEVENTEMITTER_EVENT, log); |
||||
NewEventEmitterEventResponse typedResponse = new NewEventEmitterEventResponse(); |
||||
typedResponse.log = log; |
||||
typedResponse.contractAddress = |
||||
(String) eventValues.getNonIndexedValues().get(0).getValue(); |
||||
return typedResponse; |
||||
} |
||||
}); |
||||
} |
||||
|
||||
public Flowable<NewEventEmitterEventResponse> newEventEmitterEventFlowable( |
||||
DefaultBlockParameter startBlock, DefaultBlockParameter endBlock) { |
||||
EthFilter filter = new EthFilter(startBlock, endBlock, getContractAddress()); |
||||
filter.addSingleTopic(EventEncoder.encode(NEWEVENTEMITTER_EVENT)); |
||||
return newEventEmitterEventFlowable(filter); |
||||
} |
||||
|
||||
@Deprecated |
||||
public static CrossContractReader load( |
||||
String contractAddress, |
||||
Web3j web3j, |
||||
Credentials credentials, |
||||
BigInteger gasPrice, |
||||
BigInteger gasLimit) { |
||||
return new CrossContractReader(contractAddress, web3j, credentials, gasPrice, gasLimit); |
||||
} |
||||
|
||||
@Deprecated |
||||
public static CrossContractReader load( |
||||
String contractAddress, |
||||
Web3j web3j, |
||||
TransactionManager transactionManager, |
||||
BigInteger gasPrice, |
||||
BigInteger gasLimit) { |
||||
return new CrossContractReader(contractAddress, web3j, transactionManager, gasPrice, gasLimit); |
||||
} |
||||
|
||||
public static CrossContractReader load( |
||||
String contractAddress, |
||||
Web3j web3j, |
||||
Credentials credentials, |
||||
ContractGasProvider contractGasProvider) { |
||||
return new CrossContractReader(contractAddress, web3j, credentials, contractGasProvider); |
||||
} |
||||
|
||||
public static CrossContractReader load( |
||||
String contractAddress, |
||||
Web3j web3j, |
||||
TransactionManager transactionManager, |
||||
ContractGasProvider contractGasProvider) { |
||||
return new CrossContractReader(contractAddress, web3j, transactionManager, contractGasProvider); |
||||
} |
||||
|
||||
public static RemoteCall<CrossContractReader> deploy( |
||||
Web3j web3j, Credentials credentials, ContractGasProvider contractGasProvider) { |
||||
return deployRemoteCall( |
||||
CrossContractReader.class, web3j, credentials, contractGasProvider, BINARY, ""); |
||||
} |
||||
|
||||
@Deprecated |
||||
public static RemoteCall<CrossContractReader> deploy( |
||||
Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) { |
||||
return deployRemoteCall( |
||||
CrossContractReader.class, web3j, credentials, gasPrice, gasLimit, BINARY, ""); |
||||
} |
||||
|
||||
public static RemoteCall<CrossContractReader> deploy( |
||||
Web3j web3j, TransactionManager transactionManager, ContractGasProvider contractGasProvider) { |
||||
return deployRemoteCall( |
||||
CrossContractReader.class, web3j, transactionManager, contractGasProvider, BINARY, ""); |
||||
} |
||||
|
||||
@Deprecated |
||||
public static RemoteCall<CrossContractReader> deploy( |
||||
Web3j web3j, |
||||
TransactionManager transactionManager, |
||||
BigInteger gasPrice, |
||||
BigInteger gasLimit) { |
||||
return deployRemoteCall( |
||||
CrossContractReader.class, web3j, transactionManager, gasPrice, gasLimit, BINARY, ""); |
||||
} |
||||
|
||||
public static class NewEventEmitterEventResponse extends BaseEventResponse { |
||||
public String contractAddress; |
||||
} |
||||
} |
@ -0,0 +1,162 @@ |
||||
/* |
||||
* 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.CrossContractReader; |
||||
import org.hyperledger.besu.tests.web3j.generated.EventEmitter; |
||||
|
||||
import java.math.BigInteger; |
||||
|
||||
import org.junit.Before; |
||||
import org.junit.Test; |
||||
import org.web3j.protocol.core.methods.response.TransactionReceipt; |
||||
import org.web3j.protocol.pantheon.response.privacy.PrivateTransactionReceipt; |
||||
import org.web3j.tx.exceptions.ContractCallException; |
||||
|
||||
public class PrivateContractPublicStateAcceptanceTest extends PrivacyAcceptanceTestBase { |
||||
private static final long POW_CHAIN_ID = 2018; |
||||
|
||||
private PrivacyNode minerNode; |
||||
|
||||
@Before |
||||
public void setUp() throws Exception { |
||||
minerNode = |
||||
privacyBesu.createPrivateTransactionEnabledMinerNode( |
||||
"miner-node", privacyAccountResolver.resolve(0)); |
||||
privacyCluster.start(minerNode); |
||||
} |
||||
|
||||
@Test |
||||
public void mustAllowAccessToPublicStateFromPrivateTx() throws Exception { |
||||
final EventEmitter publicEventEmitter = |
||||
minerNode.getBesu().execute((contractTransactions.createSmartContract(EventEmitter.class))); |
||||
|
||||
final TransactionReceipt receipt = publicEventEmitter.store(BigInteger.valueOf(12)).send(); |
||||
assertThat(receipt).isNotNull(); |
||||
|
||||
final CrossContractReader reader = |
||||
minerNode |
||||
.getBesu() |
||||
.execute( |
||||
privateContractTransactions.createSmartContract( |
||||
CrossContractReader.class, |
||||
minerNode.getTransactionSigningKey(), |
||||
POW_CHAIN_ID, |
||||
minerNode.getEnclaveKey())); |
||||
|
||||
assertThat(reader.read(publicEventEmitter.getContractAddress()).send()) |
||||
.isEqualTo(BigInteger.valueOf(12)); |
||||
} |
||||
|
||||
@Test(expected = ContractCallException.class) |
||||
public void mustNotAllowAccessToPrivateStateFromPublicTx() throws Exception { |
||||
final EventEmitter privateEventEmitter = |
||||
minerNode |
||||
.getBesu() |
||||
.execute( |
||||
(privateContractTransactions.createSmartContract( |
||||
EventEmitter.class, |
||||
minerNode.getTransactionSigningKey(), |
||||
POW_CHAIN_ID, |
||||
minerNode.getEnclaveKey()))); |
||||
|
||||
final TransactionReceipt receipt = privateEventEmitter.store(BigInteger.valueOf(12)).send(); |
||||
assertThat(receipt).isNotNull(); |
||||
|
||||
final CrossContractReader publicReader = |
||||
minerNode |
||||
.getBesu() |
||||
.execute(contractTransactions.createSmartContract(CrossContractReader.class)); |
||||
|
||||
publicReader.read(privateEventEmitter.getContractAddress()).send(); |
||||
} |
||||
|
||||
@Test |
||||
public void privateContractMustNotBeAbleToCallPublicContractWhichChangesState() throws Exception { |
||||
final CrossContractReader privateReader = |
||||
minerNode |
||||
.getBesu() |
||||
.execute( |
||||
privateContractTransactions.createSmartContract( |
||||
CrossContractReader.class, |
||||
minerNode.getTransactionSigningKey(), |
||||
POW_CHAIN_ID, |
||||
minerNode.getEnclaveKey())); |
||||
|
||||
final CrossContractReader publicReader = |
||||
minerNode |
||||
.getBesu() |
||||
.execute(contractTransactions.createSmartContract(CrossContractReader.class)); |
||||
|
||||
final PrivateTransactionReceipt transactionReceipt = |
||||
(PrivateTransactionReceipt) |
||||
privateReader.incrementRemote(publicReader.getContractAddress()).send(); |
||||
|
||||
assertThat(transactionReceipt.getOutput()).isEqualTo("0x"); |
||||
} |
||||
|
||||
@Test |
||||
public void privateContractMustNotBeAbleToCallPublicContractWhichInstantiatesContract() |
||||
throws Exception { |
||||
final CrossContractReader privateReader = |
||||
minerNode |
||||
.getBesu() |
||||
.execute( |
||||
privateContractTransactions.createSmartContract( |
||||
CrossContractReader.class, |
||||
minerNode.getTransactionSigningKey(), |
||||
POW_CHAIN_ID, |
||||
minerNode.getEnclaveKey())); |
||||
|
||||
final CrossContractReader publicReader = |
||||
minerNode |
||||
.getBesu() |
||||
.execute(contractTransactions.createSmartContract(CrossContractReader.class)); |
||||
|
||||
final PrivateTransactionReceipt transactionReceipt = |
||||
(PrivateTransactionReceipt) |
||||
privateReader.deployRemote(publicReader.getContractAddress()).send(); |
||||
|
||||
assertThat(transactionReceipt.getLogs().size()).isEqualTo(0); |
||||
} |
||||
|
||||
@Test |
||||
public void privateContractMustNotBeAbleToCallSelfDetructOfPublicContract() throws Exception { |
||||
final CrossContractReader privateReader = |
||||
minerNode |
||||
.getBesu() |
||||
.execute( |
||||
privateContractTransactions.createSmartContract( |
||||
CrossContractReader.class, |
||||
minerNode.getTransactionSigningKey(), |
||||
POW_CHAIN_ID, |
||||
minerNode.getEnclaveKey())); |
||||
|
||||
final CrossContractReader publicReader = |
||||
minerNode |
||||
.getBesu() |
||||
.execute(contractTransactions.createSmartContract(CrossContractReader.class)); |
||||
|
||||
final PrivateTransactionReceipt transactionReceipt = |
||||
(PrivateTransactionReceipt) |
||||
privateReader.remoteDestroy(publicReader.getContractAddress()).send(); |
||||
|
||||
assertThat(transactionReceipt.getOutput()).isEqualTo("0x"); |
||||
} |
||||
} |
@ -0,0 +1,100 @@ |
||||
/* |
||||
* 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.core; |
||||
|
||||
import org.hyperledger.besu.util.bytes.Bytes32; |
||||
import org.hyperledger.besu.util.bytes.BytesValue; |
||||
import org.hyperledger.besu.util.uint.UInt256; |
||||
|
||||
import java.util.NavigableMap; |
||||
|
||||
public class DefaultEvmAccount implements EvmAccount { |
||||
private MutableAccount mutableAccount; |
||||
|
||||
public boolean isImmutable() { |
||||
return isImmutable; |
||||
} |
||||
|
||||
public void setImmutable(final boolean immutable) { |
||||
isImmutable = immutable; |
||||
} |
||||
|
||||
private boolean isImmutable; |
||||
|
||||
public DefaultEvmAccount(final MutableAccount mutableAccount) { |
||||
|
||||
this.mutableAccount = mutableAccount; |
||||
this.isImmutable = false; |
||||
} |
||||
|
||||
@Override |
||||
public MutableAccount getMutable() throws ModificationNotAllowedException { |
||||
if (isImmutable) { |
||||
throw new ModificationNotAllowedException(); |
||||
} |
||||
return mutableAccount; |
||||
} |
||||
|
||||
@Override |
||||
public Address getAddress() { |
||||
return mutableAccount.getAddress(); |
||||
} |
||||
|
||||
@Override |
||||
public Hash getAddressHash() { |
||||
return mutableAccount.getAddressHash(); |
||||
} |
||||
|
||||
@Override |
||||
public long getNonce() { |
||||
return mutableAccount.getNonce(); |
||||
} |
||||
|
||||
@Override |
||||
public Wei getBalance() { |
||||
return mutableAccount.getBalance(); |
||||
} |
||||
|
||||
@Override |
||||
public BytesValue getCode() { |
||||
return mutableAccount.getCode(); |
||||
} |
||||
|
||||
@Override |
||||
public Hash getCodeHash() { |
||||
return mutableAccount.getCodeHash(); |
||||
} |
||||
|
||||
@Override |
||||
public int getVersion() { |
||||
return mutableAccount.getVersion(); |
||||
} |
||||
|
||||
@Override |
||||
public UInt256 getStorageValue(final UInt256 key) { |
||||
return mutableAccount.getStorageValue(key); |
||||
} |
||||
|
||||
@Override |
||||
public UInt256 getOriginalStorageValue(final UInt256 key) { |
||||
return mutableAccount.getOriginalStorageValue(key); |
||||
} |
||||
|
||||
@Override |
||||
public NavigableMap<Bytes32, AccountStorageEntry> storageEntriesFrom( |
||||
final Bytes32 startKeyHash, final int limit) { |
||||
return mutableAccount.storageEntriesFrom(startKeyHash, limit); |
||||
} |
||||
} |
@ -0,0 +1,19 @@ |
||||
/* |
||||
* 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.core; |
||||
|
||||
public interface EvmAccount extends Account { |
||||
public MutableAccount getMutable() throws ModificationNotAllowedException; |
||||
} |
@ -0,0 +1,21 @@ |
||||
/* |
||||
* 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.core; |
||||
|
||||
public class ModificationNotAllowedException extends RuntimeException { |
||||
ModificationNotAllowedException() { |
||||
super("This account may not be modified"); |
||||
} |
||||
} |
@ -0,0 +1,99 @@ |
||||
/* |
||||
* 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.worldstate; |
||||
|
||||
import org.hyperledger.besu.ethereum.core.Account; |
||||
import org.hyperledger.besu.ethereum.core.Address; |
||||
import org.hyperledger.besu.ethereum.core.DefaultEvmAccount; |
||||
import org.hyperledger.besu.ethereum.core.Wei; |
||||
import org.hyperledger.besu.ethereum.core.WorldUpdater; |
||||
|
||||
import java.util.Collection; |
||||
|
||||
public class DefaultMutablePrivateWorldStateUpdater implements WorldUpdater { |
||||
|
||||
private final WorldUpdater publicWorldUpdater; |
||||
private final WorldUpdater privateWorldUpdater; |
||||
|
||||
public DefaultMutablePrivateWorldStateUpdater( |
||||
final WorldUpdater publicWorldUpdater, final WorldUpdater privateWorldUpdater) { |
||||
this.publicWorldUpdater = publicWorldUpdater; |
||||
this.privateWorldUpdater = privateWorldUpdater; |
||||
} |
||||
|
||||
@Override |
||||
public DefaultEvmAccount createAccount( |
||||
final Address address, final long nonce, final Wei balance) { |
||||
return privateWorldUpdater.createAccount(address); |
||||
} |
||||
|
||||
@Override |
||||
public DefaultEvmAccount createAccount(final Address address) { |
||||
return privateWorldUpdater.createAccount(address); |
||||
} |
||||
|
||||
@Override |
||||
public DefaultEvmAccount getOrCreate(final Address address) { |
||||
return privateWorldUpdater.getOrCreate(address); |
||||
} |
||||
|
||||
@Override |
||||
public DefaultEvmAccount getAccount(final Address address) { |
||||
final DefaultEvmAccount privateAccount = privateWorldUpdater.getAccount(address); |
||||
if (privateAccount != null && !privateAccount.isEmpty()) { |
||||
return privateAccount; |
||||
} |
||||
final DefaultEvmAccount publicAccount = publicWorldUpdater.getAccount(address); |
||||
if (publicAccount != null && !publicAccount.isEmpty()) { |
||||
publicAccount.setImmutable(true); |
||||
return publicAccount; |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
@Override |
||||
public void deleteAccount(final Address address) { |
||||
privateWorldUpdater.deleteAccount(address); |
||||
} |
||||
|
||||
@Override |
||||
public Collection<Account> getTouchedAccounts() { |
||||
return privateWorldUpdater.getTouchedAccounts(); |
||||
} |
||||
|
||||
@Override |
||||
public void revert() { |
||||
privateWorldUpdater.revert(); |
||||
} |
||||
|
||||
@Override |
||||
public void commit() { |
||||
privateWorldUpdater.commit(); |
||||
} |
||||
|
||||
@Override |
||||
public Account get(final Address address) { |
||||
final Account privateAccount = privateWorldUpdater.get(address); |
||||
if (privateAccount != null && !privateAccount.isEmpty()) { |
||||
return privateAccount; |
||||
} |
||||
return publicWorldUpdater.get(address); |
||||
} |
||||
|
||||
@Override |
||||
public WorldUpdater updater() { |
||||
return this; |
||||
} |
||||
} |
Loading…
Reference in new issue