Skip to content

Commit

Permalink
lnwire: TestLightningWireProtocol quick check tests for taproot fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Roasbeef committed Mar 15, 2023
1 parent 5210891 commit c9b962e
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 11 deletions.
10 changes: 10 additions & 0 deletions docs/release-notes/release-notes-0.17.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Release Notes

## BOLT Specs

* The `lnwire` library is now able to [parse messages for the new experimental
taproot channels spec proposal](https://github.com/lightningnetwork/lnd/pull/7331).

# Contributors (Alphabetical Order)

* Olaoluwa Osuntokun
1 change: 1 addition & 0 deletions lnwire/fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ func FuzzNodeAnnouncement(f *testing.F) {
first.Signature.RawBytes(),
second.Signature.RawBytes(),
) {

shouldPanic = true
}

Expand Down
125 changes: 124 additions & 1 deletion lnwire/lnwire_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"bytes"
"encoding/binary"
"encoding/hex"
"fmt"
"image/color"
"io"
"math"
"math/rand"
"net"
Expand Down Expand Up @@ -39,6 +41,42 @@ var (

const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

func randLocalNonce(r *rand.Rand) *Musig2Nonce {
var nonce Musig2Nonce
_, _ = io.ReadFull(r, nonce[:])

return &nonce
}

func randPartialSig(r *rand.Rand) (*PartialSig, error) {
var sigBytes [32]byte
if _, err := r.Read(sigBytes[:]); err != nil {
return nil, fmt.Errorf("unable to generate sig: %w", err)
}

var s btcec.ModNScalar
s.SetByteSlice(sigBytes[:])

return &PartialSig{
Sig: s,
}, nil
}

func randPartialSigWithNonce(r *rand.Rand) (*PartialSigWithNonce, error) {
var sigBytes [32]byte
if _, err := r.Read(sigBytes[:]); err != nil {
return nil, fmt.Errorf("unable to generate sig: %w", err)
}

var s btcec.ModNScalar
s.SetByteSlice(sigBytes[:])

return &PartialSigWithNonce{
PartialSig: NewPartialSig(s),
Nonce: *randLocalNonce(r),
}, nil
}

func randAlias(r *rand.Rand) NodeAlias {
var a NodeAlias
for i := range a {
Expand Down Expand Up @@ -438,6 +476,8 @@ func TestLightningWireProtocol(t *testing.T) {

req.LeaseExpiry = new(LeaseExpiry)
*req.LeaseExpiry = LeaseExpiry(1337)

req.LocalNonce = randLocalNonce(r)
} else {
req.UpfrontShutdownScript = []byte{}
}
Expand Down Expand Up @@ -510,6 +550,8 @@ func TestLightningWireProtocol(t *testing.T) {

req.LeaseExpiry = new(LeaseExpiry)
*req.LeaseExpiry = LeaseExpiry(1337)

req.LocalNonce = randLocalNonce(r)
} else {
req.UpfrontShutdownScript = []byte{}
}
Expand Down Expand Up @@ -544,6 +586,16 @@ func TestLightningWireProtocol(t *testing.T) {
return
}

// 1/2 chance to attach a partial sig.
if r.Intn(2) == 0 {
req.PartialSig, err = randPartialSigWithNonce(r)
if err != nil {
t.Fatalf("unable to generate sig: %v",
err)
return
}
}

v[0] = reflect.ValueOf(req)
},
MsgFundingSigned: func(v []reflect.Value, r *rand.Rand) {
Expand All @@ -564,6 +616,16 @@ func TestLightningWireProtocol(t *testing.T) {
return
}

// 1/2 chance to attach a partial sig.
if r.Intn(2) == 0 {
req.PartialSig, err = randPartialSigWithNonce(r)
if err != nil {
t.Fatalf("unable to generate sig: %v",
err)
return
}
}

v[0] = reflect.ValueOf(req)
},
MsgFundingLocked: func(v []reflect.Value, r *rand.Rand) {
Expand All @@ -582,8 +644,43 @@ func TestLightningWireProtocol(t *testing.T) {

req := NewFundingLocked(ChannelID(c), pubKey)

if r.Int31()%2 == 0 {
scid := NewShortChanIDFromInt(uint64(r.Int63()))
req.AliasScid = &scid
req.NextLocalNonce = randLocalNonce(r)
}

v[0] = reflect.ValueOf(*req)
},
MsgShutdown: func(v []reflect.Value, r *rand.Rand) {
var c [32]byte
_, err := r.Read(c[:])
if err != nil {
t.Fatalf("unable to generate chan id: %v", err)
return
}

shutdownAddr, err := randDeliveryAddress(r)
if err != nil {
t.Fatalf("unable to generate delivery "+
"address: %v", err)
return
}

req := Shutdown{
ChannelID: ChannelID(c),
Address: shutdownAddr,
ExtraData: make([]byte, 0),
}

if r.Int31()%2 == 0 {
req.ShutdownNonce = (*ShutdownNonce)(
randLocalNonce(r),
)
}

v[0] = reflect.ValueOf(req)
},
MsgClosingSigned: func(v []reflect.Value, r *rand.Rand) {
req := ClosingSigned{
FeeSatoshis: btcutil.Amount(r.Int63()),
Expand All @@ -601,6 +698,15 @@ func TestLightningWireProtocol(t *testing.T) {
return
}

if r.Int31()%2 == 0 {
req.PartialSig, err = randPartialSig(r)
if err != nil {
t.Fatalf("unable to generate sig: %v",
err)
return
}
}

v[0] = reflect.ValueOf(req)
},
MsgCommitSig: func(v []reflect.Value, r *rand.Rand) {
Expand All @@ -620,7 +726,7 @@ func TestLightningWireProtocol(t *testing.T) {
// Only create the slice if there will be any signatures
// in it to prevent false positive test failures due to
// an empty slice versus a nil slice.
numSigs := uint16(r.Int31n(1020))
numSigs := uint16(r.Int31n(1019))
if numSigs > 0 {
req.HtlcSigs = make([]Sig, numSigs)
}
Expand All @@ -632,6 +738,16 @@ func TestLightningWireProtocol(t *testing.T) {
}
}

// 50/50 chance to attach a partial sig.
if r.Int31()%2 == 0 {
req.PartialSig, err = randPartialSigWithNonce(r)
if err != nil {
t.Fatalf("unable to generate sig: %v",
err)
return
}
}

v[0] = reflect.ValueOf(*req)
},
MsgRevokeAndAck: func(v []reflect.Value, r *rand.Rand) {
Expand All @@ -651,6 +767,11 @@ func TestLightningWireProtocol(t *testing.T) {
return
}

// 50/50 chance to attach a local nonce.
if r.Int31()%2 == 0 {
req.LocalNonce = randLocalNonce(r)
}

v[0] = reflect.ValueOf(*req)
},
MsgChannelAnnouncement: func(v []reflect.Value, r *rand.Rand) {
Expand Down Expand Up @@ -871,6 +992,8 @@ func TestLightningWireProtocol(t *testing.T) {
t.Fatalf("unable to generate key: %v", err)
return
}

req.LocalNonce = randLocalNonce(r)
}

v[0] = reflect.ValueOf(req)
Expand Down
2 changes: 1 addition & 1 deletion lnwire/signature.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ func (s *Sig) ToSignatureBytes() []byte {
// For schnorr signatures, we can use the same internal 64 bytes.
case sigTypeSchnorr:
// We'll make a copy of the signature so we don't return a
// refrence into the raw slice.
// reference into the raw slice.
var sig [64]byte
copy(sig[:], s.bytes[:])
return sig[:]
Expand Down
23 changes: 14 additions & 9 deletions lnwire/signature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ func TestSignatureSerializeDeserialize(t *testing.T) {
return err
}

e2 := e2Input.(*ecdsa.Signature)
e2, ok := e2Input.(*ecdsa.Signature)
require.True(t, ok)

if !e.IsEqual(e2) {
return fmt.Errorf("pre/post-serialize sigs don't " +
Expand Down Expand Up @@ -192,15 +193,19 @@ func TestNewSigFromRawSignature(t *testing.T) {
expectedSig: Sig{
bytes: [64]byte{
// r value
0x4e, 0x45, 0xe1, 0x69, 0x32, 0xb8, 0xaf, 0x51,
0x49, 0x61, 0xa1, 0xd3, 0xa1, 0xa2, 0x5f, 0xdf,
0x3f, 0x4f, 0x77, 0x32, 0xe9, 0xd6, 0x24, 0xc6,
0xc6, 0x15, 0x48, 0xab, 0x5f, 0xb8, 0xcd, 0x41,
0x4e, 0x45, 0xe1, 0x69, 0x32, 0xb8,
0xaf, 0x51, 0x49, 0x61, 0xa1, 0xd3,
0xa1, 0xa2, 0x5f, 0xdf, 0x3f, 0x4f,
0x77, 0x32, 0xe9, 0xd6, 0x24, 0xc6,
0xc6, 0x15, 0x48, 0xab, 0x5f, 0xb8,
0xcd, 0x41,
// s value
0x18, 0x15, 0x22, 0xec, 0x8e, 0xca, 0x07, 0xde,
0x48, 0x60, 0xa4, 0xac, 0xdd, 0x12, 0x90, 0x9d,
0x83, 0x1c, 0xc5, 0x6c, 0xbb, 0xac, 0x46, 0x22,
0x08, 0x22, 0x21, 0xa8, 0x76, 0x8d, 0x1d, 0x09,
0x18, 0x15, 0x22, 0xec, 0x8e, 0xca,
0x07, 0xde, 0x48, 0x60, 0xa4, 0xac,
0xdd, 0x12, 0x90, 0x9d, 0x83, 0x1c,
0xc5, 0x6c, 0xbb, 0xac, 0x46, 0x22,
0x08, 0x22, 0x21, 0xa8, 0x76, 0x8d,
0x1d, 0x09,
},
},
},
Expand Down

0 comments on commit c9b962e

Please sign in to comment.