-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Adds priv_getcode #408
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
Closed
josh-richardson
wants to merge
9
commits into
besu-eth:master
from
josh-richardson:priv-getcode-reimpl
Closed
Adds priv_getcode #408
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3f0f301
Adds priv_getcode
josh-richardson a592da1
Spotless
josh-richardson 654d1b7
imports
josh-richardson 523a449
Addresses PR comments
josh-richardson f4394e5
Merge branch 'master' into priv-getcode-reimpl
josh-richardson 2ec4fb5
Bumps web3j version
josh-richardson 3a7707b
Fixes compilation issue
josh-richardson 235d950
Param ordering
josh-richardson cba76f6
Merge branch 'master' into priv-getcode-reimpl
josh-richardson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
.../org/hyperledger/besu/tests/acceptance/dsl/privacy/condition/ExpectValidContractCode.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
.../org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetCode.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| /* | ||
| * 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.RpcMethod; | ||
| import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext; | ||
| 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.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.privacy.PrivateStateRootResolver; | ||
| import org.hyperledger.besu.ethereum.worldstate.WorldStateArchive; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| import org.apache.tuweni.bytes.Bytes; | ||
| import org.apache.tuweni.bytes.Bytes32; | ||
|
|
||
| public class PrivGetCode extends AbstractBlockParameterMethod { | ||
|
|
||
| private final BlockchainQueries blockchain; | ||
| private final PrivateStateRootResolver privateStateRootResolver; | ||
| private final WorldStateArchive privateWorldStateArchive; | ||
|
|
||
| public PrivGetCode( | ||
| final BlockchainQueries blockchainQueries, | ||
| final WorldStateArchive privateWorldStateArchive, | ||
| final PrivateStateRootResolver privateStateRootResolver) { | ||
| super(blockchainQueries); | ||
| this.privateWorldStateArchive = privateWorldStateArchive; | ||
| this.blockchain = blockchainQueries; | ||
| this.privateStateRootResolver = privateStateRootResolver; | ||
| } | ||
|
|
||
| @Override | ||
| public String getName() { | ||
| return RpcMethod.PRIV_GET_CODE.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 String privacyGroupId = request.getRequiredParameter(0, String.class); | ||
|
|
||
| final Address address = Address.fromHexString(request.getRequiredParameter(1, String.class)); | ||
|
|
||
| final Hash latestStateRoot = | ||
| privateStateRootResolver.resolveLastStateRoot( | ||
| Bytes32.wrap(Bytes.fromBase64String(privacyGroupId)), | ||
| blockchain.getBlockchain().getBlockByNumber(blockNumber).orElseThrow().getHash()); | ||
|
|
||
| return privateWorldStateArchive | ||
| .get(latestStateRoot) | ||
| .flatMap( | ||
| pws -> | ||
| Optional.ofNullable(pws.get(address)).map(account -> account.getCode().toString())) | ||
| .map(c -> new JsonRpcSuccessResponse(request.getRequest().getId(), c)) | ||
| .orElse(new JsonRpcSuccessResponse(request.getRequest().getId(), null)); | ||
| } | ||
|
|
||
| @Override | ||
| public JsonRpcResponse response(final JsonRpcRequestContext requestContext) { | ||
| return (JsonRpcResponse) findResultByParamType(requestContext); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 151 additions & 0 deletions
151
.../hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/priv/PrivGetCodeTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.mockito.Mock; | ||
| import org.mockito.junit.MockitoJUnitRunner; | ||
|
|
||
| @RunWith(MockitoJUnitRunner.class) | ||
| public class PrivGetCodeTest { | ||
|
|
||
| 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.getPrivateWorldStateArchive(), | ||
| 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[] { | ||
| "Ko2bVqD+nNlNYL5EE7y3IdOnviftjiizpjRt+HTuFBs=", | ||
| resultantContractAddress.toHexString(), | ||
| "latest" | ||
| })); | ||
|
|
||
| final JsonRpcResponse response = method.response(request); | ||
|
|
||
| assertThat(response).isInstanceOf(JsonRpcSuccessResponse.class); | ||
| assertThat(((JsonRpcSuccessResponse) response).getResult()) | ||
| .isEqualTo(fakeAccountCode.toString()); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.