From c6249054419f72405c392697479d99a3b15f5a73 Mon Sep 17 00:00:00 2001 From: muXxer Date: Tue, 5 Dec 2023 18:18:58 +0100 Subject: [PATCH] Add LatestParentBlockIssuingTime to BlockIssuance endpoint --- components/restapi/core/blocks.go | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/components/restapi/core/blocks.go b/components/restapi/core/blocks.go index bab9de006..cd3b91bd0 100644 --- a/components/restapi/core/blocks.go +++ b/components/restapi/core/blocks.go @@ -1,6 +1,8 @@ package core import ( + "time" + "github.com/labstack/echo/v4" "github.com/iotaledger/hive.go/ierrors" @@ -84,12 +86,28 @@ func blockIssuance() (*api.IssuanceBlockHeaderResponse, error) { return nil, ierrors.Wrap(echo.ErrServiceUnavailable, "no strong parents available") } + // get the latest parent block issuing time + var latestParentBlockIssuingTime time.Time + for _, parentType := range []iotago.ParentsType{iotago.StrongParentType, iotago.WeakParentType, iotago.ShallowLikeParentType} { + for _, blockID := range references[parentType] { + block, exists := deps.Protocol.Engines.Main.Get().Block(blockID) + if !exists { + return nil, ierrors.Wrapf(echo.ErrNotFound, "no block found for parent, block ID: %s", blockID.ToHex()) + } + + if latestParentBlockIssuingTime.Before(block.ProtocolBlock().Header.IssuingTime) { + latestParentBlockIssuingTime = block.ProtocolBlock().Header.IssuingTime + } + } + } + resp := &api.IssuanceBlockHeaderResponse{ - StrongParents: references[iotago.StrongParentType], - WeakParents: references[iotago.WeakParentType], - ShallowLikeParents: references[iotago.ShallowLikeParentType], - LatestFinalizedSlot: deps.Protocol.Engines.Main.Get().SyncManager.LatestFinalizedSlot(), - LatestCommitment: deps.Protocol.Engines.Main.Get().SyncManager.LatestCommitment().Commitment(), + StrongParents: references[iotago.StrongParentType], + WeakParents: references[iotago.WeakParentType], + ShallowLikeParents: references[iotago.ShallowLikeParentType], + LatestParentBlockIssuingTime: latestParentBlockIssuingTime, + LatestFinalizedSlot: deps.Protocol.Engines.Main.Get().SyncManager.LatestFinalizedSlot(), + LatestCommitment: deps.Protocol.Engines.Main.Get().SyncManager.LatestCommitment().Commitment(), } return resp, nil