github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/engine/access/rest/routes/events.go (about)

     1  package routes
     2  
     3  import (
     4  	"fmt"
     5  
     6  	entitiesproto "github.com/onflow/flow/protobuf/go/flow/entities"
     7  
     8  	"github.com/onflow/flow-go/access"
     9  	"github.com/onflow/flow-go/engine/access/rest/models"
    10  	"github.com/onflow/flow-go/engine/access/rest/request"
    11  )
    12  
    13  const BlockQueryParam = "block_ids"
    14  const EventTypeQuery = "type"
    15  
    16  // GetEvents for the provided block range or list of block IDs filtered by type.
    17  func GetEvents(r *request.Request, backend access.API, _ models.LinkGenerator) (interface{}, error) {
    18  	req, err := r.GetEventsRequest()
    19  	if err != nil {
    20  		return nil, models.NewBadRequestError(err)
    21  	}
    22  
    23  	// if the request has block IDs provided then return events for block IDs
    24  	var blocksEvents models.BlocksEvents
    25  	if len(req.BlockIDs) > 0 {
    26  		events, err := backend.GetEventsForBlockIDs(
    27  			r.Context(),
    28  			req.Type,
    29  			req.BlockIDs,
    30  			entitiesproto.EventEncodingVersion_JSON_CDC_V0,
    31  		)
    32  		if err != nil {
    33  			return nil, err
    34  		}
    35  
    36  		blocksEvents.Build(events)
    37  		return blocksEvents, nil
    38  	}
    39  
    40  	// if end height is provided with special values then load the height
    41  	if req.EndHeight == request.FinalHeight || req.EndHeight == request.SealedHeight {
    42  		latest, _, err := backend.GetLatestBlockHeader(r.Context(), req.EndHeight == request.SealedHeight)
    43  		if err != nil {
    44  			return nil, err
    45  		}
    46  
    47  		req.EndHeight = latest.Height
    48  		// special check after we resolve special height value
    49  		if req.StartHeight > req.EndHeight {
    50  			return nil, models.NewBadRequestError(fmt.Errorf("current retrieved end height value is lower than start height"))
    51  		}
    52  	}
    53  
    54  	// if request provided block height range then return events for that range
    55  	events, err := backend.GetEventsForHeightRange(
    56  		r.Context(),
    57  		req.Type,
    58  		req.StartHeight,
    59  		req.EndHeight,
    60  		entitiesproto.EventEncodingVersion_JSON_CDC_V0,
    61  	)
    62  	if err != nil {
    63  		return nil, err
    64  	}
    65  
    66  	blocksEvents.Build(events)
    67  	return blocksEvents, nil
    68  }