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 7 commits
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
2 changes: 0 additions & 2 deletions components/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"github.com/iotaledger/hive.go/app"
"github.com/iotaledger/hive.go/app/components/profiling"
"github.com/iotaledger/hive.go/app/components/shutdown"
"github.com/iotaledger/iota-core/components/blockissuer"
"github.com/iotaledger/iota-core/components/dashboard"
dashboardmetrics "github.com/iotaledger/iota-core/components/dashboard_metrics"
"github.com/iotaledger/iota-core/components/debugapi"
Expand Down Expand Up @@ -39,7 +38,6 @@ func App() *app.App {
debugapi.Component,
metricstracker.Component,
protocol.Component,
blockissuer.Component,
validator.Component,
dashboardmetrics.Component,
dashboard.Component,
Expand Down
63 changes: 0 additions & 63 deletions components/blockissuer/component.go

This file was deleted.

32 changes: 0 additions & 32 deletions components/blockissuer/params.go

This file was deleted.

20 changes: 6 additions & 14 deletions components/inx/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"github.com/iotaledger/hive.go/app"
"github.com/iotaledger/iota-core/components/protocol"
"github.com/iotaledger/iota-core/pkg/blockfactory"
"github.com/iotaledger/iota-core/pkg/blockhandler"
"github.com/iotaledger/iota-core/pkg/daemon"
protocolpkg "github.com/iotaledger/iota-core/pkg/protocol"
restapipkg "github.com/iotaledger/iota-core/pkg/restapi"
Expand All @@ -22,22 +22,20 @@ func init() {
IsEnabled: func(c *dig.Container) bool {
return ParamsINX.Enabled
},
Provide: provide,
Configure: configure,
Run: run,
Provide: provide,
Run: run,
}
}

var (
Component *app.Component
deps dependencies
blockIssuerAccount blockfactory.Account
Component *app.Component
deps dependencies
)

type dependencies struct {
dig.In
Protocol *protocolpkg.Protocol
BlockIssuer *blockfactory.BlockIssuer
BlockHandler *blockhandler.BlockHandler
Echo *echo.Echo `optional:"true"`
RestRouteManager *restapipkg.RestRouteManager
INXServer *Server
Expand All @@ -54,12 +52,6 @@ func provide(c *dig.Container) error {
return nil
}

func configure() error {
blockIssuerAccount = blockfactory.AccountFromParams(ParamsINX.BlockIssuerAccount, ParamsINX.BlockIssuerPrivateKey)

return nil
}

func run() error {
if err := Component.Daemon().BackgroundWorker("INX", func(ctx context.Context) {
Component.LogInfo("Starting INX ... done")
Expand Down
4 changes: 0 additions & 4 deletions components/inx/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ type ParametersINX struct {
Enabled bool `default:"false" usage:"whether the INX plugin is enabled"`
// the bind address on which the INX can be accessed from
BindAddress string `default:"localhost:9029" usage:"the bind address on which the INX can be accessed from"`
// BlockIssuerAccount the accountID of the account that will issue the blocks.
BlockIssuerAccount string `default:"" usage:"the accountID of the account that will issue the blocks"`
// BlockIssuerPrivateKey the private key of the account that will issue the blocks.
BlockIssuerPrivateKey string `default:"" usage:"the private key of the account that will issue the blocks"`
}

var ParamsINX = &ParametersINX{}
Expand Down
8 changes: 4 additions & 4 deletions components/inx/server_blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/iotaledger/hive.go/runtime/event"
"github.com/iotaledger/hive.go/runtime/workerpool"
inx "github.com/iotaledger/inx/go"
"github.com/iotaledger/iota-core/pkg/blockfactory"
"github.com/iotaledger/iota-core/pkg/blockhandler"
"github.com/iotaledger/iota-core/pkg/protocol/engine/blocks"
iotago "github.com/iotaledger/iota.go/v4"
)
Expand Down Expand Up @@ -130,13 +130,13 @@ func (s *Server) attachBlock(ctx context.Context, block *iotago.ProtocolBlock) (
mergedCtx, mergedCtxCancel := contextutils.MergeContexts(ctx, Component.Daemon().ContextStopped())
defer mergedCtxCancel()

blockID, err := deps.BlockIssuer.AttachBlock(mergedCtx, block, blockIssuerAccount)
blockID, err := deps.BlockHandler.AttachBlock(mergedCtx, block)
if err != nil {
switch {
case ierrors.Is(err, blockfactory.ErrBlockAttacherInvalidBlock):
case ierrors.Is(err, blockhandler.ErrBlockAttacherInvalidBlock):
return nil, status.Errorf(codes.InvalidArgument, "failed to attach block: %s", err.Error())

case ierrors.Is(err, blockfactory.ErrBlockAttacherAttachingNotPossible):
case ierrors.Is(err, blockhandler.ErrBlockAttacherAttachingNotPossible):
return nil, status.Errorf(codes.Internal, "failed to attach block: %s", err.Error())

default:
Expand Down
67 changes: 0 additions & 67 deletions components/restapi/core/blocks.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
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/blockfactory"
"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 @@ -64,67 +61,3 @@ 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 {
return nil, ierrors.Wrapf(httpserver.ErrInvalidParameter, "invalid block, error: %w", err)
}

case httpserver.MIMEApplicationVendorIOTASerializerV2:
version, _, err := iotago.VersionFromBytes(bytes)
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.BlockIssuer.AttachBlock(c.Request().Context(), iotaBlock, blockIssuerAccount)
if err != nil {
switch {
case ierrors.Is(err, blockfactory.ErrBlockAttacherInvalidBlock):
return nil, ierrors.Wrapf(httpserver.ErrInvalidParameter, "failed to attach block: %w", err)

case ierrors.Is(err, blockfactory.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
}
44 changes: 3 additions & 41 deletions components/restapi/core/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/iotaledger/iota-core/components/metricstracker"
"github.com/iotaledger/iota-core/components/protocol"
"github.com/iotaledger/iota-core/components/restapi"
"github.com/iotaledger/iota-core/pkg/blockfactory"
"github.com/iotaledger/iota-core/pkg/blockhandler"
protocolpkg "github.com/iotaledger/iota-core/pkg/protocol"
restapipkg "github.com/iotaledger/iota-core/pkg/restapi"
)
Expand All @@ -39,14 +39,6 @@ const (
// GET returns block metadata.
RouteBlockMetadata = "/blocks/:" + restapipkg.ParameterBlockID + "/metadata"

// RouteBlocks is the route for creating 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.
// By providing only the protocolVersion and payload transaction user can POST a transaction.
// MIMEApplicationJSON => json.
// MIMEVendorIOTASerializer => bytes.
RouteBlocks = "/blocks"
cyberphysic4l marked this conversation as resolved.
Show resolved Hide resolved

// 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 @@ -134,8 +126,7 @@ var (
Component *app.Component
deps dependencies

blockIssuerAccount blockfactory.Account
features = []string{}
features = []string{}
)

type dependencies struct {
Expand All @@ -144,7 +135,7 @@ type dependencies struct {
AppInfo *app.Info
RestRouteManager *restapipkg.RestRouteManager
Protocol *protocolpkg.Protocol
BlockIssuer *blockfactory.BlockIssuer `optional:"true"`
BlockHandler *blockhandler.BlockHandler
MetricsTracker *metricstracker.MetricsTracker
BaseToken *protocol.BaseToken
}
Expand All @@ -157,12 +148,6 @@ func configure() error {

routeGroup := deps.RestRouteManager.AddRoute("core/v3")

if restapi.ParamsRestAPI.AllowIncompleteBlock {
AddFeature("allowIncompleteBlock")
}

blockIssuerAccount = blockfactory.AccountFromParams(restapi.ParamsRestAPI.BlockIssuerAccount, restapi.ParamsRestAPI.BlockIssuerPrivateKey)

routeGroup.GET(RouteInfo, func(c echo.Context) error {
resp := info()

Expand All @@ -187,16 +172,6 @@ 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(), checkUpcomingUnsupportedProtocolVersion())

routeGroup.GET(RouteBlockIssuance, func(c echo.Context) error {
resp, err := blockIssuance(c)
if err != nil {
Expand Down Expand Up @@ -360,19 +335,6 @@ func checkNodeSynced() echo.MiddlewareFunc {
}
}

func checkUpcomingUnsupportedProtocolVersion() echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
// todo update with protocol upgrades support
// if !deps.ProtocolManager.NextPendingSupported() {
// return ierrors.Wrap(echo.ErrServiceUnavailable, "node does not support the upcoming protocol upgrade")
// }

return next(c)
}
}
}

func responseByHeader(c echo.Context, obj any) error {
mimeType, err := httpserver.GetAcceptHeaderContentType(c, httpserver.MIMEApplicationVendorIOTASerializerV2, echo.MIMEApplicationJSON)
if err != nil && err != httpserver.ErrNotAcceptable {
Expand Down
8 changes: 0 additions & 8 deletions components/restapi/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ type ParametersRestAPI struct {
ProtectedRoutes []string `usage:"the HTTP REST routes which need to be called with authorization. Wildcards using * are allowed"`
// whether the debug logging for requests should be enabled
DebugRequestLoggerEnabled bool `default:"false" usage:"whether the debug logging for requests should be enabled"`
// AllowIncompleteBlock defines whether the node allows to fill in incomplete block and issue it for user.
AllowIncompleteBlock bool `default:"false" usage:"whether the node allows to fill in incomplete block and issue it for user"`
// MaxPageSize defines the maximum number of results per page.
MaxPageSize uint32 `default:"100" usage:"the maximum number of results per page"`
// RequestsMemoryCacheGranularity defines per how many slots a cache is created for big API requests.
Expand All @@ -36,12 +34,6 @@ type ParametersRestAPI struct {
// the maximum number of results that may be returned by an endpoint
MaxResults int `default:"1000" usage:"the maximum number of results that may be returned by an endpoint"`
}

// BlockIssuerAccount the accountID of the account that will issue the blocks.
BlockIssuerAccount string `default:"" usage:"the accountID of the account that will issue the blocks"`

// BlockIssuerPrivateKey the private key of the account that will issue the blocks.
BlockIssuerPrivateKey string `default:"" usage:"the private key of the account that will issue the blocks"`
}

var ParamsRestAPI = &ParametersRestAPI{
Expand Down
Loading
Loading