Skip to content

Commit

Permalink
Merge pull request #759 from The-K-R-O-K/get-protocol-state-snapshot-…
Browse files Browse the repository at this point in the history
…endpoints

GetProtocolStateSnapshotByBlockID and GetProtocolStateSnapshotByHeight endpoints
  • Loading branch information
peterargue authored Sep 27, 2024
2 parents b6c05d3 + 7b58582 commit 195b667
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 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
26 changes: 26 additions & 0 deletions access/grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,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

0 comments on commit 195b667

Please sign in to comment.