features_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. package lnwire
  2. import (
  3. "bytes"
  4. "math"
  5. "reflect"
  6. "sort"
  7. "testing"
  8. "github.com/stretchr/testify/require"
  9. )
  10. var testFeatureNames = map[FeatureBit]string{
  11. 0: "feature1",
  12. 3: "feature2",
  13. 4: "feature3",
  14. 5: "feature3",
  15. }
  16. func TestFeatureVectorSetUnset(t *testing.T) {
  17. t.Parallel()
  18. tests := []struct {
  19. bits []FeatureBit
  20. expectedFeatures []bool
  21. }{
  22. // No features are enabled if no bits are set.
  23. {
  24. bits: nil,
  25. expectedFeatures: []bool{false, false, false, false, false, false, false, false},
  26. },
  27. // Test setting an even bit for an even-only bit feature. The
  28. // corresponding odd bit should not be seen as set.
  29. {
  30. bits: []FeatureBit{0},
  31. expectedFeatures: []bool{true, false, false, false, false, false, false, false},
  32. },
  33. // Test setting an odd bit for an even-only bit feature. The
  34. // corresponding even bit should not be seen as set.
  35. {
  36. bits: []FeatureBit{1},
  37. expectedFeatures: []bool{false, true, false, false, false, false, false, false},
  38. },
  39. // Test setting an even bit for an odd-only bit feature. The bit should
  40. // be seen as set and the odd bit should not.
  41. {
  42. bits: []FeatureBit{2},
  43. expectedFeatures: []bool{false, false, true, false, false, false, false, false},
  44. },
  45. // Test setting an odd bit for an odd-only bit feature. The bit should
  46. // be seen as set and the even bit should not.
  47. {
  48. bits: []FeatureBit{3},
  49. expectedFeatures: []bool{false, false, false, true, false, false, false, false},
  50. },
  51. // Test setting an even bit for even-odd pair feature. Both bits in the
  52. // pair should be seen as set.
  53. {
  54. bits: []FeatureBit{4},
  55. expectedFeatures: []bool{false, false, false, false, true, true, false, false},
  56. },
  57. // Test setting an odd bit for even-odd pair feature. Both bits in the
  58. // pair should be seen as set.
  59. {
  60. bits: []FeatureBit{5},
  61. expectedFeatures: []bool{false, false, false, false, true, true, false, false},
  62. },
  63. // Test setting an even bit for an unknown feature. The bit should be
  64. // seen as set and the odd bit should not.
  65. {
  66. bits: []FeatureBit{6},
  67. expectedFeatures: []bool{false, false, false, false, false, false, true, false},
  68. },
  69. // Test setting an odd bit for an unknown feature. The bit should be
  70. // seen as set and the odd bit should not.
  71. {
  72. bits: []FeatureBit{7},
  73. expectedFeatures: []bool{false, false, false, false, false, false, false, true},
  74. },
  75. }
  76. fv := NewFeatureVector(nil, testFeatureNames)
  77. for i, test := range tests {
  78. for _, bit := range test.bits {
  79. fv.Set(bit)
  80. }
  81. for j, expectedSet := range test.expectedFeatures {
  82. if fv.HasFeature(FeatureBit(j)) != expectedSet {
  83. t.Errorf("Expectation failed in case %d, bit %d", i, j)
  84. break
  85. }
  86. }
  87. for _, bit := range test.bits {
  88. fv.Unset(bit)
  89. }
  90. }
  91. }
  92. // TestFeatureVectorRequiresFeature tests that if a feature vector only
  93. // includes a required feature bit (it's even), then the RequiresFeature method
  94. // will return true for both that bit as well as it's optional counter party.
  95. func TestFeatureVectorRequiresFeature(t *testing.T) {
  96. t.Parallel()
  97. // Create a new feature vector with the features above, and set only
  98. // the set of required bits. These will be all the even features
  99. // referenced above.
  100. fv := NewFeatureVector(nil, testFeatureNames)
  101. fv.Set(0)
  102. fv.Set(4)
  103. // Next we'll query for those exact bits, these should show up as being
  104. // required.
  105. require.True(t, fv.RequiresFeature(0))
  106. require.True(t, fv.RequiresFeature(4))
  107. // If we query for the odd (optional) counter party to each of the
  108. // features, the method should still return that the backing feature
  109. // vector requires the feature to be set.
  110. require.True(t, fv.RequiresFeature(1))
  111. require.True(t, fv.RequiresFeature(5))
  112. }
  113. func TestFeatureVectorEncodeDecode(t *testing.T) {
  114. t.Parallel()
  115. tests := []struct {
  116. bits []FeatureBit
  117. expectedEncoded []byte
  118. }{
  119. {
  120. bits: nil,
  121. expectedEncoded: []byte{0x00, 0x00},
  122. },
  123. {
  124. bits: []FeatureBit{2, 3, 7},
  125. expectedEncoded: []byte{0x00, 0x01, 0x8C},
  126. },
  127. {
  128. bits: []FeatureBit{2, 3, 8},
  129. expectedEncoded: []byte{0x00, 0x02, 0x01, 0x0C},
  130. },
  131. }
  132. for i, test := range tests {
  133. fv := NewRawFeatureVector(test.bits...)
  134. // Test that Encode produces the correct serialization.
  135. buffer := new(bytes.Buffer)
  136. err := fv.Encode(buffer)
  137. if err != nil {
  138. t.Errorf("Failed to encode feature vector in case %d: %v", i, err)
  139. continue
  140. }
  141. encoded := buffer.Bytes()
  142. if !bytes.Equal(encoded, test.expectedEncoded) {
  143. t.Errorf("Wrong encoding in case %d: got %v, expected %v",
  144. i, encoded, test.expectedEncoded)
  145. continue
  146. }
  147. // Test that decoding then re-encoding produces the same result.
  148. fv2 := NewRawFeatureVector()
  149. err = fv2.Decode(bytes.NewReader(encoded))
  150. if err != nil {
  151. t.Errorf("Failed to decode feature vector in case %d: %v", i, err)
  152. continue
  153. }
  154. buffer2 := new(bytes.Buffer)
  155. err = fv2.Encode(buffer2)
  156. if err != nil {
  157. t.Errorf("Failed to re-encode feature vector in case %d: %v",
  158. i, err)
  159. continue
  160. }
  161. reencoded := buffer2.Bytes()
  162. if !bytes.Equal(reencoded, test.expectedEncoded) {
  163. t.Errorf("Wrong re-encoding in case %d: got %v, expected %v",
  164. i, reencoded, test.expectedEncoded)
  165. }
  166. }
  167. }
  168. func TestFeatureVectorUnknownFeatures(t *testing.T) {
  169. t.Parallel()
  170. tests := []struct {
  171. bits []FeatureBit
  172. expectedUnknown []FeatureBit
  173. }{
  174. {
  175. bits: nil,
  176. expectedUnknown: nil,
  177. },
  178. // Since bits {0, 3, 4, 5} are known, and only even bits are considered
  179. // required (according to the "it's OK to be odd rule"), that leaves
  180. // {2, 6} as both unknown and required.
  181. {
  182. bits: []FeatureBit{0, 1, 2, 3, 4, 5, 6, 7},
  183. expectedUnknown: []FeatureBit{2, 6},
  184. },
  185. }
  186. for i, test := range tests {
  187. rawVector := NewRawFeatureVector(test.bits...)
  188. fv := NewFeatureVector(rawVector, testFeatureNames)
  189. unknown := fv.UnknownRequiredFeatures()
  190. // Sort to make comparison independent of order
  191. sort.Slice(unknown, func(i, j int) bool {
  192. return unknown[i] < unknown[j]
  193. })
  194. if !reflect.DeepEqual(unknown, test.expectedUnknown) {
  195. t.Errorf("Wrong unknown features in case %d: got %v, expected %v",
  196. i, unknown, test.expectedUnknown)
  197. }
  198. }
  199. }
  200. func TestFeatureNames(t *testing.T) {
  201. t.Parallel()
  202. tests := []struct {
  203. bit FeatureBit
  204. expectedName string
  205. expectedKnown bool
  206. }{
  207. {
  208. bit: 0,
  209. expectedName: "feature1",
  210. expectedKnown: true,
  211. },
  212. {
  213. bit: 1,
  214. expectedName: "unknown",
  215. expectedKnown: false,
  216. },
  217. {
  218. bit: 2,
  219. expectedName: "unknown",
  220. expectedKnown: false,
  221. },
  222. {
  223. bit: 3,
  224. expectedName: "feature2",
  225. expectedKnown: true,
  226. },
  227. {
  228. bit: 4,
  229. expectedName: "feature3",
  230. expectedKnown: true,
  231. },
  232. {
  233. bit: 5,
  234. expectedName: "feature3",
  235. expectedKnown: true,
  236. },
  237. {
  238. bit: 6,
  239. expectedName: "unknown",
  240. expectedKnown: false,
  241. },
  242. {
  243. bit: 7,
  244. expectedName: "unknown",
  245. expectedKnown: false,
  246. },
  247. }
  248. fv := NewFeatureVector(nil, testFeatureNames)
  249. for _, test := range tests {
  250. name := fv.Name(test.bit)
  251. if name != test.expectedName {
  252. t.Errorf("Name for feature bit %d is incorrect: "+
  253. "expected %s, got %s", test.bit, name, test.expectedName)
  254. }
  255. known := fv.IsKnown(test.bit)
  256. if known != test.expectedKnown {
  257. t.Errorf("IsKnown for feature bit %d is incorrect: "+
  258. "expected %v, got %v", test.bit, known, test.expectedKnown)
  259. }
  260. }
  261. }
  262. // TestIsRequired asserts that feature bits properly return their IsRequired
  263. // status. We require that even features be required and odd features be
  264. // optional.
  265. func TestIsRequired(t *testing.T) {
  266. optional := FeatureBit(1)
  267. if optional.IsRequired() {
  268. t.Fatalf("optional feature should not be required")
  269. }
  270. required := FeatureBit(0)
  271. if !required.IsRequired() {
  272. t.Fatalf("required feature should be required")
  273. }
  274. }
  275. // TestFeatures asserts that the Features() method on a FeatureVector properly
  276. // returns the set of feature bits it stores internally.
  277. func TestFeatures(t *testing.T) {
  278. tests := []struct {
  279. name string
  280. exp map[FeatureBit]struct{}
  281. }{
  282. {
  283. name: "empty",
  284. exp: map[FeatureBit]struct{}{},
  285. },
  286. {
  287. name: "one",
  288. exp: map[FeatureBit]struct{}{
  289. 5: {},
  290. },
  291. },
  292. {
  293. name: "several",
  294. exp: map[FeatureBit]struct{}{
  295. 0: {},
  296. 5: {},
  297. 23948: {},
  298. },
  299. },
  300. }
  301. toRawFV := func(set map[FeatureBit]struct{}) *RawFeatureVector {
  302. var bits []FeatureBit
  303. for bit := range set {
  304. bits = append(bits, bit)
  305. }
  306. return NewRawFeatureVector(bits...)
  307. }
  308. for _, test := range tests {
  309. test := test
  310. t.Run(test.name, func(t *testing.T) {
  311. fv := NewFeatureVector(
  312. toRawFV(test.exp), Features,
  313. )
  314. if !reflect.DeepEqual(fv.Features(), test.exp) {
  315. t.Fatalf("feature mismatch, want: %v, got: %v",
  316. test.exp, fv.Features())
  317. }
  318. })
  319. }
  320. }
  321. func TestRawFeatureVectorOnlyContains(t *testing.T) {
  322. t.Parallel()
  323. features := []FeatureBit{
  324. StaticRemoteKeyOptional,
  325. AnchorsZeroFeeHtlcTxOptional,
  326. ExplicitChannelTypeRequired,
  327. }
  328. fv := NewRawFeatureVector(features...)
  329. require.True(t, fv.OnlyContains(features...))
  330. require.False(t, fv.OnlyContains(features[:1]...))
  331. }
  332. func TestEqualRawFeatureVectors(t *testing.T) {
  333. t.Parallel()
  334. a := NewRawFeatureVector(
  335. StaticRemoteKeyOptional,
  336. AnchorsZeroFeeHtlcTxOptional,
  337. ExplicitChannelTypeRequired,
  338. )
  339. b := a.Clone()
  340. require.True(t, a.Equals(b))
  341. b.Unset(ExplicitChannelTypeRequired)
  342. require.False(t, a.Equals(b))
  343. b.Set(ExplicitChannelTypeOptional)
  344. require.False(t, a.Equals(b))
  345. }
  346. func TestIsEmptyFeatureVector(t *testing.T) {
  347. t.Parallel()
  348. fv := NewRawFeatureVector()
  349. require.True(t, fv.IsEmpty())
  350. fv.Set(StaticRemoteKeyOptional)
  351. require.False(t, fv.IsEmpty())
  352. fv.Unset(StaticRemoteKeyOptional)
  353. require.True(t, fv.IsEmpty())
  354. }
  355. // TestValidatePairs tests that feature vectors can only set the required or
  356. // optional feature bit in a pair, not both.
  357. func TestValidatePairs(t *testing.T) {
  358. t.Parallel()
  359. rfv := NewRawFeatureVector(
  360. StaticRemoteKeyOptional,
  361. StaticRemoteKeyRequired,
  362. )
  363. require.Equal(t, ErrFeaturePairExists, rfv.ValidatePairs())
  364. rfv = NewRawFeatureVector(
  365. StaticRemoteKeyOptional,
  366. PaymentAddrRequired,
  367. )
  368. require.Nil(t, rfv.ValidatePairs())
  369. }
  370. // TestValidateUpdate tests validation of an update to a feature vector.
  371. func TestValidateUpdate(t *testing.T) {
  372. t.Parallel()
  373. testCases := []struct {
  374. name string
  375. currentFeatures []FeatureBit
  376. newFeatures []FeatureBit
  377. maximumValue FeatureBit
  378. err error
  379. }{
  380. {
  381. name: "defined feature bit set, can include",
  382. currentFeatures: []FeatureBit{
  383. StaticRemoteKeyOptional,
  384. },
  385. newFeatures: []FeatureBit{
  386. StaticRemoteKeyOptional,
  387. },
  388. err: nil,
  389. },
  390. {
  391. name: "defined feature bit not already set",
  392. currentFeatures: []FeatureBit{
  393. StaticRemoteKeyOptional,
  394. },
  395. newFeatures: []FeatureBit{
  396. StaticRemoteKeyOptional,
  397. PaymentAddrRequired,
  398. },
  399. err: ErrFeatureStandard,
  400. },
  401. {
  402. name: "known feature missing",
  403. currentFeatures: []FeatureBit{
  404. StaticRemoteKeyOptional,
  405. PaymentAddrRequired,
  406. },
  407. newFeatures: []FeatureBit{
  408. StaticRemoteKeyOptional,
  409. },
  410. err: ErrFeatureStandard,
  411. },
  412. {
  413. name: "can set unknown feature",
  414. currentFeatures: []FeatureBit{
  415. StaticRemoteKeyOptional,
  416. },
  417. newFeatures: []FeatureBit{
  418. StaticRemoteKeyOptional,
  419. FeatureBit(1001),
  420. },
  421. err: nil,
  422. },
  423. {
  424. name: "can unset unknown feature",
  425. currentFeatures: []FeatureBit{
  426. StaticRemoteKeyOptional,
  427. FeatureBit(1001),
  428. },
  429. newFeatures: []FeatureBit{
  430. StaticRemoteKeyOptional,
  431. },
  432. err: nil,
  433. },
  434. {
  435. name: "at allowed maximum",
  436. currentFeatures: []FeatureBit{
  437. StaticRemoteKeyOptional,
  438. },
  439. newFeatures: []FeatureBit{
  440. StaticRemoteKeyOptional,
  441. 100,
  442. },
  443. maximumValue: 100,
  444. err: nil,
  445. },
  446. {
  447. name: "above allowed maximum",
  448. currentFeatures: []FeatureBit{
  449. StaticRemoteKeyOptional,
  450. },
  451. newFeatures: []FeatureBit{
  452. StaticRemoteKeyOptional,
  453. 101,
  454. },
  455. maximumValue: 100,
  456. err: ErrFeatureBitMaximum,
  457. },
  458. }
  459. for _, testCase := range testCases {
  460. testCase := testCase
  461. t.Run(testCase.name, func(t *testing.T) {
  462. t.Parallel()
  463. currentFV := NewRawFeatureVector(
  464. testCase.currentFeatures...,
  465. )
  466. newFV := NewRawFeatureVector(testCase.newFeatures...)
  467. // Set maximum value if not populated in the test case.
  468. maximumValue := testCase.maximumValue
  469. if testCase.maximumValue == 0 {
  470. maximumValue = math.MaxUint16
  471. }
  472. err := currentFV.ValidateUpdate(newFV, maximumValue)
  473. require.ErrorIs(t, err, testCase.err)
  474. })
  475. }
  476. }