github.com/koko1123/flow-go-1@v0.29.6/engine/access/state_stream/api.go (about)

     1  package state_stream
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/onflow/flow/protobuf/go/flow/entities"
     7  
     8  	"github.com/koko1123/flow-go-1/engine/common/rpc"
     9  	"github.com/koko1123/flow-go-1/engine/common/rpc/convert"
    10  	"github.com/koko1123/flow-go-1/model/flow"
    11  	"github.com/koko1123/flow-go-1/module/executiondatasync/execution_data"
    12  	"github.com/koko1123/flow-go-1/storage"
    13  )
    14  
    15  type API interface {
    16  	GetExecutionDataByBlockID(ctx context.Context, blockID flow.Identifier) (*entities.BlockExecutionData, error)
    17  }
    18  
    19  type StateStreamBackend struct {
    20  	headers       storage.Headers
    21  	seals         storage.Seals
    22  	results       storage.ExecutionResults
    23  	execDataStore execution_data.ExecutionDataStore
    24  }
    25  
    26  func New(
    27  	headers storage.Headers,
    28  	seals storage.Seals,
    29  	results storage.ExecutionResults,
    30  	execDataStore execution_data.ExecutionDataStore,
    31  ) *StateStreamBackend {
    32  	return &StateStreamBackend{
    33  		headers:       headers,
    34  		seals:         seals,
    35  		results:       results,
    36  		execDataStore: execDataStore,
    37  	}
    38  }
    39  
    40  func (s *StateStreamBackend) GetExecutionDataByBlockID(ctx context.Context, blockID flow.Identifier) (*entities.BlockExecutionData, error) {
    41  	header, err := s.headers.ByBlockID(blockID)
    42  	if err != nil {
    43  		return nil, rpc.ConvertStorageError(err)
    44  	}
    45  
    46  	seal, err := s.seals.FinalizedSealForBlock(header.ID())
    47  	if err != nil {
    48  		return nil, rpc.ConvertStorageError(err)
    49  	}
    50  
    51  	result, err := s.results.ByID(seal.ResultID)
    52  	if err != nil {
    53  		return nil, rpc.ConvertStorageError(err)
    54  	}
    55  
    56  	blockExecData, err := s.execDataStore.GetExecutionData(ctx, result.ExecutionDataID)
    57  	if err != nil {
    58  		return nil, err
    59  	}
    60  
    61  	message, err := convert.BlockExecutionDataToMessage(blockExecData)
    62  	if err != nil {
    63  		return nil, err
    64  	}
    65  	return message, nil
    66  }