Skip to content
Snippets Groups Projects
Unverified Commit 4e70593d authored by Wolfgang Welz's avatar Wolfgang Welz Committed by GitHub
Browse files

Fix: Ignore rejected peering requests (#137)

* fix: ignore rejected peering requests

* Do not store local services in DB
parent 12eefe1c
No related branches found
No related tags found
No related merge requests found
...@@ -50,18 +50,9 @@ func NewLocal(network string, address string, db DB) (*Local, error) { ...@@ -50,18 +50,9 @@ func NewLocal(network string, address string, db DB) (*Local, error) {
if l := len(key); l != ed25519.PrivateKeySize { if l := len(key); l != ed25519.PrivateKeySize {
return nil, fmt.Errorf("invalid key length: %d, need %d", l, ed25519.PublicKeySize) return nil, fmt.Errorf("invalid key length: %d, need %d", l, ed25519.PublicKeySize)
} }
services, err := db.LocalServices() // update the external address used for the peering
if err != nil { serviceRecord := service.New()
return nil, err
}
serviceRecord := services.CreateRecord()
// update the external address used for the peering and store back in DB
serviceRecord.Update(service.PeeringKey, network, address) serviceRecord.Update(service.PeeringKey, network, address)
err = db.UpdateLocalServices(serviceRecord)
if err != nil {
return nil, err
}
return newLocal(key, serviceRecord, db), nil return newLocal(key, serviceRecord, db), nil
} }
...@@ -81,12 +72,8 @@ func (l *Local) UpdateService(key service.Key, network string, address string) e ...@@ -81,12 +72,8 @@ func (l *Local) UpdateService(key service.Key, network string, address string) e
l.mu.Lock() l.mu.Lock()
defer l.mu.Unlock() defer l.mu.Unlock()
// update the service in the read protected map and store back in DB // update the service in the read protected map
l.serviceRecord.Update(key, network, address) l.serviceRecord.Update(key, network, address)
err := l.db.UpdateLocalServices(l.serviceRecord)
if err != nil {
return err
}
// create a new peer with the corresponding services // create a new peer with the corresponding services
l.Peer = *NewPeer(l.key.Public(), l.serviceRecord) l.Peer = *NewPeer(l.key.Public(), l.serviceRecord)
......
...@@ -4,7 +4,6 @@ import ( ...@@ -4,7 +4,6 @@ import (
"sync" "sync"
"time" "time"
"github.com/iotaledger/goshimmer/packages/autopeering/peer/service"
"github.com/iotaledger/hive.go/logger" "github.com/iotaledger/hive.go/logger"
) )
...@@ -13,7 +12,6 @@ type mapDB struct { ...@@ -13,7 +12,6 @@ type mapDB struct {
mutex sync.RWMutex mutex sync.RWMutex
m map[string]peerEntry m map[string]peerEntry
key PrivateKey key PrivateKey
services *service.Record
log *logger.Logger log *logger.Logger
...@@ -35,7 +33,6 @@ type peerPropEntry struct { ...@@ -35,7 +33,6 @@ type peerPropEntry struct {
func NewMemoryDB(log *logger.Logger) DB { func NewMemoryDB(log *logger.Logger) DB {
db := &mapDB{ db := &mapDB{
m: make(map[string]peerEntry), m: make(map[string]peerEntry),
services: service.New(),
log: log, log: log,
closing: make(chan struct{}), closing: make(chan struct{}),
} }
...@@ -70,24 +67,6 @@ func (db *mapDB) LocalPrivateKey() (PrivateKey, error) { ...@@ -70,24 +67,6 @@ func (db *mapDB) LocalPrivateKey() (PrivateKey, error) {
return db.key, nil return db.key, nil
} }
// LocalServices returns the services stored in the database or creates an empty map.
func (db *mapDB) LocalServices() (service.Service, error) {
db.mutex.RLock()
defer db.mutex.RUnlock()
return db.services, nil
}
// UpdateLocalServices updates the services stored in the database.
func (db *mapDB) UpdateLocalServices(services service.Service) error {
record := services.CreateRecord()
db.mutex.Lock()
defer db.mutex.Unlock()
db.services = record
return nil
}
// LastPing returns that property for the given peer ID and address. // LastPing returns that property for the given peer ID and address.
func (db *mapDB) LastPing(id ID, address string) time.Time { func (db *mapDB) LastPing(id ID, address string) time.Time {
db.mutex.RLock() db.mutex.RLock()
......
...@@ -7,7 +7,6 @@ import ( ...@@ -7,7 +7,6 @@ import (
"sync" "sync"
"time" "time"
"github.com/iotaledger/goshimmer/packages/autopeering/peer/service"
"github.com/iotaledger/goshimmer/packages/database" "github.com/iotaledger/goshimmer/packages/database"
"github.com/iotaledger/hive.go/logger" "github.com/iotaledger/hive.go/logger"
) )
...@@ -29,10 +28,6 @@ const ( ...@@ -29,10 +28,6 @@ const (
type DB interface { type DB interface {
// LocalPrivateKey returns the private key stored in the database or creates a new one. // LocalPrivateKey returns the private key stored in the database or creates a new one.
LocalPrivateKey() (PrivateKey, error) LocalPrivateKey() (PrivateKey, error)
// LocalServices returns the services stored in the database or creates an empty services.
LocalServices() (service.Service, error)
// UpdateLocalServices updates the services stored in the database.
UpdateLocalServices(services service.Service) error
// Peer retrieves a peer from the database. // Peer retrieves a peer from the database.
Peer(id ID) *Peer Peer(id ID) *Peer
...@@ -73,7 +68,6 @@ const ( ...@@ -73,7 +68,6 @@ const (
// Local information is keyed by ID only. Use localFieldKey to create those keys. // Local information is keyed by ID only. Use localFieldKey to create those keys.
dbLocalKey = "key" dbLocalKey = "key"
dbLocalServices = "services"
) )
// NewPersistentDB creates a new persistent DB. // NewPersistentDB creates a new persistent DB.
...@@ -192,33 +186,6 @@ func (db *persistentDB) LocalPrivateKey() (PrivateKey, error) { ...@@ -192,33 +186,6 @@ func (db *persistentDB) LocalPrivateKey() (PrivateKey, error) {
return key, nil return key, nil
} }
// LocalServices returns the services stored in the database or creates an empty services.
func (db *persistentDB) LocalServices() (service.Service, error) {
key, err := db.db.Get(localFieldKey(dbLocalServices))
if err == database.ErrKeyNotFound {
return service.New(), nil
}
if err != nil {
return nil, err
}
services, err := service.Unmarshal(key)
if err != nil {
return nil, err
}
return services, nil
}
// UpdateLocalServices updates the services stored in the database.
func (db *persistentDB) UpdateLocalServices(services service.Service) error {
value, err := services.CreateRecord().CreateRecord().Marshal()
if err != nil {
return err
}
return db.db.Set(localFieldKey(dbLocalServices), value)
}
// LastPing returns that property for the given peer ID and address. // LastPing returns that property for the given peer ID and address.
func (db *persistentDB) LastPing(id ID, address string) time.Time { func (db *persistentDB) LastPing(id ID, address string) time.Time {
return time.Unix(db.getInt64(nodeFieldKey(id, address, dbNodePing)), 0) return time.Unix(db.getInt64(nodeFieldKey(id, address, dbNodePing)), 0)
......
...@@ -94,18 +94,22 @@ func setupHooks(plugin *node.Plugin, conn *network.ManagedConnection, eventDispa ...@@ -94,18 +94,22 @@ func setupHooks(plugin *node.Plugin, conn *network.ManagedConnection, eventDispa
eventDispatchers.RemoveNode(ev.Peer.ID().Bytes()) eventDispatchers.RemoveNode(ev.Peer.ID().Bytes())
}) })
onAddChosenNeighbor := events.NewClosure(func(ev *selection.PeeringEvent) {
if ev.Status {
eventDispatchers.ConnectNodes(local.GetInstance().ID().Bytes(), ev.Peer.ID().Bytes())
}
})
onAddAcceptedNeighbor := events.NewClosure(func(ev *selection.PeeringEvent) { onAddAcceptedNeighbor := events.NewClosure(func(ev *selection.PeeringEvent) {
if ev.Status {
eventDispatchers.ConnectNodes(ev.Peer.ID().Bytes(), local.GetInstance().ID().Bytes()) eventDispatchers.ConnectNodes(ev.Peer.ID().Bytes(), local.GetInstance().ID().Bytes())
}
}) })
onRemoveNeighbor := events.NewClosure(func(ev *selection.DroppedEvent) { onRemoveNeighbor := events.NewClosure(func(ev *selection.DroppedEvent) {
eventDispatchers.DisconnectNodes(ev.DroppedID.Bytes(), local.GetInstance().ID().Bytes()) eventDispatchers.DisconnectNodes(ev.DroppedID.Bytes(), local.GetInstance().ID().Bytes())
}) })
onAddChosenNeighbor := events.NewClosure(func(ev *selection.PeeringEvent) {
eventDispatchers.ConnectNodes(local.GetInstance().ID().Bytes(), ev.Peer.ID().Bytes())
})
// setup hooks ///////////////////////////////////////////////////////////////////////////////////////////////////// // setup hooks /////////////////////////////////////////////////////////////////////////////////////////////////////
discover.Events.PeerDiscovered.Attach(onDiscoverPeer) discover.Events.PeerDiscovered.Attach(onDiscoverPeer)
......
...@@ -51,10 +51,14 @@ func configureEvents() { ...@@ -51,10 +51,14 @@ func configureEvents() {
log.Infof("Salt updated; expires=%s", ev.Public.GetExpiration().Format(time.RFC822)) log.Infof("Salt updated; expires=%s", ev.Public.GetExpiration().Format(time.RFC822))
})) }))
selection.Events.OutgoingPeering.Attach(events.NewClosure(func(ev *selection.PeeringEvent) { selection.Events.OutgoingPeering.Attach(events.NewClosure(func(ev *selection.PeeringEvent) {
if ev.Status {
log.Infof("Peering chosen: %s / %s", ev.Peer.Address(), ev.Peer.ID()) log.Infof("Peering chosen: %s / %s", ev.Peer.Address(), ev.Peer.ID())
}
})) }))
selection.Events.IncomingPeering.Attach(events.NewClosure(func(ev *selection.PeeringEvent) { selection.Events.IncomingPeering.Attach(events.NewClosure(func(ev *selection.PeeringEvent) {
if ev.Status {
log.Infof("Peering accepted: %s / %s", ev.Peer.Address(), ev.Peer.ID()) log.Infof("Peering accepted: %s / %s", ev.Peer.Address(), ev.Peer.ID())
}
})) }))
selection.Events.Dropped.Attach(events.NewClosure(func(ev *selection.DroppedEvent) { selection.Events.Dropped.Attach(events.NewClosure(func(ev *selection.DroppedEvent) {
log.Infof("Peering dropped: %s", ev.DroppedID) log.Infof("Peering dropped: %s", ev.DroppedID)
......
...@@ -39,6 +39,9 @@ func configureEvents() { ...@@ -39,6 +39,9 @@ func configureEvents() {
}() }()
})) }))
selection.Events.IncomingPeering.Attach(events.NewClosure(func(ev *selection.PeeringEvent) { selection.Events.IncomingPeering.Attach(events.NewClosure(func(ev *selection.PeeringEvent) {
if !ev.Status {
return // ignore rejected peering
}
go func() { go func() {
if err := mgr.AddInbound(ev.Peer); err != nil { if err := mgr.AddInbound(ev.Peer); err != nil {
log.Debugw("error adding inbound", "id", ev.Peer.ID(), "err", err) log.Debugw("error adding inbound", "id", ev.Peer.ID(), "err", err)
...@@ -46,6 +49,9 @@ func configureEvents() { ...@@ -46,6 +49,9 @@ func configureEvents() {
}() }()
})) }))
selection.Events.OutgoingPeering.Attach(events.NewClosure(func(ev *selection.PeeringEvent) { selection.Events.OutgoingPeering.Attach(events.NewClosure(func(ev *selection.PeeringEvent) {
if !ev.Status {
return // ignore rejected peering
}
go func() { go func() {
if err := mgr.AddOutbound(ev.Peer); err != nil { if err := mgr.AddOutbound(ev.Peer); err != nil {
log.Debugw("error adding outbound", "id", ev.Peer.ID(), "err", err) log.Debugw("error adding outbound", "id", ev.Peer.ID(), "err", err)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment