diff --git a/api/client.go b/api/client.go index 31a81017e4d6825f5f3d64f79d4844685ef1b110..8f316a986d98f1e50180197f1464752f3a60e011 100644 --- a/api/client.go +++ b/api/client.go @@ -8,6 +8,7 @@ package api import ( + "gitlab.com/xx_network/primitives/id" "time" "github.com/pkg/errors" @@ -517,9 +518,9 @@ func (c *Client) GetNetworkInterface() interfaces.NetworkManager { } // GetNodeRegistrationStatus gets the current status of node registration. It -// returns the number of nodes that the client is registered and the number of -// in progress node registrations. An error is returned if the network is not -// healthy. +// returns the the total number of nodes in the NDF and the number of those +// which are currently registers with. An error is returned if the network is +// not healthy. func (c *Client) GetNodeRegistrationStatus() (int, int, error) { // Return an error if the network is not healthy if !c.GetHealth().IsHealthy() { @@ -527,13 +528,24 @@ func (c *Client) GetNodeRegistrationStatus() (int, int, error) { "network is not healthy") } - // Get the number of nodes that client is registered with - registeredNodes := c.storage.Cmix().Count() + nodes := c.GetNetworkInterface().GetInstance().GetPartialNdf().Get().Nodes - // Get the number of in progress node registrations - inProgress := c.network.InProgressRegistrations() + cmixStore := c.storage.Cmix() + + 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++ + } + } - return registeredNodes, inProgress, nil + // Get the number of in progress node registrations + return numRegistered, len(nodes), nil } // ----- Utility Functions ----- diff --git a/bindings/client.go b/bindings/client.go index 04e2eba6e55645dfe1cd9f7fb7a14378dde5486d..296e9040978fcd7acd6cebfdfe16be27547fed5e 100644 --- a/bindings/client.go +++ b/bindings/client.go @@ -346,7 +346,7 @@ func (c *Client) GetUser() *User { } // 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) { registered, inProgress, err := c.api.GetNodeRegistrationStatus() diff --git a/bindings/registrationStatus.go b/bindings/registrationStatus.go index 22446376436a0a8145c97e20e868073e1984d0a0..5d33cbd6e2031b39455363e096153afcf6c38833 100644 --- a/bindings/registrationStatus.go +++ b/bindings/registrationStatus.go @@ -11,7 +11,7 @@ package bindings // for bindings. type NodeRegistrationsStatus struct { registered int - inProgress int + total int } // GetRegistered returns the number of nodes registered with the client. @@ -19,7 +19,7 @@ func (nrs *NodeRegistrationsStatus) GetRegistered() int { return nrs.registered } -// GetInProgress return the number of nodes currently registering. -func (nrs *NodeRegistrationsStatus) GetInProgress() int { - return nrs.inProgress +// GetTotal return the total of nodes currently in the network. +func (nrs *NodeRegistrationsStatus) GetTotal() int { + return nrs.total } diff --git a/cmd/root.go b/cmd/root.go index b025af41c87e773955399a7c69fbfb0d157ea765..bb0e5cb91837cf9d50085e80c2ab0d1fd9ea86dd 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -99,15 +99,15 @@ var rootCmd = &cobra.Command{ // After connection, make sure we have registered with at least // 85% of the nodes numReg := 1 - numNotReg := 100 - for numReg < 3*numNotReg { + total := 100 + for numReg < (total*3)/4 { time.Sleep(1 * time.Second) - numReg, numNotReg, err = client.GetNodeRegistrationStatus() + numReg, total, err = client.GetNodeRegistrationStatus() if err != nil { jww.FATAL.Panicf("%+v", err) } jww.INFO.Printf("Registering with nodes (%d/%d)...", - numReg, (numReg + numNotReg)) + numReg, total) } // Send Messages diff --git a/storage/cmix/store.go b/storage/cmix/store.go index ad0a10cd0995abca38207dd1ea77ff5d0dc905ae..8a2c514ef1bc2087a2bafc0ef420a3f8ce9333a4 100644 --- a/storage/cmix/store.go +++ b/storage/cmix/store.go @@ -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. func (s *Store) Remove(nid *id.ID) { s.mux.Lock() diff --git a/storage/cmix/store_test.go b/storage/cmix/store_test.go index 8e05e0c39438f1b22b63ea6ff1d3f3b3648b2b73..f4dfc3a7af59ee29511a2172779c6b919ef6ec6b 100644 --- a/storage/cmix/store_test.go +++ b/storage/cmix/store_test.go @@ -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 func TestLoadStore(t *testing.T) { // Uncomment to print keys that Set and Get are called on