Skip to content
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

dbft: replace boring cycle with fancy condition #123

Merged
merged 1 commit into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 5 additions & 12 deletions dbft.go
Original file line number Diff line number Diff line change
Expand Up @@ -605,19 +605,12 @@ func (d *DBFT[H]) onCommit(msg ConsensusPayload[H]) {

func (d *DBFT[H]) onRecoveryRequest(msg ConsensusPayload[H]) {
if !d.CommitSent() && (!d.isAntiMEVExtensionEnabled() || !d.PreCommitSent()) {
// Limit recoveries to be sent from no more than F nodes
// TODO replace loop with a single if
shouldSend := false

for i := 1; i <= d.F()+1; i++ {
ind := (int(msg.ValidatorIndex()) + i) % len(d.Validators)
if ind == d.MyIndex {
shouldSend = true
break
}
}
// Ignore the message if our index is not in F+1 range of the
// next (%N) ones from the sender. This limits recovery
// messages to be broadcasted through the network and F+1
// guarantees that at least one node responds.

if !shouldSend {
if (d.MyIndex-int(msg.ValidatorIndex())+d.N()-1)%d.N() > d.F() {
return
}
}
Expand Down
47 changes: 47 additions & 0 deletions dbft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,53 @@ func TestDBFT_OnReceiveRecoveryRequest(t *testing.T) {
})
}

func TestDBFT_OnReceiveRecoveryRequestResponds(t *testing.T) {
type recoveryset struct {
nodes int
sender int
receiver int
replies bool
}
var params []recoveryset

for _, nodes := range []int{4, 5, 7, 10} { // 5 is a bad BFT number, but we want to test the logic anyway.
for sender := 0; sender < nodes; sender++ {
for recv := 0; recv < nodes; recv++ {
params = append(params, recoveryset{nodes, sender, recv, false})

for i := 1; i <= ((nodes-1)/3)+1; i++ {
ind := (sender + i) % nodes
if ind == recv {
params[len(params)-1].replies = true
break
}
}
}
}
}

for _, param := range params {
t.Run(fmt.Sprintf("%d nodes, %d sender, %d receiver", param.nodes, param.sender, param.receiver), func(t *testing.T) {
s := newTestState(param.receiver, param.nodes)
s.currHeight = 1
service, _ := dbft.New[crypto.Uint256](s.getOptions()...)
service.Start(uint64(param.receiver))

_ = s.tryRecv() // Flush the queue if primary.

rr := s.getRecoveryRequest(uint16(param.sender))
service.OnReceive(rr)
rm := s.tryRecv()
if param.replies {
require.NotNil(t, rm)
require.Equal(t, dbft.RecoveryMessageType, rm.Type())
} else {
require.Nil(t, rm)
}
})
}
}

func TestDBFT_OnReceiveChangeView(t *testing.T) {
s := newTestState(2, 4)
t.Run("change view correctly", func(t *testing.T) {
Expand Down
Loading