Skip to content

Commit

Permalink
1. Created event type & encoder.
Browse files Browse the repository at this point in the history
2. Updated filter request API.
3. Created filter integration tests.
4. Migrated IntegrationTestConfig over to being an interface.
5. Moved common scenario code out into dedicated class.
6. Fixed multiple function value decoding bug.
7. Bumped version
  • Loading branch information
conor10 committed Oct 4, 2016
1 parent 73b439d commit 57ccba1
Show file tree
Hide file tree
Showing 24 changed files with 648 additions and 141 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Add the following dependency to your project:
<dependency>
<groupId>org.web3j</groupId>
<artifactId>core</artifactId>
<version>0.3.0</version>
<version>0.3.1</version>
</dependency>
```

Expand All @@ -46,7 +46,7 @@ Add the following dependency to your project:
repositories {
maven {url "http://dl.bintray.com/web3j/maven"}
}
compile ("org.web3j:core:0.3.0")
compile ("org.web3j:core:0.3.1")
```

Start up an Ethereum client if you don't already have one running, such as [Geth](https://github.com/ethereum/go-ethereum/wiki/geth):
Expand Down Expand Up @@ -115,6 +115,10 @@ org.web3j.codegen.SolidityFunctionWrapperGenerator /path/to/<smart-contract>.abi
See `org.web3j.protocol.scenarios.FunctionWrappersIT` for an example of using a generated smart contract Java wrapper.


## Working with filters

See `org.web3j.protocol.scenarios.EventFilterIT` for an example.


## Tested clients

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ apply plugin: 'jacoco'
apply plugin: 'maven-publish'

group 'org.web3j'
version '0.3.0'
version '0.3.1'

sourceCompatibility = 1.8

Expand Down
57 changes: 40 additions & 17 deletions src/integration-test/java/org/web3j/protocol/core/CoreIT.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package org.web3j.protocol.core;

import java.math.BigInteger;
import java.util.List;

import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

import org.web3j.protocol.core.methods.request.EthFilter;
import org.web3j.protocol.core.methods.response.EthCall;
import org.web3j.protocol.core.methods.response.EthSendTransaction;
import org.web3j.protocol.http.HttpService;
Expand All @@ -17,6 +19,7 @@
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

/**
Expand Down Expand Up @@ -358,8 +361,31 @@ public void testEthCompileSerpent() throws Exception {
}

@Test
public void testEthNewFilter() throws Exception {
public void testFiltersByFilterId() throws Exception {
EthFilter ethFilter = new EthFilter(
DefaultBlockParameterName.EARLIEST,
DefaultBlockParameterName.LATEST,
config.validContractAddress());

String eventSignature = config.encodedEvent();
ethFilter.addSingleTopic(eventSignature);

// eth_newFilter
EthNewFilter ethNewFilter = web3j.ethNewFilter(ethFilter).send();
BigInteger filterId = ethNewFilter.getFilterId();

// eth_getFilterLogs
EthLog ethFilterLogs = web3j.ethGetFilterLogs(filterId).send();
List<EthLog.LogResult> filterLogs = ethFilterLogs.getLogs();
assertFalse(filterLogs.isEmpty());

// eth_getFilterChanges - nothing will have changed in this interval
EthLog ethLog = web3j.ethGetFilterChanges(filterId).send();
assertNull(ethLog.getLogs());

// eth_uninstallFilter
EthUninstallFilter ethUninstallFilter = web3j.ethUninstallFilter(filterId).send();
assertTrue(ethUninstallFilter.isUninstalled());
}

@Test
Expand All @@ -370,27 +396,24 @@ public void testEthNewBlockFilter() throws Exception {

@Test
public void testEthNewPendingTransactionFilter() throws Exception {

}

@Test
public void testEthUninstallFilter() throws Exception {

EthNewPendingTransactionFilter ethNewPendingTransactionFilter =
web3j.ethNewPendingTransactionFilter().send();
assertNotNull(ethNewPendingTransactionFilter.getFilterId());
}

@Test
public void testEthGetFilterChanges() throws Exception {

}
public void testEthGetLogs() throws Exception {
EthFilter ethFilter = new EthFilter(
DefaultBlockParameterName.EARLIEST,
DefaultBlockParameterName.LATEST,
config.validContractAddress()
);

@Test
public void testEthGetFilterLogs() throws Exception {

}
ethFilter.addSingleTopic(config.encodedEvent());

@Test
public void testEthGetLogs() throws Exception {

EthLog ethLog = web3j.ethGetLogs(ethFilter).send();
List<EthLog.LogResult> logs = ethLog.getLogs();
assertFalse(logs.isEmpty());
}

// @Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@
/**
* Common values used by integration tests.
*/
public abstract class IntegrationTestConfig {
public abstract String validBlockHash();
public abstract BigInteger validBlock();
public abstract BigInteger validBlockTransactionCount();
public abstract BigInteger validBlockUncleCount();
public abstract String validAccount();
public abstract String validContractAddress();
public abstract String validContractAddressPositionZero();
public abstract String validContractCode();
public abstract EthSendTransaction ethSendTransaction();
public abstract EthCall ethCall();
public abstract String validTransactionHash();
public abstract String validUncleBlockHash();
public abstract BigInteger validUncleBlock();
public interface IntegrationTestConfig {
String validBlockHash();
BigInteger validBlock();
BigInteger validBlockTransactionCount();
BigInteger validBlockUncleCount();
String validAccount();
String validContractAddress();
String validContractAddressPositionZero();
String validContractCode();
EthSendTransaction ethSendTransaction();
EthCall ethCall();
String validTransactionHash();
String validUncleBlockHash();
BigInteger validUncleBlock();
String encodedEvent();
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package org.web3j.protocol.core;

import java.math.BigInteger;
import java.util.Collections;

import org.web3j.abi.EventEncoder;
import org.web3j.abi.datatypes.Event;
import org.web3j.abi.datatypes.Uint;
import org.web3j.protocol.core.methods.request.EthCall;
import org.web3j.protocol.core.methods.request.EthSendTransaction;

/**
* Mordon Testnet Configuration.
*/
public class MordenTestnetConfig extends IntegrationTestConfig {
public class MordenTestnetConfig implements IntegrationTestConfig {

@Override
public String validBlockHash() {
Expand Down Expand Up @@ -40,8 +44,8 @@ public String validAccount() {

@Override
public String validContractAddress() {
// https://testnet.etherscan.io/address/0xbe5422d15f39373eb0a97ff8c10fbd0e40e29338
return "0xbe5422d15f39373eb0a97ff8c10fbd0e40e29338";
// Deployed fibonacci example
return "0x3c05b2564139fb55820b18b72e94b2178eaace7d";
}

@Override
Expand Down Expand Up @@ -84,4 +88,14 @@ public String validUncleBlockHash() {
public BigInteger validUncleBlock() {
return BigInteger.valueOf(1640092);
}

@Override
public String encodedEvent() {

Event event = new Event<>("Notify",
Collections.singletonList(Uint.class),
Collections.singletonList(Uint.class));

return EventEncoder.encode(event);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,44 +30,19 @@
/**
* Integration test demonstrating the full contract deployment workflow.
*/
public class DeployContractIT {
private static final String ADDRESS = "";
private static final String PASSWORD = "";
private static final BigInteger ACCOUNT_UNLOCK_DURATION = BigInteger.valueOf(30);

private Parity parity;

public DeployContractIT() {
System.setProperty("org.apache.commons.logging.Log","org.apache.commons.logging.impl.SimpleLog");
System.setProperty("org.apache.commons.logging.simplelog.showdatetime", "true");
System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "DEBUG");
}

@Before
public void setUp() {
this.parity = Parity.build(new HttpService());
}
public class DeployContractIT extends Scenario {

@Test
public void testContractCreation() throws Exception {
boolean accountUnlocked = unlockAccount();
assertTrue(accountUnlocked);

String transactionHash = sendTransaction();
// String transactionHash = "0xb4820c8369a4061187d18a2114919ca2357996923c6d59ffa024a71cc9ce3ad8";
assertFalse(transactionHash.isEmpty());

int sleepDuration = 10000;
int attempts = 10;

Optional<EthGetTransactionReceipt.TransactionReceipt> transactionReceiptOptional =
getTransactionReceipt(transactionHash, sleepDuration, attempts);

if (!transactionReceiptOptional.isPresent()) {
fail("Transaction reciept not generated after " + attempts + " attempts");
}

EthGetTransactionReceipt.TransactionReceipt transactionReceipt =
transactionReceiptOptional.get();
waitForTransactionReceipt(transactionHash);

assertThat(transactionReceipt.getTransactionHash(), is(transactionHash));
Optional<String> contractAddressOptional = transactionReceipt.getContractAddress();
Expand All @@ -80,18 +55,11 @@ public void testContractCreation() throws Exception {
String responseValue = callSmartContractFunction(function, contractAddress);
assertFalse(responseValue.isEmpty());

List<Uint> uint = FunctionReturnDecoder.decode(responseValue, function);
List<Uint> uint = FunctionReturnDecoder.decode(responseValue, function.getOutputParameters());
assertThat(uint.size(), is(1));
assertThat(uint.get(0).getValue(), equalTo(BigInteger.valueOf(55)));
}

private boolean unlockAccount() throws Exception {
PersonalUnlockAccount personalUnlockAccount =
parity.personalUnlockAccount(ADDRESS, PASSWORD, ACCOUNT_UNLOCK_DURATION)
.sendAsync().get();
return personalUnlockAccount.accountUnlocked();
}

private String sendTransaction() throws Exception {
EthSendTransaction ethSendTransaction = new EthSendTransaction(
ADDRESS,
Expand All @@ -106,31 +74,6 @@ private String sendTransaction() throws Exception {
return transactionResponse.getTransactionHash();
}

private Optional<EthGetTransactionReceipt.TransactionReceipt> getTransactionReceipt(
String transactionHash, int sleepDuration, int attempts) throws Exception {

Optional<EthGetTransactionReceipt.TransactionReceipt> receiptOptional =
sendTransactionReceiptRequest(transactionHash);
for (int i = 0; i < attempts; i++) {
if (!receiptOptional.isPresent()) {
Thread.sleep(sleepDuration);
receiptOptional = sendTransactionReceiptRequest(transactionHash);
} else {
break;
}
}

return receiptOptional;
}

private Optional<EthGetTransactionReceipt.TransactionReceipt> sendTransactionReceiptRequest(
String transactionHash) throws Exception {
EthGetTransactionReceipt transactionReceipt =
parity.ethGetTransactionReceipt(transactionHash).sendAsync().get();

return transactionReceipt.getTransactionReceipt();
}

private String callSmartContractFunction(
Function function, String contractAddress) throws Exception {

Expand All @@ -143,11 +86,4 @@ private String callSmartContractFunction(

return response.getValue();
}

private Function createFibonacciFunction() {
return new Function<>(
"fibonacci",
Arrays.asList(new Uint(BigInteger.valueOf(10))),
Arrays.asList(Uint.class));
}
}
Loading

0 comments on commit 57ccba1

Please sign in to comment.