Skip to content
This repository has been archived by the owner on Jan 24, 2025. It is now read-only.

Commit

Permalink
Add RouteBlockWithMetadata to REST API
Browse files Browse the repository at this point in the history
  • Loading branch information
muXxer committed Nov 20, 2023
1 parent 425d4e4 commit 88b02fb
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 10 deletions.
26 changes: 24 additions & 2 deletions components/restapi/core/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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) {
Expand All @@ -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)

Expand Down
25 changes: 20 additions & 5 deletions components/restapi/core/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions components/restapi/core/utxo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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))
Expand All @@ -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))
Expand Down

0 comments on commit 88b02fb

Please sign in to comment.