From 7f4a5ce156fb8b06ad1cf5e95c1a0d1dfb96c1c4 Mon Sep 17 00:00:00 2001 From: Conor Date: Tue, 16 May 2017 21:47:06 +1000 Subject: [PATCH] 1. Updated block/transaction replay Observables to use DefaultBlockParameters instead of BigInteger values. 2. Added additional catchup replay Observables 3. Changed Ether value parameter to Wei value in contract wrappers. 4. Regenerated Solidity wrapper classes. --- .../java/org/web3j/generated/Arrays.java | 10 +- .../java/org/web3j/generated/Fibonacci.java | 8 +- .../java/org/web3j/generated/Greeter.java | 10 +- .../web3j/generated/HumanStandardToken.java | 18 ++-- .../java/org/web3j/generated/ShipIt.java | 10 +- .../org/web3j/protocol/core/ObservableIT.java | 7 +- .../codegen/SolidityFunctionWrapper.java | 8 +- .../core/DefaultBlockParameterNumber.java | 4 + .../web3j/protocol/core/JsonRpc2_0Web3j.java | 33 +++++-- .../org/web3j/protocol/rx/JsonRpc2_0Rx.java | 91 ++++++++++++++++--- .../java/org/web3j/protocol/rx/Web3jRx.java | 53 ++++++++--- src/main/java/org/web3j/tx/Contract.java | 14 +-- .../codegen/SolidityFunctionWrapperTest.java | 4 +- .../web3j/protocol/rx/JsonRpc2_0RxTest.java | 8 +- 14 files changed, 194 insertions(+), 84 deletions(-) diff --git a/src/integration-test/java/org/web3j/generated/Arrays.java b/src/integration-test/java/org/web3j/generated/Arrays.java index a69da7ef6..24b4173c8 100644 --- a/src/integration-test/java/org/web3j/generated/Arrays.java +++ b/src/integration-test/java/org/web3j/generated/Arrays.java @@ -16,7 +16,7 @@ import org.web3j.tx.TransactionManager; /** - *

Auto generated code.
+ * Auto generated code.
* Do not modify!
* Please use {@link org.web3j.codegen.SolidityFunctionWrapperGenerator} to update. * @@ -43,12 +43,12 @@ public Future dynamicReverse(DynamicArray input) { return executeTransactionAsync(function); } - public static Future deploy(Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit, BigInteger initialEtherValue) { - return deployAsync(Arrays.class, web3j, credentials, gasPrice, gasLimit, BINARY, "", initialEtherValue); + public static Future deploy(Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit, BigInteger initialWeiValue) { + return deployAsync(Arrays.class, web3j, credentials, gasPrice, gasLimit, BINARY, "", initialWeiValue); } - public static Future deploy(Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit, BigInteger initialEtherValue) { - return deployAsync(Arrays.class, web3j, transactionManager, gasPrice, gasLimit, BINARY, "", initialEtherValue); + public static Future deploy(Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit, BigInteger initialWeiValue) { + return deployAsync(Arrays.class, web3j, transactionManager, gasPrice, gasLimit, BINARY, "", initialWeiValue); } public static Arrays load(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) { diff --git a/src/integration-test/java/org/web3j/generated/Fibonacci.java b/src/integration-test/java/org/web3j/generated/Fibonacci.java index 3cbccd623..02ef84ec7 100644 --- a/src/integration-test/java/org/web3j/generated/Fibonacci.java +++ b/src/integration-test/java/org/web3j/generated/Fibonacci.java @@ -87,12 +87,12 @@ public Future fibonacci(Uint256 number) { return executeCallSingleValueReturnAsync(function); } - public static Future deploy(Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit, BigInteger initialEtherValue) { - return deployAsync(Fibonacci.class, web3j, credentials, gasPrice, gasLimit, BINARY, "", initialEtherValue); + public static Future deploy(Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit, BigInteger initialWeiValue) { + return deployAsync(Fibonacci.class, web3j, credentials, gasPrice, gasLimit, BINARY, "", initialWeiValue); } - public static Future deploy(Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit, BigInteger initialEtherValue) { - return deployAsync(Fibonacci.class, web3j, transactionManager, gasPrice, gasLimit, BINARY, "", initialEtherValue); + public static Future deploy(Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit, BigInteger initialWeiValue) { + return deployAsync(Fibonacci.class, web3j, transactionManager, gasPrice, gasLimit, BINARY, "", initialWeiValue); } public static Fibonacci load(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) { diff --git a/src/integration-test/java/org/web3j/generated/Greeter.java b/src/integration-test/java/org/web3j/generated/Greeter.java index e13572484..b22b0a182 100644 --- a/src/integration-test/java/org/web3j/generated/Greeter.java +++ b/src/integration-test/java/org/web3j/generated/Greeter.java @@ -16,7 +16,7 @@ import org.web3j.tx.TransactionManager; /** - *

Auto generated code.
+ * Auto generated code.
* Do not modify!
* Please use {@link org.web3j.codegen.SolidityFunctionWrapperGenerator} to update. * @@ -45,14 +45,14 @@ public Future greet() { return executeCallSingleValueReturnAsync(function); } - public static Future deploy(Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit, BigInteger initialEtherValue, Utf8String _greeting) { + public static Future deploy(Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit, BigInteger initialWeiValue, Utf8String _greeting) { String encodedConstructor = FunctionEncoder.encodeConstructor(Arrays.asList(_greeting)); - return deployAsync(Greeter.class, web3j, credentials, gasPrice, gasLimit, BINARY, encodedConstructor, initialEtherValue); + return deployAsync(Greeter.class, web3j, credentials, gasPrice, gasLimit, BINARY, encodedConstructor, initialWeiValue); } - public static Future deploy(Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit, BigInteger initialEtherValue, Utf8String _greeting) { + public static Future deploy(Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit, BigInteger initialWeiValue, Utf8String _greeting) { String encodedConstructor = FunctionEncoder.encodeConstructor(Arrays.asList(_greeting)); - return deployAsync(Greeter.class, web3j, transactionManager, gasPrice, gasLimit, BINARY, encodedConstructor, initialEtherValue); + return deployAsync(Greeter.class, web3j, transactionManager, gasPrice, gasLimit, BINARY, encodedConstructor, initialWeiValue); } public static Greeter load(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) { diff --git a/src/integration-test/java/org/web3j/generated/HumanStandardToken.java b/src/integration-test/java/org/web3j/generated/HumanStandardToken.java index a69b9736c..ed033cbc6 100644 --- a/src/integration-test/java/org/web3j/generated/HumanStandardToken.java +++ b/src/integration-test/java/org/web3j/generated/HumanStandardToken.java @@ -30,7 +30,7 @@ import rx.functions.Func1; /** - *

Auto generated code.
+ * Auto generated code.
* Do not modify!
* Please use {@link org.web3j.codegen.SolidityFunctionWrapperGenerator} to update. * @@ -51,9 +51,9 @@ public List getTransferEvents(TransactionReceipt transact final Event event = new Event("Transfer", Arrays.>asList(new TypeReference

() {}, new TypeReference
() {}), Arrays.>asList(new TypeReference() {})); - List valueList = extractEventParameters(event,transactionReceipt); + List valueList = extractEventParameters(event, transactionReceipt); ArrayList responses = new ArrayList(valueList.size()); - for(EventValues eventValues : valueList) { + for (EventValues eventValues : valueList) { TransferEventResponse typedResponse = new TransferEventResponse(); typedResponse._from = (Address) eventValues.getIndexedValues().get(0); typedResponse._to = (Address) eventValues.getIndexedValues().get(1); @@ -86,9 +86,9 @@ public List getApprovalEvents(TransactionReceipt transact final Event event = new Event("Approval", Arrays.>asList(new TypeReference
() {}, new TypeReference
() {}), Arrays.>asList(new TypeReference() {})); - List valueList = extractEventParameters(event,transactionReceipt); + List valueList = extractEventParameters(event, transactionReceipt); ArrayList responses = new ArrayList(valueList.size()); - for(EventValues eventValues : valueList) { + for (EventValues eventValues : valueList) { ApprovalEventResponse typedResponse = new ApprovalEventResponse(); typedResponse._owner = (Address) eventValues.getIndexedValues().get(0); typedResponse._spender = (Address) eventValues.getIndexedValues().get(1); @@ -186,14 +186,14 @@ public Future allowance(Address _owner, Address _spender) { return executeCallSingleValueReturnAsync(function); } - public static Future deploy(Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit, BigInteger initialEtherValue, Uint256 _initialAmount, Utf8String _tokenName, Uint8 _decimalUnits, Utf8String _tokenSymbol) { + public static Future deploy(Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit, BigInteger initialWeiValue, Uint256 _initialAmount, Utf8String _tokenName, Uint8 _decimalUnits, Utf8String _tokenSymbol) { String encodedConstructor = FunctionEncoder.encodeConstructor(Arrays.asList(_initialAmount, _tokenName, _decimalUnits, _tokenSymbol)); - return deployAsync(HumanStandardToken.class, web3j, credentials, gasPrice, gasLimit, BINARY, encodedConstructor, initialEtherValue); + return deployAsync(HumanStandardToken.class, web3j, credentials, gasPrice, gasLimit, BINARY, encodedConstructor, initialWeiValue); } - public static Future deploy(Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit, BigInteger initialEtherValue, Uint256 _initialAmount, Utf8String _tokenName, Uint8 _decimalUnits, Utf8String _tokenSymbol) { + public static Future deploy(Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit, BigInteger initialWeiValue, Uint256 _initialAmount, Utf8String _tokenName, Uint8 _decimalUnits, Utf8String _tokenSymbol) { String encodedConstructor = FunctionEncoder.encodeConstructor(Arrays.asList(_initialAmount, _tokenName, _decimalUnits, _tokenSymbol)); - return deployAsync(HumanStandardToken.class, web3j, transactionManager, gasPrice, gasLimit, BINARY, encodedConstructor, initialEtherValue); + return deployAsync(HumanStandardToken.class, web3j, transactionManager, gasPrice, gasLimit, BINARY, encodedConstructor, initialWeiValue); } public static HumanStandardToken load(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) { diff --git a/src/integration-test/java/org/web3j/generated/ShipIt.java b/src/integration-test/java/org/web3j/generated/ShipIt.java index 1fba51536..0abada727 100644 --- a/src/integration-test/java/org/web3j/generated/ShipIt.java +++ b/src/integration-test/java/org/web3j/generated/ShipIt.java @@ -18,7 +18,7 @@ import org.web3j.tx.TransactionManager; /** - *

Auto generated code.
+ * Auto generated code.
* Do not modify!
* Please use {@link org.web3j.codegen.SolidityFunctionWrapperGenerator} to update. * @@ -42,12 +42,12 @@ public Future> shipments(Address param0) { return executeCallMultipleValueReturnAsync(function); } - public static Future deploy(Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit, BigInteger initialEtherValue) { - return deployAsync(ShipIt.class, web3j, credentials, gasPrice, gasLimit, BINARY, "", initialEtherValue); + public static Future deploy(Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit, BigInteger initialWeiValue) { + return deployAsync(ShipIt.class, web3j, credentials, gasPrice, gasLimit, BINARY, "", initialWeiValue); } - public static Future deploy(Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit, BigInteger initialEtherValue) { - return deployAsync(ShipIt.class, web3j, transactionManager, gasPrice, gasLimit, BINARY, "", initialEtherValue); + public static Future deploy(Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit, BigInteger initialWeiValue) { + return deployAsync(ShipIt.class, web3j, transactionManager, gasPrice, gasLimit, BINARY, "", initialWeiValue); } public static ShipIt load(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) { diff --git a/src/integration-test/java/org/web3j/protocol/core/ObservableIT.java b/src/integration-test/java/org/web3j/protocol/core/ObservableIT.java index 61b4ad1bc..c6332abb1 100644 --- a/src/integration-test/java/org/web3j/protocol/core/ObservableIT.java +++ b/src/integration-test/java/org/web3j/protocol/core/ObservableIT.java @@ -58,7 +58,9 @@ public void testLogObservable() throws Exception { @Test public void testReplayObservable() throws Exception { - run(web3j.replayBlocksObservable(BigInteger.ZERO, BigInteger.valueOf(EVENT_COUNT), true)); + run(web3j.replayBlocksObservable( + new DefaultBlockParameterNumber(0), + new DefaultBlockParameterNumber(EVENT_COUNT), true)); } @Test @@ -67,7 +69,8 @@ public void testCatchUpToLatestAndSubscribeToNewBlocksObservable() throws Except .send(); BigInteger latestBlockNumber = ethBlock.getBlock().getNumber(); run(web3j.catchUpToLatestAndSubscribeToNewBlocksObservable( - latestBlockNumber.subtract(BigInteger.ONE), false)); + new DefaultBlockParameterNumber(latestBlockNumber.subtract(BigInteger.ONE)), + false)); } private void run(Observable observable) throws Exception { diff --git a/src/main/java/org/web3j/codegen/SolidityFunctionWrapper.java b/src/main/java/org/web3j/codegen/SolidityFunctionWrapper.java index 99044e7b5..99fcfba1d 100644 --- a/src/main/java/org/web3j/codegen/SolidityFunctionWrapper.java +++ b/src/main/java/org/web3j/codegen/SolidityFunctionWrapper.java @@ -58,14 +58,14 @@ public class SolidityFunctionWrapper { private static final String WEB3J = "web3j"; private static final String CREDENTIALS = "credentials"; private static final String TRANSACTION_MANAGER = "transactionManager"; - private static final String INITIAL_VALUE = "initialEtherValue"; + private static final String INITIAL_VALUE = "initialWeiValue"; private static final String CONTRACT_ADDRESS = "contractAddress"; private static final String GAS_PRICE = "gasPrice"; private static final String GAS_LIMIT = "gasLimit"; private static final String START_BLOCK = "startBlock"; private static final String END_BLOCK = "endBlock"; - private static final String CODEGEN_WARNING = "

Auto generated code.
\n" + + private static final String CODEGEN_WARNING = "Auto generated code.
\n" + "Do not modify!
\n" + "Please use {@link " + SolidityFunctionWrapperGenerator.class.getName() + "} to update.\n"; @@ -449,12 +449,12 @@ static MethodSpec buildEventTransactionReceiptFunction(String responseClassName, buildVariableLengthEventConstructor( transactionMethodBuilder, functionName, indexedParameters, nonIndexedParameters); - transactionMethodBuilder.addStatement("$T valueList = extractEventParameters(event," + + transactionMethodBuilder.addStatement("$T valueList = extractEventParameters(event, " + "transactionReceipt)", ParameterizedTypeName.get(List.class, EventValues.class)) .addStatement("$1T responses = new $1T(valueList.size())", ParameterizedTypeName.get(ClassName.get(ArrayList.class), ClassName.get("", responseClassName))) - .beginControlFlow("for($T eventValues : valueList)", EventValues.class) + .beginControlFlow("for ($T eventValues : valueList)", EventValues.class) .addStatement("$1T typedResponse = new $1T()", ClassName.get("", responseClassName)) .addCode(buildTypedResponse("typedResponse", indexedParameters, diff --git a/src/main/java/org/web3j/protocol/core/DefaultBlockParameterNumber.java b/src/main/java/org/web3j/protocol/core/DefaultBlockParameterNumber.java index 21926de78..1e745f07d 100644 --- a/src/main/java/org/web3j/protocol/core/DefaultBlockParameterNumber.java +++ b/src/main/java/org/web3j/protocol/core/DefaultBlockParameterNumber.java @@ -26,4 +26,8 @@ public DefaultBlockParameterNumber(long blockNumber) { public String getValue() { return Numeric.encodeQuantity(blockNumber); } + + public BigInteger getBlockNumber() { + return blockNumber; + } } diff --git a/src/main/java/org/web3j/protocol/core/JsonRpc2_0Web3j.java b/src/main/java/org/web3j/protocol/core/JsonRpc2_0Web3j.java index 411e78c26..ce15dbdc8 100644 --- a/src/main/java/org/web3j/protocol/core/JsonRpc2_0Web3j.java +++ b/src/main/java/org/web3j/protocol/core/JsonRpc2_0Web3j.java @@ -780,37 +780,50 @@ public Observable blockObservable(boolean fullTransactionObjects) { @Override public Observable replayBlocksObservable( - BigInteger startBlockNumber, BigInteger endBlockNumber, boolean fullTransactionObjects) { - return web3jRx.replayBlocksObservable(startBlockNumber, endBlockNumber, fullTransactionObjects); + DefaultBlockParameter startBlock, DefaultBlockParameter endBlock, + boolean fullTransactionObjects) { + return web3jRx.replayBlocksObservable(startBlock, endBlock, fullTransactionObjects); } @Override public Observable replayTransactionsObservable( - BigInteger startBlockNumber, BigInteger endBlockNumber) { - return web3jRx.replayTransactionsObservable(startBlockNumber, endBlockNumber); + DefaultBlockParameter startBlock, DefaultBlockParameter endBlock) { + return web3jRx.replayTransactionsObservable(startBlock, endBlock); } @Override public Observable catchUpToLatestBlockObservable( - BigInteger startBlockNumber, boolean fullTransactionObjects, + DefaultBlockParameter startBlock, boolean fullTransactionObjects, Observable onCompleteObservable) { return web3jRx.catchUpToLatestBlockObservable( - startBlockNumber, fullTransactionObjects, onCompleteObservable); + startBlock, fullTransactionObjects, onCompleteObservable); + } + + @Override + public Observable catchUpToLatestBlockObservable( + DefaultBlockParameter startBlock, boolean fullTransactionObjects) { + return web3jRx.catchUpToLatestBlockObservable(startBlock, fullTransactionObjects); + } + + @Override + public Observable + catchUpToLatestTransactionObservable(DefaultBlockParameter startBlock) { + return web3jRx.catchUpToLatestTransactionObservable(startBlock); } @Override public Observable catchUpToLatestAndSubscribeToNewBlocksObservable( - BigInteger startBlockNumber, boolean fullTransactionObjects) { + DefaultBlockParameter startBlock, boolean fullTransactionObjects) { return web3jRx.catchUpToLatestAndSubscribeToNewBlocksObservable( - startBlockNumber, fullTransactionObjects, blockTime); + startBlock, fullTransactionObjects, blockTime); } @Override public Observable catchUpToLatestAndSubscribeToNewTransactionsObservable( - BigInteger startBlockNumber) { + DefaultBlockParameter startBlock) { return web3jRx.catchUpToLatestAndSubscribeToNewTransactionsObservable( - startBlockNumber, blockTime); + startBlock, blockTime); } } diff --git a/src/main/java/org/web3j/protocol/rx/JsonRpc2_0Rx.java b/src/main/java/org/web3j/protocol/rx/JsonRpc2_0Rx.java index 49e3004a9..7a1968404 100644 --- a/src/main/java/org/web3j/protocol/rx/JsonRpc2_0Rx.java +++ b/src/main/java/org/web3j/protocol/rx/JsonRpc2_0Rx.java @@ -7,10 +7,13 @@ import java.util.stream.Collectors; import rx.Observable; +import rx.Scheduler; import rx.Subscriber; +import rx.schedulers.Schedulers; import rx.subscriptions.Subscriptions; import org.web3j.protocol.Web3j; +import org.web3j.protocol.core.DefaultBlockParameter; import org.web3j.protocol.core.DefaultBlockParameterName; import org.web3j.protocol.core.DefaultBlockParameterNumber; import org.web3j.protocol.core.filters.BlockFilter; @@ -28,10 +31,12 @@ public class JsonRpc2_0Rx { private final Web3j web3j; private final ScheduledExecutorService scheduledExecutorService; + private final Scheduler scheduler; public JsonRpc2_0Rx(Web3j web3j, ScheduledExecutorService scheduledExecutorService) { this.web3j = web3j; this.scheduledExecutorService = scheduledExecutorService; + this.scheduler = Schedulers.from(scheduledExecutorService); } public Observable ethBlockHashObservable(long pollingInterval) { @@ -89,26 +94,56 @@ public Observable blockObservable( } public Observable replayBlocksObservable( - BigInteger startBlockNumber, BigInteger endBlockNumber, + DefaultBlockParameter startBlock, DefaultBlockParameter endBlock, + boolean fullTransactionObjects) { + // We use a scheduler to ensure this Observable runs asynchronously for users to be + // consistent with the other Observables + return replayBlocksObservableSync(startBlock, endBlock, fullTransactionObjects) + .subscribeOn(scheduler); + } + + private Observable replayBlocksObservableSync( + DefaultBlockParameter startBlock, DefaultBlockParameter endBlock, boolean fullTransactionObjects) { + BigInteger startBlockNumber = null; + BigInteger endBlockNumber = null; + try { + startBlockNumber = getBlockNumber(startBlock); + endBlockNumber = getBlockNumber(endBlock); + } catch (IOException e) { + Observable.error(e); + } + return Observables.range(startBlockNumber, endBlockNumber) .flatMap(i -> web3j.ethGetBlockByNumber( new DefaultBlockParameterNumber(i), fullTransactionObjects).observable()); } public Observable replayTransactionsObservable( - BigInteger startBlockNumber, BigInteger endBlockNumber) { - return replayBlocksObservable(startBlockNumber, endBlockNumber, true) + DefaultBlockParameter startBlock, DefaultBlockParameter endBlock) { + return replayBlocksObservable(startBlock, endBlock, true) .flatMapIterable(JsonRpc2_0Rx::toTransactions); } public Observable catchUpToLatestBlockObservable( - BigInteger startBlockNumber, boolean fullTransactionObjects, + DefaultBlockParameter startBlock, boolean fullTransactionObjects, + Observable onCompleteObservable) { + // We use a scheduler to ensure this Observable runs asynchronously for users to be + // consistent with the other Observables + return catchUpToLatestBlockObservableSync( + startBlock, fullTransactionObjects, onCompleteObservable) + .subscribeOn(scheduler); + } + + private Observable catchUpToLatestBlockObservableSync( + DefaultBlockParameter startBlock, boolean fullTransactionObjects, Observable onCompleteObservable) { + BigInteger startBlockNumber; BigInteger latestBlockNumber; try { + startBlockNumber = getBlockNumber(startBlock); latestBlockNumber = getLatestBlockNumber(); } catch (IOException e) { return Observable.error(e); @@ -118,36 +153,62 @@ public Observable catchUpToLatestBlockObservable( return onCompleteObservable; } else { return Observable.concat( - replayBlocksObservable( - startBlockNumber, latestBlockNumber, + replayBlocksObservableSync( + new DefaultBlockParameterNumber(startBlockNumber), + new DefaultBlockParameterNumber(latestBlockNumber), fullTransactionObjects), - Observable.defer(() -> catchUpToLatestBlockObservable( - latestBlockNumber.add(BigInteger.ONE), fullTransactionObjects, + Observable.defer(() -> catchUpToLatestBlockObservableSync( + new DefaultBlockParameterNumber(latestBlockNumber.add(BigInteger.ONE)), + fullTransactionObjects, onCompleteObservable))); } } + public Observable catchUpToLatestBlockObservable( + DefaultBlockParameter startBlock, boolean fullTransactionObjects) { + return catchUpToLatestBlockObservable( + startBlock, fullTransactionObjects, Observable.empty()); + } + + public Observable catchUpToLatestTransactionObservable( + DefaultBlockParameter startBlock) { + return catchUpToLatestBlockObservable( + startBlock, true, Observable.empty()) + .flatMapIterable(JsonRpc2_0Rx::toTransactions); + } + public Observable catchUpToLatestAndSubscribeToNewBlocksObservable( - BigInteger startBlockNumber, boolean fullTransactionObjects, long pollingInterval) { + DefaultBlockParameter startBlock, boolean fullTransactionObjects, long pollingInterval) { return catchUpToLatestBlockObservable( - startBlockNumber, fullTransactionObjects, + startBlock, fullTransactionObjects, blockObservable(fullTransactionObjects, pollingInterval)); } public Observable catchUpToLatestAndSubscribeToNewTransactionsObservable( - BigInteger startBlockNumber, long pollingInterval) { + DefaultBlockParameter startBlock, long pollingInterval) { return catchUpToLatestAndSubscribeToNewBlocksObservable( - startBlockNumber, true, pollingInterval) + startBlock, true, pollingInterval) .flatMapIterable(JsonRpc2_0Rx::toTransactions); } private BigInteger getLatestBlockNumber() throws IOException { - EthBlock latestEthBlock = web3j.ethGetBlockByNumber( - DefaultBlockParameterName.LATEST, false).send(); - return latestEthBlock.getBlock().getNumber(); + return getBlockNumber(DefaultBlockParameterName.LATEST); + } + + private BigInteger getBlockNumber( + DefaultBlockParameter defaultBlockParameter) throws IOException { + if (defaultBlockParameter instanceof DefaultBlockParameterNumber) { + return ((DefaultBlockParameterNumber) defaultBlockParameter).getBlockNumber(); + } else { + EthBlock latestEthBlock = web3j.ethGetBlockByNumber( + defaultBlockParameter, false).send(); + return latestEthBlock.getBlock().getNumber(); + } } private static List toTransactions(EthBlock ethBlock) { + // If you ever see an exception thrown here, it's probably due to an incomplete chain in + // Geth/Parity. You should resync to solve. return ethBlock.getBlock().getTransactions().stream() .map(transactionResult -> (Transaction) transactionResult.get()) .collect(Collectors.toList()); diff --git a/src/main/java/org/web3j/protocol/rx/Web3jRx.java b/src/main/java/org/web3j/protocol/rx/Web3jRx.java index 141b289a4..d12f5c48e 100644 --- a/src/main/java/org/web3j/protocol/rx/Web3jRx.java +++ b/src/main/java/org/web3j/protocol/rx/Web3jRx.java @@ -4,6 +4,7 @@ import rx.Observable; +import org.web3j.protocol.core.DefaultBlockParameter; import org.web3j.protocol.core.methods.request.EthFilter; import org.web3j.protocol.core.methods.response.EthBlock; import org.web3j.protocol.core.methods.response.Log; @@ -67,25 +68,26 @@ public interface Web3jRx { * Create an Observable that emits all blocks from the blockchain contained within the * requested range. * - * @param startBlockNumber block number to commence with - * @param endBlockNumber block number to finish with + * @param startBlock block number to commence with + * @param endBlock block number to finish with * @param fullTransactionObjects if true, provides transactions embedded in blocks, otherwise * transaction hashes * @return Observable to emit these blocks */ Observable replayBlocksObservable( - BigInteger startBlockNumber, BigInteger endBlockNumber, boolean fullTransactionObjects); + DefaultBlockParameter startBlock, DefaultBlockParameter endBlock, + boolean fullTransactionObjects); /** * Create an Observable that emits all transactions from the blockchain contained within the * requested range. * - * @param startBlockNumber block number to commence with - * @param endBlockNumber block number to finish with + * @param startBlock block number to commence with + * @param endBlock block number to finish with * @return Observable to emit these transactions in the order they appear in the blocks */ Observable replayTransactionsObservable( - BigInteger startBlockNumber, BigInteger endBlockNumber); + DefaultBlockParameter startBlock, DefaultBlockParameter endBlock); /** * Create an Observable that emits all transactions from the blockchain starting with a @@ -93,9 +95,9 @@ Observable replayTransactionsObservable( * Observable is invoked. * *

To automatically subscribe to new blocks, use - * {@link #catchUpToLatestAndSubscribeToNewBlocksObservable(BigInteger, boolean)}. + * {@link #catchUpToLatestAndSubscribeToNewBlocksObservable(DefaultBlockParameter, boolean)}. * - * @param startBlockNumber the block number we wish to request from + * @param startBlock the block number we wish to request from * @param fullTransactionObjects if we require full {@link Transaction} objects to be provided in * the {@link EthBlock} responses * @param onCompleteObservable a subsequent Observable that we wish to run once we are caught @@ -103,29 +105,52 @@ Observable replayTransactionsObservable( * @return Observable to emit all requested blocks */ Observable catchUpToLatestBlockObservable( - BigInteger startBlockNumber, boolean fullTransactionObjects, + DefaultBlockParameter startBlock, boolean fullTransactionObjects, Observable onCompleteObservable); + /** + * Creates an Observable that emits all blocks from the requested block number to the most + * current. Once it has emitted the most current block, onComplete is called. + * + * @param startBlock the block number we wish to request from + * @param fullTransactionObjects if we require full {@link Transaction} objects to be provided in + * the {@link EthBlock} responses + * @return Observable to emit all requested blocks + */ + Observable catchUpToLatestBlockObservable( + DefaultBlockParameter startBlock, boolean fullTransactionObjects); + + /** + * Creates an Observable that emits all transactions from the requested block number to the most + * current. Once it has emitted the most current block's transactions, onComplete is called. + * + * @param startBlock the block number we wish to request from + * @return Observable to emit all requested transactions + */ + Observable catchUpToLatestTransactionObservable( + DefaultBlockParameter startBlock); + /** * Creates an Observable that emits all blocks from the requested block number to the most * current. Once it has emitted the most current block, it starts emitting new blocks as they * are created. * - * @param startBlockNumber the block number we wish to request from + * @param startBlock the block number we wish to request from * @param fullTransactionObjects if we require full {@link Transaction} objects to be provided in * the {@link EthBlock} responses * @return Observable to emit all requested blocks and future */ Observable catchUpToLatestAndSubscribeToNewBlocksObservable( - BigInteger startBlockNumber, boolean fullTransactionObjects); + DefaultBlockParameter startBlock, boolean fullTransactionObjects); /** - * As per {@link #catchUpToLatestAndSubscribeToNewBlocksObservable(BigInteger, boolean)}, + * As per + * {@link #catchUpToLatestAndSubscribeToNewBlocksObservable(DefaultBlockParameter, boolean)}, * except that all transactions contained within the blocks are emitted. * - * @param startBlockNumber the block number we wish to request from + * @param startBlock the block number we wish to request from * @return Observable to emit all requested transactions and future */ Observable catchUpToLatestAndSubscribeToNewTransactionsObservable( - BigInteger startBlockNumber); + DefaultBlockParameter startBlock); } diff --git a/src/main/java/org/web3j/tx/Contract.java b/src/main/java/org/web3j/tx/Contract.java index 7b599ce55..441103ceb 100644 --- a/src/main/java/org/web3j/tx/Contract.java +++ b/src/main/java/org/web3j/tx/Contract.java @@ -167,7 +167,7 @@ protected TransactionReceipt executeTransaction(Function function) throws Interr * recommended via {@link Contract#executeTransactionAsync}. * * @param data to send in transaction - * @param value in Wei to send in transaction + * @param weiValue in Wei to send in transaction * @return {@link Optional} containing our transaction receipt * @throws InterruptedException if the current thread was interrupted * while waiting @@ -175,10 +175,10 @@ protected TransactionReceipt executeTransaction(Function function) throws Interr * @throws TransactionTimeoutException if the transaction was not mined while waiting */ protected TransactionReceipt executeTransaction( - String data, BigInteger value) + String data, BigInteger weiValue) throws InterruptedException, IOException, TransactionTimeoutException { - return send(contractAddress, data, value, gasPrice, gasLimit); + return send(contractAddress, data, weiValue, gasPrice, gasLimit); } /** @@ -283,18 +283,18 @@ private static T create( protected static CompletableFuture deployAsync( Class type, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit, - String binary, String encodedConstructor, BigInteger initialEtherValue) { + String binary, String encodedConstructor, BigInteger initialWeiValue) { return Async.run(() -> deploy(type, web3j, credentials, gasPrice, gasLimit, - binary, encodedConstructor, initialEtherValue)); + binary, encodedConstructor, initialWeiValue)); } protected static CompletableFuture deployAsync( Class type, Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit, - String binary, String encodedConstructor, BigInteger initialEtherValue) { + String binary, String encodedConstructor, BigInteger initialWeiValue) { return Async.run(() -> deploy(type, web3j, transactionManager, gasPrice, gasLimit, - binary, encodedConstructor, initialEtherValue)); + binary, encodedConstructor, initialWeiValue)); } } diff --git a/src/test/java/org/web3j/codegen/SolidityFunctionWrapperTest.java b/src/test/java/org/web3j/codegen/SolidityFunctionWrapperTest.java index 05b1028a9..4d17d4e1c 100644 --- a/src/test/java/org/web3j/codegen/SolidityFunctionWrapperTest.java +++ b/src/test/java/org/web3j/codegen/SolidityFunctionWrapperTest.java @@ -193,10 +193,10 @@ public void testBuildEventConstantMultipleValueReturn() throws Exception { " java.util.Arrays.>asList(new org.web3j" + ".abi.TypeReference() {}));\n" + " java.util.List valueList = extractEventParameters" + - "(event,transactionReceipt);\n" + + "(event, transactionReceipt);\n" + " java.util.ArrayList responses = new java.util" + ".ArrayList(valueList.size());\n" + - " for(org.web3j.abi.EventValues eventValues : valueList) {\n" + + " for (org.web3j.abi.EventValues eventValues : valueList) {\n" + " TransferEventResponse typedResponse = new TransferEventResponse();\n" + " typedResponse.from = (org.web3j.abi.datatypes.Address) eventValues" + ".getIndexedValues().get(0);\n" + diff --git a/src/test/java/org/web3j/protocol/rx/JsonRpc2_0RxTest.java b/src/test/java/org/web3j/protocol/rx/JsonRpc2_0RxTest.java index 6d214367c..5a120d99d 100644 --- a/src/test/java/org/web3j/protocol/rx/JsonRpc2_0RxTest.java +++ b/src/test/java/org/web3j/protocol/rx/JsonRpc2_0RxTest.java @@ -19,6 +19,7 @@ import org.web3j.protocol.ObjectMapperFactory; import org.web3j.protocol.Web3j; import org.web3j.protocol.Web3jService; +import org.web3j.protocol.core.DefaultBlockParameterNumber; import org.web3j.protocol.core.Request; import org.web3j.protocol.core.methods.response.EthBlock; import org.web3j.protocol.core.methods.response.EthFilter; @@ -61,7 +62,9 @@ public void testReplayBlocksObservable() throws Exception { } Observable observable = web3j.replayBlocksObservable( - BigInteger.ZERO, BigInteger.valueOf(2), false); + new DefaultBlockParameterNumber(BigInteger.ZERO), + new DefaultBlockParameterNumber(BigInteger.valueOf(2)), + false); CountDownLatch transactionLatch = new CountDownLatch(ethBlocks.size()); CountDownLatch completedLatch = new CountDownLatch(1); @@ -123,7 +126,8 @@ public void testCatchUpToLatestAndSubscribeToNewBlockObservable() throws Excepti .thenReturn(ethUninstallFilter); Observable observable = web3j.catchUpToLatestAndSubscribeToNewBlocksObservable( - BigInteger.ZERO, false); + new DefaultBlockParameterNumber(BigInteger.ZERO), + false); CountDownLatch transactionLatch = new CountDownLatch(expected.size()); CountDownLatch completedLatch = new CountDownLatch(1);