0

I'm looking to use this function:

func (ec *Client) FilterLogs(ctx context.Context, q ethereum.FilterQuery) ([]types.Log, error)

but I'm unsure if the range of the FilterQuery is inclusive or exclusive of ToBlock:

type FilterQuery struct {
    FromBlock *big.Int         // beginning of the queried range, nil means genesis block
    ToBlock   *big.Int         // end of the range, nil means latest block
 ...
}

I'm unsure if "end of the range" is inclusive or exclusive.

1

I demonstrated it's inclusive. Here's the experiment I came up with to test this, running against a local geth blockchain:

ethLogs, err := client.FilterLogs(context.Background(), ethereum.FilterQuery{
        FromBlock: big.NewInt(10),
        ToBlock:   big.NewInt(11),
        Addresses: []common.Address{address},
    })
if err != nil {
    panic(err)
}

for _, ethLog := range ethLogs {
    log.Infof("Processed events from block %v", ethLog.BlockNumber)
}

The logs show that both blocks 10 and 11 are processed.

INFO[2019-12-31 12:02:00.736624+0900] Processed events from block 10
INFO[2019-12-31 12:02:00.736643+0900] Processed events from block 10
INFO[2019-12-31 12:02:00.736650+0900] Processed events from block 11
INFO[2019-12-31 12:02:00.736655+0900] Processed events from block 11
INFO[2019-12-31 12:02:00.736660+0900] Processed events from block 11

| improve this answer | |
0

Both parameters are inclusive. You could try with just a block:

query := ethereum.FilterQuery{
  FromBlock: big.NewInt(133420),
  ToBlock:   big.NewInt(133420),
  Addresses: []common.Address{
    contractAddress,
  },
}

That code, create a FilterQuery for the block number 133420. And them, you could use in FilterLogs function. Something like this:

logs, err := client.FilterLogs(context.Background(), query)
if err != nil {
  log.Fatal(err)
}

(Update)

I saw the source code and test with rpc call and etherscan API.

Source Code:

// FilterQuery https://github.com/ethereum/go-ethereum/blob/master/eth/filters/api.go

FilterQuery use NewRangeFilter:

// NewRangeFilter https://github.com/ethereum/go-ethereum/blob/master/eth/filters/filter.go

That use filter function:

/ Logs searches the blockchain for matching log entries, returning all from the
// first block that contains matches, updating the start of the filter accordingly.
func (f *Filter) Logs(ctx context.Context) ([]*types.Log, error) {

Ways to try

With your posted code!

Test with etherscan api

Call:

https://api.etherscan.io/api?module=logs&action=getLogs&fromBlock=3997264&toBlock=3997264&address=0x6090a6e47849629b7245dfa1ca21d94cd15878ef&topic0=0x0f0c27adfd84b60b6f456b0e87cdccb1e5fb9603991588d87fa99f5b6b61e670&apikey=49W852FPN57GP4S3DR351SBKFYUP1N9TDD

Result:

{"status":"1","message":"OK","result":[{"address":"0x6090a6e47849629b7245dfa1ca21d94cd15878ef","topics":["0x0f0c27adfd84b60b6f456b0e87cdccb1e5fb9603991588d87fa99f5b6b61e670","0x4981ae698e550a328ba9008e83467cf24b3ab26163ae47bad5d0d109a717fc45","0x0000000000000000000000003fb14ab42b18736fc4b3ec9f88b80fe5829527fb"],"data":"0x000000000000000000000000000000000000000000000000002386f26fc10000000000000000000000000000000000000000000000000000000000005961c025","blockNumber":"0x3cfe50","timeStamp":"0x5961e543","gasPrice":"0x4dcfa4e5a","gasUsed":"0x1860f","logIndex":"0x1d","transactionHash":"0x05712456eca3acb77a524960757240088af05f0ddb2a4fb2a7cd6d8fb2a8880f","transactionIndex":"0x1f"},{"address":"0x6090a6e47849629b7245dfa1ca21d94cd15878ef","topics":["0x0f0c27adfd84b60b6f456b0e87cdccb1e5fb9603991588d87fa99f5b6b61e670","0x7d4646bbc88eccd949cb3aeed6da58f399c61f33beaa210d19dc535f2485801a","0x00000000000000000000000022786eb851d2172bbf8145e0f9a31d217ef19521"],"data":"0x000000000000000000000000000000000000000000000000002386f26fc10000000000000000000000000000000000000000000000000000000000005948d62a","blockNumber":"0x3cfe50","timeStamp":"0x5961e543","gasPrice":"0x4dcfa4e5a","gasUsed":"0x1864f","logIndex":"0x20","transactionHash":"0x085805dcf58263e9b3f78dfcb3fcfc2fdabd664a40040bf0d40870e846d5f57c","transactionIndex":"0x22"}]}

Test with web3 filter (a wrapper to RPC API, "eth.filter" end using the same filter than logFilters )

// block: 3997264 / https://etherscan.io/block/3997264
geth attach --exec 'var logs = []; var filter = web3.eth.filter({ fromBlock: "0x3cfe50", toBlock: "0x3cfe50", address: "0x6090a6e47849629b7245dfa1ca21d94cd15878ef", topics: ["0x0f0c27adfd84b60b6f456b0e87cdccb1e5fb9603991588d87fa99f5b6b61e670", null, null]}); filter.get(function(error, result){ logs.push(result) }); logs' ~/.ethereum/rinkeby/geth.ipc

Expect to get the following log entry back (from https://etherscan.io/tx/0x05712456eca3acb77a524960757240088af05f0ddb2a4fb2a7cd6d8fb2a8880f).

[{'address': '0x6090a6e47849629b7245dfa1ca21d94cd15878ef',
  'blockHash': '0x3f4d8282bcdd2d2637ada6693ac8f2df498475223c2626ca33a16eb56f74e2e0',
  'blockNumber': '0x3cfe50',
  'data': '0x000000000000000000000000000000000000000000000000002386f26fc10000000000000000000000000000000000000000000000000000000000005961c025',
  'logIndex': '0x1d',
  'removed': False,
  'topics': ['0x0f0c27adfd84b60b6f456b0e87cdccb1e5fb9603991588d87fa99f5b6b61e670',
   '0x4981ae698e550a328ba9008e83467cf24b3ab26163ae47bad5d0d109a717fc45',
   '0x0000000000000000000000003fb14ab42b18736fc4b3ec9f88b80fe5829527fb'],
  'transactionHash': '0x05712456eca3acb77a524960757240088af05f0ddb2a4fb2a7cd6d8fb2a8880f',
  'transactionIndex': '0x1f'},
 {'address': '0x6090a6e47849629b7245dfa1ca21d94cd15878ef',
  'blockHash': '0x3f4d8282bcdd2d2637ada6693ac8f2df498475223c2626ca33a16eb56f74e2e0',
  'blockNumber': '0x3cfe50',
  'data': '0x000000000000000000000000000000000000000000000000002386f26fc10000000000000000000000000000000000000000000000000000000000005948d62a',
  'logIndex': '0x20',
  'removed': False,
  'topics': ['0x0f0c27adfd84b60b6f456b0e87cdccb1e5fb9603991588d87fa99f5b6b61e670',
   '0x7d4646bbc88eccd949cb3aeed6da58f399c61f33beaa210d19dc535f2485801a',
   '0x00000000000000000000000022786eb851d2172bbf8145e0f9a31d217ef19521'],
  'transactionHash': '0x085805dcf58263e9b3f78dfcb3fcfc2fdabd664a40040bf0d40870e846d5f57c',
  'transactionIndex': '0x22'}]

Test with rpc call doc: https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_getfilterlogs

curl -X POST --data '{"jsonrpc":"2.0","method":"eth_getFilterLogs","params":["0x16"],"id":74}' https://rinkeby.infura.io/v3/YOURAPIKEY
| improve this answer | |
  • That experiment doesn't demonstrate whether the ToBlock parameter is inclusive. I'd expect the FromBlock to be inclusive however. – Ann Kilzer Dec 31 '19 at 2:51
  • I edited my answer. – Diego Baranowski Dec 31 '19 at 4:37
  • The key was to make FromBlock and ToBlock different values and verify that each block was read. Thanks for getting me started, but the web3 / etherscan stuff is getting further away from geth. – Ann Kilzer Dec 31 '19 at 5:02
  • Also please make sure to remove your API key from the answer and probably regenerate a new one :) – Ann Kilzer Dec 31 '19 at 5:05
  • 1
    Yes the api key was only for test and with a new user. Already deleted. – Diego Baranowski Dec 31 '19 at 5:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.