peer and service ads now insert into peerstore
This commit is contained in:
@@ -212,7 +212,7 @@ func New(p Params) (c *Engine, e error) {
|
||||
if c.sub, e = c.topic.Subscribe(); fails(e) {
|
||||
return
|
||||
}
|
||||
log.D.Ln("subscribed to", PubSubTopic, "topic on gossip network")
|
||||
log.T.Ln("subscribed to", PubSubTopic, "topic on gossip network")
|
||||
}
|
||||
c.Manager.AddNodes(append([]*node.Node{p.Node}, p.Nodes...)...)
|
||||
// AddIntro a return session for receiving responses, ideally more of these
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/indra-labs/indra/pkg/onions/adload"
|
||||
"github.com/libp2p/go-libp2p/core/peer"
|
||||
"reflect"
|
||||
|
||||
@@ -33,16 +34,19 @@ func (ng *Engine) RunAdHandler(handler func(p *pubsub.Message,
|
||||
for {
|
||||
var m *pubsub.Message
|
||||
var e error
|
||||
log.D.Ln("waiting for next message from gossip network")
|
||||
if m, e = ng.sub.Next(ng.ctx); e != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
log.D.Ln("received message from gossip network")
|
||||
if e = handler(m, ng.ctx); fails(e) {
|
||||
continue
|
||||
}
|
||||
select {
|
||||
case <-ng.ctx.Done():
|
||||
log.D.Ln("shutting down ad handler")
|
||||
break out
|
||||
default:
|
||||
}
|
||||
}
|
||||
return
|
||||
@@ -71,7 +75,16 @@ func (ng *Engine) HandleAd(p *pubsub.Message, ctx context.Context) (e error) {
|
||||
return fmt.Errorf(ErrWrongTypeDecode,
|
||||
adaddress.Magic, reflect.TypeOf(c).String())
|
||||
} else {
|
||||
_ = addr
|
||||
// If we got to here now we can add to the PeerStore.
|
||||
log.D.S("new ad for services:", c)
|
||||
var id peer.ID
|
||||
if id, e = peer.IDFromPublicKey(addr.Key); fails(e) {
|
||||
return
|
||||
}
|
||||
if e = ng.Listener.Host.
|
||||
Peerstore().Put(id, "services", s.GetAll().ToBytes()); fails(e) {
|
||||
return
|
||||
}
|
||||
}
|
||||
case *adintro.Ad:
|
||||
log.D.Ln("received", reflect.TypeOf(c), "from gossip network")
|
||||
@@ -79,8 +92,33 @@ func (ng *Engine) HandleAd(p *pubsub.Message, ctx context.Context) (e error) {
|
||||
return fmt.Errorf(ErrWrongTypeDecode,
|
||||
adintro.Magic, reflect.TypeOf(c).String())
|
||||
} else {
|
||||
_ = intr
|
||||
|
||||
// If we got to here now we can add to the PeerStore.
|
||||
log.D.S("new ad for services:", c)
|
||||
var id peer.ID
|
||||
if id, e = peer.IDFromPublicKey(intr.Key); fails(e) {
|
||||
return
|
||||
}
|
||||
if e = ng.Listener.Host.
|
||||
Peerstore().Put(id, "services", s.GetAll().ToBytes()); fails(e) {
|
||||
return
|
||||
}
|
||||
}
|
||||
case *adload.Ad:
|
||||
log.D.Ln("received", reflect.TypeOf(c), "from gossip network")
|
||||
if lod, ok := c.(*adload.Ad); !ok {
|
||||
return fmt.Errorf(ErrWrongTypeDecode,
|
||||
adaddress.Magic, reflect.TypeOf(c).String())
|
||||
} else {
|
||||
// If we got to here now we can add to the PeerStore.
|
||||
log.D.S("new ad for services:", c)
|
||||
var id peer.ID
|
||||
if id, e = peer.IDFromPublicKey(lod.Key); fails(e) {
|
||||
return
|
||||
}
|
||||
if e = ng.Listener.Host.
|
||||
Peerstore().Put(id, "services", s.GetAll().ToBytes()); fails(e) {
|
||||
return
|
||||
}
|
||||
}
|
||||
case *adservices.Ad:
|
||||
log.D.Ln("received", reflect.TypeOf(c), "from gossip network")
|
||||
@@ -88,17 +126,16 @@ func (ng *Engine) HandleAd(p *pubsub.Message, ctx context.Context) (e error) {
|
||||
return fmt.Errorf(ErrWrongTypeDecode,
|
||||
adservices.Magic, reflect.TypeOf(c).String())
|
||||
} else {
|
||||
// If we got to here now we can add to the PeerStore.
|
||||
log.D.S("new ad for service:", c)
|
||||
var id peer.ID
|
||||
if id, e = peer.IDFromPublicKey(serv.Key); fails(e) {
|
||||
return
|
||||
}
|
||||
if e = ng.Listener.Host.
|
||||
Peerstore().Put(id, "service", s.GetAll().ToBytes()); fails(e) {
|
||||
return
|
||||
}
|
||||
|
||||
// If we got to here now we can add to the PeerStore.
|
||||
log.D.S("new ad for services:", c)
|
||||
var id peer.ID
|
||||
if id, e = peer.IDFromPublicKey(serv.Key); fails(e) {
|
||||
return
|
||||
}
|
||||
if e = ng.Listener.Host.
|
||||
Peerstore().Put(id, "services", s.GetAll().ToBytes()); fails(e) {
|
||||
return
|
||||
}
|
||||
}
|
||||
case *adpeer.Ad:
|
||||
log.D.Ln("received", reflect.TypeOf(c), "from gossip network")
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"github.com/indra-labs/indra"
|
||||
"github.com/indra-labs/indra/pkg/crypto/nonce"
|
||||
"github.com/indra-labs/indra/pkg/onions/adpeer"
|
||||
"github.com/indra-labs/indra/pkg/onions/adservices"
|
||||
"github.com/indra-labs/indra/pkg/util/splice"
|
||||
"os"
|
||||
"testing"
|
||||
@@ -54,17 +55,18 @@ func TestEngine_PeerStore(t *testing.T) {
|
||||
if e = engines[0].SendAd(newPeerAd); fails(e) {
|
||||
t.FailNow()
|
||||
}
|
||||
//time.Sleep(time.Second)
|
||||
//newServiceAd := adservices.NewServiceAd(nonce.NewID(),
|
||||
// engines[0].Manager.GetLocalNodeIdentityPrv(),
|
||||
// 20000, 54321, time.Now().Add(time.Hour*24*7))
|
||||
//ss := splice.New(newServiceAd.Len())
|
||||
//if e = newServiceAd.Encode(ss); fails(e) {
|
||||
// t.FailNow()
|
||||
//}
|
||||
//if e = engines[0].SendAd(newServiceAd); fails(e) {
|
||||
// t.FailNow()
|
||||
//}
|
||||
time.Sleep(time.Second * 3)
|
||||
newServiceAd := adservices.New(nonce.NewID(),
|
||||
engines[0].Manager.GetLocalNodeIdentityPrv(),
|
||||
[]adservices.Service{{20000, 54321}},
|
||||
time.Now().Add(time.Hour*24*7))
|
||||
ss := splice.New(newServiceAd.Len())
|
||||
if e = newServiceAd.Encode(ss); fails(e) {
|
||||
t.FailNow()
|
||||
}
|
||||
if e = engines[0].SendAd(newServiceAd); fails(e) {
|
||||
t.FailNow()
|
||||
}
|
||||
time.Sleep(time.Second * 3)
|
||||
cancel()
|
||||
for i := range engines {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package adpeer
|
||||
package adload
|
||||
|
||||
import (
|
||||
"github.com/indra-labs/indra/pkg/crypto"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package adpeer
|
||||
package adload
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
@@ -45,8 +45,8 @@ type Ad struct {
|
||||
|
||||
var _ coding.Codec = &Ad{}
|
||||
|
||||
// NewServiceAd ...
|
||||
func NewServiceAd(id nonce.ID, key *crypto.Prv, services []Service,
|
||||
// New ...
|
||||
func New(id nonce.ID, key *crypto.Prv, services []Service,
|
||||
expiry time.Time) (sv *Ad) {
|
||||
|
||||
s := splice.New(adintro.Len)
|
||||
|
||||
@@ -19,7 +19,7 @@ func TestServiceAd(t *testing.T) {
|
||||
var e error
|
||||
pr, _, _ := crypto.NewSigner()
|
||||
id := nonce.NewID()
|
||||
sv := NewServiceAd(id, pr, []Service{{80, 50000},{443, 50000}}, time.Now().Add(time.Hour))
|
||||
sv := New(id, pr, []Service{{80, 50000},{443, 50000}}, time.Now().Add(time.Hour))
|
||||
log.D.S("service", sv)
|
||||
s := splice.New(sv.Len())
|
||||
if e = sv.Encode(s); fails(e) {
|
||||
|
||||
Reference in New Issue
Block a user