Skip to content

Commit

Permalink
1. Added FastRawTransactionManager and associated integration test.
Browse files Browse the repository at this point in the history
2. Cleaned up imports.
  • Loading branch information
conor10 committed Feb 2, 2017
1 parent d2e085a commit e5632f9
Show file tree
Hide file tree
Showing 1,133 changed files with 1,688 additions and 180 deletions.
2 changes: 1 addition & 1 deletion src/integration-test/java/org/web3j/generated/Arrays.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package org.web3j.generated;

import java.lang.String;
import java.math.BigInteger;
import java.util.Collections;
import java.util.concurrent.Future;

import org.web3j.abi.TypeReference;
import org.web3j.abi.datatypes.DynamicArray;
import org.web3j.abi.datatypes.Function;
Expand Down
8 changes: 4 additions & 4 deletions src/integration-test/java/org/web3j/generated/Fibonacci.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package org.web3j.generated;


import java.lang.Override;
import java.lang.String;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Future;

import rx.Observable;
import rx.functions.Func1;

import org.web3j.abi.EventEncoder;
import org.web3j.abi.EventValues;
import org.web3j.abi.TypeReference;
Expand All @@ -24,8 +26,6 @@
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.tx.Contract;
import org.web3j.tx.TransactionManager;
import rx.Observable;
import rx.functions.Func1;

/**
* <p>Auto generated code.<br>
Expand Down
2 changes: 1 addition & 1 deletion src/integration-test/java/org/web3j/generated/Greeter.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package org.web3j.generated;

import java.lang.String;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Collections;
import java.util.concurrent.Future;

import org.web3j.abi.FunctionEncoder;
import org.web3j.abi.TypeReference;
import org.web3j.abi.datatypes.Function;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package org.web3j.generated;


import java.lang.Override;
import java.lang.String;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Future;

import rx.Observable;
import rx.functions.Func1;

import org.web3j.abi.EventEncoder;
import org.web3j.abi.EventValues;
import org.web3j.abi.FunctionEncoder;
Expand All @@ -29,8 +31,6 @@
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.tx.Contract;
import org.web3j.tx.TransactionManager;
import rx.Observable;
import rx.functions.Func1;

/**
* <p>Auto generated code.<br>
Expand Down
2 changes: 1 addition & 1 deletion src/integration-test/java/org/web3j/generated/ShipIt.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package org.web3j.generated;

import java.lang.String;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Future;

import org.web3j.abi.TypeReference;
import org.web3j.abi.datatypes.Address;
import org.web3j.abi.datatypes.Function;
Expand Down
40 changes: 38 additions & 2 deletions src/integration-test/java/org/web3j/protocol/core/CoreIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,47 @@
import org.junit.Ignore;
import org.junit.Test;

import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.methods.response.EthAccounts;
import org.web3j.protocol.core.methods.response.EthBlock;
import org.web3j.protocol.core.methods.response.EthBlockNumber;
import org.web3j.protocol.core.methods.response.EthCall;
import org.web3j.protocol.core.methods.response.EthCoinbase;
import org.web3j.protocol.core.methods.response.EthCompileLLL;
import org.web3j.protocol.core.methods.response.EthCompileSerpent;
import org.web3j.protocol.core.methods.response.EthCompileSolidity;
import org.web3j.protocol.core.methods.response.EthEstimateGas;
import org.web3j.protocol.core.methods.response.EthFilter;
import org.web3j.protocol.core.methods.response.EthGasPrice;
import org.web3j.protocol.core.methods.response.EthGetBalance;
import org.web3j.protocol.core.methods.response.EthGetBlockTransactionCountByHash;
import org.web3j.protocol.core.methods.response.EthGetBlockTransactionCountByNumber;
import org.web3j.protocol.core.methods.response.EthGetCode;
import org.web3j.protocol.core.methods.response.EthGetCompilers;
import org.web3j.protocol.core.methods.response.EthGetStorageAt;
import org.web3j.protocol.core.methods.response.EthGetTransactionCount;
import org.web3j.protocol.core.methods.response.EthGetTransactionReceipt;
import org.web3j.protocol.core.methods.response.EthGetUncleCountByBlockHash;
import org.web3j.protocol.core.methods.response.EthGetUncleCountByBlockNumber;
import org.web3j.protocol.core.methods.response.EthHashrate;
import org.web3j.protocol.core.methods.response.EthLog;
import org.web3j.protocol.core.methods.response.EthMining;
import org.web3j.protocol.core.methods.response.EthProtocolVersion;
import org.web3j.protocol.core.methods.response.EthSendTransaction;
import org.web3j.protocol.core.methods.response.EthSyncing;
import org.web3j.protocol.core.methods.response.EthTransaction;
import org.web3j.protocol.core.methods.response.EthUninstallFilter;
import org.web3j.protocol.core.methods.response.NetListening;
import org.web3j.protocol.core.methods.response.NetPeerCount;
import org.web3j.protocol.core.methods.response.NetVersion;
import org.web3j.protocol.core.methods.response.ShhNewGroup;
import org.web3j.protocol.core.methods.response.ShhNewIdentity;
import org.web3j.protocol.core.methods.response.ShhVersion;
import org.web3j.protocol.core.methods.response.Transaction;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.protocol.core.methods.response.Web3ClientVersion;
import org.web3j.protocol.core.methods.response.Web3Sha3;
import org.web3j.protocol.http.HttpService;
import org.web3j.protocol.core.methods.response.*;
import org.web3j.protocol.Web3j;

import static junit.framework.TestCase.assertFalse;
import static org.hamcrest.CoreMatchers.equalTo;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
import org.web3j.protocol.parity.methods.response.PersonalUnlockAccount;

import static junit.framework.TestCase.assertFalse;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

import org.junit.Test;

import org.web3j.protocol.core.methods.request.RawTransaction;
import org.web3j.crypto.TransactionEncoder;
import org.web3j.protocol.core.DefaultBlockParameterName;
import org.web3j.protocol.core.methods.request.RawTransaction;
import org.web3j.protocol.core.methods.response.EthGetTransactionCount;
import org.web3j.protocol.core.methods.response.EthSendTransaction;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
Expand All @@ -15,7 +15,7 @@

import static junit.framework.TestCase.assertFalse;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.*;
import static org.junit.Assert.assertThat;

/**
* Create, sign and send a raw transaction.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
import org.web3j.protocol.core.DefaultBlockParameterName;
import org.web3j.protocol.core.methods.request.EthFilter;
import org.web3j.protocol.core.methods.request.Transaction;
import org.web3j.protocol.core.methods.response.*;
import org.web3j.protocol.core.methods.response.EthEstimateGas;
import org.web3j.protocol.core.methods.response.EthLog;
import org.web3j.protocol.core.methods.response.Log;
import org.web3j.protocol.core.methods.response.TransactionReceipt;

import static junit.framework.TestCase.assertFalse;
import static org.hamcrest.CoreMatchers.is;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package org.web3j.protocol.scenarios;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.Future;

import org.junit.Test;

import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.tx.FastRawTransactionManager;
import org.web3j.tx.Transfer;
import org.web3j.utils.Convert;

import static junit.framework.TestCase.assertFalse;
import static junit.framework.TestCase.assertTrue;

public class FastRawTransactionManagerIT extends Scenario {

private static final int COUNT = 1; // don't set too high if using a real Ethereum network

@Test
public void testTransaction() throws Exception {

List<Future<TransactionReceipt>> transactionReceipts = new LinkedList<>();

FastRawTransactionManager transactionManager = new FastRawTransactionManager(
parity, ALICE);

// We need to explicitly reset the nonce here, due to a race condition is taking place in
// this test, where we experience deadlock in the entry point of the
// RawTransactionManager.getNonce() method called within the
// FastRawTransactionManager.getNonce() method. Using a sync call to getNonce() instead
// of async as is currently the case also stops the issue from occurring.
//
// Bizarrely this issue does not occur when the gas price is not set on the transaction,
// and getGasPrice() is called on prior to sending each transaction. i.e. instead we use:
//
// Future<TransactionReceipt> transactionReceiptFuture = transfer.sendFundsAsync(
// BOB.getAddress(), BigDecimal.valueOf(0.1), Convert.Unit.ETHER);
//
// One fix would be to migrate to synchronous calls throughout the
// ManagedTransaction parent and child classes.
transactionManager.resetNonce();

Transfer transfer = new Transfer(parity, transactionManager);

BigInteger gasPrice = transfer.getGasPrice();

for (int i = 0; i < COUNT; i++) {

Future<TransactionReceipt> transactionReceiptFuture = transfer.sendFundsAsync(
BOB.getAddress(), BigDecimal.valueOf(1.0), Convert.Unit.ETHER,
gasPrice, Transfer.GAS_LIMIT);
transactionReceipts.add(transactionReceiptFuture);
}

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

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

if (transactionReceiptFuture.isDone()) {
TransactionReceipt transactionReceipt = transactionReceiptFuture.get();
System.out.println(transactionReceipt.getBlockHash());
assertFalse(transactionReceipt.getBlockHash().isEmpty());
iterator.remove();
}
}
}

assertTrue(transactionReceipts.isEmpty());
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package org.web3j.protocol.scenarios;

import java.math.BigInteger;
import java.util.Arrays;

import org.junit.Test;

import org.web3j.abi.EventValues;
import org.web3j.abi.datatypes.Uint;
import org.web3j.abi.datatypes.generated.Uint256;
import org.web3j.generated.Fibonacci;
import org.web3j.protocol.Web3j;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package org.web3j.protocol.scenarios;

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

import org.junit.Test;

import org.web3j.abi.EventValues;
import org.web3j.abi.datatypes.Address;
import org.web3j.abi.datatypes.Utf8String;
import org.web3j.abi.datatypes.generated.Uint256;
Expand All @@ -16,7 +13,9 @@

import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertThat;
import static org.web3j.generated.HumanStandardToken.*;
import static org.web3j.generated.HumanStandardToken.ApprovalEventResponse;
import static org.web3j.generated.HumanStandardToken.TransferEventResponse;
import static org.web3j.generated.HumanStandardToken.deploy;

/**
* Generated HumanStandardToken integration test for all supported scenarios.
Expand All @@ -28,6 +27,9 @@ public void testContract() throws Exception {
BigInteger aliceQty = BigInteger.valueOf(1_000_000);
BigInteger bobQty = BigInteger.ZERO;

Address aliceAddress = new Address(ALICE.getAddress());
Address bobAddress = new Address(BOB.getAddress());

HumanStandardToken contract = deploy(parity, ALICE,
GAS_PRICE, GAS_LIMIT,
BigInteger.ZERO,
Expand All @@ -54,30 +56,30 @@ public void testContract() throws Exception {

TransferEventResponse aliceTransferEventValues = contract.getTransferEvents(aliceTransferReceipt).get(0);
assertThat(aliceTransferEventValues._from,
equalTo(ALICE.getAddress()));
equalTo(aliceAddress));
assertThat(aliceTransferEventValues._to,
equalTo(BOB.getAddress()));
equalTo(bobAddress));
assertThat(aliceTransferEventValues._value,
equalTo(new Uint256(transferQuantity)));

// set an allowance
assertThat(contract.allowance(
new Address(ALICE.getAddress()), new Address(BOB.getAddress())).get(),
aliceAddress, bobAddress).get(),
equalTo(new Uint256(BigInteger.ZERO)));

transferQuantity = BigInteger.valueOf(50);
TransactionReceipt approveReceipt = contract.approve(new Address(BOB.getAddress()), new Uint256(transferQuantity)).get();

ApprovalEventResponse approvalEventValues = contract.getApprovalEvents(approveReceipt).get(0);
assertThat(approvalEventValues._owner,
equalTo(ALICE.getAddress()));
equalTo(aliceAddress));
assertThat(approvalEventValues._spender,
equalTo(BOB.getAddress()));
equalTo(bobAddress));
assertThat(approvalEventValues._value,
equalTo(new Uint256(transferQuantity)));

assertThat(contract.allowance(
new Address(ALICE.getAddress()), new Address(BOB.getAddress())).get(),
aliceAddress, bobAddress).get(),
equalTo(new Uint256(transferQuantity)));

// perform a transfer as Bob
Expand All @@ -88,23 +90,23 @@ public void testContract() throws Exception {
contract.getContractAddress(), parity, BOB, GAS_PRICE, GAS_LIMIT);

TransactionReceipt bobTransferReceipt = bobsContract.transferFrom(
new Address(ALICE.getAddress()),
new Address(BOB.getAddress()),
aliceAddress,
bobAddress,
new Uint256(transferQuantity)).get();

aliceQty = aliceQty.subtract(transferQuantity);
bobQty = bobQty.add(transferQuantity);

assertThat(contract.balanceOf(new Address(ALICE.getAddress())).get(),
assertThat(contract.balanceOf(aliceAddress).get(),
equalTo(new Uint256(aliceQty)));
assertThat(contract.balanceOf(new Address(BOB.getAddress())).get(),
assertThat(contract.balanceOf(bobAddress).get(),
equalTo(new Uint256(bobQty)));

TransferEventResponse bobTransferEventValues = contract.getTransferEvents(bobTransferReceipt).get(0);
assertThat(bobTransferEventValues._from,
equalTo(ALICE.getAddress()));
equalTo(aliceAddress));
assertThat(bobTransferEventValues._to,
equalTo(BOB.getAddress()));
equalTo(bobAddress));
assertThat(bobTransferEventValues._value,
equalTo(new Uint256(transferQuantity)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

import org.junit.Test;

import org.web3j.abi.EventEncoder;
import org.web3j.abi.FunctionEncoder;
import org.web3j.abi.FunctionReturnDecoder;
import org.web3j.abi.TypeReference;
import org.web3j.abi.datatypes.*;
import org.web3j.abi.datatypes.Address;
import org.web3j.abi.datatypes.Bool;
import org.web3j.abi.datatypes.Event;
import org.web3j.abi.datatypes.Function;
import org.web3j.abi.datatypes.Type;
import org.web3j.abi.datatypes.Utf8String;
import org.web3j.abi.datatypes.generated.Uint256;
import org.web3j.abi.datatypes.generated.Uint8;
import org.web3j.crypto.Credentials;
Expand Down
Loading

0 comments on commit e5632f9

Please sign in to comment.