diff --git a/components/restapi/core/blocks.go b/components/restapi/core/blocks.go index 2b58599ea..0764cf329 100644 --- a/components/restapi/core/blocks.go +++ b/components/restapi/core/blocks.go @@ -12,7 +12,7 @@ import ( "github.com/iotaledger/iota.go/v4/nodeclient/apimodels" ) -func blockByID(c echo.Context) (*model.Block, error) { +func blockByID(c echo.Context) (*iotago.Block, error) { blockID, err := httpserver.ParseBlockIDParam(c, restapi.ParameterBlockID) if err != nil { return nil, ierrors.Wrapf(err, "failed to parse block ID %s", c.Param(restapi.ParameterBlockID)) @@ -23,7 +23,7 @@ func blockByID(c echo.Context) (*model.Block, error) { return nil, ierrors.Wrapf(echo.ErrNotFound, "block not found: %s", blockID.ToHex()) } - return block, nil + return block.ProtocolBlock(), nil } func blockMetadataByBlockID(blockID iotago.BlockID) (*apimodels.BlockMetadataResponse, error) { @@ -44,6 +44,28 @@ func blockMetadataByID(c echo.Context) (*apimodels.BlockMetadataResponse, error) return blockMetadataByBlockID(blockID) } +func blockWithMetadataByID(c echo.Context) (*apimodels.BlockWithMetadataResponse, error) { + blockID, err := httpserver.ParseBlockIDParam(c, restapi.ParameterBlockID) + if err != nil { + return nil, ierrors.Wrapf(err, "failed to parse block ID %s", c.Param(restapi.ParameterBlockID)) + } + + block, exists := deps.Protocol.MainEngineInstance().Block(blockID) + if !exists { + return nil, ierrors.Wrapf(echo.ErrNotFound, "block not found: %s", blockID.ToHex()) + } + + blockMetadata, err := blockMetadataByBlockID(blockID) + if err != nil { + return nil, err + } + + return &apimodels.BlockWithMetadataResponse{ + Block: block.ProtocolBlock(), + Metadata: blockMetadata, + }, nil +} + func blockIssuanceBySlot(slotIndex iotago.SlotIndex) (*apimodels.IssuanceBlockHeaderResponse, error) { references := deps.Protocol.MainEngineInstance().TipSelection.SelectTips(iotago.BasicBlockMaxParents) diff --git a/components/restapi/core/component.go b/components/restapi/core/component.go index 8361b8d8f..6785dc462 100644 --- a/components/restapi/core/component.go +++ b/components/restapi/core/component.go @@ -44,6 +44,12 @@ const ( // MIMEApplicationVendorIOTASerializerV2 => bytes. RouteBlockMetadata = "/blocks/:" + restapipkg.ParameterBlockID + "/metadata" + // RouteBlockWithMetadata is the route for getting a block, together with its metadata by its blockID. + // GET returns the block and metadata. + // MIMEApplicationJSON => json. + // MIMEApplicationVendorIOTASerializerV2 => bytes. + RouteBlockWithMetadata = "/blocks/:" + restapipkg.ParameterBlockID + "/full" + // RouteBlocks is the route for sending new blocks. // POST creates a single new block and returns the new block ID. // The block is parsed based on the given type in the request "Content-Type" header. @@ -181,12 +187,12 @@ func configure() error { }) routeGroup.GET(RouteBlock, func(c echo.Context) error { - block, err := blockByID(c) + resp, err := blockByID(c) if err != nil { return err } - return responseByHeader(c, block.ProtocolBlock()) + return responseByHeader(c, resp) }) routeGroup.GET(RouteBlockMetadata, func(c echo.Context) error { @@ -198,6 +204,15 @@ func configure() error { return responseByHeader(c, resp) }, checkNodeSynced()) + routeGroup.GET(RouteBlockWithMetadata, func(c echo.Context) error { + resp, err := blockWithMetadataByID(c) + if err != nil { + return err + } + + return responseByHeader(c, resp) + }, checkNodeSynced()) + routeGroup.POST(RouteBlocks, func(c echo.Context) error { resp, err := sendBlock(c) if err != nil { @@ -276,7 +291,7 @@ func configure() error { }) routeGroup.GET(RouteOutput, func(c echo.Context) error { - resp, err := getOutput(c) + resp, err := outputByID(c) if err != nil { return err } @@ -285,7 +300,7 @@ func configure() error { }) routeGroup.GET(RouteOutputMetadata, func(c echo.Context) error { - resp, err := getOutputMetadata(c) + resp, err := outputMetadataByID(c) if err != nil { return err } @@ -294,7 +309,7 @@ func configure() error { }) routeGroup.GET(RouteOutputWithMetadata, func(c echo.Context) error { - resp, err := getOutputWithMetadata(c) + resp, err := outputWithMetadataByID(c) if err != nil { return err } diff --git a/components/restapi/core/utxo.go b/components/restapi/core/utxo.go index bdf62c89c..2fd9a7fad 100644 --- a/components/restapi/core/utxo.go +++ b/components/restapi/core/utxo.go @@ -10,7 +10,7 @@ import ( "github.com/iotaledger/iota.go/v4/nodeclient/apimodels" ) -func getOutput(c echo.Context) (*apimodels.OutputResponse, error) { +func outputByID(c echo.Context) (*apimodels.OutputResponse, error) { outputID, err := httpserver.ParseOutputIDParam(c, restapipkg.ParameterOutputID) if err != nil { return nil, ierrors.Wrapf(err, "failed to parse output ID %s", c.Param(restapipkg.ParameterOutputID)) @@ -27,7 +27,7 @@ func getOutput(c echo.Context) (*apimodels.OutputResponse, error) { }, nil } -func getOutputMetadata(c echo.Context) (*apimodels.OutputMetadata, error) { +func outputMetadataByID(c echo.Context) (*apimodels.OutputMetadata, error) { outputID, err := httpserver.ParseOutputIDParam(c, restapipkg.ParameterOutputID) if err != nil { return nil, ierrors.Wrapf(err, "failed to parse output ID %s", c.Param(restapipkg.ParameterOutputID)) @@ -45,7 +45,7 @@ func getOutputMetadata(c echo.Context) (*apimodels.OutputMetadata, error) { return newOutputMetadataResponse(output) } -func getOutputWithMetadata(c echo.Context) (*apimodels.OutputWithMetadataResponse, error) { +func outputWithMetadataByID(c echo.Context) (*apimodels.OutputWithMetadataResponse, error) { outputID, err := httpserver.ParseOutputIDParam(c, restapipkg.ParameterOutputID) if err != nil { return nil, ierrors.Wrapf(err, "failed to parse output ID %s", c.Param(restapipkg.ParameterOutputID))