Skip to content

Add P-chain GetBlockByHeight API method #1448

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

Merged
merged 19 commits into from
May 31, 2023
Merged
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
14 changes: 14 additions & 0 deletions vms/platformvm/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,8 @@ type Client interface {
GetValidatorsAt(ctx context.Context, subnetID ids.ID, height uint64, options ...rpc.Option) (map[ids.NodeID]uint64, error)
// GetBlock returns the block with the given id.
GetBlock(ctx context.Context, blockID ids.ID, options ...rpc.Option) ([]byte, error)
// GetBlockByHeight returns the block at the given [height].
GetBlockByHeight(ctx context.Context, height uint64, options ...rpc.Option) ([]byte, error)
}

// Client implementation for interacting with the P Chain endpoint
Expand Down Expand Up @@ -866,3 +868,15 @@ func (c *client) GetBlock(ctx context.Context, blockID ids.ID, options ...rpc.Op
}
return formatting.Decode(res.Encoding, res.Block)
}

func (c *client) GetBlockByHeight(ctx context.Context, height uint64, options ...rpc.Option) ([]byte, error) {
res := &api.FormattedBlock{}
err := c.requester.SendRequest(ctx, "platform.getBlockByHeight", &api.GetBlockByHeightArgs{
Height: json.Uint64(height),
Encoding: formatting.HexNC,
}, res, options...)
if err != nil {
return nil, err
}
return formatting.Decode(res.Encoding, res.Block)
}
37 changes: 37 additions & 0 deletions vms/platformvm/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2595,6 +2595,43 @@ func (s *Service) GetBlock(_ *http.Request, args *api.GetBlockArgs, response *ap
return nil
}

// GetBlockByHeight returns the block at the given height.
func (s *Service) GetBlockByHeight(_ *http.Request, args *api.GetBlockByHeightArgs, response *api.GetBlockResponse) error {
s.vm.ctx.Log.Debug("API called",
zap.String("service", "platform"),
zap.String("method", "getBlockByHeight"),
zap.Uint64("height", uint64(args.Height)),
)

response.Encoding = args.Encoding

blockID, err := s.vm.state.GetBlockIDAtHeight(uint64(args.Height))
if err != nil {
return fmt.Errorf("couldn't get block at height %d: %w", args.Height, err)
}
block, err := s.vm.manager.GetStatelessBlock(blockID)
if err != nil {
s.vm.ctx.Log.Error("couldn't get accepted block",
zap.Stringer("blkID", blockID),
zap.Error(err),
)
return fmt.Errorf("couldn't get block with id %s: %w", blockID, err)
}

if args.Encoding == formatting.JSON {
block.InitCtx(s.vm.ctx)
response.Block = block
return nil
}

response.Block, err = formatting.Encode(args.Encoding, block.Bytes())
if err != nil {
return fmt.Errorf("couldn't encode block %s as string: %w", blockID, err)
}

return nil
}

func (s *Service) getAPIUptime(staker *state.Staker) (*json.Float32, error) {
// Only report uptimes that we have been actively tracking.
if constants.PrimaryNetworkID != staker.SubnetID && !s.vm.TrackedSubnets.Contains(staker.SubnetID) {
Expand Down
191 changes: 191 additions & 0 deletions vms/platformvm/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (

stdjson "encoding/json"

"github.com/golang/mock/gomock"

"github.com/stretchr/testify/require"

"github.com/ava-labs/avalanchego/api"
Expand All @@ -23,6 +25,7 @@ import (
"github.com/ava-labs/avalanchego/database/manager"
"github.com/ava-labs/avalanchego/database/prefixdb"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/snow/consensus/snowman"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/crypto/secp256k1"
Expand Down Expand Up @@ -776,3 +779,191 @@ func TestGetBlock(t *testing.T) {
})
}
}

func TestServiceGetBlockByHeight(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

blockID := ids.GenerateTestID()
blockHeight := uint64(1337)

type test struct {
name string
serviceAndExpectedBlockFunc func(t *testing.T, ctrl *gomock.Controller) (*Service, interface{})
encoding formatting.Encoding
expectedErr error
}

tests := []test{
{
name: "block height not found",
serviceAndExpectedBlockFunc: func(_ *testing.T, ctrl *gomock.Controller) (*Service, interface{}) {
state := state.NewMockState(ctrl)
state.EXPECT().GetBlockIDAtHeight(blockHeight).Return(ids.Empty, database.ErrNotFound)

manager := blockexecutor.NewMockManager(ctrl)
return &Service{
vm: &VM{
state: state,
manager: manager,
ctx: &snow.Context{
Log: logging.NoLog{},
},
},
}, nil
},
encoding: formatting.Hex,
expectedErr: database.ErrNotFound,
},
{
name: "block not found",
serviceAndExpectedBlockFunc: func(_ *testing.T, ctrl *gomock.Controller) (*Service, interface{}) {
state := state.NewMockState(ctrl)
state.EXPECT().GetBlockIDAtHeight(blockHeight).Return(blockID, nil)

manager := blockexecutor.NewMockManager(ctrl)
manager.EXPECT().GetStatelessBlock(blockID).Return(nil, database.ErrNotFound)
return &Service{
vm: &VM{
state: state,
manager: manager,
ctx: &snow.Context{
Log: logging.NoLog{},
},
},
}, nil
},
encoding: formatting.Hex,
expectedErr: database.ErrNotFound,
},
{
name: "JSON format",
serviceAndExpectedBlockFunc: func(_ *testing.T, ctrl *gomock.Controller) (*Service, interface{}) {
block := blocks.NewMockBlock(ctrl)
block.EXPECT().InitCtx(gomock.Any())

state := state.NewMockState(ctrl)
state.EXPECT().GetBlockIDAtHeight(blockHeight).Return(blockID, nil)

manager := blockexecutor.NewMockManager(ctrl)
manager.EXPECT().GetStatelessBlock(blockID).Return(block, nil)
return &Service{
vm: &VM{
state: state,
manager: manager,
ctx: &snow.Context{
Log: logging.NoLog{},
},
},
}, block
},
encoding: formatting.JSON,
expectedErr: nil,
},
{
name: "hex format",
serviceAndExpectedBlockFunc: func(t *testing.T, ctrl *gomock.Controller) (*Service, interface{}) {
block := blocks.NewMockBlock(ctrl)
blockBytes := []byte("hi mom")
block.EXPECT().Bytes().Return(blockBytes)

state := state.NewMockState(ctrl)
state.EXPECT().GetBlockIDAtHeight(blockHeight).Return(blockID, nil)

expected, err := formatting.Encode(formatting.Hex, blockBytes)
require.NoError(t, err)

manager := blockexecutor.NewMockManager(ctrl)
manager.EXPECT().GetStatelessBlock(blockID).Return(block, nil)
return &Service{
vm: &VM{
state: state,
manager: manager,
ctx: &snow.Context{
Log: logging.NoLog{},
},
},
}, expected
},
encoding: formatting.Hex,
expectedErr: nil,
},
{
name: "hexc format",
serviceAndExpectedBlockFunc: func(t *testing.T, ctrl *gomock.Controller) (*Service, interface{}) {
block := blocks.NewMockBlock(ctrl)
blockBytes := []byte("hi mom")
block.EXPECT().Bytes().Return(blockBytes)

state := state.NewMockState(ctrl)
state.EXPECT().GetBlockIDAtHeight(blockHeight).Return(blockID, nil)

expected, err := formatting.Encode(formatting.HexC, blockBytes)
require.NoError(t, err)

manager := blockexecutor.NewMockManager(ctrl)
manager.EXPECT().GetStatelessBlock(blockID).Return(block, nil)
return &Service{
vm: &VM{
state: state,
manager: manager,
ctx: &snow.Context{
Log: logging.NoLog{},
},
},
}, expected
},
encoding: formatting.HexC,
expectedErr: nil,
},
{
name: "hexnc format",
serviceAndExpectedBlockFunc: func(t *testing.T, ctrl *gomock.Controller) (*Service, interface{}) {
block := blocks.NewMockBlock(ctrl)
blockBytes := []byte("hi mom")
block.EXPECT().Bytes().Return(blockBytes)

state := state.NewMockState(ctrl)
state.EXPECT().GetBlockIDAtHeight(blockHeight).Return(blockID, nil)

expected, err := formatting.Encode(formatting.HexNC, blockBytes)
require.NoError(t, err)

manager := blockexecutor.NewMockManager(ctrl)
manager.EXPECT().GetStatelessBlock(blockID).Return(block, nil)
return &Service{
vm: &VM{
state: state,
manager: manager,
ctx: &snow.Context{
Log: logging.NoLog{},
},
},
}, expected
},
encoding: formatting.HexNC,
expectedErr: nil,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
require := require.New(t)

service, expected := tt.serviceAndExpectedBlockFunc(t, ctrl)

args := &api.GetBlockByHeightArgs{
Height: json.Uint64(blockHeight),
Encoding: tt.encoding,
}
reply := &api.GetBlockResponse{}
err := service.GetBlockByHeight(nil, args, reply)
require.ErrorIs(err, tt.expectedErr)
if tt.expectedErr == nil {
return
}
require.Equal(tt.encoding, reply.Encoding)
require.Equal(expected, reply.Block)
})
}
}