I use wss (eth_subscribe) to subscribe logs. Sometime I receive logs with the removed property set to true.
I want to known is it true that only the latest logs can be removed?
Suppose I already received following logs:
log1 -> log2 -> log3 -> log4 -> log5
If I receive log3 with removed property set to true, I will definitely receive log4/log5 with removed property set to true also? In other words, only the latest logs can be removed?
If so, is there a determinate order of the removed logs? Remove log3 firstly, or remove log5 firstly?
Hi @hello thanks for joining our community. This somewhat complicated question based on the structure of logs and blocks on Ethereum. Technically logs belong to blocks and therefore if a block containing log 3 is reorged, you are correct that logs 4 and 5 would also be reorged. Logs can also be re-added in the reorged blocks but as members of a different block, possibly with the same block number but definitely with a different block hash.
Is there somewhere in your code that you are managing for reorgs and is it possible to add your logs requests there?
Thanks for your answer. I’m creating a dapp. I don’t know how to rollback my business changes after receiving log with removed property set to true.
Suppose, I do following changes in mysql table:
field X: change from value 100 to value 101
field Y: change from value 200 to value 201
field X: change from value 101 to value 102
field Z: change from value 300 to value 102
field Y: change from value 201 to value 203
After log3/log4/log5 removed, I expect field X/Y/Z rollback to its original values(100/200/300).
I can record old value before make change. It’s useful when receive removed logs.
If I receive removed logs in following order:
log5(removed) -> log4(removed) -> log3(removed)
It easy to rollback field X/Y/Z to its original values, just undo it in sequence.
However, If I receive removed logs in following order:
log3(removed) -> log4(removed) -> log5(removed)
It hard to rollback field X/Y/Z to its original values (The value of field X may be rollback to 101 when rollback log4).
What the exact order of removed logs if log3/log4/log5 is removed when chain reorganization?
Since it sounds like you are persisting these logs in a database where you could also include the block number and block hash associated with each log entry, I would recommend that if you get a
log removed event, you remove all logs from that block and any blocks after that block. I think this is the safest process to ensure the integrity of your accounting system.
Logs from Block A are added to the database
Logs from Block B are added to the database
Logs from Block C are added to the database
Logs from Block D are added to the database
Log removal event for a log event contained in Block B
Remove all logs from B,C, and D
Depending on the structure of your database you could potentially structure it in a way where the database can handle the cascading delete for you. For example a cascading delete of Block B logs will also delete the logs of Block C and Block D so your code does not have to do it explicitly. This of course depends on the structure of your database schema.
They key is that the removal of log events contained in a block are done atomically for a given block. The specific order of log event removals within a block shouldn’t matter at that point. Think about it as blocks not individual log events.
I hope that helps.