github.com/koko1123/flow-go-1@v0.29.6/engine/access/rest/request/get_block.go (about) 1 package request 2 3 import ( 4 "fmt" 5 6 "github.com/koko1123/flow-go-1/model/flow" 7 ) 8 9 const heightQuery = "height" 10 const startHeightQuery = "start_height" 11 const endHeightQuery = "end_height" 12 const MaxBlockRequestHeightRange = 50 13 const idParam = "id" 14 15 type GetBlock struct { 16 Heights []uint64 17 StartHeight uint64 18 EndHeight uint64 19 FinalHeight bool 20 SealedHeight bool 21 } 22 23 func (g *GetBlock) Build(r *Request) error { 24 return g.Parse( 25 r.GetQueryParams(heightQuery), 26 r.GetQueryParam(startHeightQuery), 27 r.GetQueryParam(endHeightQuery), 28 ) 29 } 30 31 func (g *GetBlock) HasHeights() bool { 32 return len(g.Heights) > 0 33 } 34 35 func (g *GetBlock) Parse(rawHeights []string, rawStart string, rawEnd string) error { 36 var height Height 37 err := height.Parse(rawStart) 38 if err != nil { 39 return err 40 } 41 g.StartHeight = height.Flow() 42 err = height.Parse(rawEnd) 43 if err != nil { 44 return err 45 } 46 g.EndHeight = height.Flow() 47 48 var heights Heights 49 err = heights.Parse(rawHeights) 50 if err != nil { 51 return err 52 } 53 g.Heights = heights.Flow() 54 55 // if both height and one or both of start and end height are provided 56 if len(g.Heights) > 0 && (g.StartHeight != EmptyHeight || g.EndHeight != EmptyHeight) { 57 return fmt.Errorf("can only provide either heights or start and end height range") 58 } 59 60 // if neither height nor start and end height are provided 61 if len(heights) == 0 && (g.StartHeight == EmptyHeight || g.EndHeight == EmptyHeight) { 62 return fmt.Errorf("must provide either heights or start and end height range") 63 } 64 65 if g.StartHeight > g.EndHeight { 66 return fmt.Errorf("start height must be less than or equal to end height") 67 } 68 // check if range exceeds maximum but only if end is not equal to special value which is not known yet 69 if g.EndHeight-g.StartHeight >= MaxBlockRequestHeightRange && g.EndHeight != FinalHeight && g.EndHeight != SealedHeight { 70 return fmt.Errorf("height range %d exceeds maximum allowed of %d", g.EndHeight-g.StartHeight, MaxBlockRequestHeightRange) 71 } 72 73 if len(heights) > MaxBlockRequestHeightRange { 74 return fmt.Errorf("at most %d heights can be requested at a time", MaxBlockRequestHeightRange) 75 } 76 77 // check that if sealed or final are used they are provided as only value as mix and matching heights with sealed is not encouraged 78 if len(heights) > 1 { 79 for _, h := range heights { 80 if h == Height(SealedHeight) || h == Height(FinalHeight) { 81 return fmt.Errorf("can not provide '%s' or '%s' values with other height values", final, sealed) 82 } 83 } 84 } else if len(heights) == 1 { 85 // if we have special values for heights set the booleans 86 g.FinalHeight = heights[0] == Height(FinalHeight) 87 g.SealedHeight = heights[0] == Height(SealedHeight) 88 } 89 90 return nil 91 } 92 93 type GetBlockByIDs struct { 94 IDs []flow.Identifier 95 } 96 97 func (g *GetBlockByIDs) Build(r *Request) error { 98 return g.Parse( 99 r.GetVars(idParam), 100 ) 101 } 102 103 func (g *GetBlockByIDs) Parse(rawIds []string) error { 104 var ids IDs 105 err := ids.Parse(rawIds) 106 if err != nil { 107 return err 108 } 109 g.IDs = ids.Flow() 110 111 return nil 112 } 113 114 type GetBlockPayload struct { 115 GetByIDRequest 116 }