mirror of https://github.com/certusone/vouch.git
68 lines
2.5 KiB
Go
68 lines
2.5 KiB
Go
// 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 first
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
eth2client "github.com/attestantio/go-eth2-client"
|
|
"github.com/attestantio/go-eth2-client/spec/phase0"
|
|
"github.com/attestantio/vouch/util"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
// AggregateAttestation provides the aggregate attestation from a number of beacon nodes.
|
|
func (s *Service) AggregateAttestation(ctx context.Context, slot phase0.Slot, attestationDataRoot phase0.Root) (*phase0.Attestation, error) {
|
|
started := time.Now()
|
|
log := util.LogWithID(ctx, log, "strategy_id")
|
|
|
|
// We create a cancelable context with a timeout. When a provider responds we cancel the context to cancel the other requests.
|
|
ctx, cancel := context.WithTimeout(ctx, s.timeout)
|
|
|
|
respCh := make(chan *phase0.Attestation, 1)
|
|
for name, provider := range s.aggregateAttestationProviders {
|
|
go func(ctx context.Context,
|
|
name string,
|
|
provider eth2client.AggregateAttestationProvider,
|
|
ch chan *phase0.Attestation) {
|
|
log := log.With().Str("provider", name).Uint64("slot", uint64(slot)).Logger()
|
|
|
|
aggregate, err := provider.AggregateAttestation(ctx, slot, attestationDataRoot)
|
|
s.clientMonitor.ClientOperation(name, "aggregate attestation", err == nil, time.Since(started))
|
|
if err != nil {
|
|
log.Warn().Dur("elapsed", time.Since(started)).Err(err).Msg("Failed to obtain aggregate attestation")
|
|
return
|
|
}
|
|
if aggregate == nil {
|
|
log.Warn().Dur("elapsed", time.Since(started)).Err(err).Msg("Returned empty aggregate attestation")
|
|
return
|
|
}
|
|
log.Trace().Str("provider", name).Dur("elapsed", time.Since(started)).Msg("Obtained aggregate attestation")
|
|
|
|
ch <- aggregate
|
|
}(ctx, name, provider, respCh)
|
|
}
|
|
|
|
select {
|
|
case <-ctx.Done():
|
|
cancel()
|
|
log.Warn().Msg("Failed to obtain aggregate attestation before timeout")
|
|
return nil, errors.New("failed to obtain aggregate attestation before timeout")
|
|
case aggregate := <-respCh:
|
|
cancel()
|
|
return aggregate, nil
|
|
}
|
|
}
|