Skip to content

Commit

Permalink
Merge pull request #400 from iotaledger/feat/block-issuer-tests
Browse files Browse the repository at this point in the history
Extract block issuer from node in tests and components
  • Loading branch information
cyberphysic4l authored Oct 9, 2023
2 parents cf8338d + 5815c1a commit e93e442
Show file tree
Hide file tree
Showing 33 changed files with 927 additions and 746 deletions.
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
26 changes: 8 additions & 18 deletions components/restapi/core/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import (
"github.com/labstack/echo/v4"

"github.com/iotaledger/hive.go/ierrors"
"github.com/iotaledger/hive.go/serializer/v2/serix"
"github.com/iotaledger/inx-app/pkg/httpserver"
"github.com/iotaledger/iota-core/pkg/blockfactory"
"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 @@ -71,7 +72,7 @@ func sendBlock(c echo.Context) (*apimodels.BlockCreatedResponse, error) {
return nil, ierrors.Wrapf(httpserver.ErrInvalidParameter, "invalid block, error: %w", err)
}

var iotaBlock = &iotago.ProtocolBlock{}
var iotaBlock *iotago.ProtocolBlock

if c.Request().Body == nil {
// bad request
Expand All @@ -85,38 +86,27 @@ func sendBlock(c echo.Context) (*apimodels.BlockCreatedResponse, error) {

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 {
if err := deps.Protocol.CurrentAPI().JSONDecode(bytes, iotaBlock, serix.WithValidation()); err != nil {
return nil, ierrors.Wrapf(httpserver.ErrInvalidParameter, "invalid block, error: %w", err)
}

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

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

default:
Expand Down
31 changes: 5 additions & 26 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,10 +39,9 @@ const (
// GET returns block metadata.
RouteBlockMetadata = "/blocks/:" + restapipkg.ParameterBlockID + "/metadata"

// RouteBlocks is the route for creating new blocks.
// 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.
// By providing only the protocolVersion and payload transaction user can POST a transaction.
// MIMEApplicationJSON => json.
// MIMEVendorIOTASerializer => bytes.
RouteBlocks = "/blocks"
Expand Down Expand Up @@ -134,8 +133,7 @@ var (
Component *app.Component
deps dependencies

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

type dependencies struct {
Expand All @@ -144,7 +142,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 +155,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 Down Expand Up @@ -195,7 +187,7 @@ func configure() error {
c.Response().Header().Set(echo.HeaderLocation, resp.BlockID.ToHex())

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

routeGroup.GET(RouteBlockIssuance, func(c echo.Context) error {
resp, err := blockIssuance(c)
Expand Down Expand Up @@ -360,19 +352,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

0 comments on commit e93e442

Please sign in to comment.