Skip to content

Commit

Permalink
Add start and end timestamp to SlotInfo (#2631)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkrvivian authored May 12, 2023
1 parent 9b67c00 commit 96b47a1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
18 changes: 12 additions & 6 deletions packages/app/jsonmodels/slots.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
package jsonmodels

import (
"time"

"github.com/iotaledger/goshimmer/packages/core/commitment"
"github.com/iotaledger/hive.go/core/slot"
"github.com/iotaledger/hive.go/ds/types"
)

type SlotInfo struct {
ID string `json:"id"`
Index uint64 `json:"index"`
RootsID string `json:"rootsID"`
PrevID string `json:"prevID"`
CumulativeWeight int64 `json:"cumulativeWeight"`
ID string `json:"id"`
Index uint64 `json:"index"`
RootsID string `json:"rootsID"`
PrevID string `json:"prevID"`
CumulativeWeight int64 `json:"cumulativeWeight"`
StartTimestamp time.Time `json:"startTimestamp"`
EndTimestamp time.Time `json:"endTimestamp"`
}

func SlotInfoFromRecord(c *commitment.Commitment) *SlotInfo {
func SlotInfoFromRecord(c *commitment.Commitment, start, end time.Time) *SlotInfo {
return &SlotInfo{
ID: c.ID().Base58(),
Index: uint64(c.Index()),
RootsID: c.RootsID().Base58(),
PrevID: c.PrevID().Base58(),
CumulativeWeight: c.CumulativeWeight(),
StartTimestamp: start,
EndTimestamp: end,
}
}

Expand Down
20 changes: 17 additions & 3 deletions plugins/webapi/slot/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/http"
"strconv"
"sync"
"time"

"github.com/labstack/echo/v4"
"github.com/pkg/errors"
Expand Down Expand Up @@ -73,7 +74,9 @@ func GetCurrentSC(c echo.Context) error {
currentSlotCommitmentLock.RLock()
defer currentSlotCommitmentLock.RUnlock()

return c.JSON(http.StatusOK, jsonmodels.SlotInfoFromRecord(currentSlotCommitment))
start, end := startEndTimestamps(currentSlotCommitment.Index())

return c.JSON(http.StatusOK, jsonmodels.SlotInfoFromRecord(currentSlotCommitment, start, end))
}

func GetCommittedSlot(c echo.Context) error {
Expand All @@ -87,7 +90,9 @@ func GetCommittedSlot(c echo.Context) error {
return c.JSON(http.StatusBadRequest, jsonmodels.NewErrorResponse(errors.New("commitment not exists")))
}

return c.JSON(http.StatusOK, jsonmodels.SlotInfoFromRecord(cc.M.Commitment))
start, end := startEndTimestamps(index)

return c.JSON(http.StatusOK, jsonmodels.SlotInfoFromRecord(cc.M.Commitment, start, end))
}

func GetCommittedSlotByCommitment(c echo.Context) error {
Expand All @@ -102,7 +107,9 @@ func GetCommittedSlotByCommitment(c echo.Context) error {
return c.JSON(http.StatusBadRequest, jsonmodels.NewErrorResponse(errors.New("commitment not exists")))
}

return c.JSON(http.StatusOK, jsonmodels.SlotInfoFromRecord(cc.M.Commitment))
start, end := startEndTimestamps(ID.Index())

return c.JSON(http.StatusOK, jsonmodels.SlotInfoFromRecord(cc.M.Commitment, start, end))
}

func GetUTXOs(c echo.Context) error {
Expand Down Expand Up @@ -178,6 +185,13 @@ func getIndex(c echo.Context) (slot.Index, error) {
return slot.Index(uint64(indexNumber)), nil
}

func startEndTimestamps(index slot.Index) (start, end time.Time) {
start = deps.Protocol.SlotTimeProvider().StartTime(index)
end = deps.Protocol.SlotTimeProvider().EndTime(index)

return
}

// func getVotersWeight(c echo.Context) error {
// ei, err := getIndex(c)
// if err != nil {
Expand Down

0 comments on commit 96b47a1

Please sign in to comment.