From 4951c2623dc5112a902cd8994818ce38a6ef6834 Mon Sep 17 00:00:00 2001 From: Jim McDonald Date: Sun, 24 Sep 2023 22:46:58 +0100 Subject: [PATCH] Linting. --- main.go | 187 ++++++++++++----------- mock/builderbidprovider.go | 2 +- strategies/builderbid/best/builderbid.go | 151 ++++++++++-------- 3 files changed, 186 insertions(+), 154 deletions(-) diff --git a/main.go b/main.go index 8e813c88..6546dada 100644 --- a/main.go +++ b/main.go @@ -1398,93 +1398,7 @@ func selectSubmitterStrategy(ctx context.Context, monitor metrics.Service, eth2C switch viper.GetString("submitter.style") { case "multinode", "all": log.Info().Msg("Starting multinode submitter strategy") - - aggregateAttestationSubmitters := make(map[string]eth2client.AggregateAttestationsSubmitter) - for _, address := range util.BeaconNodeAddresses("submitter.aggregateattestation.multinode") { - client, err := fetchClient(ctx, address) - if err != nil { - return nil, errors.Wrap(err, fmt.Sprintf("failed to fetch client %s for aggregate attestation submitter strategy", address)) - } - aggregateAttestationSubmitters[address] = client.(eth2client.AggregateAttestationsSubmitter) - } - - attestationsSubmitters := make(map[string]eth2client.AttestationsSubmitter) - for _, address := range util.BeaconNodeAddresses("submitter.attestation.multinode") { - client, err := fetchClient(ctx, address) - if err != nil { - return nil, errors.Wrap(err, fmt.Sprintf("failed to fetch client %s for attestation submitter strategy", address)) - } - attestationsSubmitters[address] = client.(eth2client.AttestationsSubmitter) - } - - beaconBlockSubmitters := make(map[string]eth2client.BeaconBlockSubmitter) - for _, address := range util.BeaconNodeAddresses("submitter.beaconblock.multinode") { - client, err := fetchClient(ctx, address) - if err != nil { - return nil, errors.Wrap(err, fmt.Sprintf("failed to fetch client %s for beacon block submitter strategy", address)) - } - beaconBlockSubmitters[address] = client.(eth2client.BeaconBlockSubmitter) - } - - beaconCommitteeSubscriptionsSubmitters := make(map[string]eth2client.BeaconCommitteeSubscriptionsSubmitter) - for _, address := range util.BeaconNodeAddresses("submitter.beaconcommitteesubscription.multinode") { - client, err := fetchClient(ctx, address) - if err != nil { - return nil, errors.Wrap(err, fmt.Sprintf("failed to fetch client %s for beacon committee subscription submitter strategy", address)) - } - beaconCommitteeSubscriptionsSubmitters[address] = client.(eth2client.BeaconCommitteeSubscriptionsSubmitter) - } - - proposalPreparationSubmitters := make(map[string]eth2client.ProposalPreparationsSubmitter) - for _, address := range util.BeaconNodeAddresses("submitter.proposalpreparation.multinode") { - client, err := fetchClient(ctx, address) - if err != nil { - return nil, errors.Wrap(err, fmt.Sprintf("failed to fetch client %s for proposal preparation submitter strategy", address)) - } - proposalPreparationSubmitters[address] = client.(eth2client.ProposalPreparationsSubmitter) - } - - syncCommitteeContributionsSubmitters := make(map[string]eth2client.SyncCommitteeContributionsSubmitter) - for _, address := range util.BeaconNodeAddresses("submitter.synccommitteecontribution.multinode") { - client, err := fetchClient(ctx, address) - if err != nil { - return nil, errors.Wrap(err, fmt.Sprintf("failed to fetch client %s for sync committee contribution submitter strategy", address)) - } - syncCommitteeContributionsSubmitters[address] = client.(eth2client.SyncCommitteeContributionsSubmitter) - } - - syncCommitteeMessagesSubmitters := make(map[string]eth2client.SyncCommitteeMessagesSubmitter) - for _, address := range util.BeaconNodeAddresses("submitter.synccommitteemessage.multinode") { - client, err := fetchClient(ctx, address) - if err != nil { - return nil, errors.Wrap(err, fmt.Sprintf("failed to fetch client %s for sync committee message submitter strategy", address)) - } - syncCommitteeMessagesSubmitters[address] = client.(eth2client.SyncCommitteeMessagesSubmitter) - } - - syncCommitteeSubscriptionsSubmitters := make(map[string]eth2client.SyncCommitteeSubscriptionsSubmitter) - for _, address := range util.BeaconNodeAddresses("submitter.synccommitteesubscription.multinode") { - client, err := fetchClient(ctx, address) - if err != nil { - return nil, errors.Wrap(err, fmt.Sprintf("failed to fetch client %s for sync committee subscription submitter strategy", address)) - } - syncCommitteeSubscriptionsSubmitters[address] = client.(eth2client.SyncCommitteeSubscriptionsSubmitter) - } - - submitter, err = multinodesubmitter.New(ctx, - multinodesubmitter.WithClientMonitor(monitor.(metrics.ClientMonitor)), - multinodesubmitter.WithProcessConcurrency(util.ProcessConcurrency("submitter.multinode")), - multinodesubmitter.WithLogLevel(util.LogLevel("submitter.multinode")), - multinodesubmitter.WithTimeout(util.Timeout("submitter.multinode")), - multinodesubmitter.WithBeaconBlockSubmitters(beaconBlockSubmitters), - multinodesubmitter.WithAttestationsSubmitters(attestationsSubmitters), - multinodesubmitter.WithSyncCommitteeMessagesSubmitters(syncCommitteeMessagesSubmitters), - multinodesubmitter.WithSyncCommitteeContributionsSubmitters(syncCommitteeContributionsSubmitters), - multinodesubmitter.WithSyncCommitteeSubscriptionsSubmitters(syncCommitteeSubscriptionsSubmitters), - multinodesubmitter.WithAggregateAttestationsSubmitters(aggregateAttestationSubmitters), - multinodesubmitter.WithBeaconCommitteeSubscriptionsSubmitters(beaconCommitteeSubscriptionsSubmitters), - multinodesubmitter.WithProposalPreparationsSubmitters(proposalPreparationSubmitters), - ) + submitter, err = startMultinodeSubmitter(ctx, monitor) default: log.Info().Msg("Starting standard submitter strategy") submitter, err = immediatesubmitter.New(ctx, @@ -1506,6 +1420,105 @@ func selectSubmitterStrategy(ctx context.Context, monitor metrics.Service, eth2C return submitter, nil } +func startMultinodeSubmitter(ctx context.Context, + monitor metrics.Service, +) ( + submitter.Service, + error, +) { + aggregateAttestationSubmitters := make(map[string]eth2client.AggregateAttestationsSubmitter) + for _, address := range util.BeaconNodeAddresses("submitter.aggregateattestation.multinode") { + client, err := fetchClient(ctx, address) + if err != nil { + return nil, errors.Wrap(err, fmt.Sprintf("failed to fetch client %s for aggregate attestation submitter strategy", address)) + } + aggregateAttestationSubmitters[address] = client.(eth2client.AggregateAttestationsSubmitter) + } + + attestationsSubmitters := make(map[string]eth2client.AttestationsSubmitter) + for _, address := range util.BeaconNodeAddresses("submitter.attestation.multinode") { + client, err := fetchClient(ctx, address) + if err != nil { + return nil, errors.Wrap(err, fmt.Sprintf("failed to fetch client %s for attestation submitter strategy", address)) + } + attestationsSubmitters[address] = client.(eth2client.AttestationsSubmitter) + } + + beaconBlockSubmitters := make(map[string]eth2client.BeaconBlockSubmitter) + for _, address := range util.BeaconNodeAddresses("submitter.beaconblock.multinode") { + client, err := fetchClient(ctx, address) + if err != nil { + return nil, errors.Wrap(err, fmt.Sprintf("failed to fetch client %s for beacon block submitter strategy", address)) + } + beaconBlockSubmitters[address] = client.(eth2client.BeaconBlockSubmitter) + } + + beaconCommitteeSubscriptionsSubmitters := make(map[string]eth2client.BeaconCommitteeSubscriptionsSubmitter) + for _, address := range util.BeaconNodeAddresses("submitter.beaconcommitteesubscription.multinode") { + client, err := fetchClient(ctx, address) + if err != nil { + return nil, errors.Wrap(err, fmt.Sprintf("failed to fetch client %s for beacon committee subscription submitter strategy", address)) + } + beaconCommitteeSubscriptionsSubmitters[address] = client.(eth2client.BeaconCommitteeSubscriptionsSubmitter) + } + + proposalPreparationSubmitters := make(map[string]eth2client.ProposalPreparationsSubmitter) + for _, address := range util.BeaconNodeAddresses("submitter.proposalpreparation.multinode") { + client, err := fetchClient(ctx, address) + if err != nil { + return nil, errors.Wrap(err, fmt.Sprintf("failed to fetch client %s for proposal preparation submitter strategy", address)) + } + proposalPreparationSubmitters[address] = client.(eth2client.ProposalPreparationsSubmitter) + } + + syncCommitteeContributionsSubmitters := make(map[string]eth2client.SyncCommitteeContributionsSubmitter) + for _, address := range util.BeaconNodeAddresses("submitter.synccommitteecontribution.multinode") { + client, err := fetchClient(ctx, address) + if err != nil { + return nil, errors.Wrap(err, fmt.Sprintf("failed to fetch client %s for sync committee contribution submitter strategy", address)) + } + syncCommitteeContributionsSubmitters[address] = client.(eth2client.SyncCommitteeContributionsSubmitter) + } + + syncCommitteeMessagesSubmitters := make(map[string]eth2client.SyncCommitteeMessagesSubmitter) + for _, address := range util.BeaconNodeAddresses("submitter.synccommitteemessage.multinode") { + client, err := fetchClient(ctx, address) + if err != nil { + return nil, errors.Wrap(err, fmt.Sprintf("failed to fetch client %s for sync committee message submitter strategy", address)) + } + syncCommitteeMessagesSubmitters[address] = client.(eth2client.SyncCommitteeMessagesSubmitter) + } + + syncCommitteeSubscriptionsSubmitters := make(map[string]eth2client.SyncCommitteeSubscriptionsSubmitter) + for _, address := range util.BeaconNodeAddresses("submitter.synccommitteesubscription.multinode") { + client, err := fetchClient(ctx, address) + if err != nil { + return nil, errors.Wrap(err, fmt.Sprintf("failed to fetch client %s for sync committee subscription submitter strategy", address)) + } + syncCommitteeSubscriptionsSubmitters[address] = client.(eth2client.SyncCommitteeSubscriptionsSubmitter) + } + + submitter, err := multinodesubmitter.New(ctx, + multinodesubmitter.WithClientMonitor(monitor.(metrics.ClientMonitor)), + multinodesubmitter.WithProcessConcurrency(util.ProcessConcurrency("submitter.multinode")), + multinodesubmitter.WithLogLevel(util.LogLevel("submitter.multinode")), + multinodesubmitter.WithTimeout(util.Timeout("submitter.multinode")), + multinodesubmitter.WithBeaconBlockSubmitters(beaconBlockSubmitters), + multinodesubmitter.WithAttestationsSubmitters(attestationsSubmitters), + multinodesubmitter.WithSyncCommitteeMessagesSubmitters(syncCommitteeMessagesSubmitters), + multinodesubmitter.WithSyncCommitteeContributionsSubmitters(syncCommitteeContributionsSubmitters), + multinodesubmitter.WithSyncCommitteeSubscriptionsSubmitters(syncCommitteeSubscriptionsSubmitters), + multinodesubmitter.WithAggregateAttestationsSubmitters(aggregateAttestationSubmitters), + multinodesubmitter.WithBeaconCommitteeSubscriptionsSubmitters(beaconCommitteeSubscriptionsSubmitters), + multinodesubmitter.WithProposalPreparationsSubmitters(proposalPreparationSubmitters), + ) + if err != nil { + return nil, err + } + + return submitter, nil +} + // runCommands potentially runs commands. // Returns true if Vouch should exit. func runCommands(ctx context.Context, diff --git a/mock/builderbidprovider.go b/mock/builderbidprovider.go index e3697a8a..70400739 100644 --- a/mock/builderbidprovider.go +++ b/mock/builderbidprovider.go @@ -24,7 +24,7 @@ import ( type BuilderBidProvider struct{} // BuilderBid returns a builder bid. -func (b BuilderBidProvider) BuilderBid(_ context.Context, +func (BuilderBidProvider) BuilderBid(_ context.Context, _ phase0.Slot, _ phase0.Hash32, _ phase0.BLSPubKey, diff --git a/strategies/builderbid/best/builderbid.go b/strategies/builderbid/best/builderbid.go index 3b001625..75263fe3 100644 --- a/strategies/builderbid/best/builderbid.go +++ b/strategies/builderbid/best/builderbid.go @@ -30,6 +30,7 @@ import ( "github.com/attestantio/vouch/util" "github.com/holiman/uint256" "github.com/pkg/errors" + "github.com/rs/zerolog/log" e2types "github.com/wealdtech/go-eth2-types/v2" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/attribute" @@ -218,12 +219,11 @@ func (s *Service) builderBid(ctx context.Context, span.AddEvent("grace period over") } - log := s.log.With().Str("bidder", provider.Address()).Logger() - builderBid, err := provider.BuilderBid(ctx, slot, parentHash, pubkey) + builderBid, err := s.obtainBid(ctx, provider, slot, parentHash, pubkey) if err != nil { errCh <- &builderBidError{ provider: provider, - err: errors.Wrap(err, "failed to obtain builder bid"), + err: err, } return } @@ -234,40 +234,16 @@ func (s *Service) builderBid(ctx context.Context, } return } - if e := log.Trace(); e.Enabled() { - data, err := json.Marshal(builderBid) - if err != nil { - errCh <- &builderBidError{ - provider: provider, - err: errors.Wrap(err, "cannot marshal bid"), - } - return - } - e.RawJSON("builder_bid", data).Msg("Obtained builder bid") - } - if builderBid.IsEmpty() { - errCh <- &builderBidError{ - provider: provider, - err: errors.New("builder bid empty"), - } - return - } - value, err := builderBid.Value() + value, err := s.getBidValue(ctx, builderBid) if err != nil { errCh <- &builderBidError{ provider: provider, - err: fmt.Errorf("value: %w", err), - } - return - } - if zeroValue.Cmp(value) == 0 { - errCh <- &builderBidError{ - provider: provider, - err: errors.New("zero value"), + err: err, } return } + if value.ToBig().Cmp(relayConfig.MinValue.BigInt()) < 0 { log.Debug().Stringer("value", value.ToBig()).Stringer("min_value", relayConfig.MinValue.BigInt()).Msg("Value below minimum; ignoring") respCh <- &builderBidResponse{ @@ -277,60 +253,103 @@ func (s *Service) builderBid(ctx context.Context, return } - feeRecipient, err := builderBid.FeeRecipient() - if err != nil { + if err := s.verifyBidDetails(ctx, builderBid, slot, relayConfig, provider); err != nil { errCh <- &builderBidError{ provider: provider, - err: fmt.Errorf("fee recipient: %w", err), + err: err, } return } - if bytes.Equal(feeRecipient[:], zeroExecutionAddress[:]) { - errCh <- &builderBidError{ - provider: provider, - err: errors.New("zero fee recipient"), - } - return + + respCh <- &builderBidResponse{ + bid: builderBid, + provider: provider, + score: value.ToBig(), } +} - timestamp, err := builderBid.Timestamp() +func (s *Service) obtainBid(ctx context.Context, + provider builderclient.BuilderBidProvider, + slot phase0.Slot, + parentHash phase0.Hash32, + pubkey phase0.BLSPubKey, +) ( + *builderspec.VersionedSignedBuilderBid, + error, +) { + log := s.log.With().Str("bidder", provider.Address()).Logger() + builderBid, err := provider.BuilderBid(ctx, slot, parentHash, pubkey) if err != nil { - errCh <- &builderBidError{ - provider: provider, - err: fmt.Errorf("timestamp: %w", err), + return nil, errors.Wrap(err, "failed to obtain builder bid") + } + if builderBid == nil { + return nil, nil + } + + if e := log.Trace(); e.Enabled() { + data, err := json.Marshal(builderBid) + if err != nil { + return nil, errors.Wrap(err, "failed to unmarshal bid") } - return + e.RawJSON("builder_bid", data).Msg("Obtained builder bid") + } + + if builderBid.IsEmpty() { + return nil, errors.New("builder bid empty") + } + + return builderBid, nil +} + +func (s *Service) getBidValue(_ context.Context, + bid *builderspec.VersionedSignedBuilderBid, +) ( + *uint256.Int, + error, +) { + value, err := bid.Value() + if err != nil { + return nil, errors.Wrap(err, "failed to obtain bid value") + } + if zeroValue.Cmp(value) == 0 { + return nil, errors.New("bid has 0 value") + } + + return value, nil +} + +func (s *Service) verifyBidDetails(ctx context.Context, + bid *builderspec.VersionedSignedBuilderBid, + slot phase0.Slot, + relayConfig *beaconblockproposer.RelayConfig, + provider builderclient.BuilderBidProvider, +) error { + feeRecipient, err := bid.FeeRecipient() + if err != nil { + return errors.Wrap(err, "failed to obtain builder bid fee recipient") + } + if bytes.Equal(feeRecipient[:], zeroExecutionAddress[:]) { + return errors.New("zero fee recipient") + } + + timestamp, err := bid.Timestamp() + if err != nil { + return errors.Wrap(err, "failed to obtain builder bid timestamp") } if uint64(s.chainTime.StartOfSlot(slot).Unix()) != timestamp { - errCh <- &builderBidError{ - provider: provider, - err: fmt.Errorf("provided timestamp %d for slot %d not expected value of %d", timestamp, slot, s.chainTime.StartOfSlot(slot).Unix()), - } - return + return fmt.Errorf("provided timestamp %d for slot %d not expected value of %d", timestamp, slot, s.chainTime.StartOfSlot(slot).Unix()) } - verified, err := s.verifyBidSignature(ctx, relayConfig, builderBid, provider) + verified, err := s.verifyBidSignature(ctx, relayConfig, bid, provider) if err != nil { - errCh <- &builderBidError{ - provider: provider, - err: errors.Wrap(err, "error verifying bid signature"), - } - return + return err } if !verified { - log.Warn().Msg("Failed to verify bid signature") - errCh <- &builderBidError{ - provider: provider, - err: fmt.Errorf("%s: invalid signature", provider.Address()), - } - return + s.log.Warn().Msg("Failed to verify bid signature") + return errors.New("invalid signature") } - respCh <- &builderBidResponse{ - bid: builderBid, - provider: provider, - score: value.ToBig(), - } + return nil } // verifyBidSignature verifies the signature of a bid to ensure it comes from the expected source.