-
Notifications
You must be signed in to change notification settings - Fork 455
Implement BFT fork choice rules #3725
Implement BFT fork choice rules #3725
Conversation
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.
@limiaspasdaniel A suggestion to make this logic look more readable.
// Case 2
const isValidBlock = (last, current) =>
last.height + 1 === current.height && last.id === current.previousBlock;
// Case 1
const isIdenticalBlock = (last, current) => last.id === current.id;
const isDuplicateBlock = (last, current) =>
last.height === current.height &&
last.heightPrevoted === current.heightPrevoted &&
last.previousBlock === current.previousBlock;
// Case 3
const isDoubleForging = (last, current) =>
isDuplicateBlock(last, current) &&
last.generatorPublicKey === current.generatorPublicKey;
// Case 4
const isTieBlocks = (last, current) =>
isDuplicateBlock(last, current) &&
slots.getSlotNumber(last.timestamp) <
slots.getSlotNumber(current.timestamp) &&
!this._receivedInSlot(last, modules.blocks.lastReceipt.get()) &&
this._receivedInSlot(current, current.timestamp);
// Case 5
const isDifferentChain = (last, current) =>
last.heightPrevoted < current.heightPrevoted ||
(last.height < current.height &&
last.heightPrevoted === current.heightPrevoted);
if(isIdenticalBlock(last, current)){
return this._handleSameBlockReceived(current);
} else if(isValidBlock(last, current)) {
return this._handleGoodBlock(current);
} else if(isDoubleForging(last, current)) {
return this._handleDoubleForging(current);
} else if(isTieBlocks(last, current)) {
return this._handleDoubleForgingTieBreak(current);
}else if(isDifferentChain(last, current)) {
return this._handleMovingToDifferentChain(current);
} else {
return this._handleDiscardedBlock(current);
}
@nazarhussain, regarding your suggestion to make the logic more readable: I like the format and it definitely reads better and clearer, however, I wouldn't nest functions inside functions, this complicates testing. What I can do instead is move those functions as private functions with the following format |
c730343
to
5a39ee9
Compare
framework/test/mocha/unit/modules/chain/submodules/blocks/process.js
Outdated
Show resolved
Hide resolved
fc9ea4c
to
9925aa5
Compare
f00c222
to
a3d45aa
Compare
d01290c
to
5659c57
Compare
5659c57
to
99d9da9
Compare
6ecb34d
to
9286cda
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.
Also. logs take first argument as metadata object and second parameter is the string
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.
Some small feedback. Looks good overall.
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.
Overall LGTM
6131a57
to
eda13f4
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.
Thanks for making the changes!
What was the problem?
New fork choice rules needed to be implemented to comply with the new BFT consensus protocol
How did I fix it?
Implementing Fork Choice Rules according to LIP-0014
How to test it?
Run unit tests for
blocks/process.js
Review checklist