Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into android
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/main/java/org/web3j/tx/ManagedTransaction.java
  • Loading branch information
conor10 committed Mar 7, 2017
2 parents dad2d59 + d121c17 commit 0c7c903
Show file tree
Hide file tree
Showing 14 changed files with 198 additions and 122 deletions.
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ Java 8:
<dependency>
<groupId>org.web3j</groupId>
<artifactId>core</artifactId>
<version>2.0.2</version>
<version>2.1.0</version>
</dependency>
Android:
Expand All @@ -105,7 +105,7 @@ Java 8:

.. code-block:: groovy
compile ('org.web3j:core:2.0.2')
compile ('org.web3j:core:2.1.0')
Android:

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ apply plugin: 'com.github.johnrengelman.shadow'
apply plugin: 'application'

group 'org.web3j'
version '2.0.2'
version '2.1.0'

sourceCompatibility = 1.6
targetCompatibility = 1.6
Expand Down
2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
# The short X.Y version.
version = u'2.0'
# The full version, including alpha/beta/rc tags.
release = u'2.0.2'
release = u'2.1.0'

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
Expand Down
4 changes: 2 additions & 2 deletions docs/source/getting_started.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Java 8:
<dependency>
<groupId>org.web3j</groupId>
<artifactId>core</artifactId>
<version>2.0.2</version>
<version>2.1.0</version>
</dependency>
Android:
Expand All @@ -33,7 +33,7 @@ Java 8:

.. code-block:: groovy
compile ('org.web3j:core:2.0.2')
compile ('org.web3j:core:2.1.0')
Android:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ public void testTransaction() throws Exception {
transactionReceipts.add(transactionReceiptFuture);
}

for (int i = 0; i < transfer.getAttempts() && !transactionReceipts.isEmpty(); i++) {
Thread.sleep(transfer.getSleepDuration());
for (int i = 0; i < 30 && !transactionReceipts.isEmpty(); i++) {
Thread.sleep(15000);

for (Iterator<Future<TransactionReceipt>> iterator = transactionReceipts.iterator(); iterator.hasNext(); ) {
Future<TransactionReceipt> transactionReceiptFuture = iterator.next();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/web3j/crypto/TransactionEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ static List<RlpType> asRlpValues(

// an empty to address (contract creation) should not be encoded as a numeric 0 value
String to = rawTransaction.getTo();
if (to.length() > 0) {
if (to != null && to.length() > 0) {
result.add(RlpString.create(Numeric.toBigInt(to)));
} else {
result.add(RlpString.create(""));
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/web3j/tx/ChainId.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Ethereum chain ids.
*/
public class ChainId {
public static final byte NONE = -1;
public static final byte MAIN_NET = 1;
public static final byte TEST_NET = 3;
}
17 changes: 15 additions & 2 deletions src/main/java/org/web3j/tx/ClientTransactionManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,27 @@
*
* <p><b>Note</b>: accounts must be unlocked on the node for transactions to be successful.
*/
public class ClientTransactionManager implements TransactionManager {
public class ClientTransactionManager extends TransactionManager {

private final Web3j web3j;
private final String fromAddress;

public ClientTransactionManager(
Web3j web3j, String fromAddress) {
super(web3j);
this.web3j = web3j;
this.fromAddress = fromAddress;
}

public ClientTransactionManager(
Web3j web3j, String fromAddress, int attempts, int sleepDuration) {
super(web3j, attempts, sleepDuration);
this.web3j = web3j;
this.fromAddress = fromAddress;
}

@Override
public EthSendTransaction executeTransaction(
public EthSendTransaction sendTransaction(
BigInteger gasPrice, BigInteger gasLimit, String to,
String data, BigInteger value)
throws ExecutionException, InterruptedException, TransactionTimeoutException {
Expand All @@ -36,4 +44,9 @@ public EthSendTransaction executeTransaction(
return web3j.ethSendTransaction(transaction)
.sendAsync().get();
}

@Override
public String getFromAddress() {
return fromAddress;
}
}
37 changes: 27 additions & 10 deletions src/main/java/org/web3j/tx/Contract.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public abstract class Contract extends ManagedTransaction {
private String contractAddress;
private final BigInteger gasPrice;
private final BigInteger gasLimit;
private TransactionReceipt transactionReceipt;

protected Contract(String contractAddress, Web3j web3j, TransactionManager transactionManager,
BigInteger gasPrice, BigInteger gasLimit) {
Expand All @@ -61,6 +62,21 @@ public String getContractAddress() {
return contractAddress;
}

public void setTransactionReceipt(TransactionReceipt transactionReceipt) {
this.transactionReceipt = transactionReceipt;
}

/**
* If this Contract instance was created at deployment, the TransactionReceipt associated
* with the initial creation will be provided, e.g. via a <em>deploy</em> method. This will not persist
* for Contracts instances constructed via a <em>load</em> method.
*
* @return the TransactionReceipt generated at contract deployment
*/
public Optional<TransactionReceipt> getTransactionReceipt() {
return Optional.ofNullable(transactionReceipt);
}

/**
* Execute constant function call - i.e. a call that does not change state of the contract
*
Expand Down Expand Up @@ -201,17 +217,10 @@ protected static <T extends Contract> T deploy(
BigInteger.class, BigInteger.class);
constructor.setAccessible(true);

T contract = constructor.newInstance("", web3j, credentials, gasPrice, gasLimit);
TransactionReceipt transactionReceipt =
contract.executeTransaction(binary + encodedConstructor, value);

String contractAddress = transactionReceipt.getContractAddress();
if (contractAddress == null) {
throw new RuntimeException("Empty contract address returned");
}
contract.setContractAddress(contractAddress);
// we want to use null here to ensure that "to" parameter on message is not populated
T contract = constructor.newInstance(null, web3j, credentials, gasPrice, gasLimit);

return contract;
return create(contract, binary, encodedConstructor, value);
}

protected static <T extends Contract> T deploy(
Expand All @@ -227,6 +236,13 @@ protected static <T extends Contract> T deploy(

// we want to use null here to ensure that "to" parameter on message is not populated
T contract = constructor.newInstance(null, web3j, transactionManager, gasPrice, gasLimit);

return create(contract, binary, encodedConstructor, value);
}

private static <T extends Contract> T create(
T contract, String binary, String encodedConstructor, BigInteger value)
throws InterruptedException, ExecutionException, TransactionTimeoutException {
TransactionReceipt transactionReceipt =
contract.executeTransaction(binary + encodedConstructor, value);

Expand All @@ -235,6 +251,7 @@ protected static <T extends Contract> T deploy(
throw new RuntimeException("Empty contract address returned");
}
contract.setContractAddress(contractAddress);
contract.setTransactionReceipt(transactionReceipt);

return contract;
}
Expand Down
79 changes: 1 addition & 78 deletions src/main/java/org/web3j/tx/ManagedTransaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.methods.response.EthGasPrice;
import org.web3j.protocol.core.methods.response.EthGetTransactionReceipt;
import org.web3j.protocol.core.methods.response.EthSendTransaction;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.protocol.exceptions.TransactionTimeoutException;

Expand All @@ -19,38 +17,15 @@ public abstract class ManagedTransaction {
// https://www.reddit.com/r/ethereum/comments/5g8ia6/attention_miners_we_recommend_raising_gas_limit/
public static final BigInteger GAS_PRICE = BigInteger.valueOf(20000000000L);

private static final int SLEEP_DURATION = 15000;
private static final int ATTEMPTS = 40;

protected Web3j web3j;

private TransactionManager transactionManager;

private int sleepDuration = SLEEP_DURATION;
private int attempts = ATTEMPTS;

protected ManagedTransaction(Web3j web3j, TransactionManager transactionManager) {
this.transactionManager = transactionManager;
this.web3j = web3j;
}

// In case anyone wishes to override the defaults
public int getSleepDuration() {
return sleepDuration;
}

public void setSleepDuration(int sleepDuration) {
this.sleepDuration = sleepDuration;
}

public int getAttempts() {
return attempts;
}

public void setAttempts(int attempts) {
this.attempts = attempts;
}

public BigInteger getGasPrice() throws InterruptedException, ExecutionException {
EthGasPrice ethGasPrice = web3j.ethGasPrice().sendAsync().get();

Expand All @@ -61,59 +36,7 @@ protected TransactionReceipt send(
String to, String data, BigInteger value, BigInteger gasPrice, BigInteger gasLimit)
throws InterruptedException, ExecutionException, TransactionTimeoutException {

EthSendTransaction transaction = transactionManager.executeTransaction(
return transactionManager.executeTransaction(
gasPrice, gasLimit, to, data, value);
return processResponse(transaction);
}

protected TransactionReceipt processResponse(EthSendTransaction transactionResponse)
throws InterruptedException, ExecutionException, TransactionTimeoutException {
if (transactionResponse.hasError()) {
throw new RuntimeException("Error processing transaction request: " +
transactionResponse.getError().getMessage());
}

String transactionHash = transactionResponse.getTransactionHash();

return waitForTransactionReceipt(transactionHash);
}

private TransactionReceipt waitForTransactionReceipt(
String transactionHash) throws InterruptedException, ExecutionException,
TransactionTimeoutException {

return getTransactionReceipt(transactionHash, sleepDuration, attempts);
}

private TransactionReceipt getTransactionReceipt(
String transactionHash, int sleepDuration, int attempts)
throws InterruptedException, ExecutionException, TransactionTimeoutException {

TransactionReceipt receiptOptional =
sendTransactionReceiptRequest(transactionHash);
for (int i = 0; i < attempts; i++) {
if (receiptOptional == null) {
Thread.sleep(sleepDuration);
receiptOptional = sendTransactionReceiptRequest(transactionHash);
} else {
return receiptOptional;
}
}

throw new TransactionTimeoutException("Transaction receipt was not generated after " +
((sleepDuration * attempts) / 1000 +
" seconds for transaction: " + transactionHash));
}

private TransactionReceipt sendTransactionReceiptRequest(
String transactionHash) throws InterruptedException, ExecutionException {
EthGetTransactionReceipt transactionReceipt =
web3j.ethGetTransactionReceipt(transactionHash).sendAsync().get();
if (transactionReceipt.hasError()) {
throw new RuntimeException("Error processing request: " +
transactionReceipt.getError().getMessage());
}

return transactionReceipt.getTransactionReceipt();
}
}
28 changes: 24 additions & 4 deletions src/main/java/org/web3j/tx/RawTransactionManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,37 @@
* <p>This transaction manager provides support for specifying the chain id for transactions as per
* <a href="https://github.com/ethereum/EIPs/issues/155">EIP155</a>.
*/
public class RawTransactionManager implements TransactionManager {
public class RawTransactionManager extends TransactionManager {

private final Web3j web3j;
final Credentials credentials;

private final byte chainId;

public RawTransactionManager(Web3j web3j, Credentials credentials, byte chainId) {
super(web3j);
this.web3j = web3j;
this.credentials = credentials;

this.chainId = chainId;
}

public RawTransactionManager(
Web3j web3j, Credentials credentials, byte chainId, int attempts, int sleepDuration) {
super(web3j, attempts, sleepDuration);
this.web3j = web3j;
this.credentials = credentials;

this.chainId = chainId;
}

public RawTransactionManager(Web3j web3j, Credentials credentials) {
this(web3j, credentials, (byte) -1);
this(web3j, credentials, ChainId.NONE);
}

public RawTransactionManager(
Web3j web3j, Credentials credentials, int attempts, int sleepDuration) {
this(web3j, credentials, ChainId.NONE, attempts, sleepDuration);
}

BigInteger getNonce() throws ExecutionException, InterruptedException {
Expand All @@ -45,7 +60,7 @@ BigInteger getNonce() throws ExecutionException, InterruptedException {
}

@Override
public EthSendTransaction executeTransaction(
public EthSendTransaction sendTransaction(
BigInteger gasPrice, BigInteger gasLimit, String to,
String data, BigInteger value) throws ExecutionException, InterruptedException {

Expand All @@ -67,7 +82,7 @@ public EthSendTransaction signAndSend(RawTransaction rawTransaction)

byte[] signedMessage;

if (chainId > 0) {
if (chainId > ChainId.NONE) {
signedMessage = TransactionEncoder.signMessage(rawTransaction, chainId, credentials);
} else {
signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
Expand All @@ -77,4 +92,9 @@ public EthSendTransaction signAndSend(RawTransaction rawTransaction)

return web3j.ethSendRawTransaction(hexValue).sendAsync().get();
}

@Override
public String getFromAddress() {
return credentials.getAddress();
}
}
Loading

0 comments on commit 0c7c903

Please sign in to comment.