Repository: lightningnetwork/lnd (language: go)

Issue:
panic: runtime error: invalid memory address or nil pointer dereference
### Background

```
lnd                   | 2021-05-15 23:53:15.047 [ERR] RPCS: [/lnrpc.Lightning/GetNodeInfo]: rpc error: code = NotFound desc = unable to find node
lnd                   | panic: runtime error: invalid memory address or nil pointer dereference
lnd                   | [signal SIGSEGV: segmentation violation code=0x1 addr=0x41 pc=0x1195897]
lnd                   |
lnd                   | goroutine 1503 [running]:
lnd                   | github.com/lightningnetwork/lnd.marshalDbEdge(0xc002950000, 0x0, 0x0, 0xc0021afd00)
lnd                   |         /opt/lnd/src/github.com/lightningnetwork/lnd/rpcserver.go:5169 +0x37
lnd                   | github.com/lightningnetwork/lnd.(*rpcServer).DescribeGraph.func2(0xc002950000, 0x0, 0x0, 0xc0021afd40, 0x7f5edc29df10)
lnd                   |         /opt/lnd/src/github.com/lightningnetwork/lnd/rpcserver.go:5152 +0x56
lnd                   | github.com/lightningnetwork/lnd/channeldb.(*ChannelGraph).ForEachChannel.func1.1(0x7f5e8225c040, 0x8, 0x8, 0x7f5e8225c048, 0x1f9, 0x1f9, 0x1f9, 0x7f5ea43728a8)
lnd                   |         /opt/lnd/src/github.com/lightningnetwork/lnd/channeldb/graph.go:265 +0x30d
lnd                   | go.etcd.io/bbolt.(*Bucket).ForEach(0xc0021afd80, 0xc00219ae10, 0xc0026034f8, 0x40ff98)
lnd                   |         /opt/lnd/pkg/mod/go.etcd.io/bbolt@v1.3.5-0.20200615073812-232d8fc87f50/bucket.go:390 +0x103
lnd                   | github.com/btcsuite/btcwallet/walletdb/bdb.(*bucket).ForEach(0xc0021afd80, 0xc00219ae10, 0xa, 0xa)
lnd                   |         /opt/lnd/pkg/mod/github.com/btcsuite/btcwallet/walletdb@v1.3.5/bdb/db.go:190 +0x39
lnd                   | github.com/lightningnetwork/lnd/channeldb.(*ChannelGraph).ForEachChannel.func1(0x18672c8, 0xc0041884e0, 0xc0041884e0, 0x0)
lnd                   |         /opt/lnd/src/github.com/lightningnetwork/lnd/channeldb/graph.go:247 +0x1f4
lnd                   | github.com/btcsuite/btcwallet/walletdb.View(0x186f408, 0xc00089af00, 0xc0021b2768, 0x0, 0x0)
lnd                   |         /opt/lnd/pkg/mod/github.com/btcsuite/btcwallet/walletdb@v1.3.5/interface.go:239 +0xbb
lnd                   | github.com/lightningnetwork/lnd/channeldb.(*DB).View(0xc00089af00, 0xc0021b2768, 0x1700f58, 0x7f5edc29e048, 0xc00089af00)
lnd                   |         /opt/lnd/src/github.com/lightningnetwork/lnd/channeldb/db.go:244 +0xc5
lnd                   | github.com/lightningnetwork/lnd/kvdb.View(0x186f408, 0xc00089af00, 0xc0021b2768, 0x1700f58, 0x1, 0xc0021b2750)
lnd                   |         /opt/lnd/src/github.com/lightningnetwork/lnd/kvdb/interface.go:34 +0x74
lnd                   | github.com/lightningnetwork/lnd/channeldb.(*ChannelGraph).ForEachChannel(0xc0010b21e0, 0xc0021b2750, 0x0, 0x0)
lnd                   |         /opt/lnd/src/github.com/lightningnetwork/lnd/channeldb/graph.go:224 +0x85
lnd                   | github.com/lightningnetwork/lnd.(*rpcServer).DescribeGraph(0xc000574000, 0x186e918, 0xc002a02000, 0xc002a02030, 0xc000574000, 0xc00022e040, 0xc00022e040)
lnd                   |         /opt/lnd/src/github.com/lightningnetwork/lnd/rpcserver.go:5141 +0x10f
lnd                   | github.com/lightningnetwork/lnd/lnrpc._Lightning_DescribeGraph_Handler.func1(0x186e918, 0xc002a02000, 0x14144e0, 0xc002a02030, 0x1048d04, 0xc00022e040, 0xc002a024b0, 0x3)
lnd                   |         /opt/lnd/src/github.com/lightningnetwork/lnd/lnrpc/rpc.pb.go:22258 +0x89
lnd                   | github.com/grpc-ecosystem/go-grpc-middleware.ChainUnaryServer.func1.1(0x186e918, 0xc002a02000, 0x14144e0, 0xc002a02030, 0x1e, 0xc002258410, 0xc0003a5a80, 0xc0026038b0)
lnd                   |         /opt/lnd/pkg/mod/github.com/grpc-ecosystem/go-grpc-middleware@v1.0.1-0.20190118093823-f849b5445de4/chain.go:31 +0x114
lnd                   | github.com/grpc-ecosystem/go-grpc-prometheus.(*ServerMetrics).UnaryServerInterceptor.func1(0x186e918, 0xc002a02000, 0x14144e0, 0xc002a02030, 0xc000453da0, 0xc002258050, 0xc000e9c3e0, 0xc0006419b8, 0xc0003e9dd0, 0x13a52c0)
lnd                   |         /opt/lnd/pkg/mod/github.com/grpc-ecosystem/go-grpc-prometheus@v1.2.0/server_metrics.go:107 +0xb0
lnd                   | github.com/grpc-ecosystem/go-grpc-middleware.ChainUnaryServer.func1.1(0x186e918, 0xc002a02000, 0x14144e0, 0xc002a02030, 0x1e, 0x0, 0x0, 0x1)
lnd                   |         /opt/lnd/pkg/mod/github.com/grpc-ecosystem/go-grpc-middleware@v1.0.1-0.20190118093823-f849b5445de4/chain.go:34 +0x9c
lnd                   | github.com/lightningnetwork/lnd/rpcperms.(*InterceptorChain).MacaroonUnaryServerInterceptor.func1(0x186e918, 0xc002a02000, 0x14144e0, 0xc002a02030, 0xc000453da0, 0xc002258050, 0x0, 0x0, 0x0, 0x0)
lnd                   |         /opt/lnd/src/github.com/lightningnetwork/lnd/rpcperms/interceptor.go:485 +0xc2
lnd                   | github.com/grpc-ecosystem/go-grpc-middleware.ChainUnaryServer.func1.1(0x186e918, 0xc002a02000, 0x14144e0, 0xc002a02030, 0x0, 0x203000, 0x41aa73, 0x7f5edc494f48)
lnd                   |         /opt/lnd/pkg/mod/github.com/grpc-ecosystem/go-grpc-middleware@v1.0.1-0.20190118093823-f849b5445de4/chain.go:34 +0x9c
lnd                   | github.com/lightningnetwork/lnd/rpcperms.(*InterceptorChain).rpcStateUnaryServerInterceptor.func1(0x186e918, 0xc002a02000, 0x14144e0, 0xc002a02030, 0xc000453da0, 0xc002258050, 0xc002603a80, 0x40f83b, 0xc002258050, 0x50)
lnd                   |         /opt/lnd/src/github.com/lightningnetwork/lnd/rpcperms/interceptor.go:576 +0xad
lnd                   | github.com/grpc-ecosystem/go-grpc-middleware.ChainUnaryServer.func1.1(0x186e918, 0xc002a02000, 0x14144e0, 0xc002a02030, 0x7f5f05b213c8, 0x50, 0xc002258050, 0xe6d679)
lnd                   |         /opt/lnd/pkg/mod/github.com/grpc-ecosystem/go-grpc-middleware@v1.0.1-0.20190118093823-f849b5445de4/chain.go:34 +0x9c
lnd                   | github.com/lightningnetwork/lnd/rpcperms.errorLogUnaryServerInterceptor.func1(0x186e918, 0xc002a02000, 0x14144e0, 0xc002a02030, 0xc000453da0, 0xc002258050, 0xc000d00000, 0x0, 0xc002603b30, 0x40ff98)
lnd                   |         /opt/lnd/src/github.com/lightningnetwork/lnd/rpcperms/interceptor.go:401 +0x82
lnd                   | github.com/grpc-ecosystem/go-grpc-middleware.ChainUnaryServer.func1(0x186e918, 0xc002a02000, 0x14144e0, 0xc002a02030, 0xc000453da0, 0xc003afe048, 0xc001589ba0, 0x5b7c66, 0x148c2e0, 0xc002a02000)
lnd                   |         /opt/lnd/pkg/mod/github.com/grpc-ecosystem/go-grpc-middleware@v1.0.1-0.20190118093823-f849b5445de4/chain.go:39 +0x15b
lnd                   | github.com/lightningnetwork/lnd/lnrpc._Lightning_DescribeGraph_Handler(0x155fde0, 0xc000574000, 0x186e918, 0xc002a02000, 0xc002a00000, 0xc000818a20, 0x186e918, 0xc002a02000, 0x0, 0x0)
lnd                   |         /opt/lnd/src/github.com/lightningnetwork/lnd/lnrpc/rpc.pb.go:22260 +0x150
lnd                   | google.golang.org/grpc.(*Server).processUnaryRPC(0xc00063cb60, 0x187d8b8, 0xc00208a300, 0xc000df5600, 0xc000818cf0, 0x2104b98, 0x0, 0x0, 0x0)
lnd                   |         /opt/lnd/pkg/mod/google.golang.org/grpc@v1.29.1/server.go:1082 +0x52b
lnd                   | google.golang.org/grpc.(*Server).handleStream(0xc00063cb60, 0x187d8b8, 0xc00208a300, 0xc000df5600, 0x0)
lnd                   |         /opt/lnd/pkg/mod/google.golang.org/grpc@v1.29.1/server.go:1405 +0xccf
lnd                   | google.golang.org/grpc.(*Server).serveStreams.func1.1(0xc002b20530, 0xc00063cb60, 0x187d8b8, 0xc00208a300, 0xc000df5600)
lnd                   |         /opt/lnd/pkg/mod/google.golang.org/grpc@v1.29.1/server.go:746 +0xab
lnd                   | created by google.golang.org/grpc.(*Server).serveStreams.func1
lnd                   |         /opt/lnd/pkg/mod/google.golang.org/grpc@v1.29.1/server.go:744 +0xa5

```

### Your environment

* v0.13.0-beta.rc2
* debian buster
* btcd version 0.21.0-beta


### Steps to reproduce

always crash at startup

### Expected behaviour

since last update, same result, it worked with v0.12.1-beta

### Actual behaviour

Tell us what happens instead



Discussion / hints:
Also crashed out with a similar error.

Possibly related: https://github.com/lightningnetwork/lnd/pull/5295

```
2021-05-16 03:53:07.788 [INF] NTFN: New block epoch subscription
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x41 pc=0xb4bf00]

goroutine 804799 [running]:
github.com/lightningnetwork/lnd.marshalDbEdge(0x4002220160, 0x0, 0x0, 0x1d8)
        /home/ubuntu/lnd/rpcserver.go:5169 +0x20
github.com/lightningnetwork/lnd.(*rpcServer).DescribeGraph.func2(0x4002220160, 0x0, 0x0, 0x40011b4440, 0xffff79825518)
        /home/ubuntu/lnd/rpcserver.go:5152 +0x48
github.com/lightningnetwork/lnd/channeldb.(*ChannelGraph).ForEachChannel.func1.1(0xffff4d19f080, 0x8, 0x8, 0xffff4d19f088, 0x1f9, 0x1f9, 0x1f9, 0x48)
        /home/ubuntu/lnd/channeldb/graph.go:265 +0x228
go.etcd.io/bbolt.(*Bucket).ForEach(0x40011b4480, 0x400118c2d0, 0x4003beb578, 0x5d2cac)
        /home/ubuntu/gocode/pkg/mod/go.etcd.io/bbolt@v1.3.5-0.20200615073812-232d8fc87f50/bucket.go:390 +0xe0
github.com/btcsuite/btcwallet/walletdb/bdb.(*bucket).ForEach(0x40011b4480, 0x400118c2d0, 0xa, 0xa)
        /home/ubuntu/gocode/pkg/mod/github.com/btcsuite/btcwallet/walletdb@v1.3.5/bdb/db.go:190 +0x30
github.com/lightningnetwork/lnd/channeldb.(*ChannelGraph).ForEachChannel.func1(0x12062f8, 0x40037362c0, 0x40037362c0, 0x0)
        /home/ubuntu/lnd/channeldb/graph.go:247 +0x1f0
github.com/btcsuite/btcwallet/walletdb.View(0x120dfd0, 0x400160c1c0, 0x4003678cd8, 0x0, 0x0)
        /home/ubuntu/gocode/pkg/mod/github.com/btcsuite/btcwallet/walletdb@v1.3.5/interface.go:239 +0x98
github.com/lightningnetwork/lnd/channeldb.(*DB).View(0x400160c1c0, 0x4003678cd8, 0x10a3bf0, 0xffff786d2810, 0x400160c1c0)
        /home/ubuntu/lnd/channeldb/db.go:244 +0xb4
github.com/lightningnetwork/lnd/kvdb.View(0x120dfd0, 0x400160c1c0, 0x4003678cd8, 0x10a3bf0, 0x1, 0x4003678cc0)
        /home/ubuntu/lnd/kvdb/interface.go:34 +0x68
github.com/lightningnetwork/lnd/channeldb.(*ChannelGraph).ForEachChannel(0x4000e03040, 0x4003678cc0, 0x0, 0x0)
        /home/ubuntu/lnd/channeldb/graph.go:224 +0x84
github.com/lightningnetwork/lnd.(*rpcServer).DescribeGraph(0x4000727790, 0x120d4e0, 0x40009b3530, 0x40009b35c0, 0x4000727790, 0x40008c00e0, 0x1)
        /home/ubuntu/lnd/rpcserver.go:5141 +0xfc
github.com/lightningnetwork/lnd/lnrpc._Lightning_DescribeGraph_Handler.func1(0x120d4e0, 0x40009b3530, 0xdc2c40, 0x40009b35c0, 0x1, 0x1, 0x40008c00e0, 0x40008be0c8)
        /home/ubuntu/lnd/lnrpc/rpc.pb.go:22258 +0x7c
github.com/grpc-ecosystem/go-grpc-middleware.ChainUnaryServer.func1.1(0x120d4e0, 0x40009b3530, 0xdc2c40, 0x40009b35c0, 0x1e, 0x0, 0x0, 0x0)
        /home/ubuntu/gocode/pkg/mod/github.com/grpc-ecosystem/go-grpc-middleware@v1.0.1-0.20190118093823-f849b5445de4/chain.go:31 +0xf8
github.com/lightningnetwork/lnd/rpcperms.(*InterceptorChain).MacaroonUnaryServerInterceptor.func1(0x120d4e0, 0x40009b3530, 0xdc2c40, 0x40009b35c0, 0x40030d8720, 0x4003bba230, 0x0, 0x0, 0x4003beb958, 0x91c10)
        /home/ubuntu/lnd/rpcperms/interceptor.go:485 +0xa0
github.com/grpc-ecosystem/go-grpc-middleware.ChainUnaryServer.func1.1(0x120d4e0, 0x40009b3530, 0xdc2c40, 0x40009b35c0, 0x0, 0x400398a990, 0x4001e359f8, 0x86f9a8)
        /home/ubuntu/gocode/pkg/mod/github.com/grpc-ecosystem/go-grpc-middleware@v1.0.1-0.20190118093823-f849b5445de4/chain.go:34 +0x90
github.com/lightningnetwork/lnd/rpcperms.(*InterceptorChain).rpcStateUnaryServerInterceptor.func1(0x120d4e0, 0x40009b3530, 0xdc2c40, 0x40009b35c0, 0x40030d8720, 0x4003bba230, 0x48, 0xe63ac0, 0x400398a998, 0x0)
        /home/ubuntu/lnd/rpcperms/interceptor.go:576 +0x90
github.com/grpc-ecosystem/go-grpc-middleware.ChainUnaryServer.func1.1(0x120d4e0, 0x40009b3530, 0xdc2c40, 0x40009b35c0, 0x50, 0x4003bba230, 0x4000105400, 0x18)
        /home/ubuntu/gocode/pkg/mod/github.com/grpc-ecosystem/go-grpc-middleware@v1.0.1-0.20190118093823-f849b5445de4/chain.go:34 +0x90
github.com/lightningnetwork/lnd/rpcperms.errorLogUnaryServerInterceptor.func1(0x120d4e0, 0x40009b3530, 0xdc2c40, 0x40009b35c0, 0x40030d8720, 0x4003bba230, 0x0, 0x0, 0x4001e35b18, 0x968210)
        /home/ubuntu/lnd/rpcperms/interceptor.go:401 +0x58
github.com/grpc-ecosystem/go-grpc-middleware.ChainUnaryServer.func1(0x120d4e0, 0x40009b3530, 0xdc2c40, 0x40009b35c0, 0x40030d8720, 0x4003678228, 0xe380a0, 0x40009b3530, 0x58, 0xe71b80)
        /home/ubuntu/gocode/pkg/mod/github.com/grpc-ecosystem/go-grpc-middleware@v1.0.1-0.20190118093823-f849b5445de4/chain.go:39 +0x138
github.com/lightningnetwork/lnd/lnrpc._Lightning_DescribeGraph_Handler(0xf050e0, 0x4000727790, 0x120d4e0, 0x40009b3530, 0x40035e1d40, 0x400041eb70, 0x120d4e0, 0x40009b3530, 0x0, 0x0)
        /home/ubuntu/lnd/lnrpc/rpc.pb.go:22260 +0x12c
google.golang.org/grpc.(*Server).processUnaryRPC(0x400071bd40, 0x121b958, 0x4002370c00, 0x400408b400, 0x400041ee40, 0x1a74098, 0x0, 0x0, 0x0)
        /home/ubuntu/gocode/pkg/mod/google.golang.org/grpc@v1.29.1/server.go:1082 +0x3fc
google.golang.org/grpc.(*Server).handleStream(0x400071bd40, 0x121b958, 0x4002370c00, 0x400408b400, 0x0)
        /home/ubuntu/gocode/pkg/mod/google.golang.org/grpc@v1.29.1/server.go:1405 +0xa30
google.golang.org/grpc.(*Server).serveStreams.func1.1(0x4002d239e0, 0x400071bd40, 0x121b958, 0x4002370c00, 0x400408b400)
        /home/ubuntu/gocode/pkg/mod/google.golang.org/grpc@v1.29.1/server.go:746 +0x94
created by google.golang.org/grpc.(*Server).serveStreams.func1
        /home/ubuntu/gocode/pkg/mod/google.golang.org/grpc@v1.29.1/server.go:744 +0x8c
```
maybe i'm wrong but seems to be already merged :-(
Don't worry. I am sure @wpaulino is on the case. :)

Fix patch:
diff --git a/rpcserver.go b/rpcserver.go
index 8c7f1beed1..114ccb11a7 100644
--- a/rpcserver.go
+++ b/rpcserver.go
@@ -5166,13 +5166,8 @@ func marshalDbEdge(edgeInfo *channeldb.ChannelEdgeInfo,
 
 	// Make sure the policies match the node they belong to. c1 should point
 	// to the policy for NodeKey1, and c2 for NodeKey2.
-	if c1.ChannelFlags&lnwire.ChanUpdateDirection == 1 {
-		c2, c1 = c1, c2
-	}
-
-	// Order the edges by increasing pubkey.
-	if bytes.Compare(edgeInfo.NodeKey2Bytes[:],
-		edgeInfo.NodeKey1Bytes[:]) < 0 {
+	if c1 != nil && c1.ChannelFlags&lnwire.ChanUpdateDirection == 1 ||
+		c2 != nil && c2.ChannelFlags&lnwire.ChanUpdateDirection == 0 {
 
 		c2, c1 = c1, c2
 	}


Test patch: