github.com/hyperledger/burrow@v0.34.5-0.20220512172541-77f09336001d/rpc/rpcevents/blocks.go (about)

     1  package rpcevents
     2  
     3  import (
     4  	"github.com/hyperledger/burrow/execution/exec"
     5  )
     6  
     7  // Get bounds suitable for events.Provider
     8  func (br *BlockRange) Bounds(latestBlockHeight uint64) (startHeight, endHeight uint64, streaming bool) {
     9  	return br.GetStart().Bound(latestBlockHeight), br.GetEnd().Bound(latestBlockHeight),
    10  		br.GetEnd().GetType() == Bound_STREAM
    11  }
    12  
    13  func (b *Bound) Bound(latestBlockHeight uint64) uint64 {
    14  	if b == nil {
    15  		return latestBlockHeight
    16  	}
    17  	switch b.Type {
    18  	case Bound_ABSOLUTE:
    19  		return b.GetIndex()
    20  	case Bound_RELATIVE:
    21  		if b.Index < latestBlockHeight {
    22  			return latestBlockHeight - b.Index
    23  		}
    24  		return 0
    25  	case Bound_FIRST:
    26  		return 0
    27  	case Bound_LATEST, Bound_STREAM:
    28  		return latestBlockHeight
    29  	default:
    30  		return latestBlockHeight
    31  	}
    32  }
    33  
    34  func AbsoluteBound(index uint64) *Bound {
    35  	return &Bound{
    36  		Index: index,
    37  		Type:  Bound_ABSOLUTE,
    38  	}
    39  }
    40  
    41  func RelativeBound(index uint64) *Bound {
    42  	return &Bound{
    43  		Index: index,
    44  		Type:  Bound_RELATIVE,
    45  	}
    46  }
    47  
    48  func LatestBound() *Bound {
    49  	return &Bound{
    50  		Type: Bound_LATEST,
    51  	}
    52  }
    53  
    54  func StreamBound() *Bound {
    55  	return &Bound{
    56  		Type: Bound_STREAM,
    57  	}
    58  }
    59  
    60  func NewBlockRange(start, end *Bound) *BlockRange {
    61  	return &BlockRange{
    62  		Start: start,
    63  		End:   end,
    64  	}
    65  }
    66  
    67  func AbsoluteRange(start, end uint64) *BlockRange {
    68  	return NewBlockRange(AbsoluteBound(start), AbsoluteBound(end))
    69  }
    70  
    71  func SingleBlock(height uint64) *BlockRange {
    72  	return AbsoluteRange(height, height+1)
    73  }
    74  
    75  func ConsumeBlockExecutions(stream exec.EventStream, consumer func(*exec.BlockExecution) error,
    76  	continuityOptions ...exec.ContinuityOpt) error {
    77  	var be *exec.BlockExecution
    78  	var err error
    79  	ba := exec.NewBlockAccumulator(continuityOptions...)
    80  	for be, err = ba.ConsumeBlockExecution(stream); err == nil; be, err = ba.ConsumeBlockExecution(stream) {
    81  		err = consumer(be)
    82  		if err != nil {
    83  			return err
    84  		}
    85  	}
    86  	return err
    87  }