vouch/services/submitter/multinode/submitattestations.go

92 lines
3.3 KiB
Go
Raw Normal View History

2020-09-27 23:46:00 -07:00
// Copyright © 2020 Attestant Limited.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package multinode
import (
"context"
"encoding/json"
"strings"
2020-09-27 23:46:00 -07:00
"sync"
2020-10-28 08:09:51 -07:00
"time"
2020-09-27 23:46:00 -07:00
eth2client "github.com/attestantio/go-eth2-client"
"github.com/attestantio/go-eth2-client/spec/phase0"
2020-09-27 23:46:00 -07:00
"github.com/pkg/errors"
"golang.org/x/sync/semaphore"
)
2020-11-26 12:32:04 -08:00
// SubmitAttestations submits a batch of attestations.
func (s *Service) SubmitAttestations(ctx context.Context, attestations []*phase0.Attestation) error {
2020-11-26 12:32:04 -08:00
if len(attestations) == 0 {
return errors.New("no attestations supplied")
2020-09-27 23:46:00 -07:00
}
sem := semaphore.NewWeighted(s.processConcurrency)
var wg sync.WaitGroup
2020-11-26 12:32:04 -08:00
for name, submitter := range s.attestationsSubmitters {
2020-09-27 23:46:00 -07:00
wg.Add(1)
go func(ctx context.Context,
sem *semaphore.Weighted,
wg *sync.WaitGroup,
name string,
2020-11-26 12:32:04 -08:00
submitter eth2client.AttestationsSubmitter,
2020-09-27 23:46:00 -07:00
) {
defer wg.Done()
2020-11-26 12:32:04 -08:00
log := log.With().Str("beacon_node_address", name).Uint64("slot", uint64(attestations[0].Data.Slot)).Logger()
2020-09-27 23:46:00 -07:00
if err := sem.Acquire(ctx, 1); err != nil {
log.Error().Err(err).Msg("Failed to acquire semaphore")
return
}
defer sem.Release(1)
2020-11-24 14:58:32 -08:00
serverType, address := s.serviceInfo(ctx, submitter)
2020-10-28 08:09:51 -07:00
started := time.Now()
2020-11-26 12:32:04 -08:00
err := submitter.SubmitAttestations(ctx, attestations)
s.clientMonitor.ClientOperation(address, "submit attestations", err == nil, time.Since(started))
2020-10-28 08:09:51 -07:00
if err != nil {
2020-11-24 14:58:32 -08:00
switch {
case serverType == "lighthouse" && strings.Contains(err.Error(), "PriorAttestationKnown"):
// Lighthouse rejects duplicate attestations. It is possible that an attestation we sent
// to another node already propagated to this node, so ignore the error.
log.Trace().Msg("Node already knows about attestation; ignored")
2020-11-24 14:58:32 -08:00
case serverType == "lighthouse" && strings.Contains(err.Error(), "UnknownHeadBlock"):
// Lighthouse rejects an attestation for a block that is not its current head. It is possible
// that the node is just behind, and we can't do anything about it anyway at this point having
// already signed an attestation for this slot, so ignore the error.
2020-11-26 12:32:04 -08:00
log.Debug().Err(err).Msg("Node does not know head block; rejected")
case serverType == "lighthouse" && strings.Contains(err.Error(), "InvalidSignature"):
data, err2 := json.Marshal(attestations)
if err2 != nil {
log.Error().Err(err).Msg("Failed to marshal JSON")
2020-11-27 07:26:22 -08:00
} else {
log.Warn().Err(err).Str("data", string(data)).Msg("Invalid signature!")
2020-11-26 12:32:04 -08:00
}
2020-11-24 14:58:32 -08:00
default:
log.Warn().Err(err).Msg("Failed to submit attestation")
}
2020-11-26 12:32:04 -08:00
} else {
2020-11-27 07:26:22 -08:00
data, err := json.Marshal(attestations)
if err != nil {
log.Error().Err(err).Msg("Failed to marshal JSON")
} else {
log.Trace().Str("data", string(data)).Msg("Submitted attestations")
}
2020-09-27 23:46:00 -07:00
}
}(ctx, sem, &wg, name, submitter)
}
wg.Wait()
return nil
}