github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/engine/execution/ingestion/fetcher/fetcher.go (about)

     1  package fetcher
     2  
     3  import (
     4  	"fmt"
     5  	"regexp"
     6  
     7  	"github.com/rs/zerolog"
     8  
     9  	"github.com/onflow/flow-go/model/flow"
    10  	"github.com/onflow/flow-go/model/flow/filter"
    11  	"github.com/onflow/flow-go/module"
    12  	"github.com/onflow/flow-go/state/protocol"
    13  )
    14  
    15  var onlyOnflowRegex = regexp.MustCompile(`.*\.onflow\.org:3569$`)
    16  
    17  type CollectionFetcher struct {
    18  	log     zerolog.Logger
    19  	request module.Requester // used to request collections
    20  	state   protocol.State
    21  	// This is included to temporarily work around an issue observed on a small number of ENs.
    22  	// It works around an issue where some collection nodes are not configured with enough
    23  	// file descriptors causing connection failures.
    24  	onflowOnlyLNs bool
    25  }
    26  
    27  func NewCollectionFetcher(
    28  	log zerolog.Logger,
    29  	request module.Requester,
    30  	state protocol.State,
    31  	onflowOnlyLNs bool,
    32  ) *CollectionFetcher {
    33  	return &CollectionFetcher{
    34  		log:           log.With().Str("component", "ingestion_engine_collection_fetcher").Logger(),
    35  		request:       request,
    36  		state:         state,
    37  		onflowOnlyLNs: onflowOnlyLNs,
    38  	}
    39  }
    40  
    41  // FetchCollection decides which collection nodes to fetch the collection from
    42  // No error is expected during normal operation
    43  func (e *CollectionFetcher) FetchCollection(blockID flow.Identifier, height uint64, guarantee *flow.CollectionGuarantee) error {
    44  	guarantors, err := protocol.FindGuarantors(e.state, guarantee)
    45  	if err != nil {
    46  		// execution node executes certified blocks, which means there is a quorum of consensus nodes who
    47  		// have validated the block payload. And that validation includes checking the guarantors are correct.
    48  		// Based on that assumption, failing to find guarantors for guarantees contained in an incorporated block
    49  		// should be treated as fatal error
    50  		e.log.Fatal().Err(err).Msgf("failed to find guarantors for guarantee %v at block %v, height %v",
    51  			guarantee.ID(),
    52  			blockID,
    53  			height,
    54  		)
    55  		return fmt.Errorf("could not find guarantors: %w", err)
    56  	}
    57  
    58  	filters := []flow.IdentityFilter[flow.Identity]{
    59  		filter.HasNodeID[flow.Identity](guarantors...),
    60  	}
    61  
    62  	// This is included to temporarily work around an issue observed on a small number of ENs.
    63  	// It works around an issue where some collection nodes are not configured with enough
    64  	// file descriptors causing connection failures. This will be removed once a
    65  	// proper fix is in place.
    66  	if e.onflowOnlyLNs {
    67  		// func(Identity("verification-049.mainnet20.nodes.onflow.org:3569")) => true
    68  		// func(Identity("verification-049.hello.org:3569")) => false
    69  		filters = append(filters, func(identity *flow.Identity) bool {
    70  			return onlyOnflowRegex.MatchString(identity.Address)
    71  		})
    72  	}
    73  
    74  	// queue the collection to be requested from one of the guarantors
    75  	e.request.EntityByID(guarantee.ID(), filter.And(
    76  		filters...,
    77  	))
    78  
    79  	return nil
    80  }
    81  
    82  func (e *CollectionFetcher) Force() {
    83  	e.request.Force()
    84  }