Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.

Commit

Permalink
Smart contract acceptance test (#296)
Browse files Browse the repository at this point in the history
* Upgrade Web3j to 3.6.0

* Correcting ErrorProne exclusion regex

* Deploying the simple storage contract

* Web3j to 4.0.1

* Verifying the transaction receipt
  • Loading branch information
CjHare authored Nov 23, 2018
1 parent 14a8f37 commit 1e77943
Show file tree
Hide file tree
Showing 14 changed files with 437 additions and 75 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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<TransactionReceipt> response = node.execute(transaction);

assertThat(response.isPresent()).isTrue();
assertThat(response.get()).isEqualTo(expectedReceipt);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@

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;
import tech.pegasys.pantheon.tests.acceptance.dsl.condition.eth.ExpectSuccessfulEthGetTransactionReceipt;
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;
Expand Down Expand Up @@ -50,4 +53,10 @@ 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class DeploySmartContractTransaction<T extends Contract> implements Trans

private final Class<T> clazz;

public DeploySmartContractTransaction(final Class<T> clazz) {
DeploySmartContractTransaction(final Class<T> clazz) {
this.clazz = clazz;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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.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.util.Optional;

import org.junit.Before;
import org.junit.Test;
import org.web3j.protocol.core.methods.response.TransactionReceipt;

public class DeploySmartContractAcceptanceTest extends AcceptanceTestBase {

private PantheonNode minerNode;

@Before
public void setUp() throws Exception {
minerNode = pantheon.createMinerNode("miner-node");
cluster.start(minerNode);
}

@Test
public void deployContractReceiptMustMatchEthGetTransactionReceipt() {
final SimpleStorage contract =
minerNode.execute(transactions.createSmartContract(SimpleStorage.class));
assertThat(contract).isNotNull();

final Optional<TransactionReceipt> receipt = contract.getTransactionReceipt();
assertThat(receipt).isNotNull();
assertThat(receipt.isPresent()).isTrue();

final TransactionReceipt transactionReceipt = receipt.get();
assertThat(transactionReceipt.getTransactionHash()).isNotNull();

// Contract transaction has no 'to' address or contract address
assertThat(transactionReceipt.getTo()).isNull();
assertThat(transactionReceipt.getContractAddress()).isNotBlank();

minerNode.verify(
eth.expectTransactionReceipt(transactionReceipt.getTransactionHash(), transactionReceipt));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
* 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;
pragma solidity >=0.4.0 <0.6.0;

// compile with:
// solc EventEmitter.sol --bin --abi --optimize --overwrite -o .
// then create web3j wrappers with:
// web3j solidity generate ./generated/EventEmitter.bin ./generated/EventEmitter.abi -o ../../../../../ -p tech.pegasys.pantheon.tests.web3j.generated
// web3j solidity generate -b ./generated/EventEmitter.bin -a ./generated/EventEmitter.abi -o ../../../../../ -p tech.pegasys.pantheon.tests.web3j.generated
contract EventEmitter {
address owner;
event stored(address _to, uint _amount);
Expand All @@ -31,11 +32,11 @@ contract EventEmitter {
_sender = msg.sender;
}

function value() constant public returns (uint) {
function value() view public returns (uint) {
return _value;
}

function sender() constant public returns (address) {
function sender() view public returns (address) {
return _sender;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package tech.pegasys.pantheon.tests.web3j;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

Expand All @@ -24,11 +25,12 @@
import java.math.BigInteger;
import java.util.concurrent.atomic.AtomicBoolean;

import io.reactivex.Flowable;
import io.reactivex.disposables.Disposable;
import org.junit.Before;
import org.junit.Test;
import org.web3j.protocol.core.methods.request.EthFilter;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import rx.Observable;

/*
* This class is based around the EventEmitter solidity contract
Expand All @@ -48,16 +50,19 @@ public void shouldDeployContractAndAllowLookupOfValuesAndEmittingEvents() throws
final EventEmitter eventEmitter =
node.execute(transactions.createSmartContract(EventEmitter.class));

final Observable<StoredEventResponse> storedEventResponseObservable =
eventEmitter.storedEventObservable(new EthFilter());
final Flowable<StoredEventResponse> storedEventResponseObservable =
eventEmitter.storedEventFlowable(new EthFilter());

final AtomicBoolean subscriptionReceived = new AtomicBoolean(false);

storedEventResponseObservable.subscribe(
storedEventResponse -> {
subscriptionReceived.set(true);
assertEquals(BigInteger.valueOf(12), storedEventResponse._amount);
});
final Disposable subscription =
storedEventResponseObservable.subscribe(
storedEventResponse -> {
subscriptionReceived.set(true);
assertEquals(BigInteger.valueOf(12), storedEventResponse._amount);
});

assertFalse(subscription.isDisposed());

final TransactionReceipt receipt = eventEmitter.store(BigInteger.valueOf(12)).send();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* 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.
*/
pragma solidity >=0.4.0 <0.6.0;

// compile with:
// solc SimpleStorage.sol --bin --abi --optimize --overwrite -o .
// then create web3j wrappers with:
// web3j solidity generate -b ./generated/SimpleStorage.bin -a ./generated/SimpleStorage.abi -o ../../../../../ -p tech.pegasys.pantheon.tests.web3j.generated
contract SimpleStorage {
uint data;

function set(uint value) public {
data = value;
}

function get() public view returns (uint) {
return data;
}
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
608060405234801561001057600080fd5b5060008054600160a060020a03191633179055610187806100326000396000f3006080604052600436106100565763ffffffff7c01000000000000000000000000000000000000000000000000000000006000350416633fa4f245811461005b5780636057361d1461008257806367e404ce1461009c575b600080fd5b34801561006757600080fd5b506100706100da565b60408051918252519081900360200190f35b34801561008e57600080fd5b5061009a6004356100e0565b005b3480156100a857600080fd5b506100b161013f565b6040805173ffffffffffffffffffffffffffffffffffffffff9092168252519081900360200190f35b60025490565b604080513381526020810183905281517fc9db20adedc6cf2b5d25252b101ab03e124902a73fcb12b753f3d1aaa2d8f9f5929181900390910190a16002556001805473ffffffffffffffffffffffffffffffffffffffff191633179055565b60015473ffffffffffffffffffffffffffffffffffffffff16905600a165627a7a72305820f958aea7922a9538be4c34980ad3171806aad2d3fedb62682cef2ca4e1f1f3120029
608060405234801561001057600080fd5b5060008054600160a060020a03191633179055610199806100326000396000f3fe6080604052600436106100565763ffffffff7c01000000000000000000000000000000000000000000000000000000006000350416633fa4f245811461005b5780636057361d1461008257806367e404ce146100ae575b600080fd5b34801561006757600080fd5b506100706100ec565b60408051918252519081900360200190f35b34801561008e57600080fd5b506100ac600480360360208110156100a557600080fd5b50356100f2565b005b3480156100ba57600080fd5b506100c3610151565b6040805173ffffffffffffffffffffffffffffffffffffffff9092168252519081900360200190f35b60025490565b604080513381526020810183905281517fc9db20adedc6cf2b5d25252b101ab03e124902a73fcb12b753f3d1aaa2d8f9f5929181900390910190a16002556001805473ffffffffffffffffffffffffffffffffffffffff191633179055565b60015473ffffffffffffffffffffffffffffffffffffffff169056fea165627a7a72305820c7f729cb24e05c221f5aa913700793994656f233fe2ce3b9fd9a505ea17e8d8a0029
Loading

0 comments on commit 1e77943

Please sign in to comment.