lnwire: add custom records field to type UpdateAddHtlc
- Introduce the field `CustomRecords` to the type `UpdateAddHtlc`. - Encode and decode the new field into the `ExtraData` field of the `update_add_htlc` wire message.
This commit is contained in:
1 parent
af50694643
commit
81f6a8060f
4 files changed
+272
-19
No files matched your search
@@ -2,6 +2,7 @@ package lnwire
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
crand "crypto/rand"
|
||||
"encoding/binary"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
@@ -134,6 +135,27 @@ func randPubKey() (*btcec.PublicKey, error) {
|
||||
return priv.PubKey(), nil
|
||||
}
|
||||
|
||||
// pubkeyFromHex parses a Bitcoin public key from a hex encoded string.
|
||||
func pubkeyFromHex(keyHex string) (*btcec.PublicKey, error) {
|
||||
pubKeyBytes, err := hex.DecodeString(keyHex)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return btcec.ParsePubKey(pubKeyBytes)
|
||||
}
|
||||
|
||||
// generateRandomBytes returns a slice of n random bytes.
|
||||
func generateRandomBytes(n int) ([]byte, error) {
|
||||
b := make([]byte, n)
|
||||
_, err := crand.Read(b)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return b, nil
|
||||
}
|
||||
|
||||
func randRawKey() ([33]byte, error) {
|
||||
var n [33]byte
|
||||
|
||||
@@ -389,6 +411,37 @@ func TestEmptyMessageUnknownType(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// randCustomRecords generates a random set of custom records for testing.
|
||||
func randCustomRecords(t *testing.T, r *rand.Rand) CustomRecords {
|
||||
var (
|
||||
customRecords = CustomRecords{}
|
||||
|
||||
// We'll generate a random number of records, between 1 and 10.
|
||||
numRecords = r.Intn(9) + 1
|
||||
)
|
||||
|
||||
// For each record, we'll generate a random key and value.
|
||||
for i := 0; i < numRecords; i++ {
|
||||
// Keys must be equal to or greater than
|
||||
// MinCustomRecordsTlvType.
|
||||
keyOffset := uint64(r.Intn(100))
|
||||
key := MinCustomRecordsTlvType + keyOffset
|
||||
|
||||
// Values are byte slices of any length.
|
||||
value := make([]byte, r.Intn(100))
|
||||
_, err := r.Read(value)
|
||||
require.NoError(t, err)
|
||||
|
||||
customRecords[key] = value
|
||||
}
|
||||
|
||||
// Validate the custom records as a sanity check.
|
||||
err := customRecords.Validate()
|
||||
require.NoError(t, err)
|
||||
|
||||
return customRecords
|
||||
}
|
||||
|
||||
// TestLightningWireProtocol uses the testing/quick package to create a series
|
||||
// of fuzz tests to attempt to break a primary scenario which is implemented as
|
||||
// property based testing scenario.
|
||||
@@ -1369,6 +1422,8 @@ func TestLightningWireProtocol(t *testing.T) {
|
||||
_, err = r.Read(req.OnionBlob[:])
|
||||
require.NoError(t, err)
|
||||
|
||||
req.CustomRecords = randCustomRecords(t, r)
|
||||
|
||||
// Generate a blinding point 50% of the time, since not
|
||||
// all update adds will use route blinding.
|
||||
if r.Int31()%2 == 0 {
|
||||
|
||||
Reference in new issue
Block a user