17

I want to monitor accounts for incoming transactions (RPC/IPC API) and call a function whenever any one of a number of monitored accounts receives ether which is "confirmed", as in, "sufficiently many blocks ago that the chances of double-spending are negligible".

Is there a reference implementation of this?

I'm looking into filters and the words "pending" and "latest" keep cropping up. What do they mean?

2 Answers 2

11

Latest means the latest block that is already inside your own chain. All transactions contained within can be considered successfully executed. Security wise of course there can be reorgs, but in general they are executed transactions.

Pending on the other hand is the collection of transactions that can be executed by the network (that your own node knows about), but have not yet been done so. Pending is useful to show reactive UIs where the UI can immediately show that something's inbound, although there are no guarantees when those transactions - if ever - will successfully run.

5
  • What do you mean "your own chain", exactly? Also "security wise of course there can be reorgs" -- exactly, so how do I measure/mitigate this?
    – spraff
    Commented Apr 27, 2016 at 10:27
  • @spraff Your local node maintains its own copy of the blockchain; "latest" means it's in the head block of that chain. You can mitigate the possibility of reorgs by counting how many blocks from the head of the chain the TX in question is, and not considering it fully accepted until it's some number of blocks below the head (shown as "confirmations", typically). Commented Apr 27, 2016 at 10:40
  • Ethereum's a decentralized system, so different nodes may see the system in a different state: e.g. when you are catching up/syncing, you only see an older state until you catch up. Further, blockchains are called eventually consistent, meaning that eventually all nodes agree on a state, but the current actual state my be different between them. You local chain is what your own node believes to be the current state. With regards to reorgs, it depends on the funds you work with: make sure it costs more to do a deep reorg (pow) than what can be gained with it. Commented Apr 27, 2016 at 10:43
  • E.g. if you are swapping a few dollars worth of ether (e.g. a coffee), then even a small reorg costs way too much to make it worth it. If you swap millions, then you might want to wait for quite a few blocks to ensure someone won't double spend that much money as it may easily be worth it to deploy a cluster of GPUs to try and revert that transfer. Commented Apr 27, 2016 at 10:45
  • So what's the criterion, something like $reorg < k*$amount*2^confirmations I assume? If so, do we have a rough estimate for the coefficient?
    – spraff
    Commented Apr 27, 2016 at 12:11
2

For ideas on a "reference implementation", see: How can a DApp detect a fork or chain reorganization using web3.js or additional libraries?

You probably also want to know: What number of confirmations is considered secure in Ethereum?


@Péter's answer is great and only thing to add is web3.eth.defaultBlock definition:

"latest", the latest block (current head of the blockchain)

"pending", the currently mined block (including pending transactions)

pending equals latest plus pending transactions (those that haven't been mined into a block).

2
  • This is a poor definition, which needs to be updated. As Peter mentioned, "pending" includes all known transactions in the mem pool, not just what could fit in the currently mined block, especially since most nodes are not miners. Commented Sep 7, 2021 at 22:35
  • @GViz That's a fair comment and I think they will be open to a PR to modify: github.com/ChainSafe/web3.js/blob/1.x/docs/web3-eth.rst#L193
    – eth
    Commented Sep 12, 2021 at 5:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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