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

     1  package access
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/onflow/flow/protobuf/go/flow/access"
     7  	"github.com/onflow/flow/protobuf/go/flow/entities"
     8  
     9  	"github.com/onflow/flow-go/engine/access/subscription"
    10  	"github.com/onflow/flow-go/engine/common/rpc/convert"
    11  	"github.com/onflow/flow-go/model/flow"
    12  )
    13  
    14  // API provides all public-facing functionality of the Flow Access API.
    15  type API interface {
    16  	Ping(ctx context.Context) error
    17  	GetNetworkParameters(ctx context.Context) NetworkParameters
    18  	GetNodeVersionInfo(ctx context.Context) (*NodeVersionInfo, error)
    19  
    20  	GetLatestBlockHeader(ctx context.Context, isSealed bool) (*flow.Header, flow.BlockStatus, error)
    21  	GetBlockHeaderByHeight(ctx context.Context, height uint64) (*flow.Header, flow.BlockStatus, error)
    22  	GetBlockHeaderByID(ctx context.Context, id flow.Identifier) (*flow.Header, flow.BlockStatus, error)
    23  
    24  	GetLatestBlock(ctx context.Context, isSealed bool) (*flow.Block, flow.BlockStatus, error)
    25  	GetBlockByHeight(ctx context.Context, height uint64) (*flow.Block, flow.BlockStatus, error)
    26  	GetBlockByID(ctx context.Context, id flow.Identifier) (*flow.Block, flow.BlockStatus, error)
    27  
    28  	GetCollectionByID(ctx context.Context, id flow.Identifier) (*flow.LightCollection, error)
    29  
    30  	SendTransaction(ctx context.Context, tx *flow.TransactionBody) error
    31  	GetTransaction(ctx context.Context, id flow.Identifier) (*flow.TransactionBody, error)
    32  	GetTransactionsByBlockID(ctx context.Context, blockID flow.Identifier) ([]*flow.TransactionBody, error)
    33  	GetTransactionResult(ctx context.Context, id flow.Identifier, blockID flow.Identifier, collectionID flow.Identifier, requiredEventEncodingVersion entities.EventEncodingVersion) (*TransactionResult, error)
    34  	GetTransactionResultByIndex(ctx context.Context, blockID flow.Identifier, index uint32, requiredEventEncodingVersion entities.EventEncodingVersion) (*TransactionResult, error)
    35  	GetTransactionResultsByBlockID(ctx context.Context, blockID flow.Identifier, requiredEventEncodingVersion entities.EventEncodingVersion) ([]*TransactionResult, error)
    36  	GetSystemTransaction(ctx context.Context, blockID flow.Identifier) (*flow.TransactionBody, error)
    37  	GetSystemTransactionResult(ctx context.Context, blockID flow.Identifier, requiredEventEncodingVersion entities.EventEncodingVersion) (*TransactionResult, error)
    38  
    39  	GetAccount(ctx context.Context, address flow.Address) (*flow.Account, error)
    40  	GetAccountAtLatestBlock(ctx context.Context, address flow.Address) (*flow.Account, error)
    41  	GetAccountAtBlockHeight(ctx context.Context, address flow.Address, height uint64) (*flow.Account, error)
    42  
    43  	ExecuteScriptAtLatestBlock(ctx context.Context, script []byte, arguments [][]byte) ([]byte, error)
    44  	ExecuteScriptAtBlockHeight(ctx context.Context, blockHeight uint64, script []byte, arguments [][]byte) ([]byte, error)
    45  	ExecuteScriptAtBlockID(ctx context.Context, blockID flow.Identifier, script []byte, arguments [][]byte) ([]byte, error)
    46  
    47  	GetEventsForHeightRange(ctx context.Context, eventType string, startHeight, endHeight uint64, requiredEventEncodingVersion entities.EventEncodingVersion) ([]flow.BlockEvents, error)
    48  	GetEventsForBlockIDs(ctx context.Context, eventType string, blockIDs []flow.Identifier, requiredEventEncodingVersion entities.EventEncodingVersion) ([]flow.BlockEvents, error)
    49  
    50  	GetLatestProtocolStateSnapshot(ctx context.Context) ([]byte, error)
    51  	GetProtocolStateSnapshotByBlockID(ctx context.Context, blockID flow.Identifier) ([]byte, error)
    52  	GetProtocolStateSnapshotByHeight(ctx context.Context, blockHeight uint64) ([]byte, error)
    53  
    54  	GetExecutionResultForBlockID(ctx context.Context, blockID flow.Identifier) (*flow.ExecutionResult, error)
    55  	GetExecutionResultByID(ctx context.Context, id flow.Identifier) (*flow.ExecutionResult, error)
    56  
    57  	// SubscribeBlocks
    58  
    59  	// SubscribeBlocksFromStartBlockID subscribes to the finalized or sealed blocks starting at the requested
    60  	// start block id, up until the latest available block. Once the latest is
    61  	// reached, the stream will remain open and responses are sent for each new
    62  	// block as it becomes available.
    63  	//
    64  	// Each block is filtered by the provided block status, and only
    65  	// those blocks that match the status are returned.
    66  	//
    67  	// Parameters:
    68  	// - ctx: Context for the operation.
    69  	// - startBlockID: The identifier of the starting block.
    70  	// - blockStatus: The status of the block, which could be only BlockStatusSealed or BlockStatusFinalized.
    71  	//
    72  	// If invalid parameters will be supplied SubscribeBlocksFromStartBlockID will return a failed subscription.
    73  	SubscribeBlocksFromStartBlockID(ctx context.Context, startBlockID flow.Identifier, blockStatus flow.BlockStatus) subscription.Subscription
    74  	// SubscribeBlocksFromStartHeight subscribes to the finalized or sealed blocks starting at the requested
    75  	// start block height, up until the latest available block. Once the latest is
    76  	// reached, the stream will remain open and responses are sent for each new
    77  	// block as it becomes available.
    78  	//
    79  	// Each block is filtered by the provided block status, and only
    80  	// those blocks that match the status are returned.
    81  	//
    82  	// Parameters:
    83  	// - ctx: Context for the operation.
    84  	// - startHeight: The height of the starting block.
    85  	// - blockStatus: The status of the block, which could be only BlockStatusSealed or BlockStatusFinalized.
    86  	//
    87  	// If invalid parameters will be supplied SubscribeBlocksFromStartHeight will return a failed subscription.
    88  	SubscribeBlocksFromStartHeight(ctx context.Context, startHeight uint64, blockStatus flow.BlockStatus) subscription.Subscription
    89  	// SubscribeBlocksFromLatest subscribes to the finalized or sealed blocks starting at the latest sealed block,
    90  	// up until the latest available block. Once the latest is
    91  	// reached, the stream will remain open and responses are sent for each new
    92  	// block as it becomes available.
    93  	//
    94  	// Each block is filtered by the provided block status, and only
    95  	// those blocks that match the status are returned.
    96  	//
    97  	// Parameters:
    98  	// - ctx: Context for the operation.
    99  	// - blockStatus: The status of the block, which could be only BlockStatusSealed or BlockStatusFinalized.
   100  	//
   101  	// If invalid parameters will be supplied SubscribeBlocksFromLatest will return a failed subscription.
   102  	SubscribeBlocksFromLatest(ctx context.Context, blockStatus flow.BlockStatus) subscription.Subscription
   103  
   104  	// SubscribeHeaders
   105  
   106  	// SubscribeBlockHeadersFromStartBlockID streams finalized or sealed block headers starting at the requested
   107  	// start block id, up until the latest available block header. Once the latest is
   108  	// reached, the stream will remain open and responses are sent for each new
   109  	// block header as it becomes available.
   110  	//
   111  	// Each block header are filtered by the provided block status, and only
   112  	// those block headers that match the status are returned.
   113  	//
   114  	// Parameters:
   115  	// - ctx: Context for the operation.
   116  	// - startBlockID: The identifier of the starting block.
   117  	// - blockStatus: The status of the block, which could be only BlockStatusSealed or BlockStatusFinalized.
   118  	//
   119  	// If invalid parameters will be supplied SubscribeBlockHeadersFromStartBlockID will return a failed subscription.
   120  	SubscribeBlockHeadersFromStartBlockID(ctx context.Context, startBlockID flow.Identifier, blockStatus flow.BlockStatus) subscription.Subscription
   121  	// SubscribeBlockHeadersFromStartHeight streams finalized or sealed block headers starting at the requested
   122  	// start block height, up until the latest available block header. Once the latest is
   123  	// reached, the stream will remain open and responses are sent for each new
   124  	// block header as it becomes available.
   125  	//
   126  	// Each block header are filtered by the provided block status, and only
   127  	// those block headers that match the status are returned.
   128  	//
   129  	// Parameters:
   130  	// - ctx: Context for the operation.
   131  	// - startHeight: The height of the starting block.
   132  	// - blockStatus: The status of the block, which could be only BlockStatusSealed or BlockStatusFinalized.
   133  	//
   134  	// If invalid parameters will be supplied SubscribeBlockHeadersFromStartHeight will return a failed subscription.
   135  	SubscribeBlockHeadersFromStartHeight(ctx context.Context, startHeight uint64, blockStatus flow.BlockStatus) subscription.Subscription
   136  	// SubscribeBlockHeadersFromLatest streams finalized or sealed block headers starting at the latest sealed block,
   137  	// up until the latest available block header. Once the latest is
   138  	// reached, the stream will remain open and responses are sent for each new
   139  	// block header as it becomes available.
   140  	//
   141  	// Each block header are filtered by the provided block status, and only
   142  	// those block headers that match the status are returned.
   143  	//
   144  	// Parameters:
   145  	// - ctx: Context for the operation.
   146  	// - blockStatus: The status of the block, which could be only BlockStatusSealed or BlockStatusFinalized.
   147  	//
   148  	// If invalid parameters will be supplied SubscribeBlockHeadersFromLatest will return a failed subscription.
   149  	SubscribeBlockHeadersFromLatest(ctx context.Context, blockStatus flow.BlockStatus) subscription.Subscription
   150  
   151  	// Subscribe digests
   152  
   153  	// SubscribeBlockDigestsFromStartBlockID streams finalized or sealed lightweight block starting at the requested
   154  	// start block id, up until the latest available block. Once the latest is
   155  	// reached, the stream will remain open and responses are sent for each new
   156  	// block as it becomes available.
   157  	//
   158  	// Each lightweight block are filtered by the provided block status, and only
   159  	// those blocks that match the status are returned.
   160  	//
   161  	// Parameters:
   162  	// - ctx: Context for the operation.
   163  	// - startBlockID: The identifier of the starting block.
   164  	// - blockStatus: The status of the block, which could be only BlockStatusSealed or BlockStatusFinalized.
   165  	//
   166  	// If invalid parameters will be supplied SubscribeBlockDigestsFromStartBlockID will return a failed subscription.
   167  	SubscribeBlockDigestsFromStartBlockID(ctx context.Context, startBlockID flow.Identifier, blockStatus flow.BlockStatus) subscription.Subscription
   168  	// SubscribeBlockDigestsFromStartHeight streams finalized or sealed lightweight block starting at the requested
   169  	// start block height, up until the latest available block. Once the latest is
   170  	// reached, the stream will remain open and responses are sent for each new
   171  	// block as it becomes available.
   172  	//
   173  	// Each lightweight block are filtered by the provided block status, and only
   174  	// those blocks that match the status are returned.
   175  	//
   176  	// Parameters:
   177  	// - ctx: Context for the operation.
   178  	// - startHeight: The height of the starting block.
   179  	// - blockStatus: The status of the block, which could be only BlockStatusSealed or BlockStatusFinalized.
   180  	//
   181  	// If invalid parameters will be supplied SubscribeBlockDigestsFromStartHeight will return a failed subscription.
   182  	SubscribeBlockDigestsFromStartHeight(ctx context.Context, startHeight uint64, blockStatus flow.BlockStatus) subscription.Subscription
   183  	// SubscribeBlockDigestsFromLatest streams finalized or sealed lightweight block starting at the latest sealed block,
   184  	// up until the latest available block. Once the latest is
   185  	// reached, the stream will remain open and responses are sent for each new
   186  	// block as it becomes available.
   187  	//
   188  	// Each lightweight block are filtered by the provided block status, and only
   189  	// those blocks that match the status are returned.
   190  	//
   191  	// Parameters:
   192  	// - ctx: Context for the operation.
   193  	// - blockStatus: The status of the block, which could be only BlockStatusSealed or BlockStatusFinalized.
   194  	//
   195  	// If invalid parameters will be supplied SubscribeBlockDigestsFromLatest will return a failed subscription.
   196  	SubscribeBlockDigestsFromLatest(ctx context.Context, blockStatus flow.BlockStatus) subscription.Subscription
   197  	// SubscribeTransactionStatuses streams transaction statuses starting from the reference block saved in the
   198  	// transaction itself until the block containing the transaction becomes sealed or expired. When the transaction
   199  	// status becomes TransactionStatusSealed or TransactionStatusExpired, the subscription will automatically shut down.
   200  	SubscribeTransactionStatuses(ctx context.Context, tx *flow.TransactionBody, requiredEventEncodingVersion entities.EventEncodingVersion) subscription.Subscription
   201  }
   202  
   203  // TODO: Combine this with flow.TransactionResult?
   204  type TransactionResult struct {
   205  	Status        flow.TransactionStatus
   206  	StatusCode    uint
   207  	Events        []flow.Event
   208  	ErrorMessage  string
   209  	BlockID       flow.Identifier
   210  	TransactionID flow.Identifier
   211  	CollectionID  flow.Identifier
   212  	BlockHeight   uint64
   213  }
   214  
   215  func TransactionResultToMessage(result *TransactionResult) *access.TransactionResultResponse {
   216  	return &access.TransactionResultResponse{
   217  		Status:        entities.TransactionStatus(result.Status),
   218  		StatusCode:    uint32(result.StatusCode),
   219  		ErrorMessage:  result.ErrorMessage,
   220  		Events:        convert.EventsToMessages(result.Events),
   221  		BlockId:       result.BlockID[:],
   222  		TransactionId: result.TransactionID[:],
   223  		CollectionId:  result.CollectionID[:],
   224  		BlockHeight:   result.BlockHeight,
   225  	}
   226  }
   227  
   228  func TransactionResultsToMessage(results []*TransactionResult) *access.TransactionResultsResponse {
   229  	messages := make([]*access.TransactionResultResponse, len(results))
   230  	for i, result := range results {
   231  		messages[i] = TransactionResultToMessage(result)
   232  	}
   233  
   234  	return &access.TransactionResultsResponse{
   235  		TransactionResults: messages,
   236  	}
   237  }
   238  
   239  func MessageToTransactionResult(message *access.TransactionResultResponse) *TransactionResult {
   240  
   241  	return &TransactionResult{
   242  		Status:        flow.TransactionStatus(message.Status),
   243  		StatusCode:    uint(message.StatusCode),
   244  		ErrorMessage:  message.ErrorMessage,
   245  		Events:        convert.MessagesToEvents(message.Events),
   246  		BlockID:       flow.HashToID(message.BlockId),
   247  		TransactionID: flow.HashToID(message.TransactionId),
   248  		CollectionID:  flow.HashToID(message.CollectionId),
   249  		BlockHeight:   message.BlockHeight,
   250  	}
   251  }
   252  
   253  // NetworkParameters contains the network-wide parameters for the Flow blockchain.
   254  type NetworkParameters struct {
   255  	ChainID flow.ChainID
   256  }
   257  
   258  // NodeVersionInfo contains information about node, such as semver, commit, sporkID, protocolVersion, etc
   259  type NodeVersionInfo struct {
   260  	Semver               string
   261  	Commit               string
   262  	SporkId              flow.Identifier
   263  	ProtocolVersion      uint64
   264  	SporkRootBlockHeight uint64
   265  	NodeRootBlockHeight  uint64
   266  }