github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/engine/access/rest/request/get_events.go (about) 1 package request 2 3 import ( 4 "fmt" 5 6 "github.com/onflow/flow-go/model/flow" 7 ) 8 9 const eventTypeQuery = "type" 10 const blockQuery = "block_ids" 11 const MaxEventRequestHeightRange = 250 12 13 type GetEvents struct { 14 StartHeight uint64 15 EndHeight uint64 16 Type string 17 BlockIDs []flow.Identifier 18 } 19 20 func (g *GetEvents) Build(r *Request) error { 21 return g.Parse( 22 r.GetQueryParam(eventTypeQuery), 23 r.GetQueryParam(startHeightQuery), 24 r.GetQueryParam(endHeightQuery), 25 r.GetQueryParams(blockQuery), 26 ) 27 } 28 29 func (g *GetEvents) Parse(rawType string, rawStart string, rawEnd string, rawBlockIDs []string) error { 30 var height Height 31 err := height.Parse(rawStart) 32 if err != nil { 33 return fmt.Errorf("invalid start height: %w", err) 34 } 35 g.StartHeight = height.Flow() 36 err = height.Parse(rawEnd) 37 if err != nil { 38 return fmt.Errorf("invalid end height: %w", err) 39 } 40 g.EndHeight = height.Flow() 41 42 var blockIDs IDs 43 err = blockIDs.Parse(rawBlockIDs) 44 if err != nil { 45 return err 46 } 47 g.BlockIDs = blockIDs.Flow() 48 49 // if both height and one or both of start and end height are provided 50 if len(blockIDs) > 0 && (g.StartHeight != EmptyHeight || g.EndHeight != EmptyHeight) { 51 return fmt.Errorf("can only provide either block IDs or start and end height range") 52 } 53 54 // if neither height nor start and end height are provided 55 if len(blockIDs) == 0 && (g.StartHeight == EmptyHeight || g.EndHeight == EmptyHeight) { 56 return fmt.Errorf("must provide either block IDs or start and end height range") 57 } 58 59 if rawType == "" { 60 return fmt.Errorf("event type must be provided") 61 } 62 var eventType EventType 63 err = eventType.Parse(rawType) 64 if err != nil { 65 return err 66 } 67 g.Type = eventType.Flow() 68 69 // validate start end height option 70 if g.StartHeight != EmptyHeight && g.EndHeight != EmptyHeight { 71 if g.StartHeight > g.EndHeight { 72 return fmt.Errorf("start height must be less than or equal to end height") 73 } 74 // check if range exceeds maximum but only if end is not equal to special value which is not known yet 75 if g.EndHeight-g.StartHeight >= MaxEventRequestHeightRange && g.EndHeight != FinalHeight && g.EndHeight != SealedHeight { 76 return fmt.Errorf("height range %d exceeds maximum allowed of %d", g.EndHeight-g.StartHeight, MaxEventRequestHeightRange) 77 } 78 } 79 80 return nil 81 }