-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Basic structures for onion messages into LND #9868
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 0-21-0-staging
Are you sure you want to change the base?
Changes from all commits
5c5d64c
a3aa7ed
015810f
3fd0ac8
72ecd16
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package itest | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/btcsuite/btcd/btcec/v2" | ||
"github.com/lightningnetwork/lnd/lnrpc" | ||
"github.com/lightningnetwork/lnd/lntest" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// testOnionMessage tests sending and receiving of the onion message type. | ||
func testOnionMessage(ht *lntest.HarnessTest) { | ||
alice := ht.NewNode("Alice", nil) | ||
bob := ht.NewNode("Bob", nil) | ||
|
||
// Subscribe Alice to onion messages before we send any, so that we | ||
// don't miss any. | ||
msgClient, cancel := alice.RPC.SubscribeOnionMessages() | ||
defer cancel() | ||
|
||
// Create a channel to receive onion messages on. | ||
messages := make(chan *lnrpc.OnionMessage) | ||
go func() { | ||
for { | ||
// If we fail to receive, just exit. The test should | ||
// fail elsewhere if it doesn't get a message that it | ||
// was expecting. | ||
msg, err := msgClient.Recv() | ||
if err != nil { | ||
return | ||
} | ||
|
||
// Deliver the message into our channel or exit if the | ||
// test is shutting down. | ||
select { | ||
case messages <- msg: | ||
case <-ht.Context().Done(): | ||
return | ||
} | ||
} | ||
}() | ||
|
||
// Connect alice and bob so that they can exchange messages. | ||
ht.EnsureConnected(alice, bob) | ||
|
||
// Create a random onion message. | ||
randomPriv, err := btcec.NewPrivateKey() | ||
require.NoError(ht.T, err) | ||
randomPub := randomPriv.PubKey() | ||
msgPathKey := randomPub.SerializeCompressed() | ||
// Create a random payload. The content doesn't matter for this and | ||
// doesn't need to be encrypted. It's also of arbitrary length, so it | ||
// doesn't follow the BOLT 4 spec for onion message payload length of | ||
// either 1300 or 32768 bytes. Here we just use a few bytes to keep it | ||
// simple. | ||
msgOnion := []byte{1, 2, 3} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shouldn't this fail because of the following (I know it is not a spec. MUST but why don't we follow it?):
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, we will follow it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Micro-nit: add a comment that this is only for testing purposes and the onionMsg is supposed to be either 1300 or 32... big |
||
|
||
// Send it from Bob to Alice. | ||
bobMsg := &lnrpc.SendOnionMessageRequest{ | ||
Peer: alice.PubKey[:], | ||
PathKey: msgPathKey, | ||
Onion: msgOnion, | ||
} | ||
bob.RPC.SendOnionMessage(bobMsg) | ||
|
||
// Wait for Alice to receive the message. | ||
select { | ||
case msg := <-messages: | ||
// Check our type and data and (sanity) check the peer we got | ||
// it from. | ||
require.Equal(ht, msgOnion, msg.Onion, "msg data wrong") | ||
require.Equal(ht, msgPathKey, msg.PathKey, "msg "+ | ||
"path key wrong") | ||
require.Equal(ht, bob.PubKey[:], msg.Peer, "msg peer wrong") | ||
|
||
case <-time.After(lntest.DefaultTimeout): | ||
ht.Fatalf("alice did not receive onion message: %v", bobMsg) | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.