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 4e58010
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 90 deletions.
24 changes: 24 additions & 0 deletions p2p/peer/addrinfo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package peer

import (
"github.com/libp2p/go-libp2p/core/host"
p2peer "github.com/libp2p/go-libp2p/core/peer"
)

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

return PeerAddrInfoFromID(h, pid)
}

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

return a, nil
}
37 changes: 1 addition & 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,7 @@ func ConnectAndProtect(ctx context.Context, h host.Host, pai p2peer.AddrInfo) er
if err != nil {
return err
}

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
43 changes: 43 additions & 0 deletions p2p/peer/nick.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,30 @@ func Lookup(name string) string {
return id
}

// Return a boolean whther the peer is known not
// This this should err on the side of caution and return false
// The input can be a peer ID or a nickname.
func IsKnown(id string) bool {
db, err := db.Get()
if err != nil {
return false
}

// We just need to know if the peer exists, so we select the id itself.
var peerID string
err = db.QueryRow(_LOOKUP_ID, id).Scan(&peerID)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return false
}
// Some other error occurred.
return false
}

// If we get here, it means the peer exists in the database.
return true
}

func Nicks() map[string]string {
db, err := db.Get()
if err != nil {
Expand All @@ -167,3 +191,22 @@ func Nicks() map[string]string {

return peers
}

// Function is equiavalent to ShortString() in libp2p, but it also
// checks if the peer is known in the database and returns the
// node alias if it exists.
// The ShortString() function returns the last 8 chars of the peer ID.
// The input is a full peer ID string.
// Returns the input in case of errors
func getOrCreateNick(id string) (nodeAlias string) {

if IsKnown(id) {
nodeAlias, err := LookupNick(Lookup(id))
if err == nil {
return nodeAlias
}
}

return id[len(id)-nodeAliasLength:]

}
80 changes: 29 additions & 51 deletions p2p/peer/peer.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package peer

import (
"database/sql"
"errors"

"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 @@ -39,70 +38,49 @@ func New(addrInfo p2peer.AddrInfo, nick string, allowed bool) Peer {
}
}

// 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) {

_, err := p2peer.Decode(id)
if err != nil {
return Peer{}, err
}

_p, err := Get(id)
if err == nil {
// Always do a lookup on the nick as it might've changed
_p.Nick = getOrCreateNick(id)
return _p, nil
}

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

return GetOrCreateFromAddrInfo(addrInfo)
}

// Get or create a peer from an addrinfo. This is a dead function,
// in the sense that it does not do any live P2P lookups and as such
// it's use is safe to use anytime.
// The lookup is just in the local memory cache and database.
func GetOrCreateFromAddrInfo(addrInfo p2peer.AddrInfo) (Peer, error) {

id := addrInfo.ID.String()
nick := getOrCreateNick(id)

p, err := Get(id)
if err == nil {
p.Nick = nick
return p, nil
}

return New(
addrInfo,
getOrCreateNodeAlias(id),
nick,
config.DEFAULT_ALLOW_ALL), 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 {
db, err := db.Get()
if err != nil {
return false
}

// We just need to know if the peer exists, so we select the id itself.
var peerID string
err = db.QueryRow(_SELECT_ID, id).Scan(&peerID)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
// The peer is not in the database.
return false
}
// Some other error occurred.
return false
}

// If we get here, it means the peer exists in the database.
return true
}

// Function is equiavalent to ShortString() in libp2p, but it also
// checks if the peer is known in the database and returns the
// node alias if it exists.
// The ShortString() function returns the last 8 chars of the peer ID.
// The input is a full peer ID string.
// Returns the input in case of errors
func getOrCreateNodeAlias(id string) (nodeAlias string) {

if len(id) < nodeAliasLength {
return id
}

// Nicks to lookup can be less than 8 chars
if IsKnown(id) {
nodeAlias, err := LookupNick(id)
if err == nil {
return nodeAlias
}
}

return id[len(id)-nodeAliasLength:]

}
4 changes: 2 additions & 2 deletions p2p/peer/peers.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ func Delete(id string) error {
return nil
}

// CachedPeers returns a slice of all peers in the the DB.
// Peers returns a slice of all peers in the the DB.
// NB! This is not the database and should be used to check nick and allowed status.
// It's just a cache.
func CachedPeers() ([]Peer, error) {
func Peers() ([]Peer, error) {
var pList []Peer
peers.Range(func(_, value interface{}) bool {
p, ok := value.(Peer)
Expand Down
2 changes: 1 addition & 1 deletion ui/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func (ui *ChatUI) handlePeerConnectCommand(args []string) {
if len(args) == 3 {
id := peer.Lookup(args[2])

addrInfo, err := peer.GetPeerAddrInfoFromIDString(ui.p.Host, id)
addrInfo, err := peer.PeerAddrInfoFromPeerIDString(ui.p.Host, id)
if err != nil {
ui.displaySystemMessage("Error: " + err.Error())
return
Expand Down

0 comments on commit 4e58010

Please sign in to comment.