From 435ca91fb51ede0d28deaae4f5d388bbdb3b456b Mon Sep 17 00:00:00 2001 From: Omar Abdulaziz Date: Sun, 22 Sep 2024 17:24:32 +0300 Subject: [PATCH 1/4] init node location updater: - add updater package - expose GetState from registerer - fix longitude spelling --- cmds/modules/noded/main.go | 10 +++++ pkg/geoip/geoip.go | 4 +- pkg/registrar.go | 10 +++++ pkg/registrar/register.go | 2 +- pkg/registrar/registrar.go | 39 +++++++---------- pkg/stubs/registrar_stub.go | 17 ++++++++ pkg/updater/updater.go | 84 +++++++++++++++++++++++++++++++++++++ 7 files changed, 140 insertions(+), 26 deletions(-) create mode 100644 pkg/updater/updater.go diff --git a/cmds/modules/noded/main.go b/cmds/modules/noded/main.go index ccfd9a9cf..37a6038ec 100644 --- a/cmds/modules/noded/main.go +++ b/cmds/modules/noded/main.go @@ -23,6 +23,7 @@ import ( "github.com/threefoldtech/zos/pkg/perf/publicip" "github.com/threefoldtech/zos/pkg/registrar" "github.com/threefoldtech/zos/pkg/stubs" + "github.com/threefoldtech/zos/pkg/updater" "github.com/threefoldtech/zos/pkg/utils" "github.com/rs/zerolog/log" @@ -161,6 +162,15 @@ func action(cli *cli.Context) error { WithVirtualized(len(hypervisor) != 0) go registerationServer(ctx, msgBrokerCon, env, info) + + log.Info().Msg("start node updater") + nodeUpdater := updater.NewUpdater(redis) + if err != nil { + return errors.Wrap(err, "failed to create new node updater") + } + + go nodeUpdater.Start(ctx) + log.Info().Msg("start perf scheduler") perfMon, err := perf.NewPerformanceMonitor(msgBrokerCon) diff --git a/pkg/geoip/geoip.go b/pkg/geoip/geoip.go index 88956d4fb..df108568c 100644 --- a/pkg/geoip/geoip.go +++ b/pkg/geoip/geoip.go @@ -11,7 +11,7 @@ import ( // Location holds the result of a geoip request type Location struct { - Longitute float64 `json:"longitude"` + Longitude float64 `json:"longitude"` Latitude float64 `json:"latitude"` Continent string `json:"continent"` Country string `json:"country_name"` @@ -45,7 +45,7 @@ func Fetch() (Location, error) { func getLocation(geoIPService string) (Location, error) { l := Location{ - Longitute: 0.0, + Longitude: 0.0, Latitude: 0.0, Continent: "Unknown", Country: "Unknown", diff --git a/pkg/registrar.go b/pkg/registrar.go index 1e10926cf..4c5ceb3d8 100644 --- a/pkg/registrar.go +++ b/pkg/registrar.go @@ -4,7 +4,17 @@ package pkg //go:generate zbusc -module registrar -version 0.0.1 -name registrar -package stubs github.com/threefoldtech/zos/pkg+Registrar stubs/registrar_stub.go +type RegistrationState string + +type State struct { + NodeID uint32 + TwinID uint32 + State RegistrationState + Msg string +} + type Registrar interface { NodeID() (uint32, error) TwinID() (uint32, error) + GetState() State } diff --git a/pkg/registrar/register.go b/pkg/registrar/register.go index 891d2ee37..f077bbcdf 100644 --- a/pkg/registrar/register.go +++ b/pkg/registrar/register.go @@ -138,7 +138,7 @@ func registerNode( } location := substrate.Location{ - Longitude: fmt.Sprint(info.Location.Longitute), + Longitude: fmt.Sprint(info.Location.Longitude), Latitude: fmt.Sprint(info.Location.Latitude), Country: info.Location.Country, City: info.Location.City, diff --git a/pkg/registrar/registrar.go b/pkg/registrar/registrar.go index 25e6756db..b9b90a059 100644 --- a/pkg/registrar/registrar.go +++ b/pkg/registrar/registrar.go @@ -10,18 +10,18 @@ import ( "github.com/pkg/errors" "github.com/rs/zerolog/log" "github.com/threefoldtech/zbus" + "github.com/threefoldtech/zos/pkg" "github.com/threefoldtech/zos/pkg/app" "github.com/threefoldtech/zos/pkg/environment" "github.com/threefoldtech/zos/pkg/stubs" ) // should any of this be moved to pkg? -type RegistrationState string const ( - Failed RegistrationState = "Failed" - InProgress RegistrationState = "InProgress" - Done RegistrationState = "Done" + Failed pkg.RegistrationState = "Failed" + InProgress pkg.RegistrationState = "InProgress" + Done pkg.RegistrationState = "Done" monitorAccountEvery = 30 * time.Minute ) @@ -31,15 +31,8 @@ var ( ErrFailed = errors.New("registration failed") ) -type State struct { - NodeID uint32 - TwinID uint32 - State RegistrationState - Msg string -} - -func FailedState(err error) State { - return State{ +func FailedState(err error) pkg.State { + return pkg.State{ 0, 0, Failed, @@ -47,8 +40,8 @@ func FailedState(err error) State { } } -func InProgressState() State { - return State{ +func InProgressState() pkg.State { + return pkg.State{ 0, 0, InProgress, @@ -56,8 +49,8 @@ func InProgressState() State { } } -func DoneState(nodeID uint32, twinID uint32) State { - return State{ +func DoneState(nodeID uint32, twinID uint32) pkg.State { + return pkg.State{ nodeID, twinID, Done, @@ -66,13 +59,13 @@ func DoneState(nodeID uint32, twinID uint32) State { } type Registrar struct { - state State + state pkg.State mutex sync.RWMutex } func NewRegistrar(ctx context.Context, cl zbus.Client, env environment.Environment, info RegistrationInfo) *Registrar { r := Registrar{ - State{ + pkg.State{ 0, 0, InProgress, @@ -84,13 +77,13 @@ func NewRegistrar(ctx context.Context, cl zbus.Client, env environment.Environme return &r } -func (r *Registrar) setState(s State) { +func (r *Registrar) setState(s pkg.State) { r.mutex.Lock() defer r.mutex.Unlock() r.state = s } -func (r *Registrar) getState() State { +func (r *Registrar) GetState() pkg.State { r.mutex.RLock() defer r.mutex.RUnlock() return r.state @@ -162,11 +155,11 @@ func (r *Registrar) reActivate(ctx context.Context, cl zbus.Client, env environm } func (r *Registrar) NodeID() (uint32, error) { - return r.returnIfDone(r.getState().NodeID) + return r.returnIfDone(r.GetState().NodeID) } func (r *Registrar) TwinID() (uint32, error) { - return r.returnIfDone(r.getState().TwinID) + return r.returnIfDone(r.GetState().TwinID) } func (r *Registrar) returnIfDone(v uint32) (uint32, error) { diff --git a/pkg/stubs/registrar_stub.go b/pkg/stubs/registrar_stub.go index 08d5f59fb..e775cfb83 100644 --- a/pkg/stubs/registrar_stub.go +++ b/pkg/stubs/registrar_stub.go @@ -7,6 +7,7 @@ package stubs import ( "context" zbus "github.com/threefoldtech/zbus" + pkg "github.com/threefoldtech/zos/pkg" ) type RegistrarStub struct { @@ -26,6 +27,22 @@ func NewRegistrarStub(client zbus.Client) *RegistrarStub { } } +func (s *RegistrarStub) GetState(ctx context.Context) (ret0 pkg.State) { + args := []interface{}{} + result, err := s.client.RequestContext(ctx, s.module, s.object, "GetState", args...) + if err != nil { + panic(err) + } + result.PanicOnError() + loader := zbus.Loader{ + &ret0, + } + if err := result.Unmarshal(&loader); err != nil { + panic(err) + } + return +} + func (s *RegistrarStub) NodeID(ctx context.Context) (ret0 uint32, ret1 error) { args := []interface{}{} result, err := s.client.RequestContext(ctx, s.module, s.object, "NodeID", args...) diff --git a/pkg/updater/updater.go b/pkg/updater/updater.go new file mode 100644 index 000000000..db0511256 --- /dev/null +++ b/pkg/updater/updater.go @@ -0,0 +1,84 @@ +package updater + +import ( + "context" + "fmt" + "reflect" + "time" + + "github.com/rs/zerolog/log" + substrate "github.com/threefoldtech/tfchain/clients/tfchain-client-go" + "github.com/threefoldtech/zbus" + + "github.com/threefoldtech/zos/pkg/geoip" + "github.com/threefoldtech/zos/pkg/registrar" + "github.com/threefoldtech/zos/pkg/stubs" +) + +const ( + updateInterval = 24 * time.Hour +) + +type Updater struct { + substrateGateway *stubs.SubstrateGatewayStub + registrar *stubs.RegistrarStub +} + +func NewUpdater(bus zbus.Client) *Updater { + return &Updater{ + substrateGateway: stubs.NewSubstrateGatewayStub(bus), + registrar: stubs.NewRegistrarStub(bus), + } +} + +func (u *Updater) Start(ctx context.Context) { + for { + if u.registrar.GetState(ctx).State == registrar.Done { + if err := u.updateLocation(); err != nil { + log.Error().Err(err).Msg("updating location failed") + } + log.Info().Msg("node location updated") + } + + select { + case <-ctx.Done(): + log.Info().Msg("stop node updater. context cancelled") + return + case <-time.After(updateInterval): + continue + } + } +} + +func (u *Updater) updateLocation() error { + nodeId, err := u.registrar.NodeID(context.Background()) + if err != nil { + return fmt.Errorf("failed to get node id: %w", err) + } + + node, err := u.substrateGateway.GetNode(context.Background(), nodeId) + if err != nil { + return fmt.Errorf("failed to get node from chain: %w", err) + } + + loc, err := geoip.Fetch() + if err != nil { + return fmt.Errorf("failed to fetch location info: %w", err) + } + + newLoc := substrate.Location{ + City: loc.City, + Country: loc.Country, + Latitude: fmt.Sprintf("%f", loc.Latitude), + Longitude: fmt.Sprintf("%f", loc.Longitude), + } + + if !reflect.DeepEqual(newLoc, node.Location) { + node.Location = newLoc + if _, err := u.substrateGateway.UpdateNode(context.Background(), node); err != nil { + return fmt.Errorf("failed to update node on chain: %w", err) + } + } + + return nil +} From d69a2f7e073d9b6e719dd445ce02372912a35732 Mon Sep 17 00:00:00 2001 From: Omar Abdulaziz Date: Mon, 23 Sep 2024 14:37:10 +0300 Subject: [PATCH 2/4] move the location update to the register pkg --- cmds/modules/noded/main.go | 9 ---- pkg/registrar.go | 10 ---- pkg/registrar/registrar.go | 93 ++++++++++++++++++++++++++++++------- pkg/stubs/registrar_stub.go | 17 ------- pkg/updater/updater.go | 84 --------------------------------- 5 files changed, 75 insertions(+), 138 deletions(-) delete mode 100644 pkg/updater/updater.go diff --git a/cmds/modules/noded/main.go b/cmds/modules/noded/main.go index 37a6038ec..8e686789e 100644 --- a/cmds/modules/noded/main.go +++ b/cmds/modules/noded/main.go @@ -23,7 +23,6 @@ import ( "github.com/threefoldtech/zos/pkg/perf/publicip" "github.com/threefoldtech/zos/pkg/registrar" "github.com/threefoldtech/zos/pkg/stubs" - "github.com/threefoldtech/zos/pkg/updater" "github.com/threefoldtech/zos/pkg/utils" "github.com/rs/zerolog/log" @@ -163,14 +162,6 @@ func action(cli *cli.Context) error { go registerationServer(ctx, msgBrokerCon, env, info) - log.Info().Msg("start node updater") - nodeUpdater := updater.NewUpdater(redis) - if err != nil { - return errors.Wrap(err, "failed to create new node updater") - } - - go nodeUpdater.Start(ctx) - log.Info().Msg("start perf scheduler") perfMon, err := perf.NewPerformanceMonitor(msgBrokerCon) diff --git a/pkg/registrar.go b/pkg/registrar.go index 4c5ceb3d8..1e10926cf 100644 --- a/pkg/registrar.go +++ b/pkg/registrar.go @@ -4,17 +4,7 @@ package pkg //go:generate zbusc -module registrar -version 0.0.1 -name registrar -package stubs github.com/threefoldtech/zos/pkg+Registrar stubs/registrar_stub.go -type RegistrationState string - -type State struct { - NodeID uint32 - TwinID uint32 - State RegistrationState - Msg string -} - type Registrar interface { NodeID() (uint32, error) TwinID() (uint32, error) - GetState() State } diff --git a/pkg/registrar/registrar.go b/pkg/registrar/registrar.go index b9b90a059..c58aab030 100644 --- a/pkg/registrar/registrar.go +++ b/pkg/registrar/registrar.go @@ -2,28 +2,33 @@ package registrar import ( "context" + "fmt" "os" + "reflect" "sync" "time" "github.com/cenkalti/backoff/v3" "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/zos/pkg" "github.com/threefoldtech/zos/pkg/app" "github.com/threefoldtech/zos/pkg/environment" + "github.com/threefoldtech/zos/pkg/geoip" "github.com/threefoldtech/zos/pkg/stubs" ) // should any of this be moved to pkg? +type RegistrationState string const ( - Failed pkg.RegistrationState = "Failed" - InProgress pkg.RegistrationState = "InProgress" - Done pkg.RegistrationState = "Done" + Failed RegistrationState = "Failed" + InProgress RegistrationState = "InProgress" + Done RegistrationState = "Done" - monitorAccountEvery = 30 * time.Minute + monitorAccountEvery = 30 * time.Minute + updateLocationInterval = 24 * time.Hour ) var ( @@ -31,8 +36,15 @@ var ( ErrFailed = errors.New("registration failed") ) -func FailedState(err error) pkg.State { - return pkg.State{ +type State struct { + NodeID uint32 + TwinID uint32 + State RegistrationState + Msg string +} + +func FailedState(err error) State { + return State{ 0, 0, Failed, @@ -40,8 +52,8 @@ func FailedState(err error) pkg.State { } } -func InProgressState() pkg.State { - return pkg.State{ +func InProgressState() State { + return State{ 0, 0, InProgress, @@ -49,8 +61,8 @@ func InProgressState() pkg.State { } } -func DoneState(nodeID uint32, twinID uint32) pkg.State { - return pkg.State{ +func DoneState(nodeID uint32, twinID uint32) State { + return State{ nodeID, twinID, Done, @@ -59,31 +71,32 @@ func DoneState(nodeID uint32, twinID uint32) pkg.State { } type Registrar struct { - state pkg.State + state State mutex sync.RWMutex } func NewRegistrar(ctx context.Context, cl zbus.Client, env environment.Environment, info RegistrationInfo) *Registrar { r := Registrar{ - pkg.State{ + state: State{ 0, 0, InProgress, "", }, - sync.RWMutex{}, + mutex: sync.RWMutex{}, } + go r.register(ctx, cl, env, info) return &r } -func (r *Registrar) setState(s pkg.State) { +func (r *Registrar) setState(s State) { r.mutex.Lock() defer r.mutex.Unlock() r.state = s } -func (r *Registrar) GetState() pkg.State { +func (r *Registrar) getState() State { r.mutex.RLock() defer r.mutex.RUnlock() return r.state @@ -139,6 +152,10 @@ func (r *Registrar) register(ctx context.Context, cl zbus.Client, env environmen if err := r.reActivate(ctx, cl, env); err != nil { log.Error().Err(err).Msg("failed to reactivate account") } + case <-time.After(updateLocationInterval): + if err := r.updateLocation(ctx, cl); err != nil { + log.Error().Err(err).Msg("updating location failed") + } case <-addressesUpdate: log.Info().Msg("zos address has changed, re-register") register() @@ -154,12 +171,52 @@ func (r *Registrar) reActivate(ctx context.Context, cl zbus.Client, env environm return err } +// updateLocation validates the node location on chain against the geoip +// service and update it if needed. +func (r *Registrar) updateLocation(ctx context.Context, cl zbus.Client) error { + nodeId, err := r.NodeID() + if err != nil { + return fmt.Errorf("failed to get node id: %w", err) + } + + substrateGw := stubs.NewSubstrateGatewayStub(cl) + node, err := substrateGw.GetNode(ctx, nodeId) + if err != nil { + return fmt.Errorf("failed to get node from chain: %w", err) + } + + loc, err := geoip.Fetch() + if err != nil { + return fmt.Errorf("failed to fetch location info: %w", err) + } + + newLoc := substrate.Location{ + City: loc.City, + Country: loc.Country, + Latitude: fmt.Sprintf("%f", loc.Latitude), + Longitude: fmt.Sprintf("%f", loc.Longitude), + } + + if reflect.DeepEqual(newLoc, node.Location) { + // no need to update + return nil + } + + node.Location = newLoc + if _, err := substrateGw.UpdateNode(ctx, node); err != nil { + return fmt.Errorf("failed to update node on chain: %w", err) + } + + log.Info().Msg("node location updated") + return nil +} + func (r *Registrar) NodeID() (uint32, error) { - return r.returnIfDone(r.GetState().NodeID) + return r.returnIfDone(r.getState().NodeID) } func (r *Registrar) TwinID() (uint32, error) { - return r.returnIfDone(r.GetState().TwinID) + return r.returnIfDone(r.getState().TwinID) } func (r *Registrar) returnIfDone(v uint32) (uint32, error) { diff --git a/pkg/stubs/registrar_stub.go b/pkg/stubs/registrar_stub.go index e775cfb83..08d5f59fb 100644 --- a/pkg/stubs/registrar_stub.go +++ b/pkg/stubs/registrar_stub.go @@ -7,7 +7,6 @@ package stubs import ( "context" zbus "github.com/threefoldtech/zbus" - pkg "github.com/threefoldtech/zos/pkg" ) type RegistrarStub struct { @@ -27,22 +26,6 @@ func NewRegistrarStub(client zbus.Client) *RegistrarStub { } } -func (s *RegistrarStub) GetState(ctx context.Context) (ret0 pkg.State) { - args := []interface{}{} - result, err := s.client.RequestContext(ctx, s.module, s.object, "GetState", args...) - if err != nil { - panic(err) - } - result.PanicOnError() - loader := zbus.Loader{ - &ret0, - } - if err := result.Unmarshal(&loader); err != nil { - panic(err) - } - return -} - func (s *RegistrarStub) NodeID(ctx context.Context) (ret0 uint32, ret1 error) { args := []interface{}{} result, err := s.client.RequestContext(ctx, s.module, s.object, "NodeID", args...) diff --git a/pkg/updater/updater.go b/pkg/updater/updater.go deleted file mode 100644 index db0511256..000000000 --- a/pkg/updater/updater.go +++ /dev/null @@ -1,84 +0,0 @@ -package updater - -import ( - "context" - "fmt" - "reflect" - "time" - - "github.com/rs/zerolog/log" - substrate "github.com/threefoldtech/tfchain/clients/tfchain-client-go" - "github.com/threefoldtech/zbus" - - "github.com/threefoldtech/zos/pkg/geoip" - "github.com/threefoldtech/zos/pkg/registrar" - "github.com/threefoldtech/zos/pkg/stubs" -) - -const ( - updateInterval = 24 * time.Hour -) - -type Updater struct { - substrateGateway *stubs.SubstrateGatewayStub - registrar *stubs.RegistrarStub -} - -func NewUpdater(bus zbus.Client) *Updater { - return &Updater{ - substrateGateway: stubs.NewSubstrateGatewayStub(bus), - registrar: stubs.NewRegistrarStub(bus), - } -} - -func (u *Updater) Start(ctx context.Context) { - for { - if u.registrar.GetState(ctx).State == registrar.Done { - if err := u.updateLocation(); err != nil { - log.Error().Err(err).Msg("updating location failed") - } - log.Info().Msg("node location updated") - } - - select { - case <-ctx.Done(): - log.Info().Msg("stop node updater. context cancelled") - return - case <-time.After(updateInterval): - continue - } - } -} - -func (u *Updater) updateLocation() error { - nodeId, err := u.registrar.NodeID(context.Background()) - if err != nil { - return fmt.Errorf("failed to get node id: %w", err) - } - - node, err := u.substrateGateway.GetNode(context.Background(), nodeId) - if err != nil { - return fmt.Errorf("failed to get node from chain: %w", err) - } - - loc, err := geoip.Fetch() - if err != nil { - return fmt.Errorf("failed to fetch location info: %w", err) - } - - newLoc := substrate.Location{ - City: loc.City, - Country: loc.Country, - Latitude: fmt.Sprintf("%f", loc.Latitude), - Longitude: fmt.Sprintf("%f", loc.Longitude), - } - - if !reflect.DeepEqual(newLoc, node.Location) { - node.Location = newLoc - if _, err := u.substrateGateway.UpdateNode(context.Background(), node); err != nil { - return fmt.Errorf("failed to update node on chain: %w", err) - } - } - - return nil -} From f4cb77956d3ac055c2ef876f95d60099456c67d2 Mon Sep 17 00:00:00 2001 From: Omar Abdulaziz Date: Mon, 23 Sep 2024 14:41:36 +0300 Subject: [PATCH 3/4] remove unnecessary changes --- cmds/modules/noded/main.go | 1 - pkg/registrar/registrar.go | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/cmds/modules/noded/main.go b/cmds/modules/noded/main.go index 8e686789e..ccfd9a9cf 100644 --- a/cmds/modules/noded/main.go +++ b/cmds/modules/noded/main.go @@ -161,7 +161,6 @@ func action(cli *cli.Context) error { WithVirtualized(len(hypervisor) != 0) go registerationServer(ctx, msgBrokerCon, env, info) - log.Info().Msg("start perf scheduler") perfMon, err := perf.NewPerformanceMonitor(msgBrokerCon) diff --git a/pkg/registrar/registrar.go b/pkg/registrar/registrar.go index c58aab030..c99b25066 100644 --- a/pkg/registrar/registrar.go +++ b/pkg/registrar/registrar.go @@ -77,13 +77,13 @@ type Registrar struct { func NewRegistrar(ctx context.Context, cl zbus.Client, env environment.Environment, info RegistrationInfo) *Registrar { r := Registrar{ - state: State{ + State{ 0, 0, InProgress, "", }, - mutex: sync.RWMutex{}, + sync.RWMutex{}, } go r.register(ctx, cl, env, info) From 7e0ee7dcd139c7b3ae63089637c429bc04a1852e Mon Sep 17 00:00:00 2001 From: Omar Abdulaziz Date: Sun, 29 Sep 2024 16:24:39 +0300 Subject: [PATCH 4/4] drop the updateLocation logic and re-register instead at fixed interval --- pkg/registrar/registrar.go | 53 +++----------------------------------- 1 file changed, 4 insertions(+), 49 deletions(-) diff --git a/pkg/registrar/registrar.go b/pkg/registrar/registrar.go index c99b25066..b8bc67b3c 100644 --- a/pkg/registrar/registrar.go +++ b/pkg/registrar/registrar.go @@ -2,20 +2,16 @@ package registrar import ( "context" - "fmt" "os" - "reflect" "sync" "time" "github.com/cenkalti/backoff/v3" "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/zos/pkg/app" "github.com/threefoldtech/zos/pkg/environment" - "github.com/threefoldtech/zos/pkg/geoip" "github.com/threefoldtech/zos/pkg/stubs" ) @@ -28,7 +24,7 @@ const ( Done RegistrationState = "Done" monitorAccountEvery = 30 * time.Minute - updateLocationInterval = 24 * time.Hour + updateNodeInfoInterval = 24 * time.Hour ) var ( @@ -152,10 +148,9 @@ func (r *Registrar) register(ctx context.Context, cl zbus.Client, env environmen if err := r.reActivate(ctx, cl, env); err != nil { log.Error().Err(err).Msg("failed to reactivate account") } - case <-time.After(updateLocationInterval): - if err := r.updateLocation(ctx, cl); err != nil { - log.Error().Err(err).Msg("updating location failed") - } + case <-time.After(updateNodeInfoInterval): + log.Info().Msg("update interval passed, re-register") + register() case <-addressesUpdate: log.Info().Msg("zos address has changed, re-register") register() @@ -171,46 +166,6 @@ func (r *Registrar) reActivate(ctx context.Context, cl zbus.Client, env environm return err } -// updateLocation validates the node location on chain against the geoip -// service and update it if needed. -func (r *Registrar) updateLocation(ctx context.Context, cl zbus.Client) error { - nodeId, err := r.NodeID() - if err != nil { - return fmt.Errorf("failed to get node id: %w", err) - } - - substrateGw := stubs.NewSubstrateGatewayStub(cl) - node, err := substrateGw.GetNode(ctx, nodeId) - if err != nil { - return fmt.Errorf("failed to get node from chain: %w", err) - } - - loc, err := geoip.Fetch() - if err != nil { - return fmt.Errorf("failed to fetch location info: %w", err) - } - - newLoc := substrate.Location{ - City: loc.City, - Country: loc.Country, - Latitude: fmt.Sprintf("%f", loc.Latitude), - Longitude: fmt.Sprintf("%f", loc.Longitude), - } - - if reflect.DeepEqual(newLoc, node.Location) { - // no need to update - return nil - } - - node.Location = newLoc - if _, err := substrateGw.UpdateNode(ctx, node); err != nil { - return fmt.Errorf("failed to update node on chain: %w", err) - } - - log.Info().Msg("node location updated") - return nil -} - func (r *Registrar) NodeID() (uint32, error) { return r.returnIfDone(r.getState().NodeID) }