From bd006184ff4808e95943e8c913cf5d775414200a Mon Sep 17 00:00:00 2001 From: Jono Wenger <jono@elixxir.io> Date: Tue, 18 Oct 2022 10:27:37 -0700 Subject: [PATCH] Add ReadyToSend, which checks if the network is ready to send on --- bindings/follow.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/bindings/follow.go b/bindings/follow.go index 503aa5e1a..56c653af4 100644 --- a/bindings/follow.go +++ b/bindings/follow.go @@ -10,6 +10,7 @@ package bindings import ( "encoding/json" "fmt" + jww "github.com/spf13/jwalterweatherman" "gitlab.com/elixxir/client/cmix/message" "time" @@ -82,6 +83,25 @@ func (c *Cmix) WaitForNetwork(timeoutMS int) bool { return false } +// ReadyToSend determines if the network is ready to send messages on. It +// returns true if the network is healthy and if the client has registered with +// at least 70% of the nodes. Returns false otherwise. +func (c *Cmix) ReadyToSend() bool { + // Check if the network is currently healthy + if !c.api.GetCmix().IsHealthy() { + return false + } + + // If the network is healthy, then check the number of nodes that the client + // is currently registered with + numReg, total, err := c.api.GetNodeRegistrationStatus() + if err != nil { + jww.FATAL.Panicf("Failed to get node registration status: %+v", err) + } + + return numReg >= total*7/10 +} + // NetworkFollowerStatus gets the state of the network follower. It returns a // status with the following values: // Stopped - 0 -- GitLab