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

Add GetFullCollectionById endpoint #754

2 changes: 1 addition & 1 deletion access/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ type Client interface {
GetBlockByHeight(ctx context.Context, height uint64) (*flow.Block, error)

// GetCollection gets a collection by ID.
GetCollection(ctx context.Context, colID flow.Identifier) (*flow.Collection, error)
GetCollection(ctx context.Context, colID flow.Identifier) (*flow.LightCollection, error)
illia-malachyn marked this conversation as resolved.
Show resolved Hide resolved

// SendTransaction submits a transaction to the network.
SendTransaction(ctx context.Context, tx flow.Transaction) error
Expand Down
10 changes: 9 additions & 1 deletion access/grpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,18 @@ func (c *Client) GetBlockByHeight(ctx context.Context, height uint64) (*flow.Blo
return c.grpc.GetBlockByHeight(ctx, height)
}

func (c *Client) GetCollection(ctx context.Context, colID flow.Identifier) (*flow.Collection, error) {
func (c *Client) GetCollection(ctx context.Context, colID flow.Identifier) (*flow.LightCollection, error) {
return c.grpc.GetCollection(ctx, colID)
}

func (c *Client) GetCollectionByID(ctx context.Context, id flow.Identifier) (*flow.LightCollection, 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
38 changes: 34 additions & 4 deletions access/grpc/convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ func MessageToCadenceValue(m []byte, options []jsoncdc.Option) (cadence.Value, e
return v, nil
}

func CollectionToMessage(c flow.Collection) *entities.Collection {
func LightCollectionToMessage(c flow.LightCollection) *entities.Collection {
transactionIDMessages := make([][]byte, len(c.TransactionIDs))
for i, transactionID := range c.TransactionIDs {
transactionIDMessages[i] = transactionID.Bytes()
Expand All @@ -252,9 +252,24 @@ func CollectionToMessage(c flow.Collection) *entities.Collection {
}
}

func MessageToCollection(m *entities.Collection) (flow.Collection, error) {
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 MessageToLightCollection(m *entities.Collection) (flow.LightCollection, error) {
if m == nil {
return flow.Collection{}, ErrEmptyMessage
return flow.LightCollection{}, ErrEmptyMessage
}

transactionIDMessages := m.GetTransactionIds()
Expand All @@ -264,11 +279,26 @@ func MessageToCollection(m *entities.Collection) (flow.Collection, error) {
transactionIDs[i] = flow.HashToID(transactionIDMsg)
}

return flow.Collection{
return flow.LightCollection{
TransactionIDs: transactionIDs,
}, 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
6 changes: 3 additions & 3 deletions access/grpc/convert/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,11 @@ func TestConvert_CadenceValue(t *testing.T) {
}

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

msg := CollectionToMessage(*colA)
msg := LightCollectionToMessage(*colA)

colB, err := MessageToCollection(msg)
colB, err := MessageToLightCollection(msg)
require.NoError(t, err)

assert.Equal(t, *colA, colB)
Expand Down
48 changes: 46 additions & 2 deletions access/grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ func (c *BaseClient) GetCollection(
ctx context.Context,
colID flow.Identifier,
opts ...grpc.CallOption,
) (*flow.Collection, error) {
) (*flow.LightCollection, error) {
req := &access.GetCollectionByIDRequest{
Id: colID.Bytes(),
}
Expand All @@ -306,7 +306,51 @@ func (c *BaseClient) GetCollection(
return nil, newRPCError(err)
}

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

return &result, nil
}

func (c *BaseClient) GetLightCollectionByID(
ctx context.Context,
id flow.Identifier,
opts ...grpc.CallOption,
) (*flow.LightCollection, 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.MessageToLightCollection(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)
}
Expand Down
42 changes: 40 additions & 2 deletions access/grpc/grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,14 +386,14 @@ func TestClient_GetBlockByHeight(t *testing.T) {
}

func TestClient_GetCollection(t *testing.T) {
cols := test.CollectionGenerator()
cols := test.LightCollectionGenerator()
ids := test.IdentifierGenerator()

t.Run("Success", clientTest(func(t *testing.T, ctx context.Context, rpc *mocks.MockRPCClient, c *BaseClient) {
colID := ids.New()
expectedCol := cols.New()
response := &access.CollectionResponse{
Collection: convert.CollectionToMessage(*expectedCol),
Collection: convert.LightCollectionToMessage(*expectedCol),
}

rpc.On("GetCollectionByID", ctx, mock.Anything).Return(response, nil)
Expand All @@ -417,6 +417,44 @@ func TestClient_GetCollection(t *testing.T) {
}))
}

func TestClient_GetFullCollectionById(t *testing.T) {
collections := test.FullCollectionGenerator()
ids := test.IdentifierGenerator()

t.Run("Success", clientTest(func(t *testing.T, ctx context.Context, rpc *mocks.MockRPCClient, c *BaseClient) {
expectedCollection := collections.New()
txs, err := convert.FullCollectionToTransactionsMessage(*expectedCollection)
require.NoError(t, err)

response := &access.FullCollectionResponse{
Transactions: txs,
}

rpc.
On("GetFullCollectionByID", ctx, mock.Anything).
Return(response, nil)

id := ids.New()
actualCollection, err := c.GetFullCollectionByID(ctx, id)
require.NoError(t, err)

require.Equal(t, expectedCollection, actualCollection)

}))

t.Run("Not found error", clientTest(func(t *testing.T, ctx context.Context, rpc *mocks.MockRPCClient, c *BaseClient) {
rpc.
On("GetFullCollectionByID", ctx, mock.Anything).
Return(nil, errNotFound)

id := ids.New()
col, err := c.GetFullCollectionByID(ctx, id)
assert.Error(t, err)
assert.Equal(t, codes.NotFound, status.Code(err))
assert.Nil(t, col)
}))
}

func TestClient_SendTransaction(t *testing.T) {
transactions := test.TransactionGenerator()

Expand Down
Loading
Loading