Skip to content

Commit

Permalink
Merge pull request #1976 from iotaledger/fix/node-conn-startup-if-L1-…
Browse files Browse the repository at this point in the history
…not-synced

Fix node connection on startup if L1 node is not synced
  • Loading branch information
muXxer authored Feb 22, 2023
2 parents 41c7022 + 8cf595b commit 884645a
Show file tree
Hide file tree
Showing 21 changed files with 522 additions and 292 deletions.
2 changes: 0 additions & 2 deletions core/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/iotaledger/hive.go/core/app"
"github.com/iotaledger/hive.go/core/app/core/shutdown"
"github.com/iotaledger/hive.go/core/app/plugins/profiling"
"github.com/iotaledger/inx-app/core/inx"
"github.com/iotaledger/wasp/core/chains"
"github.com/iotaledger/wasp/core/database"
"github.com/iotaledger/wasp/core/dkg"
Expand Down Expand Up @@ -37,7 +36,6 @@ func App() *app.App {
app.WithVersionCheck("iotaledger", "wasp"),
app.WithInitComponent(InitComponent),
app.WithCoreComponents([]*app.CoreComponent{
inx.CoreComponent,
shutdown.CoreComponent,
nodeconn.CoreComponent,
users.CoreComponent,
Expand Down
8 changes: 6 additions & 2 deletions core/chains/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package chains

import (
"context"
"fmt"
"time"

"go.uber.org/dig"

"github.com/iotaledger/hive.go/core/app"
hiveshutdown "github.com/iotaledger/hive.go/core/app/pkg/shutdown"
"github.com/iotaledger/wasp/packages/chain"
"github.com/iotaledger/wasp/packages/chain/cmtLog"
"github.com/iotaledger/wasp/packages/chains"
Expand Down Expand Up @@ -41,7 +43,8 @@ var (
type dependencies struct {
dig.In

Chains *chains.Chains
ShutdownHandler *hiveshutdown.ShutdownHandler
Chains *chains.Chains
}

func initConfigPars(c *dig.Container) error {
Expand Down Expand Up @@ -116,7 +119,8 @@ func provide(c *dig.Container) error {
func run() error {
err := CoreComponent.Daemon().BackgroundWorker(CoreComponent.Name, func(ctx context.Context) {
if err := deps.Chains.Run(ctx); err != nil {
CoreComponent.LogPanicf("failed to start chains: %v", err)
deps.ShutdownHandler.SelfShutdown(fmt.Sprintf("Starting %s failed, error: %s", CoreComponent.Name, err.Error()), true)
return
}

<-ctx.Done()
Expand Down
46 changes: 26 additions & 20 deletions core/nodeconn/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package nodeconn

import (
"context"
"fmt"

"go.uber.org/dig"

Expand All @@ -19,6 +20,7 @@ func init() {
Component: &app.Component{
Name: "NodeConn",
DepsFunc: func(cDeps dependencies) { deps = cDeps },
Params: params,
Provide: provide,
Configure: configure,
},
Expand All @@ -33,24 +35,34 @@ var (
type dependencies struct {
dig.In

NodeConnection chain.NodeConnection
NodeConnection chain.NodeConnection
ShutdownHandler *shutdown.ShutdownHandler
}

func provide(c *dig.Container) error {
type nodeConnectionMetricsResult struct {
dig.Out

NodeConnectionMetrics nodeconnmetrics.NodeConnectionMetrics
}
if err := c.Provide(func() (*nodebridge.NodeBridge, error) {
nodeBridge := nodebridge.NewNodeBridge(
CoreComponent.Logger(),
nodebridge.WithTargetNetworkName(ParamsINX.TargetNetworkName),
)

if err := c.Provide(func() nodeConnectionMetricsResult {
return nodeConnectionMetricsResult{
NodeConnectionMetrics: nodeconnmetrics.New(),
if err := nodeBridge.Connect(
CoreComponent.Daemon().ContextStopped(),
ParamsINX.Address,
ParamsINX.MaxConnectionAttempts,
); err != nil {
return nil, err
}

return nodeBridge, nil
}); err != nil {
CoreComponent.LogPanic(err)
}

if err := c.Provide(nodeconnmetrics.New); err != nil {
CoreComponent.LogPanic(err)
}

type nodeConnectionDeps struct {
dig.In

Expand All @@ -59,13 +71,7 @@ func provide(c *dig.Container) error {
ShutdownHandler *shutdown.ShutdownHandler
}

type nodeConnectionResult struct {
dig.Out

NodeConnection chain.NodeConnection
}

if err := c.Provide(func(deps nodeConnectionDeps) nodeConnectionResult {
if err := c.Provide(func(deps nodeConnectionDeps) chain.NodeConnection {
nodeConnection, err := nodeconn.New(
CoreComponent.Daemon().ContextStopped(),
CoreComponent.Logger().Named("nc"),
Expand All @@ -76,9 +82,7 @@ func provide(c *dig.Container) error {
if err != nil {
CoreComponent.LogPanicf("Creating NodeConnection failed: %s", err.Error())
}
return nodeConnectionResult{
NodeConnection: nodeConnection,
}
return nodeConnection
}); err != nil {
CoreComponent.LogPanic(err)
}
Expand All @@ -89,7 +93,9 @@ func provide(c *dig.Container) error {
func configure() error {
if err := CoreComponent.Daemon().BackgroundWorker(CoreComponent.Name, func(ctx context.Context) {
CoreComponent.LogInfof("Starting %s ... done", CoreComponent.Name)
deps.NodeConnection.Run(ctx)
if err := deps.NodeConnection.Run(ctx); err != nil {
deps.ShutdownHandler.SelfShutdown(fmt.Sprintf("Starting %s failed, error: %s", CoreComponent.Name, err.Error()), true)
}
CoreComponent.LogInfof("Stopping %s ... done", CoreComponent.Name)
}, daemon.PriorityNodeConnection); err != nil {
CoreComponent.LogPanicf("failed to start worker: %s", err)
Expand Down
15 changes: 15 additions & 0 deletions core/nodeconn/params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package nodeconn

import (
"github.com/iotaledger/hive.go/core/app"
"github.com/iotaledger/inx-app/core/inx"
)

var ParamsINX = &inx.ParametersINX{}

var params = &app.ComponentParams{
Params: map[string]any{
"inx": ParamsINX,
},
Masked: nil,
}
22 changes: 18 additions & 4 deletions core/registry/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/iotaledger/hive.go/core/app"
"github.com/iotaledger/hive.go/core/ioutils"
hivep2p "github.com/iotaledger/hive.go/core/p2p"
"github.com/iotaledger/wasp/packages/chain"
"github.com/iotaledger/wasp/packages/chain/cmtLog"
"github.com/iotaledger/wasp/packages/cryptolib"
"github.com/iotaledger/wasp/packages/registry"
Expand Down Expand Up @@ -45,8 +46,14 @@ func provide(c *dig.Container) error {
CoreComponent.LogPanic(err)
}

if err := c.Provide(func() cmtLog.ConsensusStateRegistry {
consensusStateRegistry, err := registry.NewConsensusStateRegistry(ParamsRegistries.ConsensusState.Path)
type consensusRegistryDeps struct {
dig.In

NodeConnection chain.NodeConnection
}

if err := c.Provide(func(deps consensusRegistryDeps) cmtLog.ConsensusStateRegistry {
consensusStateRegistry, err := registry.NewConsensusStateRegistry(ParamsRegistries.ConsensusState.Path, deps.NodeConnection.GetBech32HRP())
if err != nil {
CoreComponent.LogPanic(err)
}
Expand All @@ -55,8 +62,15 @@ func provide(c *dig.Container) error {
CoreComponent.LogPanic(err)
}

if err := c.Provide(func(nodeIdentityProvider registry.NodeIdentityProvider) registry.DKShareRegistryProvider {
dkSharesRegistry, err := registry.NewDKSharesRegistry(ParamsRegistries.DKShares.Path, nodeIdentityProvider.NodeIdentity().GetPrivateKey())
type dkSharesRegistryDeps struct {
dig.In

NodeIdentityProvider registry.NodeIdentityProvider
NodeConnection chain.NodeConnection
}

if err := c.Provide(func(deps dkSharesRegistryDeps) registry.DKShareRegistryProvider {
dkSharesRegistry, err := registry.NewDKSharesRegistry(ParamsRegistries.DKShares.Path, deps.NodeIdentityProvider.NodeIdentity().GetPrivateKey(), deps.NodeConnection.GetBech32HRP())
if err != nil {
CoreComponent.LogPanic(err)
}
Expand Down
8 changes: 7 additions & 1 deletion packages/chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import (
"time"

"github.com/iotaledger/hive.go/core/logger"
iotago "github.com/iotaledger/iota.go/v3"
"github.com/iotaledger/wasp/packages/isc"
"github.com/iotaledger/wasp/packages/metrics/nodeconnmetrics"
"github.com/iotaledger/wasp/packages/parameters"
"github.com/iotaledger/wasp/packages/peering"
"github.com/iotaledger/wasp/packages/state"
"github.com/iotaledger/wasp/packages/vm/core/governance"
Expand All @@ -18,8 +20,12 @@ import (

type NodeConnection interface {
ChainNodeConn
Run(ctx context.Context) error
WaitUntilInitiallySynced(context.Context) error
GetMetrics() nodeconnmetrics.NodeConnectionMetrics
Run(ctx context.Context)
GetBech32HRP() iotago.NetworkPrefix
GetL1Params() *parameters.L1Params
GetL1ProtocolParams() *iotago.ProtocolParameters
}

type StateFreshness byte
Expand Down
Loading

0 comments on commit 884645a

Please sign in to comment.