Skip to content

Commit

Permalink
Merge branch 'master' into illia-malachyn/746-subscribe-block-headers…
Browse files Browse the repository at this point in the history
…-endpoints
  • Loading branch information
illia-malachyn authored Oct 1, 2024
2 parents 6c72a9a + 195b667 commit 2191d77
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 29 deletions.
8 changes: 8 additions & 0 deletions access/grpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,14 @@ func (c *Client) GetLatestProtocolStateSnapshot(ctx context.Context) ([]byte, er
return c.grpc.GetLatestProtocolStateSnapshot(ctx)
}

func (c *Client) GetProtocolStateSnapshotByBlockID(ctx context.Context, blockID flow.Identifier) ([]byte, error) {
return c.grpc.GetProtocolStateSnapshotByBlockID(ctx, blockID)
}

func (c *Client) GetProtocolStateSnapshotByHeight(ctx context.Context, blockHeight uint64) ([]byte, error) {
return c.grpc.GetProtocolStateSnapshotByHeight(ctx, blockHeight)
}

func (c *Client) GetExecutionResultForBlockID(ctx context.Context, blockID flow.Identifier) (*flow.ExecutionResult, error) {
return c.grpc.GetExecutionResultForBlockID(ctx, blockID)
}
Expand Down
32 changes: 17 additions & 15 deletions access/grpc/convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -584,14 +584,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 @@ -621,13 +622,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
26 changes: 26 additions & 0 deletions access/grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -912,6 +912,32 @@ func (c *BaseClient) GetLatestProtocolStateSnapshot(ctx context.Context, opts ..
return res.GetSerializedSnapshot(), nil
}

func (c *BaseClient) GetProtocolStateSnapshotByBlockID(ctx context.Context, blockID flow.Identifier, opts ...grpc.CallOption) ([]byte, error) {
req := &access.GetProtocolStateSnapshotByBlockIDRequest{
BlockId: blockID.Bytes(),
}

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

return res.GetSerializedSnapshot(), nil
}

func (c *BaseClient) GetProtocolStateSnapshotByHeight(ctx context.Context, blockHeight uint64, opts ...grpc.CallOption) ([]byte, error) {
req := &access.GetProtocolStateSnapshotByHeightRequest{
BlockHeight: blockHeight,
}

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

return res.GetSerializedSnapshot(), nil
}

func (c *BaseClient) GetExecutionResultForBlockID(ctx context.Context, blockID flow.Identifier, opts ...grpc.CallOption) (*flow.ExecutionResult, error) {
er, err := c.rpcClient.GetExecutionResultForBlockID(ctx, &access.GetExecutionResultForBlockIDRequest{
BlockId: convert.IdentifierToMessage(blockID),
Expand Down
58 changes: 58 additions & 0 deletions access/grpc/grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1500,6 +1500,64 @@ func TestClient_GetLatestProtocolStateSnapshot(t *testing.T) {
}))
}

func TestClient_GetProtocolStateSnapshotByBlockID(t *testing.T) {
ids := test.IdentifierGenerator()

t.Run("Success", clientTest(func(t *testing.T, ctx context.Context, rpc *mocks.MockRPCClient, c *BaseClient) {
blockID := ids.New()

expected := &access.ProtocolStateSnapshotResponse{
SerializedSnapshot: make([]byte, 128),
}
_, err := rand.Read(expected.SerializedSnapshot)
assert.NoError(t, err)

rpc.On("GetProtocolStateSnapshotByBlockID", ctx, mock.Anything).Return(expected, nil)

res, err := c.GetProtocolStateSnapshotByBlockID(ctx, blockID)
assert.NoError(t, err)
assert.Equal(t, expected.SerializedSnapshot, res)
}))

t.Run("Internal error", clientTest(func(t *testing.T, ctx context.Context, rpc *mocks.MockRPCClient, c *BaseClient) {
blockID := ids.New()

rpc.On("GetProtocolStateSnapshotByBlockID", ctx, mock.Anything).
Return(nil, errInternal)

_, err := c.GetProtocolStateSnapshotByBlockID(ctx, blockID)
assert.Error(t, err)
assert.Equal(t, codes.Internal, status.Code(err))
}))
}

func TestClient_GetProtocolStateSnapshotByHeight(t *testing.T) {
blockHeight := uint64(42)

t.Run("Success", clientTest(func(t *testing.T, ctx context.Context, rpc *mocks.MockRPCClient, c *BaseClient) {
expected := &access.ProtocolStateSnapshotResponse{
SerializedSnapshot: make([]byte, 128),
}
_, err := rand.Read(expected.SerializedSnapshot)
assert.NoError(t, err)

rpc.On("GetProtocolStateSnapshotByHeight", ctx, mock.Anything).Return(expected, nil)

res, err := c.GetProtocolStateSnapshotByHeight(ctx, blockHeight)
assert.NoError(t, err)
assert.Equal(t, expected.SerializedSnapshot, res)
}))

t.Run("Internal error", clientTest(func(t *testing.T, ctx context.Context, rpc *mocks.MockRPCClient, c *BaseClient) {
rpc.On("GetProtocolStateSnapshotByHeight", ctx, mock.Anything).
Return(nil, errInternal)

_, err := c.GetProtocolStateSnapshotByHeight(ctx, blockHeight)
assert.Error(t, err)
assert.Equal(t, codes.Internal, status.Code(err))
}))
}

func TestClient_GetExecutionResultForBlockID(t *testing.T) {
ids := test.IdentifierGenerator()
t.Run("Success", clientTest(func(t *testing.T, ctx context.Context, rpc *mocks.MockRPCClient, c *BaseClient) {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/aws/aws-sdk-go-v2 v1.27.0
github.com/aws/aws-sdk-go-v2/config v1.27.15
github.com/aws/aws-sdk-go-v2/service/kms v1.31.0
github.com/onflow/cadence v1.0.0-preview.52
github.com/onflow/cadence v1.0.0
github.com/onflow/crypto v0.25.1
github.com/onflow/flow/protobuf/go/flow v0.4.7
github.com/onflow/go-ethereum v1.13.4
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/onflow/atree v0.8.0-rc.6 h1:GWgaylK24b5ta2Hq+TvyOF7X5tZLiLzMMn7lEt59fsA=
github.com/onflow/atree v0.8.0-rc.6/go.mod h1:yccR+LR7xc1Jdic0mrjocbHvUD7lnVvg8/Ct1AA5zBo=
github.com/onflow/cadence v1.0.0-preview.52 h1:hZ92e6lL2+PQa3C1i5jJh0zZYFdW89+X1MS0Bkd6Ayo=
github.com/onflow/cadence v1.0.0-preview.52/go.mod h1:7wvvecnAZtYOspLOS3Lh+FuAmMeSrXhAWiycC3kQ1UU=
github.com/onflow/cadence v1.0.0 h1:bvT75F2LZJvDCBmmajAv7QLISK6Qp30FAKcSwqNNH+o=
github.com/onflow/cadence v1.0.0/go.mod h1:7wvvecnAZtYOspLOS3Lh+FuAmMeSrXhAWiycC3kQ1UU=
github.com/onflow/crypto v0.25.1 h1:0txy2PKPMM873JbpxQNbJmuOJtD56bfs48RQfm0ts5A=
github.com/onflow/crypto v0.25.1/go.mod h1:C8FbaX0x8y+FxWjbkHy0Q4EASCDR9bSPWZqlpCLYyVI=
github.com/onflow/flow/protobuf/go/flow v0.4.7 h1:iP6DFx4wZ3ETORsyeqzHu7neFT3d1CXF6wdK+AOOjmc=
Expand Down
9 changes: 5 additions & 4 deletions test/entities.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,10 +455,11 @@ func (g *TransactionResults) New() flow.TransactionResult {
g.events.New(),
g.events.New(),
},
BlockID: g.ids.New(),
BlockHeight: uint64(42),
TransactionID: g.ids.New(),
CollectionID: g.ids.New(),
BlockID: g.ids.New(),
BlockHeight: uint64(42),
TransactionID: g.ids.New(),
CollectionID: g.ids.New(),
ComputationUsage: uint64(42),
}
}

Expand Down
15 changes: 8 additions & 7 deletions transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -618,13 +618,14 @@ func (s signaturesList) canonicalForm() []transactionSignatureCanonicalForm {
}

type TransactionResult struct {
Status TransactionStatus
Error error
Events []Event
BlockID Identifier
BlockHeight uint64
TransactionID Identifier
CollectionID Identifier
Status TransactionStatus
Error error
Events []Event
BlockID Identifier
BlockHeight uint64
TransactionID Identifier
CollectionID Identifier
ComputationUsage uint64
}

// TransactionStatus represents the status of a transaction.
Expand Down

0 comments on commit 2191d77

Please sign in to comment.