Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 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
@@ -0,0 +1,29 @@
/*
* 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.privacy.condition;

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

import java.io.IOException;

import org.web3j.tx.Contract;

public class ExpectValidContractCode implements PrivateContractCondition {
@Override
public void verify(final Contract contract) throws IOException {
assertThat(contract).isNotNull();
assertThat(contract.isValid()).isEqualTo(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
*/
package org.hyperledger.besu.tests.acceptance.dsl.privacy.condition;

import java.io.IOException;

import org.web3j.tx.Contract;

public interface PrivateContractCondition {
void verify(final Contract contract);
void verify(final Contract contract) throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@ public ExpectValidPrivateContractDeployedReceipt validPrivateContractDeployed(
final String contractAddress, final String senderAddress) {
return new ExpectValidPrivateContractDeployedReceipt(contractAddress, senderAddress);
}

public ExpectValidContractCode validContractCodeProvided() {
return new ExpectValidContractCode();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void setUp() throws Exception {
}

@Test
public void deployingMustGiveValidReceipt() {
public void deployingMustGiveValidReceiptAndCode() throws Exception {
final String contractAddress = "0x89ce396d0f9f937ddfa71113e29b2081c4869555";

final EventEmitter eventEmitter =
Expand All @@ -50,5 +50,7 @@ public void deployingMustGiveValidReceipt() {
privateContractVerifier
.validPrivateContractDeployed(contractAddress, minerNode.getAddress().toString())
.verify(eventEmitter);

privateContractVerifier.validContractCodeProvided().verify(eventEmitter);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public enum RpcMethod {
PRIV_FIND_PRIVACY_GROUP("priv_findPrivacyGroup"),
PRIV_DISTRIBUTE_RAW_TRANSACTION("priv_distributeRawTransaction"),
PRIV_GET_EEA_TRANSACTION_COUNT("priv_getEeaTransactionCount"),
PRIV_GET_CODE("priv_getCode"),
EEA_SEND_RAW_TRANSACTION("eea_sendRawTransaction"),
ETH_ACCOUNTS("eth_accounts"),
ETH_BLOCK_NUMBER("eth_blockNumber"),
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 static org.apache.logging.log4j.LogManager.getLogger;

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.methods.JsonRpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.BlockParameter;
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.core.Address;
import org.hyperledger.besu.ethereum.core.Hash;
import org.hyperledger.besu.ethereum.core.PrivacyParameters;
import org.hyperledger.besu.ethereum.privacy.PrivateStateRootResolver;

import java.util.Optional;

import org.apache.logging.log4j.Logger;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;

public class PrivGetCode implements JsonRpcMethod {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have an abstraction for methods that have the block paramater to handle named or numbers, etc.
Take a look at PrivCall and how it extends AbstractBlockParameterMethod. I believe we should make PrivGetCode extend AbstractBlockParameterMethod.


private static final Logger LOG = getLogger();

private final BlockchainQueries blockchain;
private final PrivateStateRootResolver privateStateRootResolver;
private final PrivacyParameters privacyParameters;

public PrivGetCode(
final BlockchainQueries blockchain,
final PrivacyParameters privacyParameters,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like we are passing down the PrivacyParameter object only to get an instance of the privateStateArchive. I think we should change this to receive the privateStateArchive directly.

final PrivateStateRootResolver privateStateRootResolver) {
this.privacyParameters = privacyParameters;
this.blockchain = blockchain;
this.privateStateRootResolver = privateStateRootResolver;
}

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

@Override
public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
LOG.trace("Executing {}", RpcMethod.PRIV_GET_CODE.getMethodName());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have DEBUG log level for JSON-RPC calls. I don't think we need this one.


final Address address =

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have standardized that for any priv_ methods that have an equivalent eth_ method, we will make the first parameter (index 0) the privacyGroupId. And all other parameters will be the same.

So, the expected params for priv_getCode are:

params: [
   'privacy_group_id',
   '0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b',
   '0x2'  // 2
]

Take a look at PrivCall implementation

Address.fromHexString(requestContext.getRequiredParameter(0, String.class));

final BlockParameter blockParameter =
requestContext.getRequiredParameter(1, BlockParameter.class);

final String privacyGroupString = requestContext.getRequiredParameter(2, String.class);

final Bytes32 privacyGroupId = Bytes32.wrap(Bytes.fromBase64String(privacyGroupString));

final Hash latestStateRoot =
privateStateRootResolver.resolveLastStateRoot(
privacyGroupId,
blockParameter.isNumeric() && blockParameter.getNumber().isPresent()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I mentioned before, extending AbstractBlockParameterMethod should help you handling the block paramter.

? blockchain
.getBlockchain()
.getBlockByNumber(blockParameter.getNumber().getAsLong())
.orElseThrow()
.getHash()
: blockchain.getBlockchain().getChainHeadBlock().getHash());

return privacyParameters
.getPrivateWorldStateArchive()
.get(latestStateRoot)
.flatMap(
pws ->
Optional.ofNullable(pws.get(address)).map(account -> account.getCode().toString()))
.map(c -> new JsonRpcSuccessResponse(requestContext.getRequest().getId(), c))
.orElse(new JsonRpcSuccessResponse(requestContext.getRequest().getId(), null));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.priv.PrivDeletePrivacyGroup;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.priv.PrivDistributeRawTransaction;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.priv.PrivFindPrivacyGroup;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.priv.PrivGetCode;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.priv.PrivGetPrivacyPrecompileAddress;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.priv.PrivGetPrivateTransaction;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.priv.PrivGetTransactionCount;
Expand All @@ -32,6 +33,7 @@
import org.hyperledger.besu.ethereum.eth.transactions.TransactionPool;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.ethereum.privacy.PrivacyController;
import org.hyperledger.besu.ethereum.privacy.PrivateStateRootResolver;

import java.util.Map;

Expand Down Expand Up @@ -68,6 +70,10 @@ protected Map<String, JsonRpcMethod> create(
new PrivGetPrivateTransaction(
getBlockchainQueries(), privacyController, enclavePublicKeyProvider),
new PrivDistributeRawTransaction(privacyController, enclavePublicKeyProvider),
new PrivCall(getBlockchainQueries(), privacyController, enclavePublicKeyProvider));
new PrivCall(getBlockchainQueries(), privacyController, enclavePublicKeyProvider),
new PrivGetCode(
getBlockchainQueries(),
getPrivacyParameters(),
new PrivateStateRootResolver(getPrivacyParameters().getPrivateStateStorage())));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/*
* 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 static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

import org.hyperledger.besu.crypto.SECP256K1;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext;
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.chain.Blockchain;
import org.hyperledger.besu.ethereum.core.Account;
import org.hyperledger.besu.ethereum.core.Address;
import org.hyperledger.besu.ethereum.core.Block;
import org.hyperledger.besu.ethereum.core.Hash;
import org.hyperledger.besu.ethereum.core.PrivacyParameters;
import org.hyperledger.besu.ethereum.core.Wei;
import org.hyperledger.besu.ethereum.core.WorldState;
import org.hyperledger.besu.ethereum.privacy.PrivateStateRootResolver;
import org.hyperledger.besu.ethereum.privacy.PrivateTransaction;
import org.hyperledger.besu.ethereum.privacy.Restriction;
import org.hyperledger.besu.ethereum.worldstate.WorldStateArchive;

import java.math.BigInteger;
import java.util.Optional;

import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class PrivGetCodeTest {

@Rule public final TemporaryFolder temp = new TemporaryFolder();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

temp doesn't seem to be used


private final Address sender =
Address.fromHexString("0x0000000000000000000000000000000000000003");
private static final SECP256K1.KeyPair KEY_PAIR =
SECP256K1.KeyPair.create(
SECP256K1.PrivateKey.create(
new BigInteger(
"8f2a55949038a9610f50fb23b5883af3b4ecb3c3bb792cbcefbd1542c692be63", 16)));

private final Bytes privacyGroupId =
Bytes.fromBase64String("Ko2bVqD+nNlNYL5EE7y3IdOnviftjiizpjRt+HTuFBs=");

private final PrivateTransaction.Builder privateTransactionBuilder =
PrivateTransaction.builder()
.nonce(0)
.gasPrice(Wei.of(1000))
.gasLimit(3000000)
.to(null)
.value(Wei.ZERO)
.payload(
Bytes.fromBase64String(
"0x608060405234801561001057600080fd5b5060d08061001f60003960"
+ "00f3fe60806040526004361060485763ffffffff7c01000000"
+ "00000000000000000000000000000000000000000000000000"
+ "60003504166360fe47b18114604d5780636d4ce63c14607557"
+ "5b600080fd5b348015605857600080fd5b5060736004803603"
+ "6020811015606d57600080fd5b50356099565b005b34801560"
+ "8057600080fd5b506087609e565b6040805191825251908190"
+ "0360200190f35b600055565b6000549056fea165627a7a7230"
+ "5820cb1d0935d14b589300b12fcd0ab849a7e9019c81da24d6"
+ "daa4f6b2f003d1b0180029"))
.sender(sender)
.chainId(BigInteger.valueOf(2018))
.privateFrom(Bytes.fromBase64String("A1aVtMxLCUHmBVHXoZzzBgPbW/wj5axDpW9X8l91SGo="))
.restriction(Restriction.RESTRICTED);

private PrivGetCode method;

@Mock private BlockchainQueries mockBlockchainQueries;
@Mock private PrivacyParameters mockPrivacyParameters;
@Mock private Blockchain mockBlockchain;
@Mock private Block mockBlock;
@Mock private Hash mockHash;
@Mock private PrivateStateRootResolver mockResolver;
@Mock private WorldStateArchive mockArchive;
@Mock private WorldState mockWorldState;
@Mock private Account mockAccount;

private final Hash lastStateRoot =
Hash.fromHexString("0x2121b68f1333e93bae8cd717a3ca68c9d7e7003f6b288c36dfc59b0f87be9590");

private final Bytes fakeAccountCode = Bytes.fromBase64String("ZXhhbXBsZQ==");

@Before
public void before() {
method = new PrivGetCode(mockBlockchainQueries, mockPrivacyParameters, mockResolver);
}

@Test
public void returnValidCodeWhenCalledOnValidContract() {
PrivateTransaction transaction =
privateTransactionBuilder.privacyGroupId(privacyGroupId).signAndBuild(KEY_PAIR);

Address resultantContractAddress =
Address.privateContractAddress(
transaction.getSender(), transaction.getNonce(), privacyGroupId);

when(mockBlockchainQueries.getBlockchain()).thenReturn(mockBlockchain);
when(mockBlockchain.getChainHeadBlock()).thenReturn(mockBlock);
when(mockBlock.getHash()).thenReturn(mockHash);
when(mockResolver.resolveLastStateRoot(any(Bytes32.class), any(Hash.class)))
.thenReturn(lastStateRoot);
when(mockPrivacyParameters.getPrivateWorldStateArchive()).thenReturn(mockArchive);
when(mockArchive.get(lastStateRoot)).thenReturn(Optional.of(mockWorldState));
when(mockWorldState.get(resultantContractAddress)).thenReturn(mockAccount);
when(mockAccount.getCode()).thenReturn(fakeAccountCode);

final JsonRpcRequestContext request =
new JsonRpcRequestContext(
new JsonRpcRequest(
"2.0",
"priv_getCode",
new Object[] {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to fix the order of the parameters here.

resultantContractAddress.toHexString(),
"latest",
"Ko2bVqD+nNlNYL5EE7y3IdOnviftjiizpjRt+HTuFBs="
}));

final JsonRpcResponse response = method.response(request);

assertThat(response).isInstanceOf(JsonRpcSuccessResponse.class);
assertThat(fakeAccountCode.toString())
.isEqualTo(((JsonRpcSuccessResponse) response).getResult());
}
}