Skip to content

Commit

Permalink
only use slot for querying proposals
Browse files Browse the repository at this point in the history
  • Loading branch information
xenowits committed Oct 27, 2023
1 parent 2107bfc commit 0d1921d
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 28 deletions.
8 changes: 4 additions & 4 deletions core/dutydb/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,14 @@ func (db *MemDB) Store(_ context.Context, duty core.Duty, unsignedSet core.Unsig
}

// AwaitProposal implements core.DutyDB, see its godoc.
func (db *MemDB) AwaitProposal(ctx context.Context, opts *eth2api.ProposalOpts) (*eth2api.VersionedProposal, error) {
func (db *MemDB) AwaitProposal(ctx context.Context, slot int64) (*eth2api.VersionedProposal, error) {
cancel := make(chan struct{})
defer close(cancel)
response := make(chan *eth2api.VersionedProposal, 1)

db.mu.Lock()
db.proQueries = append(db.proQueries, proQuery{
Key: int64(opts.Slot),
Key: slot,
Response: response,
Cancel: cancel,
})
Expand All @@ -180,14 +180,14 @@ func (db *MemDB) AwaitProposal(ctx context.Context, opts *eth2api.ProposalOpts)
}

// AwaitBlindedProposal implements core.DutyDB, see its godoc.
func (db *MemDB) AwaitBlindedProposal(ctx context.Context, opts *eth2api.BlindedProposalOpts) (*eth2api.VersionedBlindedProposal, error) {
func (db *MemDB) AwaitBlindedProposal(ctx context.Context, slot int64) (*eth2api.VersionedBlindedProposal, error) {
cancel := make(chan struct{})
defer close(cancel)
response := make(chan *eth2api.VersionedBlindedProposal, 1)

db.mu.Lock()
db.builderProQueries = append(db.builderProQueries, builderProQuery{
Key: int64(opts.Slot),
Key: slot,
Response: response,
Cancel: cancel,
})
Expand Down
5 changes: 2 additions & 3 deletions core/dutydb/memory_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"context"
"testing"

eth2api "github.com/attestantio/go-eth2-client/api"
eth2p0 "github.com/attestantio/go-eth2-client/spec/phase0"
"github.com/stretchr/testify/require"

Expand All @@ -28,10 +27,10 @@ func TestCancelledQueries(t *testing.T) {
_, err = db.AwaitAggAttestation(ctx, slot, eth2p0.Root{})
require.ErrorContains(t, err, "shutdown")

_, err = db.AwaitProposal(ctx, &eth2api.ProposalOpts{Slot: slot})
_, err = db.AwaitProposal(ctx, slot)
require.ErrorContains(t, err, "shutdown")

_, err = db.AwaitBlindedProposal(ctx, &eth2api.BlindedProposalOpts{Slot: slot})
_, err = db.AwaitBlindedProposal(ctx, slot)
require.ErrorContains(t, err, "shutdown")

_, err = db.AwaitSyncContribution(ctx, slot, 0, eth2p0.Root{})
Expand Down
6 changes: 3 additions & 3 deletions core/dutydb/memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestShutdown(t *testing.T) {

errChan := make(chan error, 1)
go func() {
_, err := db.AwaitProposal(context.Background(), &eth2api.ProposalOpts{Slot: 999})
_, err := db.AwaitProposal(context.Background(), 999)
errChan <- err
}()

Expand Down Expand Up @@ -139,7 +139,7 @@ func TestMemDBProposer(t *testing.T) {
for i := 0; i < queries; i++ {
awaitResponse[i] = make(chan response)
go func(slot int) {
block, err := db.AwaitProposal(ctx, &eth2api.ProposalOpts{Slot: eth2p0.Slot(slots[slot])})
block, err := db.AwaitProposal(ctx, slots[slot])
require.NoError(t, err)
awaitResponse[slot] <- response{block: block}
}(i)
Expand Down Expand Up @@ -387,7 +387,7 @@ func TestMemDBBuilderProposer(t *testing.T) {
for i := 0; i < queries; i++ {
awaitResponse[i] = make(chan response)
go func(slot int) {
block, err := db.AwaitBlindedProposal(ctx, &eth2api.BlindedProposalOpts{Slot: eth2p0.Slot(slots[slot])})
block, err := db.AwaitBlindedProposal(ctx, slots[slot])
require.NoError(t, err)
awaitResponse[slot] <- response{block: block}
}(i)
Expand Down
20 changes: 10 additions & 10 deletions core/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ type DutyDB interface {

// AwaitProposal blocks and returns the proposed beacon block
// for the slot when available.
AwaitProposal(ctx context.Context, opts *eth2api.ProposalOpts) (*eth2api.VersionedProposal, error)
AwaitProposal(ctx context.Context, slot int64) (*eth2api.VersionedProposal, error)

// AwaitBlindedProposal blocks and returns the proposed blinded beacon block
// for the slot when available.
AwaitBlindedProposal(ctx context.Context, opts *eth2api.BlindedProposalOpts) (*eth2api.VersionedBlindedProposal, error)
AwaitBlindedProposal(ctx context.Context, slot int64) (*eth2api.VersionedBlindedProposal, error)

// AwaitAttestation blocks and returns the attestation data
// for the slot and committee index when available.
Expand Down Expand Up @@ -83,11 +83,11 @@ type Consensus interface {

// ValidatorAPI provides a beacon node API to validator clients. It serves duty data from the DutyDB and stores partial signed data in the ParSigDB.
type ValidatorAPI interface {
// RegisterAwaitProposal registers a function to query unsigned beacon block proposals by providing necessary options.
RegisterAwaitProposal(func(ctx context.Context, opts *eth2api.ProposalOpts) (*eth2api.VersionedProposal, error))
// RegisterAwaitProposal registers a function to query unsigned beacon block proposals by providing the slot.
RegisterAwaitProposal(func(ctx context.Context, slot int64) (*eth2api.VersionedProposal, error))

// RegisterAwaitBlindedProposal registers a function to query unsigned blinded beacon block proposals by providing necessary options.
RegisterAwaitBlindedProposal(func(ctx context.Context, opts *eth2api.BlindedProposalOpts) (*eth2api.VersionedBlindedProposal, error))
// RegisterAwaitBlindedProposal registers a function to query unsigned blinded beacon block proposals by providing the slot.
RegisterAwaitBlindedProposal(func(ctx context.Context, slot int64) (*eth2api.VersionedBlindedProposal, error))

// RegisterAwaitAttestation registers a function to query attestation data.
RegisterAwaitAttestation(func(ctx context.Context, slot, commIdx int64) (*eth2p0.AttestationData, error))
Expand Down Expand Up @@ -216,16 +216,16 @@ type wireFuncs struct {
ConsensusPropose func(context.Context, Duty, UnsignedDataSet) error
ConsensusSubscribe func(func(context.Context, Duty, UnsignedDataSet) error)
DutyDBStore func(context.Context, Duty, UnsignedDataSet) error
DutyDBAwaitProposal func(ctx context.Context, opts *eth2api.ProposalOpts) (*eth2api.VersionedProposal, error)
DutyDBAwaitBlindedProposal func(ctx context.Context, opts *eth2api.BlindedProposalOpts) (*eth2api.VersionedBlindedProposal, error)
DutyDBAwaitProposal func(ctx context.Context, slot int64) (*eth2api.VersionedProposal, error)
DutyDBAwaitBlindedProposal func(ctx context.Context, slot int64) (*eth2api.VersionedBlindedProposal, error)
DutyDBAwaitAttestation func(ctx context.Context, slot, commIdx int64) (*eth2p0.AttestationData, error)
DutyDBPubKeyByAttestation func(ctx context.Context, slot, commIdx, valCommIdx int64) (PubKey, error)
DutyDBAwaitAggAttestation func(ctx context.Context, slot int64, attestationRoot eth2p0.Root) (*eth2p0.Attestation, error)
DutyDBAwaitSyncContribution func(ctx context.Context, slot, subcommIdx int64, beaconBlockRoot eth2p0.Root) (*altair.SyncCommitteeContribution, error)
VAPIRegisterAwaitAttestation func(func(ctx context.Context, slot, commIdx int64) (*eth2p0.AttestationData, error))
VAPIRegisterAwaitSyncContribution func(func(ctx context.Context, slot, subcommIdx int64, beaconBlockRoot eth2p0.Root) (*altair.SyncCommitteeContribution, error))
VAPIRegisterAwaitProposal func(func(ctx context.Context, opts *eth2api.ProposalOpts) (*eth2api.VersionedProposal, error))
VAPIRegisterAwaitBlindedProposal func(func(ctx context.Context, opts *eth2api.BlindedProposalOpts) (*eth2api.VersionedBlindedProposal, error))
VAPIRegisterAwaitProposal func(func(ctx context.Context, slot int64) (*eth2api.VersionedProposal, error))
VAPIRegisterAwaitBlindedProposal func(func(ctx context.Context, slot int64) (*eth2api.VersionedBlindedProposal, error))
VAPIRegisterGetDutyDefinition func(func(context.Context, Duty) (DutyDefinitionSet, error))
VAPIRegisterPubKeyByAttestation func(func(ctx context.Context, slot, commIdx, valCommIdx int64) (PubKey, error))
VAPIRegisterAwaitAggAttestation func(func(ctx context.Context, slot int64, attestationRoot eth2p0.Root) (*eth2p0.Attestation, error))
Expand Down
12 changes: 6 additions & 6 deletions core/validatorapi/validatorapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ type Component struct {

pubKeyByAttFunc func(ctx context.Context, slot, commIdx, valCommIdx int64) (core.PubKey, error)
awaitAttFunc func(ctx context.Context, slot, commIdx int64) (*eth2p0.AttestationData, error)
awaitProposalFunc func(ctx context.Context, opts *eth2api.ProposalOpts) (*eth2api.VersionedProposal, error)
awaitBlindedProposalFunc func(ctx context.Context, opts *eth2api.BlindedProposalOpts) (*eth2api.VersionedBlindedProposal, error)
awaitProposalFunc func(ctx context.Context, slot int64) (*eth2api.VersionedProposal, error)
awaitBlindedProposalFunc func(ctx context.Context, slot int64) (*eth2api.VersionedBlindedProposal, error)
awaitSyncContributionFunc func(ctx context.Context, slot, subcommIdx int64, beaconBlockRoot eth2p0.Root) (*altair.SyncCommitteeContribution, error)
awaitAggAttFunc func(ctx context.Context, slot int64, attestationRoot eth2p0.Root) (*eth2p0.Attestation, error)
awaitAggSigDBFunc func(context.Context, core.Duty, core.PubKey) (core.SignedData, error)
Expand All @@ -165,13 +165,13 @@ type Component struct {

// RegisterAwaitProposal registers a function to query unsigned beacon block proposals by providing necessary options.
// It supports a single function, since it is an input of the component.
func (c *Component) RegisterAwaitProposal(fn func(ctx context.Context, opts *eth2api.ProposalOpts) (*eth2api.VersionedProposal, error)) {
func (c *Component) RegisterAwaitProposal(fn func(ctx context.Context, slot int64) (*eth2api.VersionedProposal, error)) {
c.awaitProposalFunc = fn
}

// RegisterAwaitBlindedProposal registers a function to query unsigned blinded beacon block proposals by providing necessary options.
// It supports a single function, since it is an input of the component.
func (c *Component) RegisterAwaitBlindedProposal(fn func(ctx context.Context, opts *eth2api.BlindedProposalOpts) (*eth2api.VersionedBlindedProposal, error)) {
func (c *Component) RegisterAwaitBlindedProposal(fn func(ctx context.Context, slot int64) (*eth2api.VersionedBlindedProposal, error)) {
c.awaitBlindedProposalFunc = fn
}

Expand Down Expand Up @@ -349,7 +349,7 @@ func (c Component) Proposal(ctx context.Context, opts *eth2api.ProposalOpts) (*e
// - Once inserted, the query below will return.

// Query unsigned proposal (this is blocking).
proposal, err := c.awaitProposalFunc(ctx, opts)
proposal, err := c.awaitProposalFunc(ctx, int64(opts.Slot))

Check failure

Code scanning / CodeQL

Incorrect conversion between integer types High

Incorrect conversion of an unsigned 64-bit integer from
strconv.ParseUint
to a lower bit size type int64 without an upper bound check.
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -407,7 +407,7 @@ func (c Component) BlindedProposal(ctx context.Context, opts *eth2api.BlindedPro
// - Once inserted, the query below will return.

// Query unsigned block (this is blocking).
proposal, err := c.awaitBlindedProposalFunc(ctx, opts)
proposal, err := c.awaitBlindedProposalFunc(ctx, int64(opts.Slot))

Check failure

Code scanning / CodeQL

Incorrect conversion between integer types High

Incorrect conversion of an unsigned 64-bit integer from
strconv.ParseUint
to a lower bit size type int64 without an upper bound check.
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions core/validatorapi/validatorapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ func TestComponent_Proposal(t *testing.T) {
return core.DutyDefinitionSet{pubkey: nil}, nil
})

component.RegisterAwaitProposal(func(ctx context.Context, opts *eth2api.ProposalOpts) (*eth2api.VersionedProposal, error) {
component.RegisterAwaitProposal(func(ctx context.Context, slot int64) (*eth2api.VersionedProposal, error) {
return block1, nil
})

Expand Down Expand Up @@ -700,7 +700,7 @@ func TestComponent_BlindedProposal(t *testing.T) {
return core.DutyDefinitionSet{pubkey: nil}, nil
})

component.RegisterAwaitBlindedProposal(func(ctx context.Context, opts *eth2api.BlindedProposalOpts) (*eth2api.VersionedBlindedProposal, error) {
component.RegisterAwaitBlindedProposal(func(ctx context.Context, slot int64) (*eth2api.VersionedBlindedProposal, error) {
return block1, nil
})

Expand Down

0 comments on commit 0d1921d

Please sign in to comment.