From d864afcdaa9bf8e33fc887bd4795557df2987cc9 Mon Sep 17 00:00:00 2001 From: CjHare Date: Wed, 28 Nov 2018 09:05:57 +1000 Subject: [PATCH 1/5] Refactoring and removing the duplicate call to web3j --- .../eth/EthGetTransactionReceiptEquals.java | 44 --------------- .../eth/ExpectContractTransactionReciept.java | 15 +++++ .../tests/acceptance/dsl/jsonrpc/Eth.java | 9 --- .../DeploySmartContractAcceptanceTest.java | 55 +++++++++++++++---- 4 files changed, 60 insertions(+), 63 deletions(-) delete mode 100644 acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/eth/EthGetTransactionReceiptEquals.java create mode 100644 acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/eth/ExpectContractTransactionReciept.java diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/eth/EthGetTransactionReceiptEquals.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/eth/EthGetTransactionReceiptEquals.java deleted file mode 100644 index ca1887a890..0000000000 --- a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/eth/EthGetTransactionReceiptEquals.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright 2018 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. - */ -package tech.pegasys.pantheon.tests.acceptance.dsl.condition.eth; - -import static org.assertj.core.api.Assertions.assertThat; - -import tech.pegasys.pantheon.tests.acceptance.dsl.condition.Condition; -import tech.pegasys.pantheon.tests.acceptance.dsl.node.Node; -import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.eth.EthGetTransactionReceiptTransaction; - -import java.util.Optional; - -import org.web3j.protocol.core.methods.response.TransactionReceipt; - -public class EthGetTransactionReceiptEquals implements Condition { - - private final EthGetTransactionReceiptTransaction transaction; - private final TransactionReceipt expectedReceipt; - - public EthGetTransactionReceiptEquals( - final EthGetTransactionReceiptTransaction transaction, - final TransactionReceipt expectedReceipt) { - this.transaction = transaction; - this.expectedReceipt = expectedReceipt; - } - - @Override - public void verify(final Node node) { - final Optional response = node.execute(transaction); - - assertThat(response.isPresent()).isTrue(); - assertThat(response.get()).isEqualTo(expectedReceipt); - } -} diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/eth/ExpectContractTransactionReciept.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/eth/ExpectContractTransactionReciept.java new file mode 100644 index 0000000000..17386854fb --- /dev/null +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/eth/ExpectContractTransactionReciept.java @@ -0,0 +1,15 @@ +/* + * Copyright 2018 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. + */ +package tech.pegasys.pantheon.tests.acceptance.dsl.condition.eth; + +public class ExpectContractTransactionReciept {} diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/jsonrpc/Eth.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/jsonrpc/Eth.java index cb94afe732..10ee67b239 100644 --- a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/jsonrpc/Eth.java +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/jsonrpc/Eth.java @@ -14,7 +14,6 @@ import tech.pegasys.pantheon.ethereum.core.Hash; import tech.pegasys.pantheon.tests.acceptance.dsl.condition.Condition; -import tech.pegasys.pantheon.tests.acceptance.dsl.condition.eth.EthGetTransactionReceiptEquals; import tech.pegasys.pantheon.tests.acceptance.dsl.condition.eth.ExpectEthAccountsException; import tech.pegasys.pantheon.tests.acceptance.dsl.condition.eth.ExpectEthGetTransactionReceiptIsAbsent; import tech.pegasys.pantheon.tests.acceptance.dsl.condition.eth.ExpectEthGetWorkException; @@ -22,8 +21,6 @@ import tech.pegasys.pantheon.tests.acceptance.dsl.condition.eth.SanityCheckEthGetWorkValues; import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.eth.EthTransactions; -import org.web3j.protocol.core.methods.response.TransactionReceipt; - public class Eth { private final EthTransactions transactions; @@ -53,10 +50,4 @@ public Condition expectNoTransactionReceipt(final String transactionHash) { return new ExpectEthGetTransactionReceiptIsAbsent( transactions.getTransactionReceipt(transactionHash)); } - - public Condition expectTransactionReceipt( - final String transactionHash, final TransactionReceipt receipt) { - return new EthGetTransactionReceiptEquals( - transactions.getTransactionReceipt(transactionHash), receipt); - } } diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/web3j/DeploySmartContractAcceptanceTest.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/web3j/DeploySmartContractAcceptanceTest.java index cc24583cbf..57ff077777 100644 --- a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/web3j/DeploySmartContractAcceptanceTest.java +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/web3j/DeploySmartContractAcceptanceTest.java @@ -18,11 +18,13 @@ import tech.pegasys.pantheon.tests.acceptance.dsl.node.PantheonNode; import tech.pegasys.pantheon.tests.web3j.generated.SimpleStorage; +import java.math.BigInteger; import java.util.Optional; import org.junit.Before; import org.junit.Test; import org.web3j.protocol.core.methods.response.TransactionReceipt; +import org.web3j.tx.Contract; public class DeploySmartContractAcceptanceTest extends AcceptanceTestBase { @@ -35,23 +37,56 @@ public void setUp() throws Exception { } @Test - public void deployContractReceiptMustMatchEthGetTransactionReceipt() { + public void deployingMustGiveValidReceipt() { final SimpleStorage contract = minerNode.execute(transactions.createSmartContract(SimpleStorage.class)); - assertThat(contract).isNotNull(); - final Optional receipt = contract.getTransactionReceipt(); - assertThat(receipt).isNotNull(); - assertThat(receipt.isPresent()).isTrue(); + final TransactionReceipt transactionReceipt = expectingPresentReceipt(contract); - final TransactionReceipt transactionReceipt = receipt.get(); - assertThat(transactionReceipt.getTransactionHash()).isNotNull(); + verifyReceipt(transactionReceipt); + } + + // TODO move this into the DSL? + private void verifyReceipt(final TransactionReceipt transactionReceipt) { // Contract transaction has no 'to' address or contract address assertThat(transactionReceipt.getTo()).isNull(); - assertThat(transactionReceipt.getContractAddress()).isNotBlank(); - minerNode.verify( - eth.expectTransactionReceipt(transactionReceipt.getTransactionHash(), transactionReceipt)); + assertThat(transactionReceipt.getRoot()).isNull(); + + // Changeable - just check existence + assertThat(transactionReceipt.getBlockHash()).isNotBlank(); + assertThat(transactionReceipt.getTransactionHash()).isNotBlank(); + assertThat(transactionReceipt.getTransactionIndex()).isNotNull(); + assertThat(transactionReceipt.getTransactionHash()).isNotNull(); + + // Block zero is the genesis, must be after that + assertThat(transactionReceipt.getBlockNumber()).isGreaterThanOrEqualTo(BigInteger.ONE); + + // Address generation is deterministic, based on the sender address, nonce and data + assertThat(transactionReceipt.getContractAddress()) + .isEqualTo("0x42699a7612a82f1d9c36148af9c77354759b210b"); + + // Success code is '0x1' + assertThat(transactionReceipt.getStatus()).isEqualTo("0x1"); + + // TODO where is the from address obtained? shouldn't it be the sender address? + assertThat(transactionReceipt.getFrom()) + .isEqualTo("0xfe3b557e8fb62b89f4916b721be55ceb828dbd73"); + + // No logs from expected from the contract deployment + assertThat(transactionReceipt.getLogs()).isNotNull(); + assertThat(transactionReceipt.getLogs().size()).isEqualTo(0); + assertThat(transactionReceipt.getLogsBloom()) + .isEqualTo( + "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"); + } + + // TODO wrap this up? (or rename) + private TransactionReceipt expectingPresentReceipt(final Contract contract) { + assertThat(contract).isNotNull(); + final Optional receipt = contract.getTransactionReceipt(); + assertThat(receipt).isNotNull(); + return receipt.get(); } } From 50f0a28e2d261981dfd4947d6541c12d5340adaf Mon Sep 17 00:00:00 2001 From: CjHare Date: Wed, 28 Nov 2018 09:46:09 +1000 Subject: [PATCH 2/5] Single shared variable for the private key for the first genesis account --- .../acceptance/dsl/account/Accounts.java | 8 ++-- .../DeploySmartContractTransaction.java | 12 +++--- .../DeploySmartContractAcceptanceTest.java | 42 +++++++++---------- 3 files changed, 29 insertions(+), 33 deletions(-) diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/account/Accounts.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/account/Accounts.java index ef39d9b0e8..1a98b59b09 100644 --- a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/account/Accounts.java +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/account/Accounts.java @@ -16,6 +16,9 @@ public class Accounts { + public static final String GENESIS_ACCOUNT_ONE_PRIVATE_KEY = + "8f2a55949038a9610f50fb23b5883af3b4ecb3c3bb792cbcefbd1542c692be63"; + private final EthTransactions eth; private final Account richBenefactorOne; private final Account richBenefactorTwo; @@ -23,10 +26,7 @@ public class Accounts { public Accounts(final EthTransactions eth) { this.eth = eth; richBenefactorOne = - Account.fromPrivateKey( - eth, - "Rich Benefactor One", - "8f2a55949038a9610f50fb23b5883af3b4ecb3c3bb792cbcefbd1542c692be63"); + Account.fromPrivateKey(eth, "Rich Benefactor One", GENESIS_ACCOUNT_ONE_PRIVATE_KEY); richBenefactorTwo = Account.fromPrivateKey( eth, diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/DeploySmartContractTransaction.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/DeploySmartContractTransaction.java index 665e527112..64ba7ee4dc 100644 --- a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/DeploySmartContractTransaction.java +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/DeploySmartContractTransaction.java @@ -12,6 +12,8 @@ */ package tech.pegasys.pantheon.tests.acceptance.dsl.transaction; +import tech.pegasys.pantheon.tests.acceptance.dsl.account.Accounts; + import java.lang.reflect.Method; import java.math.BigInteger; @@ -24,9 +26,9 @@ public class DeploySmartContractTransaction implements Trans private static final BigInteger DEFAULT_GAS_PRICE = BigInteger.valueOf(1000); private static final BigInteger DEFAULT_GAS_LIMIT = BigInteger.valueOf(3000000); - private static final Credentials GENESIS_ACCOUNT_ONE_PRIVATE_KEY = - Credentials.create("0x8f2a55949038a9610f50fb23b5883af3b4ecb3c3bb792cbcefbd1542c692be63"); private static final Object METHOD_IS_STATIC = null; + private static final Credentials BENEFACTOR_ONE = + Credentials.create(Accounts.GENESIS_ACCOUNT_ONE_PRIVATE_KEY); private final Class clazz; @@ -43,11 +45,7 @@ public T execute(final Web3j node) { final Object invoked = method.invoke( - METHOD_IS_STATIC, - node, - GENESIS_ACCOUNT_ONE_PRIVATE_KEY, - DEFAULT_GAS_PRICE, - DEFAULT_GAS_LIMIT); + METHOD_IS_STATIC, node, BENEFACTOR_ONE, DEFAULT_GAS_PRICE, DEFAULT_GAS_LIMIT); return cast(invoked).send(); } catch (final Exception e) { diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/web3j/DeploySmartContractAcceptanceTest.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/web3j/DeploySmartContractAcceptanceTest.java index 57ff077777..f27747581a 100644 --- a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/web3j/DeploySmartContractAcceptanceTest.java +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/web3j/DeploySmartContractAcceptanceTest.java @@ -41,38 +41,44 @@ public void deployingMustGiveValidReceipt() { final SimpleStorage contract = minerNode.execute(transactions.createSmartContract(SimpleStorage.class)); - final TransactionReceipt transactionReceipt = expectingPresentReceipt(contract); - - verifyReceipt(transactionReceipt); + verifyContractReceipt(contract); } - // TODO move this into the DSL? - private void verifyReceipt(final TransactionReceipt transactionReceipt) { + // TODO code reuse - does need a node, new entrypoint in DSL? + private void verifyContractReceipt(final Contract contract) { + + final String contractAddress = "0x42699a7612a82f1d9c36148af9c77354759b210b"; + final String senderAddress = accounts.getPrimaryBenefactor().getAddress(); + + assertThat(contract).isNotNull(); + final Optional receipt = contract.getTransactionReceipt(); + + // We're expecting a receipt + assertThat(receipt).isNotNull(); + assertThat(receipt.isPresent()).isTrue(); + final TransactionReceipt transactionReceipt = receipt.get(); // Contract transaction has no 'to' address or contract address assertThat(transactionReceipt.getTo()).isNull(); - assertThat(transactionReceipt.getRoot()).isNull(); - // Changeable - just check existence + // Variables outside the control of the test :. just check existence assertThat(transactionReceipt.getBlockHash()).isNotBlank(); assertThat(transactionReceipt.getTransactionHash()).isNotBlank(); assertThat(transactionReceipt.getTransactionIndex()).isNotNull(); assertThat(transactionReceipt.getTransactionHash()).isNotNull(); - // Block zero is the genesis, must be after that + // Block zero is the genesis, expecting anytime after then assertThat(transactionReceipt.getBlockNumber()).isGreaterThanOrEqualTo(BigInteger.ONE); // Address generation is deterministic, based on the sender address, nonce and data - assertThat(transactionReceipt.getContractAddress()) - .isEqualTo("0x42699a7612a82f1d9c36148af9c77354759b210b"); + assertThat(transactionReceipt.getContractAddress()).isEqualTo(contractAddress); - // Success code is '0x1' + // Expecting successful transaction (status '0x1') assertThat(transactionReceipt.getStatus()).isEqualTo("0x1"); - // TODO where is the from address obtained? shouldn't it be the sender address? - assertThat(transactionReceipt.getFrom()) - .isEqualTo("0xfe3b557e8fb62b89f4916b721be55ceb828dbd73"); + // Address for the account that signed (and paid) for the contract deployment transaction + assertThat(transactionReceipt.getFrom()).isEqualTo(senderAddress); // No logs from expected from the contract deployment assertThat(transactionReceipt.getLogs()).isNotNull(); @@ -81,12 +87,4 @@ private void verifyReceipt(final TransactionReceipt transactionReceipt) { .isEqualTo( "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"); } - - // TODO wrap this up? (or rename) - private TransactionReceipt expectingPresentReceipt(final Contract contract) { - assertThat(contract).isNotNull(); - final Optional receipt = contract.getTransactionReceipt(); - assertThat(receipt).isNotNull(); - return receipt.get(); - } } From a99d2ca73cf5f8cc8b21a81d070c0f56f72ebf55 Mon Sep 17 00:00:00 2001 From: CjHare Date: Wed, 28 Nov 2018 10:30:28 +1000 Subject: [PATCH 3/5] Refactoring verification for reuse --- .../acceptance/dsl/AcceptanceTestBase.java | 3 + .../dsl/verify/ContractVerifier.java | 72 +++++++++++++++++++ .../DeploySmartContractAcceptanceTest.java | 53 +------------- 3 files changed, 77 insertions(+), 51 deletions(-) create mode 100644 acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/verify/ContractVerifier.java diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/AcceptanceTestBase.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/AcceptanceTestBase.java index 9e04a99339..d6fa76f512 100644 --- a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/AcceptanceTestBase.java +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/AcceptanceTestBase.java @@ -23,6 +23,7 @@ import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.eth.EthTransactions; import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.net.NetTransactions; import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.web3.Web3Transactions; +import tech.pegasys.pantheon.tests.acceptance.dsl.verify.ContractVerifier; import org.junit.After; @@ -36,6 +37,7 @@ public class AcceptanceTestBase { protected final Eth eth; protected final Net net; protected final PantheonNodeFactory pantheon; + protected final ContractVerifier contractVerifier; protected AcceptanceTestBase() { final EthTransactions ethTransactions = new EthTransactions(); @@ -47,6 +49,7 @@ protected AcceptanceTestBase() { transactions = new Transactions(accounts); web3 = new Web3(new Web3Transactions()); pantheon = new PantheonNodeFactory(); + contractVerifier = new ContractVerifier(accounts.getPrimaryBenefactor()); } @After diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/verify/ContractVerifier.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/verify/ContractVerifier.java new file mode 100644 index 0000000000..9c8f3c5926 --- /dev/null +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/verify/ContractVerifier.java @@ -0,0 +1,72 @@ +/* + * Copyright 2018 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. + */ +package tech.pegasys.pantheon.tests.acceptance.dsl.verify; + +import static org.assertj.core.api.Assertions.assertThat; + +import tech.pegasys.pantheon.tests.acceptance.dsl.account.Account; + +import java.math.BigInteger; +import java.util.Optional; + +import org.web3j.protocol.core.methods.response.TransactionReceipt; +import org.web3j.tx.Contract; + +public class ContractVerifier { + + private final String senderAddress; + + public ContractVerifier(final Account sender) { + this.senderAddress = sender.getAddress(); + } + + public void verifyValidTransactionReceipt(final Contract contract, final String contractAddress) { + + assertThat(contract).isNotNull(); + final Optional receipt = contract.getTransactionReceipt(); + + // We're expecting a receipt + assertThat(receipt).isNotNull(); + assertThat(receipt.isPresent()).isTrue(); + final TransactionReceipt transactionReceipt = receipt.get(); + + // Contract transaction has no 'to' address or contract address + assertThat(transactionReceipt.getTo()).isNull(); + assertThat(transactionReceipt.getRoot()).isNull(); + + // Variables outside the control of the test :. just check existence + assertThat(transactionReceipt.getBlockHash()).isNotBlank(); + assertThat(transactionReceipt.getTransactionHash()).isNotBlank(); + assertThat(transactionReceipt.getTransactionIndex()).isNotNull(); + assertThat(transactionReceipt.getTransactionHash()).isNotNull(); + + // Block zero is the genesis, expecting anytime after then + assertThat(transactionReceipt.getBlockNumber()).isGreaterThanOrEqualTo(BigInteger.ONE); + + // Address generation is deterministic, based on the sender address and the transaction nonce + assertThat(transactionReceipt.getContractAddress()).isEqualTo(contractAddress); + + // Expecting successful transaction (status '0x1') + assertThat(transactionReceipt.getStatus()).isEqualTo("0x1"); + + // Address for the account that signed (and paid) for the contract deployment transaction + assertThat(transactionReceipt.getFrom()).isEqualTo(senderAddress); + + // No logs from expected from the contract deployment + assertThat(transactionReceipt.getLogs()).isNotNull(); + assertThat(transactionReceipt.getLogs().size()).isEqualTo(0); + assertThat(transactionReceipt.getLogsBloom()) + .isEqualTo( + "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"); + } +} diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/web3j/DeploySmartContractAcceptanceTest.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/web3j/DeploySmartContractAcceptanceTest.java index f27747581a..70a9ec3940 100644 --- a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/web3j/DeploySmartContractAcceptanceTest.java +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/web3j/DeploySmartContractAcceptanceTest.java @@ -12,19 +12,12 @@ */ package tech.pegasys.pantheon.tests.web3j; -import static org.assertj.core.api.Assertions.assertThat; - import tech.pegasys.pantheon.tests.acceptance.dsl.AcceptanceTestBase; import tech.pegasys.pantheon.tests.acceptance.dsl.node.PantheonNode; import tech.pegasys.pantheon.tests.web3j.generated.SimpleStorage; -import java.math.BigInteger; -import java.util.Optional; - import org.junit.Before; import org.junit.Test; -import org.web3j.protocol.core.methods.response.TransactionReceipt; -import org.web3j.tx.Contract; public class DeploySmartContractAcceptanceTest extends AcceptanceTestBase { @@ -41,50 +34,8 @@ public void deployingMustGiveValidReceipt() { final SimpleStorage contract = minerNode.execute(transactions.createSmartContract(SimpleStorage.class)); - verifyContractReceipt(contract); - } - - // TODO code reuse - does need a node, new entrypoint in DSL? - private void verifyContractReceipt(final Contract contract) { - - final String contractAddress = "0x42699a7612a82f1d9c36148af9c77354759b210b"; - final String senderAddress = accounts.getPrimaryBenefactor().getAddress(); - - assertThat(contract).isNotNull(); - final Optional receipt = contract.getTransactionReceipt(); - - // We're expecting a receipt - assertThat(receipt).isNotNull(); - assertThat(receipt.isPresent()).isTrue(); - final TransactionReceipt transactionReceipt = receipt.get(); - - // Contract transaction has no 'to' address or contract address - assertThat(transactionReceipt.getTo()).isNull(); - assertThat(transactionReceipt.getRoot()).isNull(); - - // Variables outside the control of the test :. just check existence - assertThat(transactionReceipt.getBlockHash()).isNotBlank(); - assertThat(transactionReceipt.getTransactionHash()).isNotBlank(); - assertThat(transactionReceipt.getTransactionIndex()).isNotNull(); - assertThat(transactionReceipt.getTransactionHash()).isNotNull(); - - // Block zero is the genesis, expecting anytime after then - assertThat(transactionReceipt.getBlockNumber()).isGreaterThanOrEqualTo(BigInteger.ONE); - - // Address generation is deterministic, based on the sender address, nonce and data - assertThat(transactionReceipt.getContractAddress()).isEqualTo(contractAddress); - - // Expecting successful transaction (status '0x1') - assertThat(transactionReceipt.getStatus()).isEqualTo("0x1"); - - // Address for the account that signed (and paid) for the contract deployment transaction - assertThat(transactionReceipt.getFrom()).isEqualTo(senderAddress); + final String expectedContractAddress = "0x42699a7612a82f1d9c36148af9c77354759b210b"; - // No logs from expected from the contract deployment - assertThat(transactionReceipt.getLogs()).isNotNull(); - assertThat(transactionReceipt.getLogs().size()).isEqualTo(0); - assertThat(transactionReceipt.getLogsBloom()) - .isEqualTo( - "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"); + contractVerifier.verifyValidTransactionReceipt(contract, expectedContractAddress); } } From e73e957be653df27d8e8e8cb4b04ef3f6d0b60a1 Mon Sep 17 00:00:00 2001 From: CjHare Date: Wed, 28 Nov 2018 10:55:52 +1000 Subject: [PATCH 4/5] Reshaping the code --- .../acceptance/dsl/AcceptanceTestBase.java | 6 ++-- .../dsl/contract/ContractVerifier.java | 28 +++++++++++++++++++ .../ExpectValidTransactionReceipt.java} | 10 ++++--- .../DeploySmartContractAcceptanceTest.java | 9 +++--- 4 files changed, 42 insertions(+), 11 deletions(-) create mode 100644 acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/contract/ContractVerifier.java rename acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/{verify/ContractVerifier.java => contract/ExpectValidTransactionReceipt.java} (90%) diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/AcceptanceTestBase.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/AcceptanceTestBase.java index d6fa76f512..085c376a06 100644 --- a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/AcceptanceTestBase.java +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/AcceptanceTestBase.java @@ -14,6 +14,7 @@ import tech.pegasys.pantheon.tests.acceptance.dsl.account.Accounts; import tech.pegasys.pantheon.tests.acceptance.dsl.blockchain.Blockchain; +import tech.pegasys.pantheon.tests.acceptance.dsl.contract.ContractVerifier; import tech.pegasys.pantheon.tests.acceptance.dsl.jsonrpc.Eth; import tech.pegasys.pantheon.tests.acceptance.dsl.jsonrpc.Net; import tech.pegasys.pantheon.tests.acceptance.dsl.jsonrpc.Web3; @@ -23,7 +24,6 @@ import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.eth.EthTransactions; import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.net.NetTransactions; import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.web3.Web3Transactions; -import tech.pegasys.pantheon.tests.acceptance.dsl.verify.ContractVerifier; import org.junit.After; @@ -37,7 +37,7 @@ public class AcceptanceTestBase { protected final Eth eth; protected final Net net; protected final PantheonNodeFactory pantheon; - protected final ContractVerifier contractVerifier; + protected final ContractVerifier contract; protected AcceptanceTestBase() { final EthTransactions ethTransactions = new EthTransactions(); @@ -49,7 +49,7 @@ protected AcceptanceTestBase() { transactions = new Transactions(accounts); web3 = new Web3(new Web3Transactions()); pantheon = new PantheonNodeFactory(); - contractVerifier = new ContractVerifier(accounts.getPrimaryBenefactor()); + contract = new ContractVerifier(accounts.getPrimaryBenefactor()); } @After diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/contract/ContractVerifier.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/contract/ContractVerifier.java new file mode 100644 index 0000000000..4b6b20845f --- /dev/null +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/contract/ContractVerifier.java @@ -0,0 +1,28 @@ +/* + * Copyright 2018 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. + */ +package tech.pegasys.pantheon.tests.acceptance.dsl.contract; + +import tech.pegasys.pantheon.tests.acceptance.dsl.account.Account; + +public class ContractVerifier { + + private final Account sender; + + public ContractVerifier(final Account sender) { + this.sender = sender; + } + + public ExpectValidTransactionReceipt validTransactionReceipt(final String contractAddress) { + return new ExpectValidTransactionReceipt(sender, contractAddress); + } +} diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/verify/ContractVerifier.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/contract/ExpectValidTransactionReceipt.java similarity index 90% rename from acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/verify/ContractVerifier.java rename to acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/contract/ExpectValidTransactionReceipt.java index 9c8f3c5926..e6dc1ad7b5 100644 --- a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/verify/ContractVerifier.java +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/contract/ExpectValidTransactionReceipt.java @@ -10,7 +10,7 @@ * 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. */ -package tech.pegasys.pantheon.tests.acceptance.dsl.verify; +package tech.pegasys.pantheon.tests.acceptance.dsl.contract; import static org.assertj.core.api.Assertions.assertThat; @@ -22,15 +22,17 @@ import org.web3j.protocol.core.methods.response.TransactionReceipt; import org.web3j.tx.Contract; -public class ContractVerifier { +public class ExpectValidTransactionReceipt { private final String senderAddress; + private final String contractAddress; - public ContractVerifier(final Account sender) { + public ExpectValidTransactionReceipt(final Account sender, final String contractAddress) { + this.contractAddress = contractAddress; this.senderAddress = sender.getAddress(); } - public void verifyValidTransactionReceipt(final Contract contract, final String contractAddress) { + public void verify(final Contract contract) { assertThat(contract).isNotNull(); final Optional receipt = contract.getTransactionReceipt(); diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/web3j/DeploySmartContractAcceptanceTest.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/web3j/DeploySmartContractAcceptanceTest.java index 70a9ec3940..c6eaf67996 100644 --- a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/web3j/DeploySmartContractAcceptanceTest.java +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/web3j/DeploySmartContractAcceptanceTest.java @@ -31,11 +31,12 @@ public void setUp() throws Exception { @Test public void deployingMustGiveValidReceipt() { - final SimpleStorage contract = - minerNode.execute(transactions.createSmartContract(SimpleStorage.class)); + // Contract address is generated from sender address and transaction nonce + final String contractAddress = "0x42699a7612a82f1d9c36148af9c77354759b210b"; - final String expectedContractAddress = "0x42699a7612a82f1d9c36148af9c77354759b210b"; + final SimpleStorage simpleStorageContract = + minerNode.execute(transactions.createSmartContract(SimpleStorage.class)); - contractVerifier.verifyValidTransactionReceipt(contract, expectedContractAddress); + contract.validTransactionReceipt(contractAddress).verify(simpleStorageContract); } } From 576d6591bdf6730c1d3bb7589badd621424018c9 Mon Sep 17 00:00:00 2001 From: CjHare Date: Wed, 28 Nov 2018 15:12:57 +1000 Subject: [PATCH 5/5] PR comments --- .../tests/acceptance/dsl/AcceptanceTestBase.java | 4 ++-- .../eth/ExpectContractTransactionReciept.java | 15 --------------- .../web3j/DeploySmartContractAcceptanceTest.java | 2 +- 3 files changed, 3 insertions(+), 18 deletions(-) delete mode 100644 acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/eth/ExpectContractTransactionReciept.java diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/AcceptanceTestBase.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/AcceptanceTestBase.java index 085c376a06..481f980974 100644 --- a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/AcceptanceTestBase.java +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/AcceptanceTestBase.java @@ -37,7 +37,7 @@ public class AcceptanceTestBase { protected final Eth eth; protected final Net net; protected final PantheonNodeFactory pantheon; - protected final ContractVerifier contract; + protected final ContractVerifier contractVerifier; protected AcceptanceTestBase() { final EthTransactions ethTransactions = new EthTransactions(); @@ -49,7 +49,7 @@ protected AcceptanceTestBase() { transactions = new Transactions(accounts); web3 = new Web3(new Web3Transactions()); pantheon = new PantheonNodeFactory(); - contract = new ContractVerifier(accounts.getPrimaryBenefactor()); + contractVerifier = new ContractVerifier(accounts.getPrimaryBenefactor()); } @After diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/eth/ExpectContractTransactionReciept.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/eth/ExpectContractTransactionReciept.java deleted file mode 100644 index 17386854fb..0000000000 --- a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/eth/ExpectContractTransactionReciept.java +++ /dev/null @@ -1,15 +0,0 @@ -/* - * Copyright 2018 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. - */ -package tech.pegasys.pantheon.tests.acceptance.dsl.condition.eth; - -public class ExpectContractTransactionReciept {} diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/web3j/DeploySmartContractAcceptanceTest.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/web3j/DeploySmartContractAcceptanceTest.java index c6eaf67996..0d5b65ce16 100644 --- a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/web3j/DeploySmartContractAcceptanceTest.java +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/web3j/DeploySmartContractAcceptanceTest.java @@ -37,6 +37,6 @@ public void deployingMustGiveValidReceipt() { final SimpleStorage simpleStorageContract = minerNode.execute(transactions.createSmartContract(SimpleStorage.class)); - contract.validTransactionReceipt(contractAddress).verify(simpleStorageContract); + contractVerifier.validTransactionReceipt(contractAddress).verify(simpleStorageContract); } }