Skip to content

Commit

Permalink
Return checkpoint IDs in checkpoints/list (maticnetwork#1183)
Browse files Browse the repository at this point in the history
* Return checkpoint ID in checkpoints/list

* Bitsize

* Refactor and add test
  • Loading branch information
shohamc1 authored Sep 23, 2024
1 parent ec8aca2 commit 956b34e
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 16 deletions.
15 changes: 2 additions & 13 deletions checkpoint/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ func latestCheckpointHandlerFunc(cliCtx context.CLIContext) http.HandlerFunc {
return
}

checkpointWithID := &CheckpointWithID{
checkpointWithID := &hmTypes.CheckpointWithID{
ID: ackCount,
Proposer: checkpointUnmarshal.Proposer,
StartBlock: checkpointUnmarshal.StartBlock,
Expand All @@ -665,17 +665,6 @@ func latestCheckpointHandlerFunc(cliCtx context.CLIContext) http.HandlerFunc {
}
}

// Temporary Checkpoint struct to store the Checkpoint ID
type CheckpointWithID struct {
ID uint64 `json:"id"`
Proposer hmTypes.HeimdallAddress `json:"proposer"`
StartBlock uint64 `json:"start_block"`
EndBlock uint64 `json:"end_block"`
RootHash hmTypes.HeimdallHash `json:"root_hash"`
BorChainID string `json:"bor_chain_id"`
TimeStamp uint64 `json:"timestamp"`
}

//swagger:parameters checkpointById
type checkpointID struct {

Expand Down Expand Up @@ -726,7 +715,7 @@ func checkpointByNumberHandlerFunc(cliCtx context.CLIContext) http.HandlerFunc {
return
}

checkpointWithID := &CheckpointWithID{
checkpointWithID := &hmTypes.CheckpointWithID{
ID: number,
Proposer: checkpointUnmarshal.Proposer,
StartBlock: checkpointUnmarshal.StartBlock,
Expand Down
26 changes: 23 additions & 3 deletions checkpoint/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,11 @@ func (k *Keeper) GetCheckpointByNumber(ctx sdk.Context, number uint64) (hmTypes.
}

// GetCheckpointList returns all checkpoints with params like page and limit
func (k *Keeper) GetCheckpointList(ctx sdk.Context, page uint64, limit uint64) ([]hmTypes.Checkpoint, error) {
func (k *Keeper) GetCheckpointList(ctx sdk.Context, page uint64, limit uint64) ([]hmTypes.CheckpointWithID, error) {
store := ctx.KVStore(k.storeKey)

// create headers
var checkpoints []hmTypes.Checkpoint
var checkpoints []hmTypes.CheckpointWithID

// have max limit
if limit > maxCheckpointListLimit {
Expand All @@ -158,7 +158,22 @@ func (k *Keeper) GetCheckpointList(ctx sdk.Context, page uint64, limit uint64) (
for ; iterator.Valid(); iterator.Next() {
var checkpoint hmTypes.Checkpoint
if err := k.cdc.UnmarshalBinaryBare(iterator.Value(), &checkpoint); err == nil {
checkpoints = append(checkpoints, checkpoint)
id, err := GetCheckpointIDFromKey(iterator.Key())
if err != nil {
continue
}

checkpointWithID := hmTypes.CheckpointWithID{
ID: id,
Proposer: checkpoint.Proposer,
StartBlock: checkpoint.StartBlock,
EndBlock: checkpoint.EndBlock,
RootHash: checkpoint.RootHash,
BorChainID: checkpoint.BorChainID,
TimeStamp: checkpoint.TimeStamp,
}

checkpoints = append(checkpoints, checkpointWithID)
}
}

Expand Down Expand Up @@ -197,6 +212,11 @@ func GetCheckpointKey(checkpointNumber uint64) []byte {
return append(CheckpointKey, checkpointNumberBytes...)
}

// GetCheckpointIDFromKey get the checkpoint ID from the DB key
func GetCheckpointIDFromKey(key []byte) (uint64, error) {
return strconv.ParseUint(string(key[1:]), 10, 64)
}

// HasStoreValue check if value exists in store or not
func (k *Keeper) HasStoreValue(ctx sdk.Context, key []byte) bool {
store := ctx.KVStore(k.storeKey)
Expand Down
4 changes: 4 additions & 0 deletions checkpoint/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ func (suite *KeeperTestSuite) TestGetCheckpointList() {
result, err := keeper.GetCheckpointList(ctx, uint64(1), uint64(20))
require.NoError(t, err)
require.LessOrEqual(t, count, len(result))

for i := range count {
require.Equal(t, uint64(i+1), result[i].ID)
}
}

func (suite *KeeperTestSuite) TestHasStoreValue() {
Expand Down
10 changes: 10 additions & 0 deletions types/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ type Checkpoint struct {
TimeStamp uint64 `json:"timestamp"`
}

type CheckpointWithID struct {
ID uint64 `json:"id"`
Proposer HeimdallAddress `json:"proposer"`
StartBlock uint64 `json:"start_block"`
EndBlock uint64 `json:"end_block"`
RootHash HeimdallHash `json:"root_hash"`
BorChainID string `json:"bor_chain_id"`
TimeStamp uint64 `json:"timestamp"`
}

// Milestone block header struct
type Milestone struct {
Proposer HeimdallAddress `json:"proposer"`
Expand Down

0 comments on commit 956b34e

Please sign in to comment.