-
Notifications
You must be signed in to change notification settings - Fork 282
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
node: event tree commit #201
Conversation
Codecov Report
@@ Coverage Diff @@
## master #201 +/- ##
==========================================
+ Coverage 52.96% 53.55% +0.58%
==========================================
Files 129 129
Lines 35746 35755 +9
Branches 6026 6029 +3
==========================================
+ Hits 18932 19147 +215
+ Misses 16814 16608 -206
Continue to review full report at Codecov.
|
To be clear, "best" and "main" are synonymous in this context, correct? You used them separately; I want to be sure my understanding is accurate. Are we sure that
|
Safety ReportThe call stack for a block being added to the chain is as follows: The logic inside of The relevant codepath: Lines 2008 to 2021 in e1c0281
If the incoming block's chainwork is less than or equal to the current tips chainwork, then Lines 1611 to 1625 in e1c0281
You can see here that the jsdoc is correct regarding $ basename $PWD
hsd
$ grep -rn setBestChain lib
lib/blockchain/chain.js:1464: // That will be done outside in setBestChain.
lib/blockchain/chain.js:1624: async setBestChain(entry, block, prev, flags) {
lib/blockchain/chain.js:2020: await this.setBestChain(entry, block, prev, flags); After doing this review, I believe that it is safe to include the event emitting exactly where it is Additional WorkI think that reorgs should trigger the event
A Lines 1605 to 1606 in e1c0281
This will ensure that the event is emitted when reorgs happen and different tree roots are committed. |
Yes that is what I meant. Sorry for using two words to mean the same thing.
I agree with the analysis and laid out evidence above. Will update the commit appropriately. |
3b51b24
to
2c61aeb
Compare
To make things clear: any time |
Just pushed up a fix to the jsdoc.
What do you think of having high level |
Emit an event named 'tree commit' from the chain on the 'chain' topic. It emits three items, the newly comitted tree root (which is not included in a block until the next block) along with the current block and the chain entry. This happens in the `setBestChain` method on the chain. The computation to determine the event is: ``` if ((entry.height % this.network.names.treeInterval) === 0) ``` The call stack for a block being added to the chain is as follows: - `Chain.add` - [source](https://github.com/handshake-org/hsd/blob/e1c0281dd9e9bbbd23f5cd832c82ebe3808e9b4f/lib/blockchain/chain.js#L1885) - `Chain._add` - [source](https://github.com/handshake-org/hsd/blob/e1c0281dd9e9bbbd23f5cd832c82ebe3808e9b4f/lib/blockchain/chain.js#L1904) - `Chain.connect` - [source](https://github.com/handshake-org/hsd/blob/e1c0281dd9e9bbbd23f5cd832c82ebe3808e9b4f/lib/blockchain/chain.js#L1979) - `Chain.setBestChain` - [source](https://github.com/handshake-org/hsd/blob/e1c0281dd9e9bbbd23f5cd832c82ebe3808e9b4f/lib/blockchain/chain.js#L1624) The logic inside of `Chain.connect` determines if `Chain. setBestChain` will be called or if `Chain.saveAlternate` is called. The relevant codepath: https://github.com/handshake-org/hsd/blob/e1c0281dd9e9bbbd23f5cd832c82ebe3808e9b4f/lib/blockchain/chain.js#L2008-L2021 If the incoming block's chainwork is less than or equal to the current tips chainwork, then `Chain.saveAlternate` is called. The only way that `Chain.setBestChain` is called is if the incoming block has greater chain work. This means that the jsdoc is __incorrect__ for `setBestChain` regarding the line `"This is called on every valid block that comes in."`. It is only called for blocks that extend the current chain and not for alternative blocks. https://github.com/handshake-org/hsd/blob/e1c0281dd9e9bbbd23f5cd832c82ebe3808e9b4f/lib/blockchain/chain.js#L1611-L1625 You can see here that the jsdoc is correct regarding `setBestChain` as a private function. The only place that it is invoked is withing `lib/blockchain/chain.js` inside of `Chain.connect`. ```bash $ basename $PWD hsd $ grep -rn setBestChain lib lib/blockchain/chain.js:1464: // That will be done outside in setBestChain. lib/blockchain/chain.js:1624: async setBestChain(entry, block, prev, flags) { lib/blockchain/chain.js:2020: await this.setBestChain(entry, block, prev, flags); ``` After doing this review, I believe that it is safe to include the event emitting exactly where it is currently in this pull request. `Chain.setBestChain` is not called for every block, just blocks extending the best chain.
This adds the `"tree commit"` event to `Chain.reconnect` which is part of the codepath for reorgs. This way the event is triggered when reorgs occur. Blocks only ever go through `setBestChain` once, every other time that they are reorged out and then reincluded in the chain, it will be through the `reconnect` method.
Emit an event over websockets for the chain's 'tree commit' event. NodeClient or any websocket client can listen to these events.
6424f89
to
5a4c491
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is good to go after you handle some commenting nits in the test.
5a4c491
to
473dbe6
Compare
@boymanjor Nits are addressed, thanks for the review. I removed the comments in the test since it reads fairly straightforward. |
Emit an event over websockets for each time that a new tree root is set.
The event is
tree commit
on the topicchain
.The event is added to
chain.setBestChain
which is called on every valid block that comes in. This means that this event could be triggered when a block that is not part of the best chain is received by the node.Should we add additional information regarding if it is part of the best chain?
Or only make it so that the event is triggered when its part of the main chain?