Skip to content
Snippets Groups Projects
Commit 81edc257 authored by Benjamin Wenger's avatar Benjamin Wenger
Browse files

improved the numRegistered call to properly function on a client loaded

from a session file
parent 7aa7a34f
No related branches found
No related tags found
No related merge requests found
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
package api package api
import ( import (
"gitlab.com/xx_network/primitives/id"
"time" "time"
"github.com/pkg/errors" "github.com/pkg/errors"
...@@ -517,9 +518,9 @@ func (c *Client) GetNetworkInterface() interfaces.NetworkManager { ...@@ -517,9 +518,9 @@ func (c *Client) GetNetworkInterface() interfaces.NetworkManager {
} }
// GetNodeRegistrationStatus gets the current status of node registration. It // GetNodeRegistrationStatus gets the current status of node registration. It
// returns the number of nodes that the client is registered and the number of // returns the the total number of nodes in the NDF and the number of those
// in progress node registrations. An error is returned if the network is not // which are currently registers with. An error is returned if the network is
// healthy. // not healthy.
func (c *Client) GetNodeRegistrationStatus() (int, int, error) { func (c *Client) GetNodeRegistrationStatus() (int, int, error) {
// Return an error if the network is not healthy // Return an error if the network is not healthy
if !c.GetHealth().IsHealthy() { if !c.GetHealth().IsHealthy() {
...@@ -527,13 +528,24 @@ func (c *Client) GetNodeRegistrationStatus() (int, int, error) { ...@@ -527,13 +528,24 @@ func (c *Client) GetNodeRegistrationStatus() (int, int, error) {
"network is not healthy") "network is not healthy")
} }
// Get the number of nodes that client is registered with nodes := c.GetNetworkInterface().GetInstance().GetPartialNdf().Get().Nodes
registeredNodes := c.storage.Cmix().Count()
// Get the number of in progress node registrations cmixStore := c.storage.Cmix()
inProgress := c.network.InProgressRegistrations()
return registeredNodes, inProgress, nil var numRegistered int
for i, n := range nodes{
nid, err := id.Unmarshal(n.ID)
if err!=nil{
return 0,0, errors.Errorf("Failed to unmarshal node ID %v " +
"(#%d): %s", n.ID, i, err.Error())
}
if cmixStore.Has(nid){
numRegistered++
}
}
// Get the number of in progress node registrations
return numRegistered, len(nodes), nil
} }
// ----- Utility Functions ----- // ----- Utility Functions -----
......
...@@ -346,7 +346,7 @@ func (c *Client) GetUser() *User { ...@@ -346,7 +346,7 @@ func (c *Client) GetUser() *User {
} }
// GetNodeRegistrationStatus returns a struct with the number of nodes the // GetNodeRegistrationStatus returns a struct with the number of nodes the
// client is registered with and the number of in progress registrations. // client is registered with and the number total.
func (c *Client) GetNodeRegistrationStatus() (*NodeRegistrationsStatus, error) { func (c *Client) GetNodeRegistrationStatus() (*NodeRegistrationsStatus, error) {
registered, inProgress, err := c.api.GetNodeRegistrationStatus() registered, inProgress, err := c.api.GetNodeRegistrationStatus()
......
...@@ -11,7 +11,7 @@ package bindings ...@@ -11,7 +11,7 @@ package bindings
// for bindings. // for bindings.
type NodeRegistrationsStatus struct { type NodeRegistrationsStatus struct {
registered int registered int
inProgress int total int
} }
// GetRegistered returns the number of nodes registered with the client. // GetRegistered returns the number of nodes registered with the client.
...@@ -21,5 +21,5 @@ func (nrs *NodeRegistrationsStatus) GetRegistered() int { ...@@ -21,5 +21,5 @@ func (nrs *NodeRegistrationsStatus) GetRegistered() int {
// GetInProgress return the number of nodes currently registering. // GetInProgress return the number of nodes currently registering.
func (nrs *NodeRegistrationsStatus) GetInProgress() int { func (nrs *NodeRegistrationsStatus) GetInProgress() int {
return nrs.inProgress return nrs.total
} }
...@@ -99,15 +99,15 @@ var rootCmd = &cobra.Command{ ...@@ -99,15 +99,15 @@ var rootCmd = &cobra.Command{
// After connection, make sure we have registered with at least // After connection, make sure we have registered with at least
// 85% of the nodes // 85% of the nodes
numReg := 1 numReg := 1
numNotReg := 100 total := 100
for numReg < 3*numNotReg { for numReg < (total*3)/4 {
time.Sleep(1 * time.Second) time.Sleep(1 * time.Second)
numReg, numNotReg, err = client.GetNodeRegistrationStatus() numReg, total, err = client.GetNodeRegistrationStatus()
if err != nil { if err != nil {
jww.FATAL.Panicf("%+v", err) jww.FATAL.Panicf("%+v", err)
} }
jww.INFO.Printf("Registering with nodes (%d/%d)...", jww.INFO.Printf("Registering with nodes (%d/%d)...",
numReg, (numReg + numNotReg)) numReg, total)
} }
// Send Messages // Send Messages
......
...@@ -110,6 +110,14 @@ func (s *Store) Add(nid *id.ID, k *cyclic.Int) { ...@@ -110,6 +110,14 @@ func (s *Store) Add(nid *id.ID, k *cyclic.Int) {
} }
} }
// Returns if the store has the node
func (s *Store) Has(nid *id.ID)bool {
s.mux.RLock()
_, exists := s.nodes[*nid]
s.mux.RUnlock()
return exists
}
// Remove removes a node key from the nodes map and saves. // Remove removes a node key from the nodes map and saves.
func (s *Store) Remove(nid *id.ID) { func (s *Store) Remove(nid *id.ID) {
s.mux.Lock() s.mux.Lock()
......
...@@ -70,6 +70,41 @@ func TestStore_AddRemove(t *testing.T) { ...@@ -70,6 +70,41 @@ func TestStore_AddRemove(t *testing.T) {
} }
} }
// Happy path Add/Has test
func TestStore_AddHas(t *testing.T) {
// Uncomment to print keys that Set and Get are called on
// jww.SetStdoutThreshold(jww.LevelTrace)
testStore, _ := makeTestStore()
nodeId := id.NewIdFromString("test", id.Node, t)
key := testStore.grp.NewInt(5)
testStore.Add(nodeId, key)
if _, exists := testStore.nodes[*nodeId]; !exists {
t.Fatal("Failed to add node key")
}
if !testStore.Has(nodeId) {
t.Fatal("cannot find the node id that that was added")
}
}
// Tests that has returns false when it doesnt have
func TestStore_DoesntHave(t *testing.T) {
// Uncomment to print keys that Set and Get are called on
// jww.SetStdoutThreshold(jww.LevelTrace)
testStore, _ := makeTestStore()
nodeId := id.NewIdFromString("test", id.Node, t)
if testStore.Has(nodeId) {
t.Fatal("found the node when it shouldnt have been found")
}
}
// Happy path // Happy path
func TestLoadStore(t *testing.T) { func TestLoadStore(t *testing.T) {
// Uncomment to print keys that Set and Get are called on // Uncomment to print keys that Set and Get are called on
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment