multi: add restriction on maximum feature bit in invoices

Base 32 encoded bolt 11 invoices only allow 10 bits to express the
length of the feature vector in a tagged field, so there is a much
lower limit on the values invoice custom features can hold.

Other places in the protocol are theoretically limited by the maximum
message size, but since we express a feature bit as u16 we don't need
to be concerned about this.

The decision is made to track maximum per-set in the feature manager,
which is conceptually aware of sets and then validate in lnwire/features
against some arbitrary maximum value provided to the caller to keep
the base features package unaware of sets.
This commit is contained in:
Carla Kirk-Cohen committed 2023-05-04 10:35:45 -04:00
1 parent 5a139fd297
commit 019127c4f4
4 files changed
+88 -14

No files matched your search

+33 -1
View File
@@ -2,6 +2,7 @@ package lnwire
import (
"bytes"
"math"
"reflect"
"sort"
"testing"
@@ -423,6 +424,7 @@ func TestValidateUpdate(t *testing.T) {
name string
currentFeatures []FeatureBit
newFeatures []FeatureBit
maximumValue FeatureBit
err error
}{
{
@@ -479,6 +481,30 @@ func TestValidateUpdate(t *testing.T) {
},
err: nil,
},
{
name: "at allowed maximum",
currentFeatures: []FeatureBit{
StaticRemoteKeyOptional,
},
newFeatures: []FeatureBit{
StaticRemoteKeyOptional,
100,
},
maximumValue: 100,
err: nil,
},
{
name: "above allowed maximum",
currentFeatures: []FeatureBit{
StaticRemoteKeyOptional,
},
newFeatures: []FeatureBit{
StaticRemoteKeyOptional,
101,
},
maximumValue: 100,
err: ErrFeatureBitMaximum,
},
}
for _, testCase := range testCases {
@@ -491,7 +517,13 @@ func TestValidateUpdate(t *testing.T) {
)
newFV := NewRawFeatureVector(testCase.newFeatures...)
err := currentFV.ValidateUpdate(newFV)
// Set maximum value if not populated in the test case.
maximumValue := testCase.maximumValue
if testCase.maximumValue == 0 {
maximumValue = math.MaxUint16
}
err := currentFV.ValidateUpdate(newFV, maximumValue)
require.ErrorIs(t, err, testCase.err)
})
}