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

     1  package request
     2  
     3  import (
     4  	"fmt"
     5  	"math"
     6  	"strconv"
     7  )
     8  
     9  const sealed = "sealed"
    10  const final = "final"
    11  
    12  // Special height values
    13  const SealedHeight uint64 = math.MaxUint64 - 1
    14  const FinalHeight uint64 = math.MaxUint64 - 2
    15  const EmptyHeight uint64 = math.MaxUint64 - 3
    16  
    17  type Height uint64
    18  
    19  func (h *Height) Parse(raw string) error {
    20  	if raw == "" { // allow empty
    21  		*h = Height(EmptyHeight)
    22  		return nil
    23  	}
    24  
    25  	if raw == sealed {
    26  		*h = Height(SealedHeight)
    27  		return nil
    28  	}
    29  	if raw == final {
    30  		*h = Height(FinalHeight)
    31  		return nil
    32  	}
    33  
    34  	height, err := strconv.ParseUint(raw, 0, 64)
    35  	if err != nil {
    36  		return fmt.Errorf("invalid height format")
    37  	}
    38  
    39  	if height >= EmptyHeight {
    40  		return fmt.Errorf("invalid height value")
    41  	}
    42  
    43  	*h = Height(height)
    44  	return nil
    45  }
    46  
    47  func (h Height) Flow() uint64 {
    48  	return uint64(h)
    49  }
    50  
    51  type Heights []Height
    52  
    53  func (h *Heights) Parse(raw []string) error {
    54  	var height Height
    55  	heights := make([]Height, 0)
    56  	uniqueHeights := make(map[string]bool)
    57  	for _, r := range raw {
    58  		err := height.Parse(r)
    59  		if err != nil {
    60  			return err
    61  		}
    62  		// don't include empty heights
    63  		if height == Height(EmptyHeight) {
    64  			continue
    65  		}
    66  
    67  		if !uniqueHeights[r] {
    68  			uniqueHeights[r] = true
    69  			heights = append(heights, height)
    70  		}
    71  	}
    72  
    73  	*h = heights
    74  	return nil
    75  }
    76  
    77  func (h Heights) Flow() []uint64 {
    78  	heights := make([]uint64, len(h))
    79  	for i, he := range h {
    80  		heights[i] = he.Flow()
    81  	}
    82  	return heights
    83  }