Skip to content

Commit b456e16

Browse files
Revert P-Chain height indexing (ava-labs#1591)
1 parent 00e61d8 commit b456e16

File tree

11 files changed

+40
-439
lines changed

11 files changed

+40
-439
lines changed

vms/avm/service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func (s *Service) GetBlockByHeight(_ *http.Request, args *api.GetBlockByHeightAr
117117
}
118118
reply.Encoding = args.Encoding
119119

120-
blockID, err := s.vm.state.GetBlockIDAtHeight(uint64(args.Height))
120+
blockID, err := s.vm.state.GetBlockID(uint64(args.Height))
121121
if err != nil {
122122
return fmt.Errorf("couldn't get block at height %d: %w", args.Height, err)
123123
}

vms/avm/service_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2777,7 +2777,7 @@ func TestServiceGetBlockByHeight(t *testing.T) {
27772777
name: "block height not found",
27782778
serviceAndExpectedBlockFunc: func(ctrl *gomock.Controller) (*Service, interface{}) {
27792779
state := states.NewMockState(ctrl)
2780-
state.EXPECT().GetBlockIDAtHeight(blockHeight).Return(ids.Empty, database.ErrNotFound)
2780+
state.EXPECT().GetBlockID(blockHeight).Return(ids.Empty, database.ErrNotFound)
27812781

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

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

28242824
state := states.NewMockState(ctrl)
2825-
state.EXPECT().GetBlockIDAtHeight(blockHeight).Return(blockID, nil)
2825+
state.EXPECT().GetBlockID(blockHeight).Return(blockID, nil)
28262826

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

28492849
state := states.NewMockState(ctrl)
2850-
state.EXPECT().GetBlockIDAtHeight(blockHeight).Return(blockID, nil)
2850+
state.EXPECT().GetBlockID(blockHeight).Return(blockID, nil)
28512851

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

28772877
state := states.NewMockState(ctrl)
2878-
state.EXPECT().GetBlockIDAtHeight(blockHeight).Return(blockID, nil)
2878+
state.EXPECT().GetBlockID(blockHeight).Return(blockID, nil)
28792879

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

29052905
state := states.NewMockState(ctrl)
2906-
state.EXPECT().GetBlockIDAtHeight(blockHeight).Return(blockID, nil)
2906+
state.EXPECT().GetBlockID(blockHeight).Return(blockID, nil)
29072907

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

vms/avm/states/diff.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func (d *diff) AddTx(tx *txs.Tx) {
104104
d.addedTxs[tx.ID()] = tx
105105
}
106106

107-
func (d *diff) GetBlockIDAtHeight(height uint64) (ids.ID, error) {
107+
func (d *diff) GetBlockID(height uint64) (ids.ID, error) {
108108
if blkID, exists := d.addedBlockIDs[height]; exists {
109109
return blkID, nil
110110
}
@@ -113,7 +113,7 @@ func (d *diff) GetBlockIDAtHeight(height uint64) (ids.ID, error) {
113113
if !ok {
114114
return ids.Empty, fmt.Errorf("%w: %s", ErrMissingParentState, d.parentID)
115115
}
116-
return parentState.GetBlockIDAtHeight(height)
116+
return parentState.GetBlockID(height)
117117
}
118118

119119
func (d *diff) GetBlock(blkID ids.ID) (blocks.Block, error) {

vms/avm/states/mock_states.go

Lines changed: 18 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vms/avm/states/state.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ type ReadOnlyChain interface {
5151
GetUTXOFromID(utxoID *avax.UTXOID) (*avax.UTXO, error)
5252

5353
GetTx(txID ids.ID) (*txs.Tx, error)
54-
GetBlockIDAtHeight(height uint64) (ids.ID, error)
54+
GetBlockID(height uint64) (ids.ID, error)
5555
GetBlock(blkID ids.ID) (blocks.Block, error)
5656
GetLastAccepted() ids.ID
5757
GetTimestamp() time.Time
@@ -288,7 +288,7 @@ func (s *state) AddTx(tx *txs.Tx) {
288288
s.addedTxs[tx.ID()] = tx
289289
}
290290

291-
func (s *state) GetBlockIDAtHeight(height uint64) (ids.ID, error) {
291+
func (s *state) GetBlockID(height uint64) (ids.ID, error) {
292292
if blkID, exists := s.addedBlockIDs[height]; exists {
293293
return blkID, nil
294294
}

vms/avm/states/state_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ func ChainTxTest(t *testing.T, c Chain) {
211211
func ChainBlockTest(t *testing.T, c Chain) {
212212
require := require.New(t)
213213

214-
fetchedBlkID, err := c.GetBlockIDAtHeight(populatedBlkHeight)
214+
fetchedBlkID, err := c.GetBlockID(populatedBlkHeight)
215215
require.NoError(err)
216216
require.Equal(populatedBlkID, fetchedBlkID)
217217

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

222222
// Pull again for the cached path
223-
fetchedBlkID, err = c.GetBlockIDAtHeight(populatedBlkHeight)
223+
fetchedBlkID, err = c.GetBlockID(populatedBlkHeight)
224224
require.NoError(err)
225225
require.Equal(populatedBlkID, fetchedBlkID)
226226

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

250-
_, err = c.GetBlockIDAtHeight(blkHeight)
250+
_, err = c.GetBlockID(blkHeight)
251251
require.ErrorIs(err, database.ErrNotFound)
252252

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

256256
// Pull again for the cached path
257-
_, err = c.GetBlockIDAtHeight(blkHeight)
257+
_, err = c.GetBlockID(blkHeight)
258258
require.ErrorIs(err, database.ErrNotFound)
259259

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

263263
c.AddBlock(blk)
264264

265-
fetchedBlkID, err = c.GetBlockIDAtHeight(blkHeight)
265+
fetchedBlkID, err = c.GetBlockID(blkHeight)
266266
require.NoError(err)
267267
require.Equal(blkID, fetchedBlkID)
268268

vms/platformvm/client.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,6 @@ type Client interface {
255255
GetValidatorsAt(ctx context.Context, subnetID ids.ID, height uint64, options ...rpc.Option) (map[ids.NodeID]uint64, error)
256256
// GetBlock returns the block with the given id.
257257
GetBlock(ctx context.Context, blockID ids.ID, options ...rpc.Option) ([]byte, error)
258-
// GetBlockByHeight returns the block at the given [height].
259-
GetBlockByHeight(ctx context.Context, height uint64, options ...rpc.Option) ([]byte, error)
260258
}
261259

262260
// Client implementation for interacting with the P Chain endpoint
@@ -868,15 +866,3 @@ func (c *client) GetBlock(ctx context.Context, blockID ids.ID, options ...rpc.Op
868866
}
869867
return formatting.Decode(res.Encoding, res.Block)
870868
}
871-
872-
func (c *client) GetBlockByHeight(ctx context.Context, height uint64, options ...rpc.Option) ([]byte, error) {
873-
res := &api.FormattedBlock{}
874-
err := c.requester.SendRequest(ctx, "platform.getBlockByHeight", &api.GetBlockByHeightArgs{
875-
Height: json.Uint64(height),
876-
Encoding: formatting.HexNC,
877-
}, res, options...)
878-
if err != nil {
879-
return nil, err
880-
}
881-
return formatting.Decode(res.Encoding, res.Block)
882-
}

vms/platformvm/service.go

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2595,43 +2595,6 @@ func (s *Service) GetBlock(_ *http.Request, args *api.GetBlockArgs, response *ap
25952595
return nil
25962596
}
25972597

2598-
// GetBlockByHeight returns the block at the given height.
2599-
func (s *Service) GetBlockByHeight(_ *http.Request, args *api.GetBlockByHeightArgs, response *api.GetBlockResponse) error {
2600-
s.vm.ctx.Log.Debug("API called",
2601-
zap.String("service", "platform"),
2602-
zap.String("method", "getBlockByHeight"),
2603-
zap.Uint64("height", uint64(args.Height)),
2604-
)
2605-
2606-
response.Encoding = args.Encoding
2607-
2608-
blockID, err := s.vm.state.GetBlockIDAtHeight(uint64(args.Height))
2609-
if err != nil {
2610-
return fmt.Errorf("couldn't get block at height %d: %w", args.Height, err)
2611-
}
2612-
block, err := s.vm.manager.GetStatelessBlock(blockID)
2613-
if err != nil {
2614-
s.vm.ctx.Log.Error("couldn't get accepted block",
2615-
zap.Stringer("blkID", blockID),
2616-
zap.Error(err),
2617-
)
2618-
return fmt.Errorf("couldn't get block with id %s: %w", blockID, err)
2619-
}
2620-
2621-
if args.Encoding == formatting.JSON {
2622-
block.InitCtx(s.vm.ctx)
2623-
response.Block = block
2624-
return nil
2625-
}
2626-
2627-
response.Block, err = formatting.Encode(args.Encoding, block.Bytes())
2628-
if err != nil {
2629-
return fmt.Errorf("couldn't encode block %s as string: %w", blockID, err)
2630-
}
2631-
2632-
return nil
2633-
}
2634-
26352598
func (s *Service) getAPIUptime(staker *state.Staker) (*json.Float32, error) {
26362599
// Only report uptimes that we have been actively tracking.
26372600
if constants.PrimaryNetworkID != staker.SubnetID && !s.vm.TrackedSubnets.Contains(staker.SubnetID) {

0 commit comments

Comments
 (0)