From 96b47a122e09324ab293254b3760f54eef185afd Mon Sep 17 00:00:00 2001 From: "Ching-Hua (Vivian) Lin" Date: Fri, 12 May 2023 21:53:10 +0800 Subject: [PATCH] Add start and end timestamp to SlotInfo (#2631) --- packages/app/jsonmodels/slots.go | 18 ++++++++++++------ plugins/webapi/slot/plugin.go | 20 +++++++++++++++++--- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/packages/app/jsonmodels/slots.go b/packages/app/jsonmodels/slots.go index 7817f699bf..e0943302d1 100644 --- a/packages/app/jsonmodels/slots.go +++ b/packages/app/jsonmodels/slots.go @@ -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, } } diff --git a/plugins/webapi/slot/plugin.go b/plugins/webapi/slot/plugin.go index e0cb339f6e..b011a44ad8 100644 --- a/plugins/webapi/slot/plugin.go +++ b/plugins/webapi/slot/plugin.go @@ -4,6 +4,7 @@ import ( "net/http" "strconv" "sync" + "time" "github.com/labstack/echo/v4" "github.com/pkg/errors" @@ -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 { @@ -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 { @@ -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 { @@ -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 {