mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-09-05 17:05:50 +02:00
lnwire: add ChannelAnnouncement2 message
And ensure that it implements the ChannelAnnouncement interface.
This commit is contained in:
@@ -19,6 +19,7 @@ import (
|
||||
"github.com/btcsuite/btcd/btcec/v2"
|
||||
"github.com/btcsuite/btcd/btcec/v2/ecdsa"
|
||||
"github.com/btcsuite/btcd/btcutil"
|
||||
"github.com/btcsuite/btcd/chaincfg"
|
||||
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
||||
"github.com/btcsuite/btcd/wire"
|
||||
"github.com/lightningnetwork/lnd/fn"
|
||||
@@ -30,17 +31,25 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
shaHash1Bytes, _ = hex.DecodeString("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")
|
||||
shaHash1, _ = chainhash.NewHash(shaHash1Bytes)
|
||||
outpoint1 = wire.NewOutPoint(shaHash1, 0)
|
||||
shaHash1Bytes, _ = hex.DecodeString("e3b0c44298fc1c149afbf4c8996fb" +
|
||||
"92427ae41e4649b934ca495991b7852b855")
|
||||
|
||||
testRBytes, _ = hex.DecodeString("8ce2bc69281ce27da07e6683571319d18e949ddfa2965fb6caa1bf0314f882d7")
|
||||
testSBytes, _ = hex.DecodeString("299105481d63e0f4bc2a88121167221b6700d72a0ead154c03be696a292d24ae")
|
||||
testRScalar = new(btcec.ModNScalar)
|
||||
testSScalar = new(btcec.ModNScalar)
|
||||
_ = testRScalar.SetByteSlice(testRBytes)
|
||||
_ = testSScalar.SetByteSlice(testSBytes)
|
||||
testSig = ecdsa.NewSignature(testRScalar, testSScalar)
|
||||
shaHash1, _ = chainhash.NewHash(shaHash1Bytes)
|
||||
outpoint1 = wire.NewOutPoint(shaHash1, 0)
|
||||
|
||||
testRBytes, _ = hex.DecodeString("8ce2bc69281ce27da07e6683571" +
|
||||
"319d18e949ddfa2965fb6caa1bf0314f882d7")
|
||||
testSBytes, _ = hex.DecodeString("299105481d63e0f4bc2a" +
|
||||
"88121167221b6700d72a0ead154c03be696a292d24ae")
|
||||
testRScalar = new(btcec.ModNScalar)
|
||||
testSScalar = new(btcec.ModNScalar)
|
||||
_ = testRScalar.SetByteSlice(testRBytes)
|
||||
_ = testSScalar.SetByteSlice(testSBytes)
|
||||
testSig = ecdsa.NewSignature(testRScalar, testSScalar)
|
||||
testSchnorrSigStr, _ = hex.DecodeString("04E7F9037658A92AFEB4F2" +
|
||||
"5BAE5339E3DDCA81A353493827D26F16D92308E49E2A25E9220867" +
|
||||
"8A2DF86970DA91B03A8AF8815A8A60498B358DAF560B347AA557")
|
||||
testSchnorrSig, _ = NewSigFromSchnorrRawSignature(testSchnorrSigStr)
|
||||
)
|
||||
|
||||
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
@@ -156,17 +165,15 @@ func generateRandomBytes(n int) ([]byte, error) {
|
||||
return b, nil
|
||||
}
|
||||
|
||||
func randRawKey() ([33]byte, error) {
|
||||
func randRawKey(t *testing.T) [33]byte {
|
||||
var n [33]byte
|
||||
|
||||
priv, err := btcec.NewPrivateKey()
|
||||
if err != nil {
|
||||
return n, err
|
||||
}
|
||||
require.NoError(t, err)
|
||||
|
||||
copy(n[:], priv.PubKey().SerializeCompressed())
|
||||
|
||||
return n, nil
|
||||
return n
|
||||
}
|
||||
|
||||
func randDeliveryAddress(r *rand.Rand) (DeliveryAddress, error) {
|
||||
@@ -993,7 +1000,13 @@ func TestLightningWireProtocol(t *testing.T) {
|
||||
MsgChannelAnnouncement: func(v []reflect.Value, r *rand.Rand) {
|
||||
var err error
|
||||
req := ChannelAnnouncement1{
|
||||
ShortChannelID: NewShortChanIDFromInt(uint64(r.Int63())),
|
||||
ShortChannelID: NewShortChanIDFromInt(
|
||||
uint64(r.Int63()),
|
||||
),
|
||||
NodeID1: randRawKey(t),
|
||||
NodeID2: randRawKey(t),
|
||||
BitcoinKey1: randRawKey(t),
|
||||
BitcoinKey2: randRawKey(t),
|
||||
Features: randRawFeatureVector(r),
|
||||
ExtraOpaqueData: make([]byte, 0),
|
||||
}
|
||||
@@ -1018,26 +1031,6 @@ func TestLightningWireProtocol(t *testing.T) {
|
||||
return
|
||||
}
|
||||
|
||||
req.NodeID1, err = randRawKey()
|
||||
if err != nil {
|
||||
t.Fatalf("unable to generate key: %v", err)
|
||||
return
|
||||
}
|
||||
req.NodeID2, err = randRawKey()
|
||||
if err != nil {
|
||||
t.Fatalf("unable to generate key: %v", err)
|
||||
return
|
||||
}
|
||||
req.BitcoinKey1, err = randRawKey()
|
||||
if err != nil {
|
||||
t.Fatalf("unable to generate key: %v", err)
|
||||
return
|
||||
}
|
||||
req.BitcoinKey2, err = randRawKey()
|
||||
if err != nil {
|
||||
t.Fatalf("unable to generate key: %v", err)
|
||||
return
|
||||
}
|
||||
if _, err := r.Read(req.ChainHash[:]); err != nil {
|
||||
t.Fatalf("unable to generate chain hash: %v", err)
|
||||
return
|
||||
@@ -1059,6 +1052,7 @@ func TestLightningWireProtocol(t *testing.T) {
|
||||
MsgNodeAnnouncement: func(v []reflect.Value, r *rand.Rand) {
|
||||
var err error
|
||||
req := NodeAnnouncement{
|
||||
NodeID: randRawKey(t),
|
||||
Features: randRawFeatureVector(r),
|
||||
Timestamp: uint32(r.Int31()),
|
||||
Alias: randAlias(r),
|
||||
@@ -1075,12 +1069,6 @@ func TestLightningWireProtocol(t *testing.T) {
|
||||
return
|
||||
}
|
||||
|
||||
req.NodeID, err = randRawKey()
|
||||
if err != nil {
|
||||
t.Fatalf("unable to generate key: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
req.Addresses, err = randAddrs(r)
|
||||
if err != nil {
|
||||
t.Fatalf("unable to generate addresses: %v", err)
|
||||
@@ -1114,7 +1102,9 @@ func TestLightningWireProtocol(t *testing.T) {
|
||||
}
|
||||
|
||||
req := ChannelUpdate1{
|
||||
ShortChannelID: NewShortChanIDFromInt(uint64(r.Int63())),
|
||||
ShortChannelID: NewShortChanIDFromInt(
|
||||
uint64(r.Int63()),
|
||||
),
|
||||
Timestamp: uint32(r.Int31()),
|
||||
MessageFlags: msgFlags,
|
||||
ChannelFlags: ChanUpdateChanFlags(r.Int31()),
|
||||
@@ -1536,6 +1526,70 @@ func TestLightningWireProtocol(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
v[0] = reflect.ValueOf(req)
|
||||
},
|
||||
MsgChannelAnnouncement2: func(v []reflect.Value, r *rand.Rand) {
|
||||
req := ChannelAnnouncement2{
|
||||
Signature: testSchnorrSig,
|
||||
ExtraOpaqueData: make([]byte, 0),
|
||||
}
|
||||
|
||||
req.ShortChannelID.Val = NewShortChanIDFromInt(
|
||||
uint64(r.Int63()),
|
||||
)
|
||||
req.Capacity.Val = rand.Uint64()
|
||||
|
||||
req.Features.Val = *randRawFeatureVector(r)
|
||||
|
||||
req.NodeID1.Val = randRawKey(t)
|
||||
req.NodeID2.Val = randRawKey(t)
|
||||
|
||||
// Sometimes set chain hash to bitcoin mainnet genesis
|
||||
// hash.
|
||||
req.ChainHash.Val = *chaincfg.MainNetParams.GenesisHash
|
||||
if r.Int31()%2 == 0 {
|
||||
_, err := r.Read(req.ChainHash.Val[:])
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
// Sometimes set the bitcoin keys.
|
||||
if r.Int31()%2 == 0 {
|
||||
btcKey1 := tlv.ZeroRecordT[
|
||||
tlv.TlvType12, [33]byte,
|
||||
]()
|
||||
btcKey1.Val = randRawKey(t)
|
||||
req.BitcoinKey1 = tlv.SomeRecordT(btcKey1)
|
||||
|
||||
btcKey2 := tlv.ZeroRecordT[
|
||||
tlv.TlvType14, [33]byte,
|
||||
]()
|
||||
btcKey2.Val = randRawKey(t)
|
||||
req.BitcoinKey2 = tlv.SomeRecordT(btcKey2)
|
||||
|
||||
// Occasionally also set the merkle root hash.
|
||||
if r.Int31()%2 == 0 {
|
||||
hash := tlv.ZeroRecordT[
|
||||
tlv.TlvType16, [32]byte,
|
||||
]()
|
||||
|
||||
_, err := r.Read(hash.Val[:])
|
||||
require.NoError(t, err)
|
||||
|
||||
req.MerkleRootHash = tlv.SomeRecordT(
|
||||
hash,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
numExtraBytes := r.Int31n(1000)
|
||||
if numExtraBytes > 0 {
|
||||
req.ExtraOpaqueData = make(
|
||||
[]byte, numExtraBytes,
|
||||
)
|
||||
_, err := r.Read(req.ExtraOpaqueData[:])
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
v[0] = reflect.ValueOf(req)
|
||||
},
|
||||
}
|
||||
@@ -1772,6 +1826,12 @@ func TestLightningWireProtocol(t *testing.T) {
|
||||
return mainScenario(&m)
|
||||
},
|
||||
},
|
||||
{
|
||||
msgType: MsgChannelAnnouncement2,
|
||||
scenario: func(m ChannelAnnouncement2) bool {
|
||||
return mainScenario(&m)
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, test := range tests {
|
||||
var config *quick.Config
|
||||
|
Reference in New Issue
Block a user