|
| 1 | +package itest |
| 2 | + |
| 3 | +import ( |
| 4 | + "time" |
| 5 | + |
| 6 | + "github.com/btcsuite/btcd/btcec/v2" |
| 7 | + "github.com/lightningnetwork/lnd/lnrpc" |
| 8 | + "github.com/lightningnetwork/lnd/lntest" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | +) |
| 11 | + |
| 12 | +// testOnionMessage tests sending and receiving of the onion message type. |
| 13 | +func testOnionMessage(ht *lntest.HarnessTest) { |
| 14 | + alice := ht.NewNode("Alice", nil) |
| 15 | + bob := ht.NewNode("Bob", nil) |
| 16 | + |
| 17 | + // Subscribe Alice to onion messages before we send any, so that we |
| 18 | + // don't miss any. |
| 19 | + msgClient, cancel := alice.RPC.SubscribeOnionMessages() |
| 20 | + defer cancel() |
| 21 | + |
| 22 | + // Create a channel to receive onion messages on. |
| 23 | + messages := make(chan *lnrpc.OnionMessage) |
| 24 | + go func() { |
| 25 | + for { |
| 26 | + // If we fail to receive, just exit. The test should |
| 27 | + // fail elsewhere if it doesn't get a message that it |
| 28 | + // was expecting. |
| 29 | + msg, err := msgClient.Recv() |
| 30 | + if err != nil { |
| 31 | + return |
| 32 | + } |
| 33 | + |
| 34 | + // Deliver the message into our channel or exit if the |
| 35 | + // test is shutting down. |
| 36 | + select { |
| 37 | + case messages <- msg: |
| 38 | + case <-ht.Context().Done(): |
| 39 | + return |
| 40 | + } |
| 41 | + } |
| 42 | + }() |
| 43 | + |
| 44 | + // Connect alice and bob so that they can exchange messages. |
| 45 | + ht.EnsureConnected(alice, bob) |
| 46 | + |
| 47 | + // Create a random onion message. |
| 48 | + randomPriv, err := btcec.NewPrivateKey() |
| 49 | + require.NoError(ht.T, err) |
| 50 | + randomPub := randomPriv.PubKey() |
| 51 | + msgPathKey := randomPub.SerializeCompressed() |
| 52 | + // Create a random payload. The content doesn't matter for this and |
| 53 | + // doesn't need to be encrypted. It's also of arbitrary length, so it |
| 54 | + // doesn't follow the BOLT 4 spec for onion message payload length of |
| 55 | + // either 1300 or 32768 bytes. Here we just use a few bytes to keep it |
| 56 | + // simple. |
| 57 | + msgOnion := []byte{1, 2, 3} |
| 58 | + |
| 59 | + // Send it from Bob to Alice. |
| 60 | + bobMsg := &lnrpc.SendOnionMessageRequest{ |
| 61 | + Peer: alice.PubKey[:], |
| 62 | + PathKey: msgPathKey, |
| 63 | + Onion: msgOnion, |
| 64 | + } |
| 65 | + bob.RPC.SendOnionMessage(bobMsg) |
| 66 | + |
| 67 | + // Wait for Alice to receive the message. |
| 68 | + select { |
| 69 | + case msg := <-messages: |
| 70 | + // Check our type and data and (sanity) check the peer we got |
| 71 | + // it from. |
| 72 | + require.Equal(ht, msgOnion, msg.Onion, "msg data wrong") |
| 73 | + require.Equal(ht, msgPathKey, msg.PathKey, "msg "+ |
| 74 | + "path key wrong") |
| 75 | + require.Equal(ht, bob.PubKey[:], msg.Peer, "msg peer wrong") |
| 76 | + |
| 77 | + case <-time.After(lntest.DefaultTimeout): |
| 78 | + ht.Fatalf("alice did not receive onion message: %v", bobMsg) |
| 79 | + } |
| 80 | +} |
0 commit comments