From 1ce924df492d1f10accde0acf4c6960f2b36ce3e Mon Sep 17 00:00:00 2001 From: Jono Wenger <jono@elixxir.io> Date: Thu, 27 Oct 2022 11:28:56 -0700 Subject: [PATCH] Move IsReady to xxDK --- bindings/follow.go | 42 +++++++++++++++--------------------------- xxdk/cmix.go | 22 ++++++++++++++++++++++ 2 files changed, 37 insertions(+), 27 deletions(-) diff --git a/bindings/follow.go b/bindings/follow.go index 5d324f2e8..08a9d0bff 100644 --- a/bindings/follow.go +++ b/bindings/follow.go @@ -115,33 +115,6 @@ type IsReadyInfo struct { HowClose float64 } -// IsReady returns true if at least percentReady of node registrations has -// completed. If not all have completed, then it returns false and howClose will -// be a percent (0-1) of node registrations completed. -// -// Parameters: -// - percentReady - The percentage of nodes required to be registered with to -// be ready. This is a number between 0 and 1. -// -// Returns: -// - JSON of [IsReadyInfo]. -func (c *Cmix) IsReady(percentReady float64) ([]byte, error) { - // Check if the network is currently healthy - if !c.api.GetCmix().IsHealthy() { - return json.Marshal(&IsReadyInfo{false, 0}) - } - - numReg, numNodes, err := c.api.GetNodeRegistrationStatus() - if err != nil { - jww.FATAL.Panicf("Failed to get node registration status: %+v", err) - } - - isReady := (float64(numReg) / float64(numNodes)) >= percentReady - howClose := float64(numReg) / (float64(numNodes) * percentReady) - - return json.Marshal(&IsReadyInfo{isReady, howClose}) -} - // NetworkFollowerStatus gets the state of the network follower. It returns a // status with the following values: // Stopped - 0 @@ -180,6 +153,21 @@ func (c *Cmix) GetNodeRegistrationStatus() ([]byte, error) { return json.Marshal(nodeRegReport) } +// IsReady returns true if at least percentReady of node registrations has +// completed. If not all have completed, then it returns false and howClose will +// be a percent (0-1) of node registrations completed. +// +// Parameters: +// - percentReady - The percentage of nodes required to be registered with to +// be ready. This is a number between 0 and 1. +// +// Returns: +// - JSON of [IsReadyInfo]. +func (c *Cmix) IsReady(percentReady float64) ([]byte, error) { + isReady, howClose := c.api.IsReady(percentReady) + return json.Marshal(&IsReadyInfo{isReady, howClose}) +} + // PauseNodeRegistrations stops all node registrations and returns a function to // resume them. // diff --git a/xxdk/cmix.go b/xxdk/cmix.go index 5189735c6..8d4e8e4d9 100644 --- a/xxdk/cmix.go +++ b/xxdk/cmix.go @@ -484,6 +484,28 @@ func (c *Cmix) GetNodeRegistrationStatus() (int, int, error) { return numRegistered, len(nodes) - numStale, nil } + + +// IsReady returns true if at least percentReady of node registrations has +// completed. If not all have completed, then it returns false and howClose will +// be a percent (0-1) of node registrations completed. +func (c *Cmix) IsReady(percentReady float64) (isReady bool, howClose float64) { + // Check if the network is currently healthy + if !c.network.IsHealthy() { + return false, 0 + } + + numReg, numNodes, err := c.GetNodeRegistrationStatus() + if err != nil { + jww.FATAL.Panicf("Failed to get node registration status: %+v", err) + } + + isReady = (float64(numReg) / float64(numNodes)) >= percentReady + howClose = float64(numReg) / (float64(numNodes) * percentReady) + + return isReady, howClose +} + // PauseNodeRegistrations stops all node registrations and returns a function to // resume them. func (c *Cmix) PauseNodeRegistrations(timeout time.Duration) error { -- GitLab