github.com/palisadeinc/bor@v0.0.0-20230615125219-ab7196213d15/consensus/bor/heimdallgrpc/checkpoint.go (about)

     1  package heimdallgrpc
     2  
     3  import (
     4  	"context"
     5  	"math/big"
     6  
     7  	"github.com/ethereum/go-ethereum/consensus/bor/heimdall/checkpoint"
     8  	"github.com/ethereum/go-ethereum/log"
     9  
    10  	proto "github.com/maticnetwork/polyproto/heimdall"
    11  	protoutils "github.com/maticnetwork/polyproto/utils"
    12  )
    13  
    14  func (h *HeimdallGRPCClient) FetchCheckpointCount(ctx context.Context) (int64, error) {
    15  	log.Info("Fetching checkpoint count")
    16  
    17  	res, err := h.client.FetchCheckpointCount(ctx, nil)
    18  	if err != nil {
    19  		return 0, err
    20  	}
    21  
    22  	log.Info("Fetched checkpoint count")
    23  
    24  	return res.Result.Result, nil
    25  }
    26  
    27  func (h *HeimdallGRPCClient) FetchCheckpoint(ctx context.Context, number int64) (*checkpoint.Checkpoint, error) {
    28  	req := &proto.FetchCheckpointRequest{
    29  		ID: number,
    30  	}
    31  
    32  	log.Info("Fetching checkpoint", "number", number)
    33  
    34  	res, err := h.client.FetchCheckpoint(ctx, req)
    35  	if err != nil {
    36  		return nil, err
    37  	}
    38  
    39  	log.Info("Fetched checkpoint", "number", number)
    40  
    41  	checkpoint := &checkpoint.Checkpoint{
    42  		StartBlock: new(big.Int).SetUint64(res.Result.StartBlock),
    43  		EndBlock:   new(big.Int).SetUint64(res.Result.EndBlock),
    44  		RootHash:   protoutils.ConvertH256ToHash(res.Result.RootHash),
    45  		Proposer:   protoutils.ConvertH160toAddress(res.Result.Proposer),
    46  		BorChainID: res.Result.BorChainID,
    47  		Timestamp:  uint64(res.Result.Timestamp.GetSeconds()),
    48  	}
    49  
    50  	return checkpoint, nil
    51  }