From 4392bd47e4de116301f897780775aa7200696534 Mon Sep 17 00:00:00 2001
From: jbhusson <jonah@elixxir.io>
Date: Mon, 2 Mar 2020 10:26:04 -0800
Subject: [PATCH] update function should add hosts that are missing

---
 go.mod                             |  8 +++++---
 go.sum                             | 15 +++++++++------
 network/dataStructures/ndf.go      | 15 ++++++++++++++-
 network/dataStructures/ndf_test.go |  7 ++++---
 network/instance.go                |  9 ++++-----
 network/securedNdf.go              |  5 +++--
 network/securedNdf_test.go         |  7 ++++---
 7 files changed, 43 insertions(+), 23 deletions(-)

diff --git a/go.mod b/go.mod
index 4cdd9869..c9337fde 100644
--- a/go.mod
+++ b/go.mod
@@ -17,12 +17,14 @@ require (
 	github.com/spf13/jwalterweatherman v1.1.0
 	github.com/stretchr/objx v0.2.0 // indirect
 	gitlab.com/elixxir/crypto v0.0.0-20200229000841-b1ee7117a1d0
-	gitlab.com/elixxir/primitives v0.0.0-20200229220646-a92f3188db53
+	gitlab.com/elixxir/primitives v0.0.0-20200301205752-350e61bd19b3
 	golang.org/x/exp v0.0.0-20200228211341-fcea875c7e85 // indirect
 	golang.org/x/image v0.0.0-20200119044424-58c23975cae1 // indirect
 	golang.org/x/mobile v0.0.0-20200222142934-3c8601c510d0 // indirect
-	golang.org/x/net v0.0.0-20200226121028-0de0cce0169b
-	golang.org/x/tools v0.0.0-20200228224639-71482053b885 // indirect
+	golang.org/x/net v0.0.0-20200301022130-244492dfa37a
+	golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527 // indirect
+	golang.org/x/tools v0.0.0-20200302155637-b1e4e04173e0 // indirect
+	google.golang.org/genproto v0.0.0-20200302123026-7795fca6ccb1 // indirect
 	google.golang.org/grpc v1.27.1
 	rsc.io/sampler v1.99.99 // indirect
 )
diff --git a/go.sum b/go.sum
index 24f0c99d..4bacce80 100644
--- a/go.sum
+++ b/go.sum
@@ -127,8 +127,8 @@ gitlab.com/elixxir/primitives v0.0.0-20200131183153-e93c6b75019f/go.mod h1:REJMc
 gitlab.com/elixxir/primitives v0.0.0-20200218211222-4193179f359c/go.mod h1:REJMcwIcyxh74VSHqy4S9yYiaEsQYObOPglRExDpk14=
 gitlab.com/elixxir/primitives v0.0.0-20200226191544-59ff3303abb3 h1:572R0Ap/rYmC6XNNQeoZfvfqXyuiRENidgUzdpCbFHw=
 gitlab.com/elixxir/primitives v0.0.0-20200226191544-59ff3303abb3/go.mod h1:REJMcwIcyxh74VSHqy4S9yYiaEsQYObOPglRExDpk14=
-gitlab.com/elixxir/primitives v0.0.0-20200229220646-a92f3188db53 h1:hINouQ1q8iPxbQ183mi92Bq5u390LGFD4vnKNOztXTs=
-gitlab.com/elixxir/primitives v0.0.0-20200229220646-a92f3188db53/go.mod h1:ZCg9za7qWcObBMVRGulVTL/0+Z2zX2BH3XkCPDOtb18=
+gitlab.com/elixxir/primitives v0.0.0-20200301205752-350e61bd19b3 h1:vPyV7UFvsrDKOmNUMdtyEPCmbzRIpleGarA/ejou6cI=
+gitlab.com/elixxir/primitives v0.0.0-20200301205752-350e61bd19b3/go.mod h1:ZCg9za7qWcObBMVRGulVTL/0+Z2zX2BH3XkCPDOtb18=
 go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
 go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8=
 go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
@@ -189,8 +189,9 @@ golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553/go.mod h1:z5CRVTTTmAJ677TzLL
 golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
 golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
 golang.org/x/net v0.0.0-20200222125558-5a598a2470a0/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
-golang.org/x/net v0.0.0-20200226121028-0de0cce0169b h1:0mm1VjtFUOIlE1SbDlwjYaDxZVDP2S5ou6y0gSgXHu8=
 golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
+golang.org/x/net v0.0.0-20200301022130-244492dfa37a h1:GuSPYbZzB5/dcLNCwLQLsg3obCJtX9IJhpXkvY7kzk0=
+golang.org/x/net v0.0.0-20200301022130-244492dfa37a/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
 golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be h1:vEDujvNQGv4jgYKudGeI/+DAX4Jffq6hpD55MmoEvKs=
 golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
 golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
@@ -218,8 +219,9 @@ golang.org/x/sys v0.0.0-20200122134326-e047566fdf82 h1:ywK/j/KkyTHcdyYSZNXGjMwgm
 golang.org/x/sys v0.0.0-20200122134326-e047566fdf82/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 golang.org/x/sys v0.0.0-20200212091648-12a6c2dcc1e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
-golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae h1:/WDfKMnPU+m5M4xB+6x4kaepxRw6jWvR5iDRdvjHgy8=
 golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527 h1:uYVVQ9WP/Ds2ROhcaGPeIdVq0RIXVLwsHlnvJ+cT1So=
+golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
 golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
 golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
@@ -259,7 +261,7 @@ golang.org/x/tools v0.0.0-20200207183749-b753a1ba74fa/go.mod h1:TB2adYChydJhpapK
 golang.org/x/tools v0.0.0-20200212150539-ea181f53ac56/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
 golang.org/x/tools v0.0.0-20200224181240-023911ca70b2/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
 golang.org/x/tools v0.0.0-20200227222343-706bc42d1f0d/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
-golang.org/x/tools v0.0.0-20200228224639-71482053b885/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
+golang.org/x/tools v0.0.0-20200302155637-b1e4e04173e0/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
 golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
 golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
 golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
@@ -295,8 +297,9 @@ google.golang.org/genproto v0.0.0-20200122232147-0452cf42e150/go.mod h1:n3cpQtvx
 google.golang.org/genproto v0.0.0-20200204135345-fa8e72b47b90/go.mod h1:GmwEX6Z4W5gMy59cAlVYjN9JhxgbQH6Gn+gFDQe2lzA=
 google.golang.org/genproto v0.0.0-20200212174721-66ed5ce911ce/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c=
 google.golang.org/genproto v0.0.0-20200224152610-e50cd9704f63/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c=
-google.golang.org/genproto v0.0.0-20200228133532-8c2c7df3a383 h1:Vo0fD5w0fUKriWlZLyrim2GXbumyN0D6euW79T9PgEE=
 google.golang.org/genproto v0.0.0-20200228133532-8c2c7df3a383/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c=
+google.golang.org/genproto v0.0.0-20200302123026-7795fca6ccb1 h1:RYJIKMPLUCjLP+fEg9ygjxF3KjfSHN4BSZw91aecq6U=
+google.golang.org/genproto v0.0.0-20200302123026-7795fca6ccb1/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c=
 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
 gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
 gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
diff --git a/network/dataStructures/ndf.go b/network/dataStructures/ndf.go
index d96c9a40..5351ba49 100644
--- a/network/dataStructures/ndf.go
+++ b/network/dataStructures/ndf.go
@@ -4,7 +4,9 @@ import (
 	"bytes"
 	"crypto/sha256"
 	"github.com/pkg/errors"
+	"gitlab.com/elixxir/comms/connect"
 	pb "gitlab.com/elixxir/comms/mixmessages"
+	id2 "gitlab.com/elixxir/primitives/id"
 	"gitlab.com/elixxir/primitives/ndf"
 	"sync"
 )
@@ -17,7 +19,7 @@ type Ndf struct {
 }
 
 //Updates to a new NDF if the passed NDF is valid
-func (file *Ndf) Update(m *pb.NDF) error {
+func (file *Ndf) Update(m *pb.NDF, comm *connect.ProtoComms) error {
 
 	//build the ndf object
 	decoded, _, err := ndf.DecodeNDF(string(m.Ndf))
@@ -32,6 +34,17 @@ func (file *Ndf) Update(m *pb.NDF) error {
 	file.pb = m
 	file.f = decoded
 
+	for _, n := range file.f.Nodes {
+		id := id2.NewNodeFromBytes(n.ID).String()
+		_, ok := comm.GetHost(id)
+		if !ok {
+			_, err := comm.AddHost(id, n.Address, []byte(n.TlsCertificate), false, true)
+			if err != nil {
+				return errors.Wrapf(err, "Error adding host for node %s", id)
+			}
+		}
+	}
+
 	//set the ndf hash
 	marshaled, err := file.f.Marshal()
 	if err != nil {
diff --git a/network/dataStructures/ndf_test.go b/network/dataStructures/ndf_test.go
index 3f30c80f..f0cdeaa8 100644
--- a/network/dataStructures/ndf_test.go
+++ b/network/dataStructures/ndf_test.go
@@ -2,6 +2,7 @@ package dataStructures
 
 import (
 	"encoding/base64"
+	"gitlab.com/elixxir/comms/connect"
 	"gitlab.com/elixxir/comms/mixmessages"
 	"testing"
 )
@@ -74,7 +75,7 @@ func setup() *Ndf {
 	}
 	ndf := &Ndf{}
 
-	_ = ndf.Update(msg)
+	_ = ndf.Update(msg, &connect.ProtoComms{})
 	return ndf
 }
 
@@ -95,12 +96,12 @@ func TestNdf_Update(t *testing.T) {
 	}
 	ndf := Ndf{}
 
-	err := ndf.Update(badMsg)
+	err := ndf.Update(badMsg, &connect.ProtoComms{})
 	if err == nil {
 		t.Error("Should have returned error when unable to decode ndf")
 	}
 
-	err = ndf.Update(msg)
+	err = ndf.Update(msg, &connect.ProtoComms{})
 	if err != nil {
 		t.Errorf("Failed to update ndf: %+v", err)
 	}
diff --git a/network/instance.go b/network/instance.go
index f8a09055..f6a02d99 100644
--- a/network/instance.go
+++ b/network/instance.go
@@ -41,7 +41,7 @@ func (i *Instance) UpdatePartialNdf(m *pb.NDF) error {
 			"for NDF partial verification")
 	}
 
-	return i.partial.update(m, perm.GetPubKey())
+	return i.partial.update(m, i.comm, perm.GetPubKey())
 }
 
 //update the full ndf
@@ -53,7 +53,7 @@ func (i *Instance) UpdateFullNdf(m *pb.NDF) error {
 			"for full NDF verification")
 	}
 
-	return i.full.update(m, perm.GetPubKey())
+	return i.full.update(m, i.comm, perm.GetPubKey())
 }
 
 func (i *Instance) GetPartialNdf() *SecuredNdf {
@@ -105,11 +105,10 @@ func (i *Instance) GetRoundUpdates(id int) ([]*pb.RoundInfo, error) {
 	return i.roundUpdates.GetUpdates(id)
 }
 
-func (i *Instance)GetLastUpdateID()int{
+func (i *Instance) GetLastUpdateID() int {
 	return i.roundUpdates.GetLastUpdateID()
 }
 
-func (i *Instance)GetLastRoundID()id.Round{
+func (i *Instance) GetLastRoundID() id.Round {
 	return i.roundData.GetLastRoundID()
 }
-
diff --git a/network/securedNdf.go b/network/securedNdf.go
index 4ea4a790..3684c9e2 100644
--- a/network/securedNdf.go
+++ b/network/securedNdf.go
@@ -2,6 +2,7 @@ package network
 
 import (
 	"github.com/pkg/errors"
+	"gitlab.com/elixxir/comms/connect"
 	pb "gitlab.com/elixxir/comms/mixmessages"
 	ds "gitlab.com/elixxir/comms/network/dataStructures"
 	"gitlab.com/elixxir/crypto/signature"
@@ -22,13 +23,13 @@ func NewSecuredNdf() *SecuredNdf {
 }
 
 // unexported NDF update code
-func (sndf *SecuredNdf) update(m *pb.NDF, key *rsa.PublicKey) error {
+func (sndf *SecuredNdf) update(m *pb.NDF, comm *connect.ProtoComms, key *rsa.PublicKey) error {
 	err := signature.Verify(m, key)
 	if err != nil {
 		return errors.WithMessage(err, "Could not validate NDF")
 	}
 
-	return sndf.f.Update(m)
+	return sndf.f.Update(m, comm)
 }
 
 func (sndf *SecuredNdf) Get() *ndf.NetworkDefinition {
diff --git a/network/securedNdf_test.go b/network/securedNdf_test.go
index ff9e69dc..0d7624bf 100644
--- a/network/securedNdf_test.go
+++ b/network/securedNdf_test.go
@@ -2,6 +2,7 @@ package network
 
 import (
 	"encoding/base64"
+	"gitlab.com/elixxir/comms/connect"
 	pb "gitlab.com/elixxir/comms/mixmessages"
 	ds "gitlab.com/elixxir/comms/network/dataStructures"
 	"gitlab.com/elixxir/crypto/signature"
@@ -79,7 +80,7 @@ func setup() *ds.Ndf {
 	}
 	ndf := &ds.Ndf{}
 
-	_ = ndf.Update(msg)
+	_ = ndf.Update(msg, &connect.ProtoComms{})
 	return ndf
 }
 
@@ -120,13 +121,13 @@ func TestSecuredNdf_update(t *testing.T) {
 	}
 
 	sndf := NewSecuredNdf()
-	err = sndf.update(&f, privKey.GetPublic())
+	err = sndf.update(&f, &connect.ProtoComms{}, privKey.GetPublic())
 
 	if err != nil {
 		t.Errorf("Could not update ndf: %s", err)
 	}
 
-	err = sndf.update(&f, badPub)
+	err = sndf.update(&f, &connect.ProtoComms{}, badPub)
 	if err == nil {
 		t.Errorf("should have received bad key error")
 	}
-- 
GitLab