Skip to content

Commit 0c7c903

Browse files
committed
Merge remote-tracking branch 'origin/master' into android
# Conflicts: # src/main/java/org/web3j/tx/ManagedTransaction.java
2 parents dad2d59 + d121c17 commit 0c7c903

File tree

14 files changed

+198
-122
lines changed

14 files changed

+198
-122
lines changed

README.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ Java 8:
8585
<dependency>
8686
<groupId>org.web3j</groupId>
8787
<artifactId>core</artifactId>
88-
<version>2.0.2</version>
88+
<version>2.1.0</version>
8989
</dependency>
9090
9191
Android:
@@ -105,7 +105,7 @@ Java 8:
105105

106106
.. code-block:: groovy
107107
108-
compile ('org.web3j:core:2.0.2')
108+
compile ('org.web3j:core:2.1.0')
109109
110110
Android:
111111

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ apply plugin: 'com.github.johnrengelman.shadow'
2929
apply plugin: 'application'
3030

3131
group 'org.web3j'
32-
version '2.0.2'
32+
version '2.1.0'
3333

3434
sourceCompatibility = 1.6
3535
targetCompatibility = 1.6

docs/source/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
# The short X.Y version.
5757
version = u'2.0'
5858
# The full version, including alpha/beta/rc tags.
59-
release = u'2.0.2'
59+
release = u'2.1.0'
6060

6161
# The language for content autogenerated by Sphinx. Refer to documentation
6262
# for a list of supported languages.

docs/source/getting_started.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Java 8:
1313
<dependency>
1414
<groupId>org.web3j</groupId>
1515
<artifactId>core</artifactId>
16-
<version>2.0.2</version>
16+
<version>2.1.0</version>
1717
</dependency>
1818
1919
Android:
@@ -33,7 +33,7 @@ Java 8:
3333

3434
.. code-block:: groovy
3535
36-
compile ('org.web3j:core:2.0.2')
36+
compile ('org.web3j:core:2.1.0')
3737
3838
Android:
3939

src/integration-test/java/org/web3j/protocol/scenarios/FastRawTransactionManagerIT.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ public void testTransaction() throws Exception {
5858
transactionReceipts.add(transactionReceiptFuture);
5959
}
6060

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

6464
for (Iterator<Future<TransactionReceipt>> iterator = transactionReceipts.iterator(); iterator.hasNext(); ) {
6565
Future<TransactionReceipt> transactionReceiptFuture = iterator.next();

src/main/java/org/web3j/crypto/TransactionEncoder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ static List<RlpType> asRlpValues(
6868

6969
// an empty to address (contract creation) should not be encoded as a numeric 0 value
7070
String to = rawTransaction.getTo();
71-
if (to.length() > 0) {
71+
if (to != null && to.length() > 0) {
7272
result.add(RlpString.create(Numeric.toBigInt(to)));
7373
} else {
7474
result.add(RlpString.create(""));

src/main/java/org/web3j/tx/ChainId.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* Ethereum chain ids.
55
*/
66
public class ChainId {
7+
public static final byte NONE = -1;
78
public static final byte MAIN_NET = 1;
89
public static final byte TEST_NET = 3;
910
}

src/main/java/org/web3j/tx/ClientTransactionManager.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,27 @@
1313
*
1414
* <p><b>Note</b>: accounts must be unlocked on the node for transactions to be successful.
1515
*/
16-
public class ClientTransactionManager implements TransactionManager {
16+
public class ClientTransactionManager extends TransactionManager {
1717

1818
private final Web3j web3j;
1919
private final String fromAddress;
2020

2121
public ClientTransactionManager(
2222
Web3j web3j, String fromAddress) {
23+
super(web3j);
24+
this.web3j = web3j;
25+
this.fromAddress = fromAddress;
26+
}
27+
28+
public ClientTransactionManager(
29+
Web3j web3j, String fromAddress, int attempts, int sleepDuration) {
30+
super(web3j, attempts, sleepDuration);
2331
this.web3j = web3j;
2432
this.fromAddress = fromAddress;
2533
}
2634

2735
@Override
28-
public EthSendTransaction executeTransaction(
36+
public EthSendTransaction sendTransaction(
2937
BigInteger gasPrice, BigInteger gasLimit, String to,
3038
String data, BigInteger value)
3139
throws ExecutionException, InterruptedException, TransactionTimeoutException {
@@ -36,4 +44,9 @@ public EthSendTransaction executeTransaction(
3644
return web3j.ethSendTransaction(transaction)
3745
.sendAsync().get();
3846
}
47+
48+
@Override
49+
public String getFromAddress() {
50+
return fromAddress;
51+
}
3952
}

src/main/java/org/web3j/tx/Contract.java

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public abstract class Contract extends ManagedTransaction {
3737
private String contractAddress;
3838
private final BigInteger gasPrice;
3939
private final BigInteger gasLimit;
40+
private TransactionReceipt transactionReceipt;
4041

4142
protected Contract(String contractAddress, Web3j web3j, TransactionManager transactionManager,
4243
BigInteger gasPrice, BigInteger gasLimit) {
@@ -61,6 +62,21 @@ public String getContractAddress() {
6162
return contractAddress;
6263
}
6364

65+
public void setTransactionReceipt(TransactionReceipt transactionReceipt) {
66+
this.transactionReceipt = transactionReceipt;
67+
}
68+
69+
/**
70+
* If this Contract instance was created at deployment, the TransactionReceipt associated
71+
* with the initial creation will be provided, e.g. via a <em>deploy</em> method. This will not persist
72+
* for Contracts instances constructed via a <em>load</em> method.
73+
*
74+
* @return the TransactionReceipt generated at contract deployment
75+
*/
76+
public Optional<TransactionReceipt> getTransactionReceipt() {
77+
return Optional.ofNullable(transactionReceipt);
78+
}
79+
6480
/**
6581
* Execute constant function call - i.e. a call that does not change state of the contract
6682
*
@@ -201,17 +217,10 @@ protected static <T extends Contract> T deploy(
201217
BigInteger.class, BigInteger.class);
202218
constructor.setAccessible(true);
203219

204-
T contract = constructor.newInstance("", web3j, credentials, gasPrice, gasLimit);
205-
TransactionReceipt transactionReceipt =
206-
contract.executeTransaction(binary + encodedConstructor, value);
207-
208-
String contractAddress = transactionReceipt.getContractAddress();
209-
if (contractAddress == null) {
210-
throw new RuntimeException("Empty contract address returned");
211-
}
212-
contract.setContractAddress(contractAddress);
220+
// we want to use null here to ensure that "to" parameter on message is not populated
221+
T contract = constructor.newInstance(null, web3j, credentials, gasPrice, gasLimit);
213222

214-
return contract;
223+
return create(contract, binary, encodedConstructor, value);
215224
}
216225

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

228237
// we want to use null here to ensure that "to" parameter on message is not populated
229238
T contract = constructor.newInstance(null, web3j, transactionManager, gasPrice, gasLimit);
239+
240+
return create(contract, binary, encodedConstructor, value);
241+
}
242+
243+
private static <T extends Contract> T create(
244+
T contract, String binary, String encodedConstructor, BigInteger value)
245+
throws InterruptedException, ExecutionException, TransactionTimeoutException {
230246
TransactionReceipt transactionReceipt =
231247
contract.executeTransaction(binary + encodedConstructor, value);
232248

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

239256
return contract;
240257
}

src/main/java/org/web3j/tx/ManagedTransaction.java

Lines changed: 1 addition & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
import org.web3j.protocol.Web3j;
77
import org.web3j.protocol.core.methods.response.EthGasPrice;
8-
import org.web3j.protocol.core.methods.response.EthGetTransactionReceipt;
9-
import org.web3j.protocol.core.methods.response.EthSendTransaction;
108
import org.web3j.protocol.core.methods.response.TransactionReceipt;
119
import org.web3j.protocol.exceptions.TransactionTimeoutException;
1210

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

22-
private static final int SLEEP_DURATION = 15000;
23-
private static final int ATTEMPTS = 40;
24-
2520
protected Web3j web3j;
2621

2722
private TransactionManager transactionManager;
2823

29-
private int sleepDuration = SLEEP_DURATION;
30-
private int attempts = ATTEMPTS;
31-
3224
protected ManagedTransaction(Web3j web3j, TransactionManager transactionManager) {
3325
this.transactionManager = transactionManager;
3426
this.web3j = web3j;
3527
}
3628

37-
// In case anyone wishes to override the defaults
38-
public int getSleepDuration() {
39-
return sleepDuration;
40-
}
41-
42-
public void setSleepDuration(int sleepDuration) {
43-
this.sleepDuration = sleepDuration;
44-
}
45-
46-
public int getAttempts() {
47-
return attempts;
48-
}
49-
50-
public void setAttempts(int attempts) {
51-
this.attempts = attempts;
52-
}
53-
5429
public BigInteger getGasPrice() throws InterruptedException, ExecutionException {
5530
EthGasPrice ethGasPrice = web3j.ethGasPrice().sendAsync().get();
5631

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

64-
EthSendTransaction transaction = transactionManager.executeTransaction(
39+
return transactionManager.executeTransaction(
6540
gasPrice, gasLimit, to, data, value);
66-
return processResponse(transaction);
67-
}
68-
69-
protected TransactionReceipt processResponse(EthSendTransaction transactionResponse)
70-
throws InterruptedException, ExecutionException, TransactionTimeoutException {
71-
if (transactionResponse.hasError()) {
72-
throw new RuntimeException("Error processing transaction request: " +
73-
transactionResponse.getError().getMessage());
74-
}
75-
76-
String transactionHash = transactionResponse.getTransactionHash();
77-
78-
return waitForTransactionReceipt(transactionHash);
79-
}
80-
81-
private TransactionReceipt waitForTransactionReceipt(
82-
String transactionHash) throws InterruptedException, ExecutionException,
83-
TransactionTimeoutException {
84-
85-
return getTransactionReceipt(transactionHash, sleepDuration, attempts);
86-
}
87-
88-
private TransactionReceipt getTransactionReceipt(
89-
String transactionHash, int sleepDuration, int attempts)
90-
throws InterruptedException, ExecutionException, TransactionTimeoutException {
91-
92-
TransactionReceipt receiptOptional =
93-
sendTransactionReceiptRequest(transactionHash);
94-
for (int i = 0; i < attempts; i++) {
95-
if (receiptOptional == null) {
96-
Thread.sleep(sleepDuration);
97-
receiptOptional = sendTransactionReceiptRequest(transactionHash);
98-
} else {
99-
return receiptOptional;
100-
}
101-
}
102-
103-
throw new TransactionTimeoutException("Transaction receipt was not generated after " +
104-
((sleepDuration * attempts) / 1000 +
105-
" seconds for transaction: " + transactionHash));
106-
}
107-
108-
private TransactionReceipt sendTransactionReceiptRequest(
109-
String transactionHash) throws InterruptedException, ExecutionException {
110-
EthGetTransactionReceipt transactionReceipt =
111-
web3j.ethGetTransactionReceipt(transactionHash).sendAsync().get();
112-
if (transactionReceipt.hasError()) {
113-
throw new RuntimeException("Error processing request: " +
114-
transactionReceipt.getError().getMessage());
115-
}
116-
117-
return transactionReceipt.getTransactionReceipt();
11841
}
11942
}

src/main/java/org/web3j/tx/RawTransactionManager.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,37 @@
1919
* <p>This transaction manager provides support for specifying the chain id for transactions as per
2020
* <a href="https://github.com/ethereum/EIPs/issues/155">EIP155</a>.
2121
*/
22-
public class RawTransactionManager implements TransactionManager {
22+
public class RawTransactionManager extends TransactionManager {
2323

2424
private final Web3j web3j;
2525
final Credentials credentials;
2626

2727
private final byte chainId;
2828

2929
public RawTransactionManager(Web3j web3j, Credentials credentials, byte chainId) {
30+
super(web3j);
31+
this.web3j = web3j;
32+
this.credentials = credentials;
33+
34+
this.chainId = chainId;
35+
}
36+
37+
public RawTransactionManager(
38+
Web3j web3j, Credentials credentials, byte chainId, int attempts, int sleepDuration) {
39+
super(web3j, attempts, sleepDuration);
3040
this.web3j = web3j;
3141
this.credentials = credentials;
3242

3343
this.chainId = chainId;
3444
}
3545

3646
public RawTransactionManager(Web3j web3j, Credentials credentials) {
37-
this(web3j, credentials, (byte) -1);
47+
this(web3j, credentials, ChainId.NONE);
48+
}
49+
50+
public RawTransactionManager(
51+
Web3j web3j, Credentials credentials, int attempts, int sleepDuration) {
52+
this(web3j, credentials, ChainId.NONE, attempts, sleepDuration);
3853
}
3954

4055
BigInteger getNonce() throws ExecutionException, InterruptedException {
@@ -45,7 +60,7 @@ BigInteger getNonce() throws ExecutionException, InterruptedException {
4560
}
4661

4762
@Override
48-
public EthSendTransaction executeTransaction(
63+
public EthSendTransaction sendTransaction(
4964
BigInteger gasPrice, BigInteger gasLimit, String to,
5065
String data, BigInteger value) throws ExecutionException, InterruptedException {
5166

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

6883
byte[] signedMessage;
6984

70-
if (chainId > 0) {
85+
if (chainId > ChainId.NONE) {
7186
signedMessage = TransactionEncoder.signMessage(rawTransaction, chainId, credentials);
7287
} else {
7388
signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
@@ -77,4 +92,9 @@ public EthSendTransaction signAndSend(RawTransaction rawTransaction)
7792

7893
return web3j.ethSendRawTransaction(hexValue).sendAsync().get();
7994
}
95+
96+
@Override
97+
public String getFromAddress() {
98+
return credentials.getAddress();
99+
}
80100
}

0 commit comments

Comments
 (0)