Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract block issuer from node in tests and components #400

Merged
merged 17 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions components/restapi/core/blocks.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package core

import (
"io"

"github.com/labstack/echo/v4"

"github.com/iotaledger/hive.go/ierrors"
"github.com/iotaledger/inx-app/pkg/httpserver"
"github.com/iotaledger/iota-core/pkg/blockhandler"
"github.com/iotaledger/iota-core/pkg/model"
"github.com/iotaledger/iota-core/pkg/restapi"
iotago "github.com/iotaledger/iota.go/v4"
Expand Down Expand Up @@ -61,3 +64,67 @@ func blockIssuance(_ echo.Context) (*apimodels.IssuanceBlockHeaderResponse, erro

return resp, nil
}

func sendBlock(c echo.Context) (*apimodels.BlockCreatedResponse, error) {
mimeType, err := httpserver.GetRequestContentType(c, httpserver.MIMEApplicationVendorIOTASerializerV2, echo.MIMEApplicationJSON)
if err != nil {
return nil, ierrors.Wrapf(httpserver.ErrInvalidParameter, "invalid block, error: %w", err)
}

var iotaBlock = &iotago.ProtocolBlock{}

if c.Request().Body == nil {
// bad request
return nil, ierrors.Wrap(httpserver.ErrInvalidParameter, "invalid block, error: request body missing")
}

bytes, err := io.ReadAll(c.Request().Body)
if err != nil {
return nil, ierrors.Wrapf(httpserver.ErrInvalidParameter, "invalid block, error: %w", err)
}

switch mimeType {
case echo.MIMEApplicationJSON:
// Do not validate here, the parents might need to be set
if err := deps.Protocol.CurrentAPI().JSONDecode(bytes, iotaBlock); err != nil {
cyberphysic4l marked this conversation as resolved.
Show resolved Hide resolved
return nil, ierrors.Wrapf(httpserver.ErrInvalidParameter, "invalid block, error: %w", err)
}

case httpserver.MIMEApplicationVendorIOTASerializerV2:
version, _, err := iotago.VersionFromBytes(bytes)
cyberphysic4l marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, ierrors.Wrapf(httpserver.ErrInvalidParameter, "invalid block, error: %w", err)
}

apiForVersion, err := deps.Protocol.APIForVersion(version)
if err != nil {
return nil, ierrors.Wrapf(httpserver.ErrInvalidParameter, "invalid block, error: %w", err)
}

// Do not validate here, the parents might need to be set
if _, err := apiForVersion.Decode(bytes, iotaBlock); err != nil {
return nil, ierrors.Wrapf(httpserver.ErrInvalidParameter, "invalid block, error: %w", err)
}

default:
return nil, echo.ErrUnsupportedMediaType
}

blockID, err := deps.BlockHandler.AttachBlock(c.Request().Context(), iotaBlock)
if err != nil {
switch {
case ierrors.Is(err, blockhandler.ErrBlockAttacherInvalidBlock):
return nil, ierrors.Wrapf(httpserver.ErrInvalidParameter, "failed to attach block: %w", err)

case ierrors.Is(err, blockhandler.ErrBlockAttacherAttachingNotPossible):
return nil, ierrors.Wrapf(echo.ErrInternalServerError, "failed to attach block: %w", err)

default:
return nil, ierrors.Wrapf(echo.ErrInternalServerError, "failed to attach block: %w", err)
}
}

return &apimodels.BlockCreatedResponse{
BlockID: blockID,
}, nil
}
17 changes: 17 additions & 0 deletions components/restapi/core/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ const (
// GET returns block metadata.
RouteBlockMetadata = "/blocks/:" + restapipkg.ParameterBlockID + "/metadata"

// 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.
// MIMEApplicationJSON => json.
// MIMEVendorIOTASerializer => bytes.
RouteBlocks = "/blocks"

// RouteOutput is the route for getting an output by its outputID (transactionHash + outputIndex).
// GET returns the output based on the given type in the request "Accept" header.
// MIMEApplicationJSON => json.
Expand Down Expand Up @@ -172,6 +179,16 @@ func configure() error {
return responseByHeader(c, resp)
}, checkNodeSynced())

routeGroup.POST(RouteBlocks, func(c echo.Context) error {
resp, err := sendBlock(c)
if err != nil {
return err
}
c.Response().Header().Set(echo.HeaderLocation, resp.BlockID.ToHex())

return httpserver.JSONResponse(c, http.StatusCreated, resp)
}, checkNodeSynced())

routeGroup.GET(RouteBlockIssuance, func(c echo.Context) error {
resp, err := blockIssuance(c)
if err != nil {
Expand Down
Loading