|
| 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 | + msgBlindingPoint := randomPub.SerializeCompressed() |
| 52 | + msgOnion := []byte{1, 2, 3} |
| 53 | + |
| 54 | + // Send it from Bob to Alice. |
| 55 | + bobMsg := &lnrpc.SendOnionMessageRequest{ |
| 56 | + Peer: alice.PubKey[:], |
| 57 | + BlindingPoint: msgBlindingPoint, |
| 58 | + Onion: msgOnion, |
| 59 | + } |
| 60 | + bob.RPC.SendOnionMessage(bobMsg) |
| 61 | + |
| 62 | + // Wait for Alice to receive the message. |
| 63 | + select { |
| 64 | + case msg := <-messages: |
| 65 | + // Check our type and data and (sanity) check the peer we got |
| 66 | + // it from. |
| 67 | + require.Equal(ht, msgOnion, msg.Onion, "msg data wrong") |
| 68 | + require.Equal(ht, msgBlindingPoint, msg.BlindingPoint, "msg "+ |
| 69 | + "blinding point wrong") |
| 70 | + require.Equal(ht, bob.PubKey[:], msg.Peer, "msg peer wrong") |
| 71 | + |
| 72 | + case <-time.After(lntest.DefaultTimeout): |
| 73 | + ht.Fatalf("alice did not receive onion message: %v", bobMsg) |
| 74 | + } |
| 75 | +} |
0 commit comments