Skip to content

Commit

Permalink
Revert P-Chain height indexing (ava-labs#1591)
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenButtolph committed Jun 6, 2023
1 parent 00e61d8 commit b456e16
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 439 deletions.
2 changes: 1 addition & 1 deletion vms/avm/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (s *Service) GetBlockByHeight(_ *http.Request, args *api.GetBlockByHeightAr
}
reply.Encoding = args.Encoding

blockID, err := s.vm.state.GetBlockIDAtHeight(uint64(args.Height))
blockID, err := s.vm.state.GetBlockID(uint64(args.Height))
if err != nil {
return fmt.Errorf("couldn't get block at height %d: %w", args.Height, err)
}
Expand Down
12 changes: 6 additions & 6 deletions vms/avm/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2777,7 +2777,7 @@ func TestServiceGetBlockByHeight(t *testing.T) {
name: "block height not found",
serviceAndExpectedBlockFunc: func(ctrl *gomock.Controller) (*Service, interface{}) {
state := states.NewMockState(ctrl)
state.EXPECT().GetBlockIDAtHeight(blockHeight).Return(ids.Empty, database.ErrNotFound)
state.EXPECT().GetBlockID(blockHeight).Return(ids.Empty, database.ErrNotFound)

manager := executor.NewMockManager(ctrl)
return &Service{
Expand All @@ -2797,7 +2797,7 @@ func TestServiceGetBlockByHeight(t *testing.T) {
name: "block not found",
serviceAndExpectedBlockFunc: func(ctrl *gomock.Controller) (*Service, interface{}) {
state := states.NewMockState(ctrl)
state.EXPECT().GetBlockIDAtHeight(blockHeight).Return(blockID, nil)
state.EXPECT().GetBlockID(blockHeight).Return(blockID, nil)

manager := executor.NewMockManager(ctrl)
manager.EXPECT().GetStatelessBlock(blockID).Return(nil, database.ErrNotFound)
Expand All @@ -2822,7 +2822,7 @@ func TestServiceGetBlockByHeight(t *testing.T) {
block.EXPECT().Txs().Return(nil)

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

manager := executor.NewMockManager(ctrl)
manager.EXPECT().GetStatelessBlock(blockID).Return(block, nil)
Expand All @@ -2847,7 +2847,7 @@ func TestServiceGetBlockByHeight(t *testing.T) {
block.EXPECT().Bytes().Return(blockBytes)

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

expected, err := formatting.Encode(formatting.Hex, blockBytes)
require.NoError(err)
Expand Down Expand Up @@ -2875,7 +2875,7 @@ func TestServiceGetBlockByHeight(t *testing.T) {
block.EXPECT().Bytes().Return(blockBytes)

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

expected, err := formatting.Encode(formatting.HexC, blockBytes)
require.NoError(err)
Expand Down Expand Up @@ -2903,7 +2903,7 @@ func TestServiceGetBlockByHeight(t *testing.T) {
block.EXPECT().Bytes().Return(blockBytes)

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

expected, err := formatting.Encode(formatting.HexNC, blockBytes)
require.NoError(err)
Expand Down
4 changes: 2 additions & 2 deletions vms/avm/states/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (d *diff) AddTx(tx *txs.Tx) {
d.addedTxs[tx.ID()] = tx
}

func (d *diff) GetBlockIDAtHeight(height uint64) (ids.ID, error) {
func (d *diff) GetBlockID(height uint64) (ids.ID, error) {
if blkID, exists := d.addedBlockIDs[height]; exists {
return blkID, nil
}
Expand All @@ -113,7 +113,7 @@ func (d *diff) GetBlockIDAtHeight(height uint64) (ids.ID, error) {
if !ok {
return ids.Empty, fmt.Errorf("%w: %s", ErrMissingParentState, d.parentID)
}
return parentState.GetBlockIDAtHeight(height)
return parentState.GetBlockID(height)
}

func (d *diff) GetBlock(blkID ids.ID) (blocks.Block, error) {
Expand Down
36 changes: 18 additions & 18 deletions vms/avm/states/mock_states.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions vms/avm/states/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type ReadOnlyChain interface {
GetUTXOFromID(utxoID *avax.UTXOID) (*avax.UTXO, error)

GetTx(txID ids.ID) (*txs.Tx, error)
GetBlockIDAtHeight(height uint64) (ids.ID, error)
GetBlockID(height uint64) (ids.ID, error)
GetBlock(blkID ids.ID) (blocks.Block, error)
GetLastAccepted() ids.ID
GetTimestamp() time.Time
Expand Down Expand Up @@ -288,7 +288,7 @@ func (s *state) AddTx(tx *txs.Tx) {
s.addedTxs[tx.ID()] = tx
}

func (s *state) GetBlockIDAtHeight(height uint64) (ids.ID, error) {
func (s *state) GetBlockID(height uint64) (ids.ID, error) {
if blkID, exists := s.addedBlockIDs[height]; exists {
return blkID, nil
}
Expand Down
10 changes: 5 additions & 5 deletions vms/avm/states/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func ChainTxTest(t *testing.T, c Chain) {
func ChainBlockTest(t *testing.T, c Chain) {
require := require.New(t)

fetchedBlkID, err := c.GetBlockIDAtHeight(populatedBlkHeight)
fetchedBlkID, err := c.GetBlockID(populatedBlkHeight)
require.NoError(err)
require.Equal(populatedBlkID, fetchedBlkID)

Expand All @@ -220,7 +220,7 @@ func ChainBlockTest(t *testing.T, c Chain) {
require.Equal(populatedBlk.ID(), fetchedBlk.ID())

// Pull again for the cached path
fetchedBlkID, err = c.GetBlockIDAtHeight(populatedBlkHeight)
fetchedBlkID, err = c.GetBlockID(populatedBlkHeight)
require.NoError(err)
require.Equal(populatedBlkID, fetchedBlkID)

Expand All @@ -247,22 +247,22 @@ func ChainBlockTest(t *testing.T, c Chain) {
blkID := blk.ID()
blkHeight := blk.Height()

_, err = c.GetBlockIDAtHeight(blkHeight)
_, err = c.GetBlockID(blkHeight)
require.ErrorIs(err, database.ErrNotFound)

_, err = c.GetBlock(blkID)
require.ErrorIs(err, database.ErrNotFound)

// Pull again for the cached path
_, err = c.GetBlockIDAtHeight(blkHeight)
_, err = c.GetBlockID(blkHeight)
require.ErrorIs(err, database.ErrNotFound)

_, err = c.GetBlock(blkID)
require.ErrorIs(err, database.ErrNotFound)

c.AddBlock(blk)

fetchedBlkID, err = c.GetBlockIDAtHeight(blkHeight)
fetchedBlkID, err = c.GetBlockID(blkHeight)
require.NoError(err)
require.Equal(blkID, fetchedBlkID)

Expand Down
14 changes: 0 additions & 14 deletions vms/platformvm/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,6 @@ 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 @@ -868,15 +866,3 @@ 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: 0 additions & 37 deletions vms/platformvm/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2595,43 +2595,6 @@ 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

0 comments on commit b456e16

Please sign in to comment.