Skip to content

Commit

Permalink
Fix bug with reset nicks
Browse files Browse the repository at this point in the history
  • Loading branch information
bahner committed Mar 9, 2024
1 parent 7042cc8 commit 2e7af56
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 36 deletions.
39 changes: 3 additions & 36 deletions p2p/peer/host.go → p2p/peer/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,42 +10,6 @@ import (
log "github.com/sirupsen/logrus"
)

// Get or creates a peer from the ID String.
// This might take sometime, but it's still very useful.
// It should normally e pretty fast.
func GetOrCreatePeerFromIDString(h host.Host, id string) (Peer, error) {

_p, err := Get(id)
if err == nil {
return _p, nil
}

addrInfo, err := GetPeerAddrInfoFromIDString(h, id)
if err != nil {
return Peer{}, err
}

return GetOrCreateFromAddrInfo(addrInfo)
}

func GetPeerAddrInfoFromIDString(h host.Host, id string) (p2peer.AddrInfo, error) {
pid, err := p2peer.Decode(id)
if err != nil {
return p2peer.AddrInfo{}, err
}

return GetPeerAddrInfoFromID(h, pid)
}

func GetPeerAddrInfoFromID(h host.Host, id p2peer.ID) (p2peer.AddrInfo, error) {
a := p2peer.AddrInfo{
ID: id,
Addrs: h.Peerstore().Addrs(id),
}

return a, nil
}

func ConnectAndProtect(ctx context.Context, h host.Host, pai p2peer.AddrInfo) error {

var (
Expand All @@ -60,6 +24,9 @@ func ConnectAndProtect(ctx context.Context, h host.Host, pai p2peer.AddrInfo) er
if err != nil {
return err
}

p.Nick = getOrCreateNodeAlias(p.ID) // This must happen early! Otherwise we'll get a different alias for the same peer in the cache.

if !IsAllowed(p.ID) { // Do an actual lookup in the database here
log.Debugf("Peer %s is explicitly denied", id)
UnprotectPeer(h, pai.ID)
Expand Down
37 changes: 37 additions & 0 deletions p2p/peer/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/bahner/go-ma-actor/config"
"github.com/bahner/go-ma-actor/config/db"
"github.com/libp2p/go-libp2p/core/host"
p2peer "github.com/libp2p/go-libp2p/core/peer"
)

Expand Down Expand Up @@ -59,6 +60,42 @@ func GetOrCreateFromAddrInfo(addrInfo p2peer.AddrInfo) (Peer, error) {

}

// Get or creates a peer from the ID String.
// This might take sometime, but it's still very useful.
// It should normally e pretty fast.
func GetOrCreatePeerFromIDString(h host.Host, id string) (Peer, error) {

_p, err := Get(id)
if err == nil {
return _p, nil
}

addrInfo, err := GetPeerAddrInfoFromIDString(h, id)
if err != nil {
return Peer{}, err
}

return GetOrCreateFromAddrInfo(addrInfo)
}

func GetPeerAddrInfoFromIDString(h host.Host, id string) (p2peer.AddrInfo, error) {
pid, err := p2peer.Decode(id)
if err != nil {
return p2peer.AddrInfo{}, err
}

return GetPeerAddrInfoFromID(h, pid)
}

func GetPeerAddrInfoFromID(h host.Host, id p2peer.ID) (p2peer.AddrInfo, error) {
a := p2peer.AddrInfo{
ID: id,
Addrs: h.Peerstore().Addrs(id),
}

return a, nil
}

// Return a boolean whther the peer is knoor not
// This this should err on the side of caution and return false
func IsKnown(id string) bool {
Expand Down

0 comments on commit 2e7af56

Please sign in to comment.