// (c) 2021-2022, Lux Partners Limited. All rights reserved.
// See the file LICENSE for licensing terms.

package message

import (
	"context"
	"fmt"

	"github.com/luxfi/node/ids"

	"github.com/ethereum/go-ethereum/common"
)

var (
	_ Request = BlockRequest{}
)

// BlockRequest is a request to retrieve Parents number of blocks starting from Hash from newest-oldest manner
type BlockRequest struct {
	Hash    common.Hash `serialize:"true"`
	Height  uint64      `serialize:"true"`
	Parents uint16      `serialize:"true"`
}

func (b BlockRequest) String() string {
	return fmt.Sprintf(
		"BlockRequest(Hash=%s, Height=%d, Parents=%d)",
		b.Hash, b.Height, b.Parents,
	)
}

func (b BlockRequest) Handle(ctx context.Context, nodeID ids.NodeID, requestID uint32, handler RequestHandler) ([]byte, error) {
	return handler.HandleBlockRequest(ctx, nodeID, requestID, b)
}

// BlockResponse is a response to a BlockRequest
// Blocks is slice of RLP encoded blocks starting with the block
// requested in BlockRequest.Hash. The next block is the parent, etc.
// handler: handlers.BlockRequestHandler
type BlockResponse struct {
	Blocks [][]byte `serialize:"true"`
}
