Skip to content
Snippets Groups Projects
Commit 0e4fae51 authored by Richard T. Carback III's avatar Richard T. Carback III
Browse files

Merge branch 'betterNumRegistered' into 'release'

improved the numRegistered call to properly function on a client loaded

See merge request !560
parents 7aa7a34f 1ebb7f0d
No related branches found
No related tags found
No related merge requests found
......@@ -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()
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 -----
......
......@@ -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()
......
......@@ -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
}
......@@ -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
......
......@@ -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()
......
......@@ -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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment