Skip to content

Commit

Permalink
Introduce gpbft.Instant to capture precise progress moment (#698)
Browse files Browse the repository at this point in the history
In a number of places, namely rebroadcast request and progress we need
information a precise moment in GPBFT progress. Prior to changes here
the changes were communicated as three separate values, which was
cumbersome to work with as the usage of this information increased with
rebroadcast.

Introduce `Instant` as a struct to capture precise instant in GPBFT
progress and reflect changes across `Progress` as well as
`RequestRebroadcast`.

Relates to #691 (comment)
  • Loading branch information
masih authored Oct 7, 2024
1 parent 23ce86a commit 0bec6d5
Show file tree
Hide file tree
Showing 14 changed files with 178 additions and 197 deletions.
4 changes: 2 additions & 2 deletions emulator/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ func (h *driverHost) RequestBroadcast(mb *gpbft.MessageBuilder) error {
return nil
}

func (h *driverHost) RequestRebroadcast(instance, round uint64, phase gpbft.Phase) error {
message, found := h.messages.Get(instance, round, phase)
func (h *driverHost) RequestRebroadcast(instant gpbft.Instant) error {
message, found := h.messages.Get(instant)
if found {
h.receivedBroadcasts = append(h.receivedBroadcasts, message)
}
Expand Down
25 changes: 8 additions & 17 deletions emulator/message_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,25 @@ package emulator

import "github.com/filecoin-project/go-f3/gpbft"

type MessageKey struct {
Instance, Round uint64
Phase gpbft.Phase
}

// MessageCache is a repository of messages keyed by their instance, round and
// phase. This cache is used for testing purposes only and has no eviction
// strategy. It is primarily used to store messages from self for rebroadcast.
type MessageCache map[MessageKey]*gpbft.GMessage
type MessageCache map[gpbft.Instant]*gpbft.GMessage

func NewMessageCache() MessageCache {
return make(map[MessageKey]*gpbft.GMessage)
return make(map[gpbft.Instant]*gpbft.GMessage)
}

func (mc MessageCache) Get(instance, round uint64, phase gpbft.Phase) (*gpbft.GMessage, bool) {
msg, found := mc[MessageKey{
Instance: instance,
Round: round,
Phase: phase,
}]
func (mc MessageCache) Get(instant gpbft.Instant) (*gpbft.GMessage, bool) {
msg, found := mc[instant]
return msg, found
}

func (mc MessageCache) PutIfAbsent(msg *gpbft.GMessage) bool {
key := MessageKey{
Instance: msg.Vote.Instance,
Round: msg.Vote.Round,
Phase: msg.Vote.Phase,
key := gpbft.Instant{
ID: msg.Vote.Instance,
Round: msg.Vote.Round,
Phase: msg.Vote.Phase,
}
if _, found := mc[key]; found {
return false
Expand Down
4 changes: 2 additions & 2 deletions f3.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,9 +415,9 @@ func (m *F3) GetPowerTable(ctx context.Context, ts gpbft.TipSetKey) (gpbft.Power
return nil, fmt.Errorf("no known network manifest")
}

func (m *F3) Progress() (instance, round uint64, phase gpbft.Phase) {
func (m *F3) Progress() (instant gpbft.Instant) {
if st := m.state.Load(); st != nil && st.runner != nil {
instance, round, phase = st.runner.Progress()
instant = st.runner.Progress()
}
return
}
10 changes: 9 additions & 1 deletion gpbft/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ import (
"time"
)

// Instant represents a particular moment in the progress of GPBFT, captured by
// instance ID, round and phase.
type Instant struct {
ID uint64
Round uint64
Phase Phase
}

type MessageValidator interface {
// Validates a Granite message.
// An invalid message can never become valid, so may be dropped.
Expand Down Expand Up @@ -97,7 +105,7 @@ type Network interface {
// RequestRebroadcast requests that a message at given instance, round and phase
// previously broadcasted via RequestBroadcast be rebroadcasted. Rebroadcast
// requests for messages that have not been broadcasted are silently ignored.
RequestRebroadcast(instance, round uint64, phase Phase) error
RequestRebroadcast(instant Instant) error
}

type Clock interface {
Expand Down
Loading

0 comments on commit 0bec6d5

Please sign in to comment.