Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Checkpoint ID in REST APIs Result #806

Merged
merged 3 commits into from
Apr 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions checkpoint/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func GetQueryCmd(cdc *codec.Codec) *cobra.Command {
GetQueryParams(cdc),
GetCheckpointBuffer(cdc),
GetLastNoACK(cdc),
GetHeaderFromIndex(cdc),
GetCheckpointByNumber(cdc),
GetCheckpointCount(cdc),
)...,
)
Expand Down Expand Up @@ -132,10 +132,10 @@ func GetLastNoACK(cdc *codec.Codec) *cobra.Command {
}

// GetHeaderFromIndex get checkpoint given header index
func GetHeaderFromIndex(cdc *codec.Codec) *cobra.Command {
func GetCheckpointByNumber(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "header",
Short: "get checkpoint (header) from index",
Short: "get checkpoint (header) by number",
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
headerNumber := viper.GetUint64(FlagHeaderNumber)
Expand Down
56 changes: 52 additions & 4 deletions checkpoint/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,15 +406,44 @@ func latestCheckpointHandlerFunc(cliCtx context.CLIContext) http.HandlerFunc {
return
}

var checkpointUnmarshal hmTypes.Checkpoint
temaniarpit27 marked this conversation as resolved.
Show resolved Hide resolved
json.Unmarshal(res, &checkpointUnmarshal)

checkpointWithID := &CheckpointWithID{
ID: ackCount,
Proposer: checkpointUnmarshal.Proposer,
StartBlock: checkpointUnmarshal.StartBlock,
EndBlock: checkpointUnmarshal.EndBlock,
RootHash: checkpointUnmarshal.RootHash,
BorChainID: checkpointUnmarshal.BorChainID,
TimeStamp: checkpointUnmarshal.TimeStamp,
}

resWithID, err := json.Marshal(checkpointWithID)
if err != nil {
hmRest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
}

// error if no checkpoint found
if ok := hmRest.ReturnNotFoundIfNoContent(w, res, "No checkpoint found"); !ok {
if ok := hmRest.ReturnNotFoundIfNoContent(w, resWithID, "No checkpoint found"); !ok {
return
}
cliCtx = cliCtx.WithHeight(height)
rest.PostProcessResponse(w, cliCtx, res)
rest.PostProcessResponse(w, cliCtx, resWithID)
}
}

//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"`
}

// get checkpoint by checkppint number from store
func checkpointByNumberHandlerFunc(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -444,12 +473,31 @@ func checkpointByNumberHandlerFunc(cliCtx context.CLIContext) http.HandlerFunc {
return
}

var checkpointUnmarshal hmTypes.Checkpoint
json.Unmarshal(res, &checkpointUnmarshal)

checkpointWithID := &CheckpointWithID{
ID: number,
Proposer: checkpointUnmarshal.Proposer,
StartBlock: checkpointUnmarshal.StartBlock,
EndBlock: checkpointUnmarshal.EndBlock,
RootHash: checkpointUnmarshal.RootHash,
BorChainID: checkpointUnmarshal.BorChainID,
TimeStamp: checkpointUnmarshal.TimeStamp,
}

resWithID, err := json.Marshal(checkpointWithID)
if err != nil {
hmRest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
}

// check content
if ok := hmRest.ReturnNotFoundIfNoContent(w, res, "No checkpoint found"); !ok {
if ok := hmRest.ReturnNotFoundIfNoContent(w, resWithID, "No checkpoint found"); !ok {
return
}
cliCtx = cliCtx.WithHeight(height)
rest.PostProcessResponse(w, cliCtx, res)
rest.PostProcessResponse(w, cliCtx, resWithID)

}
}

Expand Down