[Ethereum] web3.eth.getBlock(‘pending’) returns latest block, not pending block, is that expected

json-rpcparityweb3js

I am trying to use web3 or JSON-RPC to get a list of pending transactions.

The web3 API docs say that a call to web3.eth.getBlock('pending') will return a structure showing the block number and hash to be null. The answer to this question agrees and says that block.transactions will have pending transactions. But I see non-null values for the number and hash fields that match the latest block and a list of transactions that were included in that block. In fact, web3.eth.getBlock('pending') and web3.eth.getBlock('latest') return the same result. For example, when running a local parity node (Parity/v1.4.7-beta-f2058bd-20161227):

> let Web3 = require('web3');
> let web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
> blockL = web3.eth.getBlock('latest');
> blockP = web3.eth.getBlock('pending');
> [blockL.number, blockL.hash]
[ 2971275, '0x2187511e60d49a5ed081ccc31ee4f76365727254e9001f4b563c47cee457ed3f' ]
> [blockP.number, blockP.hash]
[ 2971275, '0x2187511e60d49a5ed081ccc31ee4f76365727254e9001f4b563c47cee457ed3f' ]

I tried making the JSON-RPC call directly and also got the latest block, not information about the pending one:

$ curl -s -X POST -H "Content-Type: application/json" -d '{"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["pending",false],"id":0}' localhost:8545
  { "result" : {
       ... ,
       "number" : "0x2d4c56", 
       ...
       "hash" : "0xb0d47255dbeae836384321b1141ae221254fa7ee4ded07f3d49afb875f05a44e"
       ... }
  }

I also tried using an Infura RPC node and got similar results.

Do I misunderstand what pending is supposed to mean? Is there another way I should use web3 or JSON-RPC to get the list of pending transactions?

(When I use web3.eth.filter('pending') I do see pending transactions as they arrive at my node, but that is different from asking for the current pending set.)

Best Answer

This answer suggests to me that web3.eth.getBlock('pending') is only relevant if you are mining. Are you?

If not, then yes.. returning the currently mined block seems to be expected behaviour.

Related Topic