mirror of https://github.com/hyperledger/besu
[PRIV] Unit tests around json rpc (#450)
* fix eea_sendRawTransaction test * add priv_getPrivateTransaction test Signed-off-by: Ivaylo Kirilov <iikirilov@gmail.com> Co-authored-by: Lucas Saldanha <lucas.saldanha@consensys.net>pull/457/head
parent
52be1faf9c
commit
ad5bfecd3f
@ -0,0 +1,150 @@ |
||||
/* |
||||
* 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 static java.nio.charset.StandardCharsets.UTF_8; |
||||
|
||||
import org.hyperledger.besu.crypto.SECP256K1; |
||||
import org.hyperledger.besu.enclave.types.ReceiveResponse; |
||||
import org.hyperledger.besu.ethereum.privacy.PrivateTransaction; |
||||
import org.hyperledger.besu.ethereum.privacy.PrivateTransactionWithMetadata; |
||||
import org.hyperledger.besu.ethereum.privacy.VersionedPrivateTransaction; |
||||
import org.hyperledger.besu.ethereum.privacy.storage.PrivateTransactionMetadata; |
||||
import org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput; |
||||
|
||||
import java.math.BigInteger; |
||||
import java.util.Collections; |
||||
import java.util.List; |
||||
|
||||
import org.apache.tuweni.bytes.Bytes; |
||||
import org.apache.tuweni.bytes.Bytes32; |
||||
|
||||
public class PrivateTransactionDataFixture { |
||||
|
||||
public static final long DEFAULT_NONCE = 0; |
||||
public static final Wei DEFAULT_GAS_PRICE = Wei.of(1000); |
||||
public static final long DEFAULT_GAS_LIMIT = 3000000; |
||||
public static final Wei DEFAULT_VALUE = Wei.of(0); |
||||
public static final Address DEFAULT_SENDER = |
||||
Address.fromHexString("0xfe3b557e8fb62b89f4916b721be55ceb828dbd73"); |
||||
public static final BigInteger DEFAULT_CHAIN_ID = BigInteger.valueOf(2018); |
||||
|
||||
public static final SECP256K1.KeyPair KEY_PAIR = |
||||
SECP256K1.KeyPair.create( |
||||
SECP256K1.PrivateKey.create( |
||||
new BigInteger( |
||||
"8f2a55949038a9610f50fb23b5883af3b4ecb3c3bb792cbcefbd1542c692be63", 16))); |
||||
|
||||
public static final Bytes VALID_BASE64_ENCLAVE_KEY = |
||||
Bytes.fromBase64String("A1aVtMxLCUHmBVHXoZzzBgPbW/wj5axDpW9X8l91SGo="); |
||||
|
||||
public static final Bytes VALID_CONTRACT_DEPLOYMENT_PAYLOAD = |
||||
Bytes.fromHexString( |
||||
"0x608060405234801561001057600080fd5b5060d08061001f60003960" |
||||
+ "00f3fe60806040526004361060485763ffffffff7c01000000" |
||||
+ "00000000000000000000000000000000000000000000000000" |
||||
+ "60003504166360fe47b18114604d5780636d4ce63c14607557" |
||||
+ "5b600080fd5b348015605857600080fd5b5060736004803603" |
||||
+ "6020811015606d57600080fd5b50356099565b005b34801560" |
||||
+ "8057600080fd5b506087609e565b6040805191825251908190" |
||||
+ "0360200190f35b600055565b6000549056fea165627a7a7230" |
||||
+ "5820cb1d0935d14b589300b12fcd0ab849a7e9019c81da24d6" |
||||
+ "daa4f6b2f003d1b0180029"); |
||||
public static final Address VALID_CONTRACT_DEPLOYMENT_ADDRESS = |
||||
Address.fromHexString("0x0bac79b78b9866ef11c989ad21a7fcf15f7a18d7"); |
||||
|
||||
public static Transaction privacyMarkerTransaction() { |
||||
return privacyMarkerTransaction(VALID_BASE64_ENCLAVE_KEY, Address.DEFAULT_PRIVACY); |
||||
} |
||||
|
||||
public static Transaction privacyMarkerTransactionOnChain() { |
||||
return privacyMarkerTransaction(VALID_BASE64_ENCLAVE_KEY, Address.ONCHAIN_PRIVACY); |
||||
} |
||||
|
||||
public static Transaction privacyMarkerTransactionOnChainAdd() { |
||||
return privacyMarkerTransaction( |
||||
Bytes.concatenate(VALID_BASE64_ENCLAVE_KEY, VALID_BASE64_ENCLAVE_KEY), |
||||
Address.ONCHAIN_PRIVACY); |
||||
} |
||||
|
||||
private static Transaction privacyMarkerTransaction( |
||||
final Bytes transactionKey, final Address precompiledContractAddress) { |
||||
return Transaction.builder() |
||||
.nonce(DEFAULT_NONCE) |
||||
.gasPrice(DEFAULT_GAS_PRICE) |
||||
.gasLimit(DEFAULT_GAS_LIMIT) |
||||
.to(precompiledContractAddress) |
||||
.value(DEFAULT_VALUE) |
||||
.payload(transactionKey) |
||||
.sender(DEFAULT_SENDER) |
||||
.chainId(DEFAULT_CHAIN_ID) |
||||
.signAndBuild(KEY_PAIR); |
||||
} |
||||
|
||||
public static PrivateTransaction privateContractDeploymentTransactionLegacy() { |
||||
return new PrivateTransactionTestFixture() |
||||
.payload(VALID_CONTRACT_DEPLOYMENT_PAYLOAD) |
||||
.privateFor(Collections.singletonList(VALID_BASE64_ENCLAVE_KEY)) |
||||
.createTransaction(KEY_PAIR); |
||||
} |
||||
|
||||
public static PrivateTransaction privateTransactionBesu() { |
||||
return new PrivateTransactionTestFixture() |
||||
.privacyGroupId(VALID_BASE64_ENCLAVE_KEY) |
||||
.createTransaction(KEY_PAIR); |
||||
} |
||||
|
||||
public static PrivateTransaction privateContractDeploymentTransactionBesu() { |
||||
return new PrivateTransactionTestFixture() |
||||
.payload(VALID_CONTRACT_DEPLOYMENT_PAYLOAD) |
||||
.privacyGroupId(VALID_BASE64_ENCLAVE_KEY) |
||||
.createTransaction(KEY_PAIR); |
||||
} |
||||
|
||||
public static ReceiveResponse generateReceiveResponse( |
||||
final PrivateTransaction privateTransaction) { |
||||
final BytesValueRLPOutput rlpOutput = new BytesValueRLPOutput(); |
||||
privateTransaction.writeTo(rlpOutput); |
||||
return new ReceiveResponse( |
||||
rlpOutput.encoded().toBase64String().getBytes(UTF_8), |
||||
privateTransaction.getPrivacyGroupId().isPresent() |
||||
? privateTransaction.getPrivacyGroupId().get().toBase64String() |
||||
: "", |
||||
null); |
||||
} |
||||
|
||||
public static ReceiveResponse generateVersionedReceiveResponse( |
||||
final PrivateTransaction privateTransaction) { |
||||
final VersionedPrivateTransaction versionedPrivateTransaction = |
||||
new VersionedPrivateTransaction(privateTransaction, Bytes32.ZERO); |
||||
final BytesValueRLPOutput rlpOutput = new BytesValueRLPOutput(); |
||||
versionedPrivateTransaction.writeTo(rlpOutput); |
||||
return new ReceiveResponse( |
||||
rlpOutput.encoded().toBase64String().getBytes(UTF_8), |
||||
privateTransaction.getPrivacyGroupId().isPresent() |
||||
? privateTransaction.getPrivacyGroupId().get().toBase64String() |
||||
: "", |
||||
null); |
||||
} |
||||
|
||||
public static List<PrivateTransactionWithMetadata> generateAddBlobResponse( |
||||
final PrivateTransaction privateTransaction, final Transaction markerTransaction) { |
||||
final PrivateTransactionWithMetadata privateTransactionWithMetadata = |
||||
new PrivateTransactionWithMetadata( |
||||
privateTransaction, |
||||
new PrivateTransactionMetadata(markerTransaction.getHash(), Hash.ZERO)); |
||||
return Collections.singletonList(privateTransactionWithMetadata); |
||||
} |
||||
} |
@ -1,85 +0,0 @@ |
||||
/* |
||||
* 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.privacy.storage.migration; |
||||
|
||||
import org.hyperledger.besu.crypto.SECP256K1; |
||||
import org.hyperledger.besu.ethereum.core.Address; |
||||
import org.hyperledger.besu.ethereum.core.BlockDataGenerator; |
||||
import org.hyperledger.besu.ethereum.core.Transaction; |
||||
import org.hyperledger.besu.ethereum.core.Wei; |
||||
import org.hyperledger.besu.ethereum.mainnet.ValidationResult; |
||||
import org.hyperledger.besu.ethereum.privacy.PrivateTransaction; |
||||
import org.hyperledger.besu.ethereum.privacy.PrivateTransactionProcessor.Result; |
||||
import org.hyperledger.besu.ethereum.privacy.Restriction; |
||||
|
||||
import java.math.BigInteger; |
||||
|
||||
import org.apache.tuweni.bytes.Bytes; |
||||
|
||||
public class PrivateTransactionDataFixture { |
||||
|
||||
private static final SECP256K1.KeyPair KEY_PAIR = |
||||
SECP256K1.KeyPair.create( |
||||
SECP256K1.PrivateKey.create( |
||||
new BigInteger( |
||||
"8f2a55949038a9610f50fb23b5883af3b4ecb3c3bb792cbcefbd1542c692be63", 16))); |
||||
|
||||
private static final BlockDataGenerator blockDataGenerator = new BlockDataGenerator(); |
||||
|
||||
public static Transaction privacyMarkerTransaction(final String transactionKey) { |
||||
return Transaction.builder() |
||||
.nonce(0) |
||||
.gasPrice(Wei.of(1000)) |
||||
.gasLimit(3000000) |
||||
.to(Address.DEFAULT_PRIVACY) |
||||
.value(Wei.ZERO) |
||||
.payload(Bytes.fromBase64String(transactionKey)) |
||||
.sender(Address.fromHexString("0xfe3b557e8fb62b89f4916b721be55ceb828dbd73")) |
||||
.chainId(BigInteger.valueOf(2018)) |
||||
.signAndBuild(KEY_PAIR); |
||||
} |
||||
|
||||
public static PrivateTransaction privateTransaction(final String privacyGroupId) { |
||||
return PrivateTransaction.builder() |
||||
.nonce(0) |
||||
.gasPrice(Wei.of(1000)) |
||||
.gasLimit(3000000) |
||||
.to(null) |
||||
.value(Wei.ZERO) |
||||
.payload( |
||||
Bytes.fromHexString( |
||||
"0x608060405234801561001057600080fd5b5060d08061001f6000396000" |
||||
+ "f3fe60806040526004361060485763ffffffff7c010000000000" |
||||
+ "0000000000000000000000000000000000000000000000600035" |
||||
+ "04166360fe47b18114604d5780636d4ce63c146075575b600080" |
||||
+ "fd5b348015605857600080fd5b50607360048036036020811015" |
||||
+ "606d57600080fd5b50356099565b005b348015608057600080fd" |
||||
+ "5b506087609e565b60408051918252519081900360200190f35b" |
||||
+ "600055565b6000549056fea165627a7a72305820cb1d0935d14b" |
||||
+ "589300b12fcd0ab849a7e9019c81da24d6daa4f6b2f003d1b018" |
||||
+ "0029")) |
||||
.sender(Address.wrap(Bytes.fromHexString("0x1c9a6e1ee3b7ac6028e786d9519ae3d24ee31e79"))) |
||||
.chainId(BigInteger.valueOf(4)) |
||||
.privateFrom(Bytes.fromBase64String("A1aVtMxLCUHmBVHXoZzzBgPbW/wj5axDpW9X8l91SGo=")) |
||||
.privacyGroupId(Bytes.fromBase64String(privacyGroupId)) |
||||
.restriction(Restriction.RESTRICTED) |
||||
.signAndBuild(KEY_PAIR); |
||||
} |
||||
|
||||
public static Result successfulPrivateTxProcessingResult() { |
||||
return Result.successful( |
||||
blockDataGenerator.logs(3, 1), 0, Bytes.EMPTY, ValidationResult.valid()); |
||||
} |
||||
} |
Loading…
Reference in new issue