Skip to content

Commit

Permalink
Merge pull request mit-dci#429 from delbonis/refactor/peerinit
Browse files Browse the repository at this point in the history
Made some changes to peer init in lnp2p
  • Loading branch information
adiabat authored Nov 21, 2018
2 parents a97608d + ea17df4 commit 087d77e
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 113 deletions.
14 changes: 12 additions & 2 deletions db/lnbolt/peerdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,13 @@ func (pdb *peerboltdb) GetPeerAddrs() ([]lncore.LnAddr, error) {
if k == nil {
break
}
atmp = append(atmp, lncore.LnAddr(string(k)))
lnaddr, err := lncore.ParseLnAddr(string(k))
if err != nil {
logging.Warnf("lnbolt/peerdb: found invalid key in DB as lnaddr: %s (error: %s)", string(k), err.Error())
continue
}

atmp = append(atmp, lnaddr)
}

// Now that we have the final array return it.
Expand Down Expand Up @@ -126,7 +132,11 @@ func (pdb *peerboltdb) GetPeerInfos() (map[lncore.LnAddr]lncore.PeerInfo, error)
return err2
}

ka := lncore.LnAddr(string(k))
ka, err2 := lncore.ParseLnAddr(string(k))
if err2 != nil {
logging.Warnf("lnbolt/peerdb: found invalid key in DB as lnaddr: %s (error: %s)", string(k), err.Error())
continue
}
mtmp[ka] = pi

}
Expand Down
10 changes: 7 additions & 3 deletions litrpc/netcmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/mit-dci/lit/bech32"
"github.com/mit-dci/lit/crypto/koblitz"
"github.com/mit-dci/lit/lncore"
"github.com/mit-dci/lit/lnp2p"
"github.com/mit-dci/lit/lnutil"
"github.com/mit-dci/lit/logging"
"github.com/mit-dci/lit/qln"
Expand Down Expand Up @@ -95,8 +94,13 @@ func (r *LitRPC) Connect(args ConnectArgs, reply *ConnectReply) error {
paddr = strings.SplitN(paddr, "@", 2)[0]
}

var pm *lnp2p.PeerManager = r.Node.PeerMan
p := pm.GetPeer(lncore.LnAddr(paddr))
pm := r.Node.PeerMan
lnaddr, err := lncore.ParseLnAddr(paddr)
if err != nil {
return err
}

p := pm.GetPeer(lnaddr)
if p == nil {
return fmt.Errorf("couldn't find peer in manager after connecting")
}
Expand Down
37 changes: 37 additions & 0 deletions lncore/peers.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,46 @@
package lncore

import (
"fmt"

"github.com/mit-dci/lit/bech32"
)

// LnAddr is just a bech32-encoded pubkey.
// TODO Move this to another package so it's more obviously not *just* IO-related.
type LnAddr string

// ParseLnAddr will verify that the string passed is a valid LN address, as in
// ln1pmclh89haeswrw0unf8awuyqeu4t2uell58nea.
func ParseLnAddr(m string) (LnAddr, error) {

prefix, raw, err := bech32.Decode(m)

// Check it's valid bech32.
if err != nil {
return "", err
}

// Check it has the right prefix.
if prefix != "ln" {
return "", fmt.Errorf("prefix is not 'ln'")
}

// Check the length of the content bytes is right.
if len(raw) > 20 {
return "", fmt.Errorf("address too long to be pubkey")
}

return LnAddr(m), nil // should be the only place we cast to this type

}

// ToString returns the LnAddr as a string. Right now it just unwraps it but it
// might do something more eventually.
func (lnaddr LnAddr) ToString() string {
return string(lnaddr)
}

// LitPeerStorage is storage for peer data.
type LitPeerStorage interface {
GetPeerAddrs() ([]LnAddr, error)
Expand Down
56 changes: 10 additions & 46 deletions lnp2p/listen.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package lnp2p

import (
"github.com/mit-dci/lit/eventbus"
"github.com/mit-dci/lit/lncore"
"github.com/mit-dci/lit/lndc"
"github.com/mit-dci/lit/logging"
)
Expand Down Expand Up @@ -48,58 +47,23 @@ func acceptConnections(listener *lndc.Listener, port int, pm *PeerManager) {
rlitaddr := convertPubkeyToLitAddr(rpk)
rnetaddr := lndcConn.RemoteAddr()

logging.Infof("New connection from %s at %s\n", rlitaddr, rnetaddr.String())

// Read the peer info from the DB.
pi, err := pm.peerdb.GetPeerInfo(rlitaddr)
if err != nil {
logging.Warnf("problem loading peer info in DB (maybe this is ok?): %s\n", err.Error())
netConn.Close()
// Make sure we can't let ourself connect to ourself.
if string(rlitaddr) == pm.GetExternalAddress() {
logging.Infof("peermgr: Got a connection from ourselves? Dropping.")
lndcConn.Close()
continue
}

// Create the actual peer object.
npeer := &Peer{
lnaddr: rlitaddr,
nickname: nil,
conn: lndcConn,
idpubkey: rpk,
logging.Infof("peermgr: New connection from %s at %s\n", rlitaddr, rnetaddr.String())

// TEMP
idx: nil,
}

// Add the peer data to the DB if we don't have it.
if pi == nil {
raddr := rnetaddr.String()
pidx, err := pm.peerdb.GetUniquePeerIdx()
if err != nil {
logging.Errorf("problem getting unique peeridx: %s\n", err.Error())
}
pi = &lncore.PeerInfo{
LnAddr: &rlitaddr,
Nickname: nil,
NetAddr: &raddr,
PeerIdx: pidx,
}
err = pm.peerdb.AddPeer(rlitaddr, *pi)
npeer.idx = &pidx
if err != nil {
// don't close it, I guess
logging.Errorf("problem saving peer info to DB: %s\n", err.Error())
}
} else {
npeer.nickname = pi.Nickname
// TEMP
npeer.idx = &pi.PeerIdx
p, err := pm.handleNewConnection(lndcConn, rlitaddr)
if err != nil {
logging.Warnf("%s\n", err.Error())
continue
}

// Don't do any locking here since registerPeer takes a lock and Go's
// mutex isn't reentrant.
pm.registerPeer(npeer)

// Start a goroutine to process inbound traffic for this peer.
go processConnectionInboundTraffic(npeer, pm)
go processConnectionInboundTraffic(p, pm)

}

Expand Down
Loading

0 comments on commit 087d77e

Please sign in to comment.