github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/admin/commands/storage/read_range_blocks.go (about)

     1  package storage
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/rs/zerolog/log"
     7  
     8  	"github.com/onflow/flow-go/admin"
     9  	"github.com/onflow/flow-go/admin/commands"
    10  	"github.com/onflow/flow-go/cmd/util/cmd/read-light-block"
    11  	"github.com/onflow/flow-go/storage"
    12  )
    13  
    14  var _ commands.AdminCommand = (*ReadRangeBlocksCommand)(nil)
    15  
    16  // 10001 instead of 10000, because 10000 won't allow a range from 10000 to 20000,
    17  // which is easier to type than [10001, 20000]
    18  const Max_Range_Block_Limit = uint64(10001)
    19  
    20  type ReadRangeBlocksCommand struct {
    21  	blocks storage.Blocks
    22  }
    23  
    24  func NewReadRangeBlocksCommand(blocks storage.Blocks) commands.AdminCommand {
    25  	return &ReadRangeBlocksCommand{
    26  		blocks: blocks,
    27  	}
    28  }
    29  
    30  func (c *ReadRangeBlocksCommand) Handler(ctx context.Context, req *admin.CommandRequest) (interface{}, error) {
    31  	reqData, err := parseHeightRangeRequestData(req)
    32  	if err != nil {
    33  		return nil, err
    34  	}
    35  
    36  	log.Info().Str("module", "admin-tool").Msgf("read range blocks, data: %v", reqData)
    37  
    38  	if reqData.Range() > Max_Range_Block_Limit {
    39  		return nil, admin.NewInvalidAdminReqErrorf("getting for more than %v blocks at a time might have an impact to node's performance and is not allowed", Max_Range_Block_Limit)
    40  	}
    41  
    42  	lights, err := read.ReadLightBlockByHeightRange(c.blocks, reqData.startHeight, reqData.endHeight)
    43  	if err != nil {
    44  		return nil, err
    45  	}
    46  	return commands.ConvertToInterfaceList(lights)
    47  }
    48  
    49  func (c *ReadRangeBlocksCommand) Validator(req *admin.CommandRequest) error {
    50  	return nil
    51  }