forked from threefoldtech/zos
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 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
1 parent
67538f8
commit 8e5ca12
Showing
3 changed files
with
95 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |