1

We have a service that pulls operations from a tezos node and inserts into our own datastore. I am wondering how we would detect a reorg using the node RPC. Or is there some algorithm we should use on the most recent N blocks in our cache?

4
  • I understand that Tezos will not accept a branch that is 5 cycles old - but since a cycle is 4096 blocks and there is 1 block per minute that would be a lot of blocks to re-import and a very long time to wait for operation finality and make the system unusable. So there must be something else going on here to determine reorgs. Aug 21, 2020 at 14:09
  • AFAIK, there is no API that tells you this. You have to simply "travel backwards" for each new block you receive. Block->predecessor and verify everything is still in order. If you find out that a predecessor does not match, then you have a reorg.
    – utdrmac
    Aug 21, 2020 at 14:13
  • I am not sure how block hashes are created. Is it reasonable to assume that if I read a block and the previous hash does not match what I think it should be then I need to walk backwards until I find a common ancestor? So that would mean that I only need to care about the most recent 2 blocks to detect a change? Aug 24, 2020 at 18:27
  • Each block metadata contains the hash of itself and the hash of the block before it (predecessor). You don't have to calculate the hash yourself. You probably need to care about more than the most recent 2 blocks, though. Technically, Tezos allows for chain reorg up to 5 cycles in the past, though I'm not sure how that would actually happen.
    – utdrmac
    Aug 25, 2020 at 13:52

1 Answer 1

2

There is the "streaming" RPC /monitor/heads/[chain_id] that tells you every time the node changes its head.

It returns a stream of JSON objects looking like {"hash":[...],"level":[...],"proto":[...],"predecessor":[...],"timestamp":[...],"validation_pass":4,[...]}

When no reorg occurred, the field predecessor is equals to the field hash of its predecessor in the stream. As soon as you see {"hash":foo,"level":[...],[...]}{"hash":[...],[...],"predecessor":bar,[...]} you spotted a reorg!

If it is a problem to parse a never terminating streaming RPC, you can hijack as the "alarm of head change" the (also streaming but this time terminating and you don't care about reading its body) RPC /chains/[chain_id]/mempool/monitor_operations as it has the property to terminates each time the node changes it head and extract information about the block hash and its predecessor from /chains/[chain_id]/blocks/head/header...

1
  • You don't even need to look for a change in the hashes, any non-strictly increasing change in level is a reorg.
    – Arthur B
    Aug 26, 2020 at 15:39

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.