Skip to content

Commit

Permalink
setup public config
Browse files Browse the repository at this point in the history
- setup the persistance file in the netlight module
- add the chain events listner to catch PublicConfigStored events for
  the node
- run the redis consumer to act on the stored event
  • Loading branch information
Omarabdul3ziz committed Jan 28, 2025
1 parent 67538f8 commit 8e5ca12
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 1 deletion.
3 changes: 3 additions & 0 deletions cmds/modules/netlightd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/threefoldtech/zosbase/pkg/netlight/bridge"
"github.com/threefoldtech/zosbase/pkg/netlight/ifaceutil"
"github.com/threefoldtech/zosbase/pkg/netlight/nft"
"github.com/threefoldtech/zosbase/pkg/netlight/public"
"github.com/threefoldtech/zosbase/pkg/netlight/resource"
"github.com/urfave/cli/v2"

Expand Down Expand Up @@ -73,6 +74,8 @@ func action(cli *cli.Context) error {
return errors.Wrap(err, "fail to create module root")
}

public.SetPersistence(root)

waitMyceliumBin()

if err := bootstrap.DefaultBridgeValid(); err != nil {
Expand Down
15 changes: 14 additions & 1 deletion cmds/modules/noded/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ var Module cli.Command = cli.Command{
}

func registerationServer(ctx context.Context, msgBrokerCon string, env environment.Environment, info registrar.RegistrationInfo) error {

redis, err := zbus.NewRedisClient(msgBrokerCon)
if err != nil {
return errors.Wrap(err, "fail to connect to message broker server")
Expand Down Expand Up @@ -92,6 +91,11 @@ func action(cli *cli.Context) error {
return errors.Wrap(err, "fail to connect to message broker server")
}

consumer, err := events.NewConsumer(msgBrokerCon, module)
if err != nil {
return errors.Wrap(err, "failed to to create event consumer")
}

if printID {
sysCl := stubs.NewSystemMonitorStub(redis)
fmt.Println(sysCl.NodeID(cli.Context))
Expand Down Expand Up @@ -232,6 +236,15 @@ func action(cli *cli.Context) error {

log.Info().Uint32("node", node).Uint32("twin", twin).Msg("node registered")

go func() {
for {
if err := startPublicConfigWatcher(ctx, node, redis, consumer); err != nil {
log.Error().Err(err).Msg("setting public config failed")
<-time.After(10 * time.Second)
}
}
}()

log.Info().Uint32("twin", twin).Msg("node has been registered")
idStub := stubs.NewIdentityManagerStub(redis)
fetchCtx, cancel := context.WithTimeout(ctx, 30*time.Second)
Expand Down
78 changes: 78 additions & 0 deletions cmds/modules/noded/public.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package noded

import (
"context"
"time"

"github.com/pkg/errors"
"github.com/rs/zerolog/log"
substrate "github.com/threefoldtech/tfchain/clients/tfchain-client-go"
"github.com/threefoldtech/zbus"
"github.com/threefoldtech/zosbase/pkg"
"github.com/threefoldtech/zosbase/pkg/events"
"github.com/threefoldtech/zosbase/pkg/stubs"
)

// public sets and watches changes to public config on chain and tries to apply the provided setup
func startPublicConfigWatcher(ctx context.Context, nodeID uint32, cl zbus.Client, events *events.RedisConsumer) error {
ch, err := events.PublicConfig(ctx)
if err != nil {
return errors.Wrap(err, "failed to subscribe to node events")
}

substrateGateway := stubs.NewSubstrateGatewayStub(cl)

reapply:
for {
node, err := substrateGateway.GetNode(ctx, nodeID)
if err != nil {
return errors.Wrap(err, "failed to get node public config")
}

var cfg *substrate.PublicConfig
if node.PublicConfig.HasValue {
cfg = &node.PublicConfig.AsValue
}

if err := setPublicConfig(ctx, cl, cfg); err != nil {
return errors.Wrap(err, "failed to set public config (reapply)")
}

for {
select {
case <-ctx.Done():
return nil
case event := <-ch:
log.Info().Msgf("got a public config update: %+v", event.PublicConfig)
var cfg *substrate.PublicConfig
if event.PublicConfig.HasValue {
cfg = &event.PublicConfig.AsValue
}
if err := setPublicConfig(ctx, cl, cfg); err != nil {
return errors.Wrap(err, "failed to set public config")
}
case <-time.After(2 * time.Hour):
// last resort, if none of the events
// was received, it will be a good idea to just
// check every 2 hours for changes.
continue reapply
}
}
}
}

func setPublicConfig(ctx context.Context, cl zbus.Client, cfg *substrate.PublicConfig) error {
log.Info().Msg("setting node public config")
netMgr := stubs.NewNetworkerLightStub(cl)

if cfg == nil {
return netMgr.UnSetPublicConfig(ctx)
}

pub, err := pkg.PublicConfigFrom(*cfg)
if err != nil {
return errors.Wrap(err, "failed to create public config from setup")
}

return netMgr.SetPublicConfig(ctx, pub)
}

0 comments on commit 8e5ca12

Please sign in to comment.