Web3j Filters – Troubleshooting Filters and Events in Web3j/TestRPC

ethereumjremixsoliditytestrpcweb3j

Help me, please, with this little simple task! I cannot solve this problem for several days. SOS! 😉
I'm trying to write a program for filtering events via web3j. I copied to start (for the test) from somewhere this simple contract:

pragma solidity ^0.4.11;
contract EventTest {                
  event NumberEvent(uint indexed number);   

  function triggerEvent(uint val) returns (uint) { 
    NumberEvent(val);
    return 123;
  }                                 
}

Then I ran TestRPC, Remix, and created a contract. Here is the state after that:

C:\>testrpc
EthereumJS TestRPC v3.0.5

Available Accounts
==================
(0) 0x5403bdee62d577b2660a2a6af4590ce9c426aceb
(1) 0x88302f871bb26f044f106f1f686fba4f07d9658d
(2) 0xd90aedb12243b420b63317936acaff289316bad6
(3) 0x537598e72b2b8cd179f3fc72ef09f7233f73ef44
(4) 0xef571d65c0e828da5091bb76169dc7ca0ff67f15
(5) 0xe251fa32bf822ad3ccb3133d97ccd64ab97cb0fd
(6) 0x20d6a6bd42ba577a1e6fa938f738b0e07191021b
(7) 0x8e76d16288f5068d2406f1cb7005df449a88790a
(8) 0xb05b5df33972324edc49c02d23ec710f31fc3d09
(9) 0x1bb116bd5916ba749caa429d08406d57030d2eb6

Private Keys
==================
(0) fbd2033e842b85c0da50d1ebc0a73deae130c887762116937ad60266a974c3dd
(1) 6fa1c314f48bb9dee169cb3d6e4d9348adb7df82fefa5229349e8aa3ebc7ce02
(2) 0995cc564f136217a7438daab9f55ca3e593e67483f36de5862826b52f264b19
(3) f642805c48f3c9fda4fac90df031ad0836a4701263d1c900f1d5b42e3b073b51
(4) d4b868c78ee8af1c7900a9a2f6ae59042ef26038aeb151f03a010547852bc99a
(5) 9f76d96f9d88e078591f38ae0d249a746287814fb0f95e59b8634165a1b71560
(6) 722f6fbbd74b471e0ab294983401abb1aecabd6e1e4584eba99c2f408b75c9e3
(7) a57e22386e8874834535e2dd02c28ef8123e7412ff6896d273e0cac69553fe81
(8) 28a53adc37ffa7e96615b0b2e20fb7f342dc99aed2d0e681ca9a447cc5101f05
(9) b92a329c6f616af0bfe7f20d9a48e5518d5ede352475eab640a555300029a7d9

HD Wallet
==================
Mnemonic:      bird ship unlock fiber drink kiwi surprise warrior over cereal item poet
Base HD Path:  m/44'/60'/0'/0/{account_index}

Listening on localhost:8545
eth_accounts
eth_accounts
eth_estimateGas
eth_getBlockByNumber
eth_sendTransaction

  Transaction: 0xc9b39a4ee3c3d02ef4a76011b3bdc00721f196e90e6b9a6708c8d9842ca03698
  Contract created: 0x53417664007120b53005a9f1a42b3fb02e6cb0c7
  Gas usage: 0x01a9f1
  Block Number: 0x01
  Block Time: Thu May 04 2017 12:08:19 GMT+0200

eth_getTransactionReceipt
eth_newFilter
eth_getFilterLogs
eth_getFilterChanges
eth_getFilterChanges
eth_getFilterChanges
eth_getFilterChanges
eth_getFilterChanges

Since in the command line from TestRPC approximately every second the message "eth_getFilterChanges" appeared, I closed the browser with Remix and then the new messages "eth_getFilterChanges" ceased to arrive.

Then I launched such Java code to "listen" to new events from this my test contract.

import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.DefaultBlockParameterName;
import org.web3j.protocol.core.methods.request.EthFilter;
import org.web3j.protocol.core.methods.response.Log;
import org.web3j.protocol.http.HttpService;

import rx.functions.Action1;

public class Subscribe {

    public static String contractAddr = "0x53417664007120b53005a9f1a42b3fb02e6cb0c7";

    public static void main(String[] args) {

        Web3j web3 = Web3j.build(new HttpService());

        EthFilter filter = new EthFilter(DefaultBlockParameterName.EARLIEST, DefaultBlockParameterName.LATEST, contractAddr);
        web3.ethLogObservable(filter).subscribe(new Action1<Log>() {
            @Override
            public void call(Log log) {
                System.out.println("log.toString(): " + log.toString());
            }
        });
    }
}

Here is the result:

eth_newFilter
eth_getFilterChanges
FilterSubprovider - no filter with that id: 0x2

Could you please explain what I'm doing wrong?
Thank you in advance for any help!

Best Answer

The problem here seems to be that test rpc doesn't recognize hex-numbers without the leading zero for single digit filters. So it recognizes "0x03" but not "0x3". And well that sucks.

But if you create and discard the first 9 filter ids you should be good to go with testrpc.

Related Topic