Skip to content

Commit

Permalink
Merge branch 'master' into get-protocol-state-snapshot-endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
AndriiDiachuk authored Sep 27, 2024
2 parents df0fe32 + b6c05d3 commit 7b58582
Show file tree
Hide file tree
Showing 11 changed files with 458 additions and 36 deletions.
24 changes: 24 additions & 0 deletions access/grpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,14 @@ func (c *Client) GetCollection(ctx context.Context, colID flow.Identifier) (*flo
return c.grpc.GetCollection(ctx, colID)
}

func (c *Client) GetCollectionByID(ctx context.Context, id flow.Identifier) (*flow.Collection, error) {
return c.grpc.GetLightCollectionByID(ctx, id)
}

func (c *Client) GetFullCollectionByID(ctx context.Context, id flow.Identifier) (*flow.FullCollection, error) {
return c.grpc.GetFullCollectionByID(ctx, id)
}

func (c *Client) SendTransaction(ctx context.Context, tx flow.Transaction) error {
return c.grpc.SendTransaction(ctx, tx)
}
Expand Down Expand Up @@ -216,6 +224,22 @@ func (c *Client) GetAccountBalanceAtBlockHeight(ctx context.Context, address flo
return c.grpc.GetAccountBalanceAtBlockHeight(ctx, address, blockHeight)
}

func (c *Client) GetAccountKeyAtLatestBlock(ctx context.Context, address flow.Address, keyIndex uint32) (*flow.AccountKey, error) {
return c.grpc.GetAccountKeyAtLatestBlock(ctx, address, keyIndex)
}

func (c *Client) GetAccountKeyAtBlockHeight(ctx context.Context, address flow.Address, keyIndex uint32, height uint64) (*flow.AccountKey, error) {
return c.grpc.GetAccountKeyAtBlockHeight(ctx, address, keyIndex, height)
}

func (c *Client) GetAccountKeysAtLatestBlock(ctx context.Context, address flow.Address) ([]flow.AccountKey, error) {
return c.grpc.GetAccountKeysAtLatestBlock(ctx, address)
}

func (c *Client) GetAccountKeysAtBlockHeight(ctx context.Context, address flow.Address, height uint64) ([]flow.AccountKey, error) {
return c.grpc.GetAccountKeysAtBlockHeight(ctx, address, height)
}

func (c *Client) ExecuteScriptAtLatestBlock(ctx context.Context, script []byte, arguments []cadence.Value) (cadence.Value, error) {
return c.grpc.ExecuteScriptAtLatestBlock(ctx, script, arguments)
}
Expand Down
77 changes: 62 additions & 15 deletions access/grpc/convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,21 @@ func MessageToAccountKey(m *entities.AccountKey) (*flow.AccountKey, error) {
}, nil
}

func MessageToAccountKeys(m []*entities.AccountKey) ([]flow.AccountKey, error) {
var accountKeys []flow.AccountKey

for _, entity := range m {
accountKey, err := MessageToAccountKey(entity)
if err != nil {
return nil, err
}

accountKeys = append(accountKeys, *accountKey)
}

return accountKeys, nil
}

func BlockToMessage(b flow.Block) (*entities.Block, error) {

t := timestamppb.New(b.BlockHeader.Timestamp)
Expand Down Expand Up @@ -252,6 +267,21 @@ func CollectionToMessage(c flow.Collection) *entities.Collection {
}
}

func FullCollectionToTransactionsMessage(tx flow.FullCollection) ([]*entities.Transaction, error) {
var convertedTxs []*entities.Transaction

for _, tx := range tx.Transactions {
convertedTx, err := TransactionToMessage(*tx)
if err != nil {
return nil, err
}

convertedTxs = append(convertedTxs, convertedTx)
}

return convertedTxs, nil
}

func MessageToCollection(m *entities.Collection) (flow.Collection, error) {
if m == nil {
return flow.Collection{}, ErrEmptyMessage
Expand All @@ -269,6 +299,21 @@ func MessageToCollection(m *entities.Collection) (flow.Collection, error) {
}, nil
}

func MessageToFullCollection(m []*entities.Transaction) (flow.FullCollection, error) {
var collection flow.FullCollection

for _, tx := range m {
convertedTx, err := MessageToTransaction(tx)
if err != nil {
return flow.FullCollection{}, err
}

collection.Transactions = append(collection.Transactions, &convertedTx)
}

return collection, nil
}

func CollectionGuaranteeToMessage(g flow.CollectionGuarantee) *entities.CollectionGuarantee {
return &entities.CollectionGuarantee{
CollectionId: g.CollectionID.Bytes(),
Expand Down Expand Up @@ -528,14 +573,15 @@ func TransactionResultToMessage(result flow.TransactionResult, encodingVersion f
}

return &access.TransactionResultResponse{
Status: entities.TransactionStatus(result.Status),
StatusCode: uint32(statusCode),
ErrorMessage: errorMsg,
Events: eventMessages,
BlockId: IdentifierToMessage(result.BlockID),
BlockHeight: result.BlockHeight,
TransactionId: IdentifierToMessage(result.TransactionID),
CollectionId: IdentifierToMessage(result.CollectionID),
Status: entities.TransactionStatus(result.Status),
StatusCode: uint32(statusCode),
ErrorMessage: errorMsg,
Events: eventMessages,
BlockId: IdentifierToMessage(result.BlockID),
BlockHeight: result.BlockHeight,
TransactionId: IdentifierToMessage(result.TransactionID),
CollectionId: IdentifierToMessage(result.CollectionID),
ComputationUsage: result.ComputationUsage,
}, nil
}

Expand Down Expand Up @@ -565,13 +611,14 @@ func MessageToTransactionResult(m *access.TransactionResultResponse, options []j
}

return flow.TransactionResult{
Status: flow.TransactionStatus(m.GetStatus()),
Error: err,
Events: events,
BlockID: flow.BytesToID(m.GetBlockId()),
BlockHeight: m.GetBlockHeight(),
TransactionID: flow.BytesToID(m.GetTransactionId()),
CollectionID: flow.BytesToID(m.GetCollectionId()),
Status: flow.TransactionStatus(m.GetStatus()),
Error: err,
Events: events,
BlockID: flow.BytesToID(m.GetBlockId()),
BlockHeight: m.GetBlockHeight(),
TransactionID: flow.BytesToID(m.GetTransactionId()),
CollectionID: flow.BytesToID(m.GetCollectionId()),
ComputationUsage: m.GetComputationUsage(),
}, nil
}

Expand Down
2 changes: 1 addition & 1 deletion access/grpc/convert/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func TestConvert_CadenceValue(t *testing.T) {
}

func TestConvert_Collection(t *testing.T) {
colA := test.CollectionGenerator().New()
colA := test.LightCollectionGenerator().New()

msg := CollectionToMessage(*colA)

Expand Down
136 changes: 136 additions & 0 deletions access/grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,50 @@ func (c *BaseClient) GetCollection(
return &result, nil
}

func (c *BaseClient) GetLightCollectionByID(
ctx context.Context,
id flow.Identifier,
opts ...grpc.CallOption,
) (*flow.Collection, error) {
req := &access.GetCollectionByIDRequest{
Id: id.Bytes(),
}

res, err := c.rpcClient.GetCollectionByID(ctx, req, opts...)
if err != nil {
return nil, newRPCError(err)
}

result, err := convert.MessageToCollection(res.GetCollection())
if err != nil {
return nil, newMessageToEntityError(entityCollection, err)
}

return &result, nil
}

func (c *BaseClient) GetFullCollectionByID(
ctx context.Context,
id flow.Identifier,
opts ...grpc.CallOption,
) (*flow.FullCollection, error) {
req := &access.GetFullCollectionByIDRequest{
Id: id.Bytes(),
}

res, err := c.rpcClient.GetFullCollectionByID(ctx, req, opts...)
if err != nil {
return nil, newRPCError(err)
}

result, err := convert.MessageToFullCollection(res.GetTransactions())
if err != nil {
return nil, newMessageToEntityError(entityCollection, err)
}

return &result, nil
}

func (c *BaseClient) SendTransaction(
ctx context.Context,
tx flow.Transaction,
Expand Down Expand Up @@ -597,6 +641,98 @@ func (c *BaseClient) GetAccountBalanceAtBlockHeight(
return response.GetBalance(), nil
}

func (c *BaseClient) GetAccountKeyAtLatestBlock(
ctx context.Context,
address flow.Address,
keyIndex uint32,
) (*flow.AccountKey, error) {
request := &access.GetAccountKeyAtLatestBlockRequest{
Address: address.Bytes(),
Index: keyIndex,
}

response, err := c.rpcClient.GetAccountKeyAtLatestBlock(ctx, request)
if err != nil {
return nil, newRPCError(err)
}

accountKey, err := convert.MessageToAccountKey(response.GetAccountKey())
if err != nil {
return nil, newMessageToEntityError(entityAccount, err)
}

return accountKey, nil
}

func (c *BaseClient) GetAccountKeyAtBlockHeight(
ctx context.Context,
address flow.Address,
keyIndex uint32,
height uint64,
) (*flow.AccountKey, error) {
request := &access.GetAccountKeyAtBlockHeightRequest{
Address: address.Bytes(),
Index: keyIndex,
BlockHeight: height,
}

response, err := c.rpcClient.GetAccountKeyAtBlockHeight(ctx, request)
if err != nil {
return nil, newRPCError(err)
}

accountKey, err := convert.MessageToAccountKey(response.GetAccountKey())
if err != nil {
return nil, newMessageToEntityError(entityAccount, err)
}

return accountKey, nil
}

func (c *BaseClient) GetAccountKeysAtLatestBlock(
ctx context.Context,
address flow.Address,
) ([]flow.AccountKey, error) {
request := &access.GetAccountKeysAtLatestBlockRequest{
Address: address.Bytes(),
}

response, err := c.rpcClient.GetAccountKeysAtLatestBlock(ctx, request)
if err != nil {
return nil, newRPCError(err)
}

accountKeys, err := convert.MessageToAccountKeys(response.GetAccountKeys())
if err != nil {
return nil, newMessageToEntityError(entityAccount, err)
}

return accountKeys, nil
}

func (c *BaseClient) GetAccountKeysAtBlockHeight(
ctx context.Context,
address flow.Address,
height uint64,
) ([]flow.AccountKey, error) {
request := &access.GetAccountKeysAtBlockHeightRequest{
Address: address.Bytes(),
BlockHeight: height,
}

response, err := c.rpcClient.GetAccountKeysAtBlockHeight(ctx, request)
if err != nil {
return nil, newRPCError(err)
}

accountKeys, err := convert.MessageToAccountKeys(response.GetAccountKeys())
if err != nil {
return nil, newMessageToEntityError(entityAccount, err)
}

return accountKeys, nil
}

func (c *BaseClient) ExecuteScriptAtLatestBlock(
ctx context.Context,
script []byte,
Expand Down
Loading

0 comments on commit 7b58582

Please sign in to comment.