Skip to content

Commit

Permalink
core/infosync: prioritise proposal types (#1731)
Browse files Browse the repository at this point in the history
Includes local proposal type in infosync protocol. Note the output isn't used yet.

category: feature
ticket: #1652
  • Loading branch information
corverroos authored Feb 1, 2023
1 parent ec8f9be commit 8fc6cd6
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 3 deletions.
20 changes: 19 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,11 @@ func wirePrioritise(ctx context.Context, conf Config, life *lifecycle.Manager, t
return err
}

sync := infosync.New(prio, version.Supported(), Protocols())
sync := infosync.New(prio,
version.Supported(),
Protocols(),
ProposalTypes(conf.BuilderAPI, conf.SyntheticBlockProposals),
)

// Trigger info syncs in last slot of the epoch (for the next epoch).
sched.SubscribeSlots(func(ctx context.Context, slot core.Slot) error {
Expand Down Expand Up @@ -884,3 +888,17 @@ func Protocols() []protocol.ID {

return resp
}

// ProposalTypes returns the local proposal types in order of precedence.
func ProposalTypes(builder bool, synthetic bool) []core.ProposalType {
var resp []core.ProposalType
if builder {
resp = append(resp, core.ProposalTypeBuilder)
}
if synthetic {
resp = append(resp, core.ProposalTypeSynthetic)
}
resp = append(resp, core.ProposalTypeFull) // Always support full as fallback.

return resp
}
1 change: 1 addition & 0 deletions app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ func (a *priorityAsserter) Callback(t *testing.T, i int) func(ctx context.Contex
expect := map[string]string{
"version": fmt.Sprint(version.Supported()),
"protocol": fmt.Sprint(app.Protocols()),
"proposal": fmt.Sprint(app.ProposalTypes(false, false)),
}

if !assert.Len(t, results, len(expect)) {
Expand Down
46 changes: 44 additions & 2 deletions core/infosync/infosync.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,16 @@ import (
const (
topicVersion = "version"
topicProtocol = "protocol"
topicProposal = "proposal"

// maxResults limits the number of results to keep.
maxResults = 100
)

// New returns a new infosync component.
func New(prioritiser *priority.Component, versions []string, protocols []protocol.ID) *Component {
func New(prioritiser *priority.Component, versions []string, protocols []protocol.ID,
proposals []core.ProposalType,
) *Component {
// Add a mock alpha protocol if alpha features enabled in order to test infosync in prod.
// TODO(corver): Remove this once we have an actual use case.
if featureset.Enabled(featureset.MockAlpha) {
Expand All @@ -50,6 +53,7 @@ func New(prioritiser *priority.Component, versions []string, protocols []protoco
prioritiser: prioritiser,
versions: versions,
protocols: protocols,
proposals: proposals,
}

prioritiser.Subscribe(func(ctx context.Context, duty core.Duty, results []priority.TopicResult) error {
Expand All @@ -64,6 +68,8 @@ func New(prioritiser *priority.Component, versions []string, protocols []protoco
res.versions = append(res.versions, prio)
case topicProtocol:
res.protocols = append(res.protocols, protocol.ID(prio))
case topicProposal:
res.proposals = append(res.proposals, core.ProposalType(prio))
}
}
}
Expand All @@ -84,6 +90,7 @@ type Component struct {
prioritiser *priority.Component
versions []string
protocols []protocol.ID
proposals []core.ProposalType

mu sync.Mutex
results []result
Expand All @@ -108,6 +115,25 @@ func (c *Component) Protocols(slot int64) []protocol.ID {
return resp
}

// Proposals returns the latest cluster wide supported proposal types before the slot.
// It returns the default "full" proposal type if no results before the slot are available.
func (c *Component) Proposals(slot int64) []core.ProposalType {
c.mu.Lock()
defer c.mu.Unlock()

resp := []core.ProposalType{core.ProposalTypeFull} // Default to "full" proposals.

for _, result := range c.results {
if result.slot > slot {
break
}

resp = result.proposals
}

return resp
}

// addResult adds the result to the results if it is different from the last result.
func (c *Component) addResult(result result) {
c.mu.Lock()
Expand Down Expand Up @@ -135,6 +161,10 @@ func (c *Component) Trigger(ctx context.Context, slot int64) error {
priority.TopicProposal{
Topic: topicProtocol,
Priorities: protocolsToStrings(c.protocols),
},
priority.TopicProposal{
Topic: topicProposal,
Priorities: proposalsToStrings(c.proposals),
})
}

Expand All @@ -148,16 +178,28 @@ func protocolsToStrings(features []protocol.ID) []string {
return resp
}

// proposalsToStrings returns the protocols as strings.
func proposalsToStrings(proposals []core.ProposalType) []string {
var resp []string
for _, proposal := range proposals {
resp = append(resp, string(proposal))
}

return resp
}

// result is a cluster-wide agreed-upon infosync result.
type result struct {
slot int64
versions []string
protocols []protocol.ID
proposals []core.ProposalType
}

// Equal returns true if the results are equal.
func (x result) Equal(y result) bool {
return x.slot == y.slot &&
fmt.Sprint(x.versions) == fmt.Sprint(y.versions) &&
fmt.Sprint(x.protocols) == fmt.Sprint(y.protocols)
fmt.Sprint(x.protocols) == fmt.Sprint(y.protocols) &&
fmt.Sprint(x.proposals) == fmt.Sprint(y.proposals)
}
14 changes: 14 additions & 0 deletions core/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,20 @@ func (d Duty) String() string {
return fmt.Sprintf("%d/%s", d.Slot, d.Type)
}

// ProposalType defines a tyoe of block proposal process.
type ProposalType string

const (
// Note these ProposalType strings MUST NOT be changed as it will break wire compatibility.

// ProposalTypeFull defines normal full beacon block proposals.
ProposalTypeFull ProposalType = "full"
// ProposalTypeBuilder defines builder API blinded beacon block proposals.
ProposalTypeBuilder ProposalType = "builder"
// ProposalTypeSynthetic defines synthetic block proposals, can be either full or builder.
ProposalTypeSynthetic ProposalType = "synthetic"
)

// NewAttesterDuty returns a new attester duty. It is a convenience function that is
// slightly more readable and concise than the struct literal equivalent:
//
Expand Down

0 comments on commit 8fc6cd6

Please sign in to comment.