NC-1890: Acceptance test smart contract (#283)

* IoC for deploying the smart contract

* Smart contract deploy as a Transaction

* Consistent transaction naming

* Avoiding a resource leak with lazy creation of Web3j connections
CJ Hare 6 years ago committed by GitHub
parent 8792c0a00a
commit ae8dc507ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 28
      acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/node/PantheonNode.java
  2. 62
      acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/DeploySmartContractTransaction.java
  3. 6
      acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/Transactions.java
  4. 18
      acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/web3j/EventEmitterAcceptanceTest.java

@ -38,7 +38,6 @@ import com.google.common.io.MoreFiles;
import com.google.common.io.RecursiveDeleteOption; import com.google.common.io.RecursiveDeleteOption;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.web3j.protocol.Web3j; import org.web3j.protocol.Web3j;
import org.web3j.protocol.Web3jService;
import org.web3j.protocol.http.HttpService; import org.web3j.protocol.http.HttpService;
import org.web3j.protocol.websocket.WebSocketService; import org.web3j.protocol.websocket.WebSocketService;
import org.web3j.utils.Async; import org.web3j.utils.Async;
@ -131,31 +130,16 @@ public class PantheonNode implements Node, NodeConfiguration, RunnableNode, Auto
return LOCALHOST; return LOCALHOST;
} }
@Deprecated private Web3j web3j() {
public Web3j web3j() {
if (!jsonRpcBaseUrl().isPresent()) {
return web3j(new HttpService("http://" + LOCALHOST + ":8545"));
}
if (web3j == null) { if (web3j == null) {
return web3j(new HttpService(jsonRpcBaseUrl().get())); if (!jsonRpcBaseUrl().isPresent()) {
} return Web3j.build(
new HttpService("http://" + LOCALHOST + ":8545"), 2000, Async.defaultExecutorService());
return web3j;
}
public void setWeb3j(final Web3jService web3jService) {
if (web3j != null) {
web3j.shutdown();
}
web3j = Web3j.build(web3jService, 2000, Async.defaultExecutorService());
} }
@Deprecated return Web3j.build(
public Web3j web3j(final Web3jService web3jService) { new HttpService(jsonRpcBaseUrl().get()), 2000, Async.defaultExecutorService());
if (web3j == null) {
web3j = Web3j.build(web3jService, 2000, Async.defaultExecutorService());
} }
return web3j; return web3j;

@ -0,0 +1,62 @@
/*
* 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.transaction;
import java.lang.reflect.Method;
import java.math.BigInteger;
import org.web3j.crypto.Credentials;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.RemoteCall;
import org.web3j.tx.Contract;
public class DeploySmartContractTransaction<T extends Contract> implements Transaction<T> {
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 final Class<T> clazz;
public DeploySmartContractTransaction(final Class<T> clazz) {
this.clazz = clazz;
}
@Override
public T execute(final Web3j node) {
try {
final Method method =
clazz.getMethod(
"deploy", Web3j.class, Credentials.class, BigInteger.class, BigInteger.class);
final Object invoked =
method.invoke(
METHOD_IS_STATIC,
node,
GENESIS_ACCOUNT_ONE_PRIVATE_KEY,
DEFAULT_GAS_PRICE,
DEFAULT_GAS_LIMIT);
return cast(invoked).send();
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
@SuppressWarnings("unchecked")
private RemoteCall<T> cast(final Object invokedMethod) {
return (RemoteCall<T>) invokedMethod;
}
}

@ -20,6 +20,7 @@ import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.account.TransferTr
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.web3j.tx.Contract;
import org.web3j.utils.Convert.Unit; import org.web3j.utils.Convert.Unit;
public class Transactions { public class Transactions {
@ -49,4 +50,9 @@ public class Transactions {
return new TransferTransactionSet(transfers); return new TransferTransactionSet(transfers);
} }
public <T extends Contract> DeploySmartContractTransaction<T> createSmartContract(
final Class<T> clazz) {
return new DeploySmartContractTransaction<>(clazz);
}
} }

@ -13,6 +13,7 @@
package tech.pegasys.pantheon.tests.web3j; package tech.pegasys.pantheon.tests.web3j;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import tech.pegasys.pantheon.tests.acceptance.dsl.AcceptanceTestBase; import tech.pegasys.pantheon.tests.acceptance.dsl.AcceptanceTestBase;
@ -26,23 +27,16 @@ import java.util.concurrent.atomic.AtomicBoolean;
import org.junit.Before; import org.junit.Before;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.web3j.crypto.Credentials;
import org.web3j.protocol.core.methods.request.EthFilter; import org.web3j.protocol.core.methods.request.EthFilter;
import org.web3j.protocol.core.methods.response.TransactionReceipt; import org.web3j.protocol.core.methods.response.TransactionReceipt;
import rx.Observable; import rx.Observable;
/* /*
* This class is based around the EventEmitter solidity contract * This class is based around the EventEmitter solidity contract
*
*/ */
@Ignore @Ignore
public class EventEmitterAcceptanceTest extends AcceptanceTestBase { public class EventEmitterAcceptanceTest extends AcceptanceTestBase {
public static final BigInteger DEFAULT_GAS_PRICE = BigInteger.valueOf(1000);
public static final BigInteger DEFAULT_GAS_LIMIT = BigInteger.valueOf(3000000);
Credentials MAIN_CREDENTIALS =
Credentials.create("0x8f2a55949038a9610f50fb23b5883af3b4ecb3c3bb792cbcefbd1542c692be63");
private PantheonNode node; private PantheonNode node;
@Before @Before
@ -53,21 +47,23 @@ public class EventEmitterAcceptanceTest extends AcceptanceTestBase {
@Test @Test
public void shouldDeployContractAndAllowLookupOfValuesAndEmittingEvents() throws Exception { public void shouldDeployContractAndAllowLookupOfValuesAndEmittingEvents() throws Exception {
System.out.println("Sending Create Contract Transaction");
final EventEmitter eventEmitter = final EventEmitter eventEmitter =
EventEmitter.deploy(node.web3j(), MAIN_CREDENTIALS, DEFAULT_GAS_PRICE, DEFAULT_GAS_LIMIT) node.execute(transactions.createSmartContract(EventEmitter.class));
.send();
final Observable<StoredEventResponse> storedEventResponseObservable = final Observable<StoredEventResponse> storedEventResponseObservable =
eventEmitter.storedEventObservable(new EthFilter()); eventEmitter.storedEventObservable(new EthFilter());
final AtomicBoolean subscriptionReceived = new AtomicBoolean(false); final AtomicBoolean subscriptionReceived = new AtomicBoolean(false);
storedEventResponseObservable.subscribe( storedEventResponseObservable.subscribe(
storedEventResponse -> { storedEventResponse -> {
subscriptionReceived.set(true); subscriptionReceived.set(true);
assertEquals(BigInteger.valueOf(12), storedEventResponse._amount); assertEquals(BigInteger.valueOf(12), storedEventResponse._amount);
}); });
final TransactionReceipt send = eventEmitter.store(BigInteger.valueOf(12)).send(); final TransactionReceipt receipt = eventEmitter.store(BigInteger.valueOf(12)).send();
assertNotNull(receipt);
assertEquals(BigInteger.valueOf(12), eventEmitter.value().send()); assertEquals(BigInteger.valueOf(12), eventEmitter.value().send());
assertTrue(subscriptionReceived.get()); assertTrue(subscriptionReceived.get());
} }

Loading…
Cancel
Save