Skip to content

Commit

Permalink
Merge pull request #1078 from maticnetwork/backport/master-to-develop
Browse files Browse the repository at this point in the history
  • Loading branch information
marcello33 authored Oct 5, 2023
2 parents beaaa4d + fa1846d commit 3fcbae3
Show file tree
Hide file tree
Showing 17 changed files with 1,931 additions and 292 deletions.
52 changes: 46 additions & 6 deletions checkpoint/client/rest/query_milestone.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
jsoniter "github.com/json-iterator/go"

"github.com/maticnetwork/heimdall/checkpoint/types"
"github.com/maticnetwork/heimdall/helper"
hmRest "github.com/maticnetwork/heimdall/types/rest"
)

Expand All @@ -29,8 +30,16 @@ func milestoneLatestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return
}

// fetch checkpoint
// Fetch latest milestone
result, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryLatestMilestone), nil)

// Return status code 503 (Service Unavailable) if HF hasn't been activated
if height < helper.GetAalborgHardForkHeight() {
hmRest.WriteErrorResponse(w, http.StatusServiceUnavailable, "Aalborg hardfork not activated yet")

return
}

if err != nil {
hmRest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())

Expand All @@ -43,11 +52,11 @@ func milestoneLatestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
}
}

// swagger:route GET /checkpoints/count checkpoint checkpointCount
// It returns the checkpoint counts
// swagger:route GET /milestone/count milestone milestoneCount
// It returns the milestone count
// responses:
//
// 200: checkpointCountResponse
// 200: milestoneCountResponse
func milestoneCountHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
Expand All @@ -56,6 +65,14 @@ func milestoneCountHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
}

countBytes, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryCount), nil)

// Return status code 503 (Service Unavailable) if HF hasn't been activated
if height < helper.GetAalborgHardForkHeight() {
hmRest.WriteErrorResponse(w, http.StatusServiceUnavailable, "Aalborg hardfork not activated yet")

return
}

if err != nil {
hmRest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
Expand Down Expand Up @@ -106,8 +123,16 @@ func milestoneByNumberHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return
}

// query checkpoint
// query milestone
res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryMilestoneByNumber), queryParams)

// Return status code 503 (Service Unavailable) if HF hasn't been activated
if height < helper.GetAalborgHardForkHeight() {
hmRest.WriteErrorResponse(w, http.StatusServiceUnavailable, "Aalborg hardfork not activated yet")

return
}

if err != nil {
hmRest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
Expand All @@ -126,8 +151,15 @@ func latestNoAckMilestoneHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return
}

// fetch checkpoint
result, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryLatestNoAckMilestone), nil)

// Return status code 503 (Service Unavailable) if HF hasn't been activated
if height < helper.GetAalborgHardForkHeight() {
hmRest.WriteErrorResponse(w, http.StatusServiceUnavailable, "Aalborg hardfork not activated yet")

return
}

if err != nil {
hmRest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
Expand Down Expand Up @@ -176,6 +208,14 @@ func noAckMilestoneByIDHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
}

result, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryNoAckMilestoneByID), queryID)

// Return status code 503 (Service Unavailable) if HF hasn't been activated
if height < helper.GetAalborgHardForkHeight() {
hmRest.WriteErrorResponse(w, http.StatusServiceUnavailable, "Aalborg hardfork not activated yet")

return
}

if err != nil {
hmRest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
Expand Down
4 changes: 2 additions & 2 deletions checkpoint/handler_milestone.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

// handleMsgMilestone validates milestone transaction
func handleMsgMilestone(ctx sdk.Context, msg types.MsgMilestone, k Keeper) sdk.Result {
logger := k.Logger(ctx)
logger := k.MilestoneLogger(ctx)
milestoneLength := helper.MilestoneLength

//
Expand Down Expand Up @@ -114,7 +114,7 @@ func handleMsgMilestone(ctx sdk.Context, msg types.MsgMilestone, k Keeper) sdk.R

// Handles milestone timeout transaction
func handleMsgMilestoneTimeout(ctx sdk.Context, _ types.MsgMilestoneTimeout, k Keeper) sdk.Result {
logger := k.Logger(ctx)
logger := k.MilestoneLogger(ctx)

// Get current block time
currentTime := ctx.BlockTime()
Expand Down
6 changes: 6 additions & 0 deletions checkpoint/keeper_milestone.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"strconv"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/tendermint/tendermint/libs/log"

cmn "github.com/maticnetwork/heimdall/common"
"github.com/maticnetwork/heimdall/helper"
Expand All @@ -19,6 +20,11 @@ var (
BlockNumberKey = []byte{0x70} //Key to store the count
)

// Logger returns a module-specific logger
func (k Keeper) MilestoneLogger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", "Milestone")
}

// AddMilestone adds milestone in the store
func (k *Keeper) AddMilestone(ctx sdk.Context, milestone hmTypes.Milestone) error {
milestoneNumber := k.GetMilestoneCount(ctx) + 1 //GetCount gives the number of previous milestone
Expand Down
6 changes: 3 additions & 3 deletions checkpoint/side_milestone_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func SideHandleMsgMilestone(ctx sdk.Context, k Keeper, msg types.MsgMilestone, c
milestoneLength := helper.MilestoneLength

// logger
logger := k.Logger(ctx)
logger := k.MilestoneLogger(ctx)

//Get the milestone count
count := k.GetMilestoneCount(ctx)
Expand Down Expand Up @@ -72,7 +72,7 @@ func SideHandleMsgMilestone(ctx sdk.Context, k Keeper, msg types.MsgMilestone, c

// PostHandleMsgMilestone handles msg milestone
func PostHandleMsgMilestone(ctx sdk.Context, k Keeper, msg types.MsgMilestone, sideTxResult abci.SideTxResultType) sdk.Result {
logger := k.Logger(ctx)
logger := k.MilestoneLogger(ctx)
timeStamp := uint64(ctx.BlockTime().Unix())

// TX bytes
Expand Down Expand Up @@ -109,7 +109,7 @@ func PostHandleMsgMilestone(ctx sdk.Context, k Keeper, msg types.MsgMilestone, s
if lastMilestone, err := k.GetLastMilestone(ctx); err == nil { // fetch last milestone from store
// make sure new milestoen is after tip
if lastMilestone.EndBlock > msg.StartBlock {
logger.Error(" already exists",
logger.Error(" Milestone already exists",
"currentTip", lastMilestone.EndBlock,
"startBlock", msg.StartBlock,
)
Expand Down
5 changes: 3 additions & 2 deletions checkpoint/types/merkel_milestone.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package types

import (
"errors"
"fmt"

"github.com/maticnetwork/heimdall/helper"
hmTypes "github.com/maticnetwork/heimdall/types"
Expand All @@ -13,12 +14,12 @@ func ValidateMilestone(start uint64, end uint64, rootHash hmTypes.HeimdallHash,

//Check for the minimum length of the milestone
if msgMilestoneLength < int64(milestoneLength) {
return false, errors.New("Invalid milestone, difference in start and end block is less than milestone length")
return false, errors.New(fmt.Sprint("Invalid milestone, difference in start and end block is less than milestone length", "Milestone Length:", milestoneLength))
}

// Check if blocks+confirmations exist locally
if !contractCaller.CheckIfBlocksExist(end + confirmations) {
return false, errors.New("blocks not found locally")
return false, errors.New(fmt.Sprint("End block number with confirmation is not availbale in the Bor chain", "EndBlock", end, "Confirmation", confirmations))
}

//Get the vote on hash of milestone from Bor
Expand Down
Loading

0 comments on commit 3fcbae3

Please sign in to comment.