github.com/hyperledger/burrow@v0.34.5-0.20220512172541-77f09336001d/execution/exec/block_execution.go (about)

     1  package exec
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  
     7  	"github.com/hyperledger/burrow/event"
     8  	"github.com/hyperledger/burrow/event/query"
     9  	"github.com/hyperledger/burrow/txs"
    10  )
    11  
    12  func EventStringBlockExecution(height uint64) string {
    13  	return fmt.Sprintf("Execution/Block/%v", height)
    14  }
    15  
    16  // Write out TxExecutions parenthetically
    17  func (be *BlockExecution) StreamEvents() []*StreamEvent {
    18  	var ses []*StreamEvent
    19  	ses = append(ses, &StreamEvent{
    20  		BeginBlock: &BeginBlock{
    21  			Height:            be.Height,
    22  			PredecessorHeight: be.PredecessorHeight,
    23  			NumTxs:            uint64(len(be.TxExecutions)),
    24  			Header:            be.Header,
    25  		},
    26  	})
    27  	for _, txe := range be.TxExecutions {
    28  		ses = append(ses, txe.StreamEvents()...)
    29  	}
    30  	return append(ses, &StreamEvent{
    31  		EndBlock: &EndBlock{
    32  			Height: be.Height,
    33  		},
    34  	})
    35  }
    36  
    37  func (*BlockExecution) EventType() EventType {
    38  	return TypeBlockExecution
    39  }
    40  
    41  func (be *BlockExecution) Tx(txEnv *txs.Envelope) *TxExecution {
    42  	txe := NewTxExecution(txEnv)
    43  	be.AppendTxs(txe)
    44  	return txe
    45  }
    46  
    47  func (be *BlockExecution) AppendTxs(tail ...*TxExecution) {
    48  	for i, txe := range tail {
    49  		txe.Index = uint64(len(be.TxExecutions) + i)
    50  		txe.Height = be.Height
    51  	}
    52  	be.TxExecutions = append(be.TxExecutions, tail...)
    53  }
    54  
    55  // Tags
    56  
    57  func (be *BlockExecution) Get(key string) (interface{}, bool) {
    58  	switch key {
    59  	case event.EventIDKey:
    60  		return EventStringBlockExecution(be.Height), true
    61  	case event.EventTypeKey:
    62  		return be.EventType(), true
    63  	}
    64  	v, ok := query.GetReflect(reflect.ValueOf(be.Header), key)
    65  	if ok {
    66  		return v, true
    67  	}
    68  	return query.GetReflect(reflect.ValueOf(be), key)
    69  }
    70  
    71  func QueryForBlockExecutionFromHeight(height uint64) *query.Builder {
    72  	return QueryForBlockExecution().AndGreaterThanOrEqual(event.HeightKey, height)
    73  }
    74  
    75  func QueryForBlockExecution() *query.Builder {
    76  	return query.NewBuilder().AndEquals(event.EventTypeKey, TypeBlockExecution)
    77  }