Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
corverroos committed May 10, 2022
1 parent ed97102 commit cc966e8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 30 deletions.
40 changes: 13 additions & 27 deletions core/qbft/qbft.go
Original file line number Diff line number Diff line change
Expand Up @@ -630,52 +630,38 @@ func getFPlus1RoundChanges[I any, V comparable](d Definition[I, V], buffer []Msg
return resp, true
}

// prepareSet defines a set of PREPARE messages (one per process)
// with identical round and value.
type prepareSet[I any, V comparable] struct {
// preparedKey defines the round and value of set of identical PREPARE messages.
type preparedKey[I any, V comparable] struct {
round int64
value V
msgs map[int64]Msg[I, V] // map[process]Msg
}

// getPrepareQuorums returns all sets of quorum PREPARE messages
// with identical rounds and values.
func getPrepareQuorums[I any, V comparable](d Definition[I, V], buffer []Msg[I, V]) [][]Msg[I, V] {
var sets []prepareSet[I, V]
for _, msg := range flatten(buffer) { // Flatten to get PREPARES included as ROUND-CHANGE justifications.
sets := make(map[preparedKey[I, V]]map[int64]Msg[I, V]) // map[preparedKey]map[process]Msg
for _, msg := range flatten(buffer) { // Flatten to get PREPARES included as ROUND-CHANGE justifications.
if msg.Type() != MsgPrepare {
continue
}

var found bool
for _, s := range sets {
if s.round != msg.Round() || s.value != msg.Value() {
continue
}
s.msgs[msg.Source()] = msg
found = true

break
}
if found {
continue
key := preparedKey[I, V]{round: msg.Round(), value: msg.Value()}
msgs, ok := sets[key]
if !ok {
msgs = make(map[int64]Msg[I, V])
}

sets = append(sets, prepareSet[I, V]{
round: msg.Round(),
value: msg.Value(),
msgs: map[int64]Msg[I, V]{msg.Source(): msg},
})
msgs[msg.Source()] = msg
sets[key] = msgs
}

// Return all quorums
var quorums [][]Msg[I, V]
for _, set := range sets {
if len(set.msgs) < d.Quorum() {
for _, msgs := range sets {
if len(msgs) < d.Quorum() {
continue
}
var quorum []Msg[I, V]
for _, msg := range set.msgs {
for _, msg := range msgs {
quorum = append(quorum, msg)
}
quorums = append(quorums, quorum)
Expand Down
3 changes: 0 additions & 3 deletions core/qbft/qbft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ import (
"github.com/obolnetwork/charon/core/qbft"
)

// Suggest running tests continuously until cancelled with Ctrl-C.
//go:generate while go test . -count=1 -timeout=5s; do; done

func TestQBFT(t *testing.T) {
t.Run("happy 0", func(t *testing.T) {
testQBFT(t, test{
Expand Down

0 comments on commit cc966e8

Please sign in to comment.