lndc+lnd: fix panic when connecting to multiple peers, plus duplicate conn detection
This commit is contained in:
parent
9780f06faf
commit
2a57f9182a
@ -25,22 +25,30 @@ type LNAdr struct {
|
|||||||
name string // human readable name? Not a thing yet.
|
name string // human readable name? Not a thing yet.
|
||||||
host string // internet host this ID is reachable at. also not a thing
|
host string // internet host this ID is reachable at. also not a thing
|
||||||
endorsement []byte // a sig confirming the name? Not implemented
|
endorsement []byte // a sig confirming the name? Not implemented
|
||||||
|
|
||||||
|
net *chaincfg.Params
|
||||||
}
|
}
|
||||||
|
|
||||||
// String...
|
// newLnAdr....
|
||||||
func (l *LNAdr) String() string {
|
func NewLnAdr(addr *net.TCPAddr, pubkey *btcec.PublicKey,
|
||||||
var encodedId []byte
|
net *chaincfg.Params) (*LNAdr, error) {
|
||||||
if l.PubKey == nil {
|
|
||||||
encodedId = l.Base58Adr.ScriptAddress()
|
hash160 := btcutil.Hash160(pubkey.SerializeCompressed())
|
||||||
} else {
|
pkh, err := btcutil.NewAddressPubKeyHash(hash160, net)
|
||||||
encodedId = l.PubKey.SerializeCompressed()
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return fmt.Sprintf("%v@%v", hex.EncodeToString(encodedId), l.NetAddr)
|
return &LNAdr{
|
||||||
|
PubKey: pubkey,
|
||||||
|
Base58Adr: pkh,
|
||||||
|
NetAddr: addr,
|
||||||
|
net: net,
|
||||||
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// newLnAddr...
|
// newLnAddr...
|
||||||
func LnAddrFromString(encodedAddr string) (*LNAdr, error) {
|
func LnAddrFromString(encodedAddr string, netParams *chaincfg.Params) (*LNAdr, error) {
|
||||||
// The format of an lnaddr is "<pubkey or pkh>@host"
|
// The format of an lnaddr is "<pubkey or pkh>@host"
|
||||||
idHost := strings.Split(encodedAddr, "@")
|
idHost := strings.Split(encodedAddr, "@")
|
||||||
if len(idHost) != 2 {
|
if len(idHost) != 2 {
|
||||||
@ -54,7 +62,7 @@ func LnAddrFromString(encodedAddr string) (*LNAdr, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
addr := &LNAdr{NetAddr: ipAddr}
|
addr := &LNAdr{NetAddr: ipAddr, net: netParams}
|
||||||
|
|
||||||
idLen := len(idHost[0])
|
idLen := len(idHost[0])
|
||||||
switch {
|
switch {
|
||||||
@ -73,14 +81,14 @@ func LnAddrFromString(encodedAddr string) (*LNAdr, error) {
|
|||||||
// got pubey, populate address from pubkey
|
// got pubey, populate address from pubkey
|
||||||
pkh := btcutil.Hash160(addr.PubKey.SerializeCompressed())
|
pkh := btcutil.Hash160(addr.PubKey.SerializeCompressed())
|
||||||
addr.Base58Adr, err = btcutil.NewAddressPubKeyHash(pkh,
|
addr.Base58Adr, err = btcutil.NewAddressPubKeyHash(pkh,
|
||||||
&chaincfg.TestNet3Params)
|
netParams)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
// Is the ID a string encoded bitcoin address?
|
// Is the ID a string encoded bitcoin address?
|
||||||
case idLen > 33 && idLen < 37:
|
case idLen > 33 && idLen < 37:
|
||||||
addr.Base58Adr, err = btcutil.DecodeAddress(idHost[0],
|
addr.Base58Adr, err = btcutil.DecodeAddress(idHost[0],
|
||||||
&chaincfg.TestNet3Params)
|
netParams)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -167,3 +175,17 @@ func (l *LNAdr) Deserialize(s []byte) error {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// String...
|
||||||
|
func (l *LNAdr) String() string {
|
||||||
|
var encodedId []byte
|
||||||
|
if l.Base58Adr != nil {
|
||||||
|
encodedId = l.Base58Adr.ScriptAddress()
|
||||||
|
} else {
|
||||||
|
pubKey := l.PubKey.SerializeCompressed()
|
||||||
|
pkh, _ := btcutil.NewAddressPubKeyHash(pubKey, l.net)
|
||||||
|
encodedId = pkh.ScriptAddress()
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Sprintf("%v@%v", hex.EncodeToString(encodedId), l.NetAddr)
|
||||||
|
}
|
||||||
|
16
peer.go
16
peer.go
@ -142,14 +142,15 @@ type peer struct {
|
|||||||
|
|
||||||
// newPeer creates a new peer from an establish connection object, and a
|
// newPeer creates a new peer from an establish connection object, and a
|
||||||
// pointer to the main server.
|
// pointer to the main server.
|
||||||
func newPeer(conn net.Conn, server *server, net wire.BitcoinNet, inbound bool) (*peer, error) {
|
func newPeer(conn net.Conn, server *server, btcNet wire.BitcoinNet, inbound bool) (*peer, error) {
|
||||||
nodePub := conn.(*lndc.LNDConn).RemotePub
|
lndcConn := conn.(*lndc.LNDConn)
|
||||||
|
nodePub := lndcConn.RemotePub
|
||||||
|
|
||||||
p := &peer{
|
p := &peer{
|
||||||
conn: conn,
|
conn: conn,
|
||||||
lightningID: wire.ShaHash(fastsha256.Sum256(nodePub.SerializeCompressed())),
|
lightningID: wire.ShaHash(fastsha256.Sum256(nodePub.SerializeCompressed())),
|
||||||
id: atomic.AddInt32(&numNodes, 1),
|
id: atomic.AddInt32(&numNodes, 1),
|
||||||
chainNet: net,
|
chainNet: btcNet,
|
||||||
inbound: inbound,
|
inbound: inbound,
|
||||||
|
|
||||||
server: server,
|
server: server,
|
||||||
@ -174,6 +175,15 @@ func newPeer(conn net.Conn, server *server, net wire.BitcoinNet, inbound bool) (
|
|||||||
quit: make(chan struct{}),
|
quit: make(chan struct{}),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO(roasbeef): re-write after lnaddr revamp, shouldn't need to use
|
||||||
|
// type assertions
|
||||||
|
var err error
|
||||||
|
tcpAddr := lndcConn.Conn.(*net.TCPConn).RemoteAddr().(*net.TCPAddr)
|
||||||
|
p.lightningAddr, err = lndc.NewLnAdr(tcpAddr, nodePub, activeNetParams.Params)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
// Initiate the pending channel identifier properly depending on if this
|
// Initiate the pending channel identifier properly depending on if this
|
||||||
// node is inbound or outbound. This value will be used in an increasing
|
// node is inbound or outbound. This value will be used in an increasing
|
||||||
// manner to track pending channels.
|
// manner to track pending channels.
|
||||||
|
@ -172,7 +172,7 @@ func (r *rpcServer) ConnectPeer(ctx context.Context,
|
|||||||
idAtHost := fmt.Sprintf("%v@%v", in.Addr.PubKeyHash, in.Addr.Host)
|
idAtHost := fmt.Sprintf("%v@%v", in.Addr.PubKeyHash, in.Addr.Host)
|
||||||
rpcsLog.Debugf("[connectpeer] peer=%v", idAtHost)
|
rpcsLog.Debugf("[connectpeer] peer=%v", idAtHost)
|
||||||
|
|
||||||
peerAddr, err := lndc.LnAddrFromString(idAtHost)
|
peerAddr, err := lndc.LnAddrFromString(idAtHost, activeNetParams.Params)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
rpcsLog.Errorf("(connectpeer): error parsing ln addr: %v", err)
|
rpcsLog.Errorf("(connectpeer): error parsing ln addr: %v", err)
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -283,13 +283,13 @@ func (s *server) handleConnectPeer(msg *connectPeerMsg) {
|
|||||||
// Ensure we're not already connected to this
|
// Ensure we're not already connected to this
|
||||||
// peer.
|
// peer.
|
||||||
for _, peer := range s.peers {
|
for _, peer := range s.peers {
|
||||||
if peer.lightningAddr.String() ==
|
if peer.lightningAddr.String() == addr.String() {
|
||||||
addr.String() {
|
|
||||||
msg.err <- fmt.Errorf(
|
msg.err <- fmt.Errorf(
|
||||||
"already connected to peer: %v",
|
"already connected to peer: %v",
|
||||||
peer.lightningAddr,
|
peer.lightningAddr,
|
||||||
)
|
)
|
||||||
msg.resp <- -1
|
msg.resp <- -1
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user