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 Jan 25, 2023
1 parent e1474b8 commit 4874fc4
Showing 1 changed file with 120 additions and 1 deletion.
121 changes: 120 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: %v", 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: %v", 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,15 @@ 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 +615,15 @@ 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 +642,42 @@ func TestLightningWireProtocol(t *testing.T) {

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

if r.Int31()%2 == 0 {
scid := NewShortChanIDFromInt(uint64(r.Int63()))
req.AliasScid = &scid
req.LocalNonce = 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 +695,14 @@ 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 +722,8 @@ 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(1020))
numSigs := uint16(r.Int31n(1019))
if numSigs > 0 {
req.HtlcSigs = make([]Sig, numSigs)
}
Expand All @@ -632,6 +735,15 @@ 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 +763,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 +988,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

0 comments on commit 4874fc4

Please sign in to comment.