Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Access] Refactor common Access API structs into models #7104

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 17 additions & 101 deletions access/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,18 @@ package access
import (
"context"

"github.com/onflow/flow/protobuf/go/flow/access"
"github.com/onflow/flow/protobuf/go/flow/entities"

"github.com/onflow/flow-go/engine/access/subscription"
"github.com/onflow/flow-go/engine/common/rpc/convert"
accessmodel "github.com/onflow/flow-go/model/access"
"github.com/onflow/flow-go/model/flow"
)

// API provides all public-facing functionality of the Flow Access API.
type API interface {
Ping(ctx context.Context) error
GetNetworkParameters(ctx context.Context) NetworkParameters
GetNodeVersionInfo(ctx context.Context) (*NodeVersionInfo, error)
GetNetworkParameters(ctx context.Context) accessmodel.NetworkParameters
GetNodeVersionInfo(ctx context.Context) (*accessmodel.NodeVersionInfo, error)

GetLatestBlockHeader(ctx context.Context, isSealed bool) (*flow.Header, flow.BlockStatus, error)
GetBlockHeaderByHeight(ctx context.Context, height uint64) (*flow.Header, flow.BlockStatus, error)
Expand All @@ -31,11 +30,11 @@ type API interface {
SendTransaction(ctx context.Context, tx *flow.TransactionBody) error
GetTransaction(ctx context.Context, id flow.Identifier) (*flow.TransactionBody, error)
GetTransactionsByBlockID(ctx context.Context, blockID flow.Identifier) ([]*flow.TransactionBody, error)
GetTransactionResult(ctx context.Context, id flow.Identifier, blockID flow.Identifier, collectionID flow.Identifier, requiredEventEncodingVersion entities.EventEncodingVersion) (*TransactionResult, error)
GetTransactionResultByIndex(ctx context.Context, blockID flow.Identifier, index uint32, requiredEventEncodingVersion entities.EventEncodingVersion) (*TransactionResult, error)
GetTransactionResultsByBlockID(ctx context.Context, blockID flow.Identifier, requiredEventEncodingVersion entities.EventEncodingVersion) ([]*TransactionResult, error)
GetTransactionResult(ctx context.Context, id flow.Identifier, blockID flow.Identifier, collectionID flow.Identifier, requiredEventEncodingVersion entities.EventEncodingVersion) (*accessmodel.TransactionResult, error)
GetTransactionResultByIndex(ctx context.Context, blockID flow.Identifier, index uint32, requiredEventEncodingVersion entities.EventEncodingVersion) (*accessmodel.TransactionResult, error)
GetTransactionResultsByBlockID(ctx context.Context, blockID flow.Identifier, requiredEventEncodingVersion entities.EventEncodingVersion) ([]*accessmodel.TransactionResult, error)
GetSystemTransaction(ctx context.Context, blockID flow.Identifier) (*flow.TransactionBody, error)
GetSystemTransactionResult(ctx context.Context, blockID flow.Identifier, requiredEventEncodingVersion entities.EventEncodingVersion) (*TransactionResult, error)
GetSystemTransactionResult(ctx context.Context, blockID flow.Identifier, requiredEventEncodingVersion entities.EventEncodingVersion) (*accessmodel.TransactionResult, error)

GetAccount(ctx context.Context, address flow.Address) (*flow.Account, error)
GetAccountAtLatestBlock(ctx context.Context, address flow.Address) (*flow.Account, error)
Expand Down Expand Up @@ -80,6 +79,7 @@ type API interface {
//
// If invalid parameters will be supplied SubscribeBlocksFromStartBlockID will return a failed subscription.
SubscribeBlocksFromStartBlockID(ctx context.Context, startBlockID flow.Identifier, blockStatus flow.BlockStatus) subscription.Subscription

// SubscribeBlocksFromStartHeight subscribes to the finalized or sealed blocks starting at the requested
// start block height, up until the latest available block. Once the latest is
// reached, the stream will remain open and responses are sent for each new
Expand All @@ -95,6 +95,7 @@ type API interface {
//
// If invalid parameters will be supplied SubscribeBlocksFromStartHeight will return a failed subscription.
SubscribeBlocksFromStartHeight(ctx context.Context, startHeight uint64, blockStatus flow.BlockStatus) subscription.Subscription

// SubscribeBlocksFromLatest subscribes to the finalized or sealed blocks starting at the latest sealed block,
// up until the latest available block. Once the latest is
// reached, the stream will remain open and responses are sent for each new
Expand Down Expand Up @@ -127,6 +128,7 @@ type API interface {
//
// If invalid parameters will be supplied SubscribeBlockHeadersFromStartBlockID will return a failed subscription.
SubscribeBlockHeadersFromStartBlockID(ctx context.Context, startBlockID flow.Identifier, blockStatus flow.BlockStatus) subscription.Subscription

// SubscribeBlockHeadersFromStartHeight streams finalized or sealed block headers starting at the requested
// start block height, up until the latest available block header. Once the latest is
// reached, the stream will remain open and responses are sent for each new
Expand All @@ -142,6 +144,7 @@ type API interface {
//
// If invalid parameters will be supplied SubscribeBlockHeadersFromStartHeight will return a failed subscription.
SubscribeBlockHeadersFromStartHeight(ctx context.Context, startHeight uint64, blockStatus flow.BlockStatus) subscription.Subscription

// SubscribeBlockHeadersFromLatest streams finalized or sealed block headers starting at the latest sealed block,
// up until the latest available block header. Once the latest is
// reached, the stream will remain open and responses are sent for each new
Expand Down Expand Up @@ -174,6 +177,7 @@ type API interface {
//
// If invalid parameters will be supplied SubscribeBlockDigestsFromStartBlockID will return a failed subscription.
SubscribeBlockDigestsFromStartBlockID(ctx context.Context, startBlockID flow.Identifier, blockStatus flow.BlockStatus) subscription.Subscription

// SubscribeBlockDigestsFromStartHeight streams finalized or sealed lightweight block starting at the requested
// start block height, up until the latest available block. Once the latest is
// reached, the stream will remain open and responses are sent for each new
Expand All @@ -189,6 +193,7 @@ type API interface {
//
// If invalid parameters will be supplied SubscribeBlockDigestsFromStartHeight will return a failed subscription.
SubscribeBlockDigestsFromStartHeight(ctx context.Context, startHeight uint64, blockStatus flow.BlockStatus) subscription.Subscription

// SubscribeBlockDigestsFromLatest streams finalized or sealed lightweight block starting at the latest sealed block,
// up until the latest available block. Once the latest is
// reached, the stream will remain open and responses are sent for each new
Expand All @@ -203,6 +208,7 @@ type API interface {
//
// If invalid parameters will be supplied SubscribeBlockDigestsFromLatest will return a failed subscription.
SubscribeBlockDigestsFromLatest(ctx context.Context, blockStatus flow.BlockStatus) subscription.Subscription

// SubscribeTransactionStatusesFromStartBlockID subscribes to transaction status updates for a given transaction ID.
// Monitoring begins from the specified block ID. The subscription streams status updates until the transaction
// reaches a final state (TransactionStatusSealed or TransactionStatusExpired). When the transaction reaches one of
Expand All @@ -214,6 +220,7 @@ type API interface {
// - startBlockID: The block ID from which to start monitoring.
// - requiredEventEncodingVersion: The version of event encoding required for the subscription.
SubscribeTransactionStatusesFromStartBlockID(ctx context.Context, txID flow.Identifier, startBlockID flow.Identifier, requiredEventEncodingVersion entities.EventEncodingVersion) subscription.Subscription

// SubscribeTransactionStatusesFromStartHeight subscribes to transaction status updates for a given transaction ID.
// Monitoring begins from the specified block height. The subscription streams status updates until the transaction
// reaches a final state (TransactionStatusSealed or TransactionStatusExpired). When the transaction reaches one of
Expand All @@ -225,6 +232,7 @@ type API interface {
// - startHeight: The block height from which to start monitoring.
// - requiredEventEncodingVersion: The version of event encoding required for the subscription.
SubscribeTransactionStatusesFromStartHeight(ctx context.Context, txID flow.Identifier, startHeight uint64, requiredEventEncodingVersion entities.EventEncodingVersion) subscription.Subscription

// SubscribeTransactionStatusesFromLatest subscribes to transaction status updates for a given transaction ID.
// Monitoring begins from the latest block. The subscription streams status updates until the transaction
// reaches a final state (TransactionStatusSealed or TransactionStatusExpired). When the transaction reaches one of
Expand All @@ -235,6 +243,7 @@ type API interface {
// - txID: The unique identifier of the transaction to monitor.
// - requiredEventEncodingVersion: The version of event encoding required for the subscription.
SubscribeTransactionStatusesFromLatest(ctx context.Context, txID flow.Identifier, requiredEventEncodingVersion entities.EventEncodingVersion) subscription.Subscription

// SendAndSubscribeTransactionStatuses sends a transaction to the network and subscribes to its status updates.
// Monitoring begins from the reference block saved in the transaction itself and streams status updates until the transaction
// reaches a final state (TransactionStatusSealed or TransactionStatusExpired). Once a final status is reached, the subscription
Expand All @@ -248,96 +257,3 @@ type API interface {
// If the transaction cannot be sent, the subscription will fail and return a failed subscription.
SendAndSubscribeTransactionStatuses(ctx context.Context, tx *flow.TransactionBody, requiredEventEncodingVersion entities.EventEncodingVersion) subscription.Subscription
}

// TODO: Combine this with flow.TransactionResult?
type TransactionResult struct {
Status flow.TransactionStatus
StatusCode uint
Events []flow.Event
ErrorMessage string
BlockID flow.Identifier
TransactionID flow.Identifier
CollectionID flow.Identifier
BlockHeight uint64
}

func TransactionResultToMessage(result *TransactionResult) *access.TransactionResultResponse {
return &access.TransactionResultResponse{
Status: entities.TransactionStatus(result.Status),
StatusCode: uint32(result.StatusCode),
ErrorMessage: result.ErrorMessage,
Events: convert.EventsToMessages(result.Events),
BlockId: result.BlockID[:],
TransactionId: result.TransactionID[:],
CollectionId: result.CollectionID[:],
BlockHeight: result.BlockHeight,
}
}

func TransactionResultsToMessage(results []*TransactionResult) *access.TransactionResultsResponse {
messages := make([]*access.TransactionResultResponse, len(results))
for i, result := range results {
messages[i] = TransactionResultToMessage(result)
}

return &access.TransactionResultsResponse{
TransactionResults: messages,
}
}

func MessageToTransactionResult(message *access.TransactionResultResponse) *TransactionResult {

return &TransactionResult{
Status: flow.TransactionStatus(message.Status),
StatusCode: uint(message.StatusCode),
ErrorMessage: message.ErrorMessage,
Events: convert.MessagesToEvents(message.Events),
BlockID: flow.HashToID(message.BlockId),
TransactionID: flow.HashToID(message.TransactionId),
CollectionID: flow.HashToID(message.CollectionId),
BlockHeight: message.BlockHeight,
}
}

// NetworkParameters contains the network-wide parameters for the Flow blockchain.
type NetworkParameters struct {
ChainID flow.ChainID
}

// CompatibleRange contains the first and the last height that the version supports.
type CompatibleRange struct {
// The first block that the version supports.
StartHeight uint64
// The last block that the version supports.
EndHeight uint64
}

// NodeVersionInfo contains information about node, such as semver, commit, sporkID, protocolVersion, etc
type NodeVersionInfo struct {
Semver string
Commit string
SporkId flow.Identifier
// ProtocolVersion is the deprecated protocol version number.
// Deprecated: Previously this referred to the major software version as of the most recent spork.
// Replaced by protocol_state_version.
ProtocolVersion uint64
// ProtocolStateVersion is the Protocol State version as of the latest finalized block.
// This tracks the schema version of the Protocol State and is used to coordinate breaking changes in the Protocol.
// Version numbers are monotonically increasing.
ProtocolStateVersion uint64
SporkRootBlockHeight uint64
NodeRootBlockHeight uint64
CompatibleRange *CompatibleRange
}

// CompatibleRangeToMessage converts a flow.CompatibleRange to a protobuf message
func CompatibleRangeToMessage(c *CompatibleRange) *entities.CompatibleRange {
if c != nil {
return &entities.CompatibleRange{
StartHeight: c.StartHeight,
EndHeight: c.EndHeight,
}
}

return nil
}
4 changes: 2 additions & 2 deletions access/legacy/convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
entitiesproto "github.com/onflow/flow/protobuf/go/flow/legacy/entities"
"google.golang.org/protobuf/types/known/timestamppb"

"github.com/onflow/flow-go/access"
"github.com/onflow/flow-go/engine/common/rpc/convert"
accessmodel "github.com/onflow/flow-go/model/access"
"github.com/onflow/flow-go/model/flow"
)

Expand Down Expand Up @@ -119,7 +119,7 @@ func TransactionToMessage(tb flow.TransactionBody) *entitiesproto.Transaction {
}
}

func TransactionResultToMessage(result access.TransactionResult) *accessproto.TransactionResultResponse {
func TransactionResultToMessage(result accessmodel.TransactionResult) *accessproto.TransactionResultResponse {
return &accessproto.TransactionResultResponse{
Status: entitiesproto.TransactionStatus(result.Status),
StatusCode: uint32(result.StatusCode),
Expand Down
Loading