| | | 1 | | using System.Collections; |
| | | 2 | | using System.Runtime.Serialization; |
| | | 3 | | using System.Text; |
| | | 4 | | using NLightning.Domain.Utils.Interfaces; |
| | | 5 | | |
| | | 6 | | namespace NLightning.Domain.Node; |
| | | 7 | | |
| | | 8 | | using Enums; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Represents the features supported by a node. <see href="https://github.com/lightning/bolts/blob/master/09-features.m |
| | | 12 | | /// </summary> |
| | | 13 | | public class FeatureSet |
| | | 14 | | { |
| | | 15 | | /// <summary> |
| | | 16 | | /// Some features are dependent on other features. This dictionary contains the dependencies. |
| | | 17 | | /// </summary> |
| | 20 | 18 | | private static readonly Dictionary<Feature, Feature[]> s_featureDependencies = new() |
| | 20 | 19 | | { |
| | 20 | 20 | | // This \/ --- Depends on this \/ |
| | 20 | 21 | | { Feature.GossipQueriesEx, [Feature.GossipQueries] }, |
| | 20 | 22 | | { Feature.PaymentSecret, [Feature.VarOnionOptin] }, |
| | 20 | 23 | | { Feature.BasicMpp, [Feature.PaymentSecret] }, |
| | 20 | 24 | | { Feature.OptionAnchorOutputs, [Feature.OptionStaticRemoteKey] }, |
| | 20 | 25 | | { Feature.OptionAnchorsZeroFeeHtlcTx, [Feature.OptionStaticRemoteKey] }, |
| | 20 | 26 | | { Feature.OptionRouteBlinding, [Feature.VarOnionOptin] }, |
| | 20 | 27 | | { Feature.OptionZeroconf, [Feature.OptionScidAlias] }, |
| | 20 | 28 | | }; |
| | | 29 | | |
| | | 30 | | internal BitArray FeatureFlags; |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Initializes a new instance of the <see cref="FeatureSet"/> class. |
| | | 34 | | /// </summary> |
| | | 35 | | /// <remarks> |
| | | 36 | | /// Always set the bit of <see cref="Feature.VarOnionOptin"/> as Optional. |
| | | 37 | | /// </remarks> |
| | 868 | 38 | | public FeatureSet() |
| | | 39 | | { |
| | 868 | 40 | | FeatureFlags = new BitArray(128); |
| | | 41 | | // Always set the compulsory bit of var_onion_optin |
| | 868 | 42 | | SetFeature(Feature.VarOnionOptin, false); |
| | 868 | 43 | | } |
| | | 44 | | |
| | | 45 | | public event EventHandler? Changed; |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Gets the last index-of-one in the BitArray and add 1 because arrays starts at 0. |
| | | 49 | | /// </summary> |
| | 304 | 50 | | public int SizeInBits => GetLastIndexOfOne(FeatureFlags); |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Sets a feature. |
| | | 54 | | /// </summary> |
| | | 55 | | /// <param name="feature">The feature to set.</param> |
| | | 56 | | /// <param name="isCompulsory">If the feature is compulsory.</param> |
| | | 57 | | /// <param name="isSet">true to set the feature, false to unset it</param> |
| | | 58 | | /// <remarks> |
| | | 59 | | /// If the feature has dependencies, they will be set first. |
| | | 60 | | /// The dependencies keep the same isCompulsory value as the feature being set. |
| | | 61 | | /// </remarks> |
| | | 62 | | public void SetFeature(Feature feature, bool isCompulsory, bool isSet = true) |
| | | 63 | | { |
| | | 64 | | // If we're setting the feature, and it has dependencies, set them first |
| | 2232 | 65 | | if (isSet) |
| | | 66 | | { |
| | 1360 | 67 | | if (s_featureDependencies.TryGetValue(feature, out var dependencies)) |
| | | 68 | | { |
| | 416 | 69 | | foreach (var dependency in dependencies) |
| | 104 | 70 | | SetFeature(dependency, isCompulsory, isSet); |
| | | 71 | | } |
| | | 72 | | } |
| | | 73 | | else // If we're unsetting the feature, and it has dependents, unset them first |
| | | 74 | | { |
| | 9648 | 75 | | foreach (var dependent in s_featureDependencies.Where(x => x.Value.Contains(feature)).Select(x => x.Key)) |
| | 600 | 76 | | SetFeature(dependent, isCompulsory, isSet); |
| | | 77 | | } |
| | | 78 | | |
| | 2232 | 79 | | var bitPosition = (int)feature; |
| | | 80 | | |
| | 2232 | 81 | | if (isCompulsory) |
| | | 82 | | { |
| | | 83 | | // Unset the non-compulsory bit |
| | 324 | 84 | | SetFeature(bitPosition, false); |
| | 324 | 85 | | --bitPosition; |
| | | 86 | | } |
| | | 87 | | else |
| | | 88 | | { |
| | | 89 | | // Unset the compulsory bit |
| | 1908 | 90 | | SetFeature(bitPosition - 1, false); |
| | | 91 | | } |
| | | 92 | | |
| | | 93 | | // Then set the feature itself |
| | 2232 | 94 | | SetFeature(bitPosition, isSet); |
| | 2232 | 95 | | } |
| | | 96 | | |
| | | 97 | | /// <summary> |
| | | 98 | | /// Sets a feature. |
| | | 99 | | /// </summary> |
| | | 100 | | /// <param name="bitPosition">The bit position of the feature to set.</param> |
| | | 101 | | /// <param name="isSet">true to set the feature, false to unset it</param> |
| | | 102 | | public void SetFeature(int bitPosition, bool isSet) |
| | | 103 | | { |
| | 4764 | 104 | | if (bitPosition >= FeatureFlags.Length) |
| | 64 | 105 | | FeatureFlags.Length = bitPosition + 1; |
| | | 106 | | |
| | 4764 | 107 | | FeatureFlags.Set(bitPosition, isSet); |
| | | 108 | | |
| | 4764 | 109 | | OnChanged(); |
| | 4764 | 110 | | } |
| | | 111 | | |
| | | 112 | | /// <summary> |
| | | 113 | | /// Checks if a feature is set either as compulsory or optional. |
| | | 114 | | /// </summary> |
| | | 115 | | /// <param name="feature">Feature to check.</param> |
| | | 116 | | /// <returns>true if the feature is set, false otherwise.</returns> |
| | | 117 | | public bool IsFeatureSet(Feature feature) |
| | | 118 | | { |
| | 0 | 119 | | var bitPosition = (int)feature; |
| | | 120 | | |
| | 0 | 121 | | return IsFeatureSet(bitPosition) || IsFeatureSet(bitPosition - 1); |
| | | 122 | | } |
| | | 123 | | |
| | | 124 | | /// <summary> |
| | | 125 | | /// Checks if a feature is set. |
| | | 126 | | /// </summary> |
| | | 127 | | /// <param name="feature">Feature to check.</param> |
| | | 128 | | /// <param name="isCompulsory">If the feature is compulsory.</param> |
| | | 129 | | /// <returns>true if the feature is set, false otherwise.</returns> |
| | | 130 | | public bool IsFeatureSet(Feature feature, bool isCompulsory) |
| | | 131 | | { |
| | 680 | 132 | | var bitPosition = (int)feature; |
| | | 133 | | |
| | | 134 | | // If the feature is compulsory, adjust the bit position to be even |
| | 680 | 135 | | if (isCompulsory) |
| | 280 | 136 | | bitPosition--; |
| | | 137 | | |
| | 680 | 138 | | return IsFeatureSet(bitPosition); |
| | | 139 | | } |
| | | 140 | | |
| | | 141 | | /// <summary> |
| | | 142 | | /// Checks if a feature is set. |
| | | 143 | | /// </summary> |
| | | 144 | | /// <param name="bitPosition">The bit position of the feature to check.</param> |
| | | 145 | | /// <param name="isCompulsory">If the feature is compulsory.</param> |
| | | 146 | | /// <returns>true if the feature is set, false otherwise.</returns> |
| | | 147 | | public bool IsFeatureSet(int bitPosition, bool isCompulsory) |
| | | 148 | | { |
| | | 149 | | // If the feature is compulsory, adjust the bit position to be even |
| | 19188 | 150 | | if (isCompulsory) |
| | 9504 | 151 | | bitPosition--; |
| | | 152 | | |
| | 19188 | 153 | | return IsFeatureSet(bitPosition); |
| | | 154 | | } |
| | | 155 | | |
| | | 156 | | /// <summary> |
| | | 157 | | /// Checks if a feature is set. |
| | | 158 | | /// </summary> |
| | | 159 | | /// <param name="bitPosition">The bit position of the feature to check.</param> |
| | | 160 | | /// <returns>true if the feature is set, false otherwise.</returns> |
| | | 161 | | private bool IsFeatureSet(int bitPosition) |
| | | 162 | | { |
| | 22400 | 163 | | return bitPosition < FeatureFlags.Length && FeatureFlags.Get(bitPosition); |
| | | 164 | | } |
| | | 165 | | |
| | | 166 | | /// <summary> |
| | | 167 | | /// Checks if the option_anchor_outputs or option_anchors_zero_fee_htlc_tx feature is set. |
| | | 168 | | /// </summary> |
| | | 169 | | /// <returns>true if one of the features is set, false otherwise.</returns> |
| | | 170 | | public bool IsOptionAnchorsSet() |
| | | 171 | | { |
| | 0 | 172 | | return IsFeatureSet(Feature.OptionAnchorOutputs, false) || |
| | 0 | 173 | | IsFeatureSet(Feature.OptionAnchorsZeroFeeHtlcTx, false); |
| | | 174 | | } |
| | | 175 | | |
| | | 176 | | /// <summary> |
| | | 177 | | /// Check if this feature set is compatible with the other provided feature set. |
| | | 178 | | /// </summary> |
| | | 179 | | /// <param name="other">The other feature set to check compatibility with.</param> |
| | | 180 | | /// <param name="negotiatedFeatureSet">The resulting negotiated feature set.</param> |
| | | 181 | | /// <returns>true if the feature sets are compatible, false otherwise.</returns> |
| | | 182 | | /// <remarks> |
| | | 183 | | /// The other feature set must support the var_onion_optin feature. |
| | | 184 | | /// The other feature set must have all the dependencies set. |
| | | 185 | | /// </remarks> |
| | | 186 | | public bool IsCompatible(FeatureSet other, out FeatureSet? negotiatedFeatureSet) |
| | | 187 | | { |
| | | 188 | | // Check if the other node supports var_onion_optin |
| | 120 | 189 | | if (!other.IsFeatureSet(Feature.VarOnionOptin, false) && !other.IsFeatureSet(Feature.VarOnionOptin, true)) |
| | | 190 | | { |
| | 4 | 191 | | negotiatedFeatureSet = null; |
| | 4 | 192 | | return false; |
| | | 193 | | } |
| | | 194 | | |
| | | 195 | | // Check which one is bigger and iterate on it |
| | 116 | 196 | | var maxLength = Math.Max(FeatureFlags.Length, other.FeatureFlags.Length); |
| | | 197 | | |
| | | 198 | | // Create a temporary feature set to store the negotiated features |
| | 116 | 199 | | negotiatedFeatureSet = new FeatureSet(); |
| | 7776 | 200 | | for (var i = 1; i < maxLength; i += 2) |
| | | 201 | | { |
| | 3784 | 202 | | var isLocalOptionalSet = IsFeatureSet(i, false); |
| | 3784 | 203 | | var isLocalCompulsorySet = IsFeatureSet(i, true); |
| | 3784 | 204 | | var isOtherOptionalSet = other.IsFeatureSet(i, false); |
| | 3784 | 205 | | var isOtherCompulsorySet = other.IsFeatureSet(i, true); |
| | | 206 | | |
| | | 207 | | // If the feature is unknown |
| | 3784 | 208 | | if (!Enum.IsDefined(typeof(Feature), i)) |
| | | 209 | | { |
| | | 210 | | // If the feature is unknown and even, close the connection |
| | 2168 | 211 | | if (isOtherCompulsorySet) |
| | | 212 | | { |
| | 4 | 213 | | negotiatedFeatureSet = null; |
| | 4 | 214 | | return false; |
| | | 215 | | } |
| | | 216 | | |
| | 2164 | 217 | | if (isOtherOptionalSet) |
| | 20 | 218 | | negotiatedFeatureSet.SetFeature(i, false); |
| | | 219 | | } |
| | | 220 | | else |
| | | 221 | | { |
| | | 222 | | // If the local feature is compulsory, the other feature should also be set (either optional or compulso |
| | 1616 | 223 | | if (isLocalCompulsorySet && !(isOtherOptionalSet || isOtherCompulsorySet)) |
| | | 224 | | { |
| | 4 | 225 | | negotiatedFeatureSet = null; |
| | 4 | 226 | | return false; |
| | | 227 | | } |
| | | 228 | | |
| | | 229 | | // If the other feature is compulsory, the local feature should also be set (either optional or compulso |
| | 1612 | 230 | | if (isOtherCompulsorySet && !(isLocalOptionalSet || isLocalCompulsorySet)) |
| | | 231 | | { |
| | 4 | 232 | | negotiatedFeatureSet = null; |
| | 4 | 233 | | return false; |
| | | 234 | | } |
| | | 235 | | |
| | 1608 | 236 | | if (isOtherCompulsorySet || isLocalCompulsorySet) |
| | | 237 | | { |
| | 64 | 238 | | negotiatedFeatureSet.SetFeature(i, true); |
| | | 239 | | } |
| | 1544 | 240 | | else if (isLocalOptionalSet && isOtherOptionalSet) |
| | | 241 | | { |
| | 136 | 242 | | negotiatedFeatureSet.SetFeature(i, false); |
| | | 243 | | } |
| | | 244 | | } |
| | | 245 | | } |
| | | 246 | | |
| | | 247 | | // Check if all the other node's dependencies are set |
| | 104 | 248 | | if (other.AreDependenciesSet()) |
| | 100 | 249 | | return true; |
| | | 250 | | |
| | 4 | 251 | | negotiatedFeatureSet = null; |
| | 4 | 252 | | return false; |
| | | 253 | | } |
| | | 254 | | |
| | | 255 | | /// <summary> |
| | | 256 | | /// Serializes the features to a byte array. |
| | | 257 | | /// </summary> |
| | | 258 | | public void WriteToBitWriter(IBitWriter bitWriter, int length, bool shouldPad) |
| | | 259 | | { |
| | | 260 | | // Check if _featureFlags is as long as the length |
| | 64 | 261 | | var extraLength = length - FeatureFlags.Length; |
| | 64 | 262 | | if (extraLength > 0) |
| | 4 | 263 | | FeatureFlags.Length += extraLength; |
| | | 264 | | |
| | 4648 | 265 | | for (var i = 0; i < length && bitWriter.HasMoreBits(1); i++) |
| | 2260 | 266 | | bitWriter.WriteBit(FeatureFlags[length - i - (shouldPad ? 0 : 1)]); |
| | 64 | 267 | | } |
| | | 268 | | |
| | | 269 | | /// <summary> |
| | | 270 | | /// Checks if a feature is set. |
| | | 271 | | /// </summary> |
| | | 272 | | /// <param name="feature">The feature to check.</param> |
| | | 273 | | /// <returns>true if the feature is set, false otherwise.</returns> |
| | | 274 | | /// <remarks> |
| | | 275 | | /// We don't care if the feature is compulsory or optional. |
| | | 276 | | /// </remarks> |
| | 0 | 277 | | public bool HasFeature(Feature feature) => IsFeatureSet(feature, false) || IsFeatureSet(feature, true); |
| | | 278 | | |
| | | 279 | | /// <summary> |
| | | 280 | | /// Deserializes the features from a byte array. |
| | | 281 | | /// </summary> |
| | | 282 | | /// <param name="data">The byte array to deserialize from.</param> |
| | | 283 | | /// <remarks> |
| | | 284 | | /// The byte array can have a length less than or equal to 8 bytes. |
| | | 285 | | /// </remarks> |
| | | 286 | | /// <returns>The deserialized features.</returns> |
| | | 287 | | /// <exception cref="SerializationException">Error deserializing Features</exception> |
| | | 288 | | public static FeatureSet DeserializeFromBytes(byte[] data) |
| | | 289 | | { |
| | | 290 | | try |
| | | 291 | | { |
| | 168 | 292 | | if (BitConverter.IsLittleEndian) |
| | 168 | 293 | | Array.Reverse(data); |
| | | 294 | | |
| | 168 | 295 | | var bitArray = new BitArray(data); |
| | 168 | 296 | | return new FeatureSet { FeatureFlags = bitArray }; |
| | | 297 | | } |
| | 0 | 298 | | catch (Exception e) |
| | | 299 | | { |
| | 0 | 300 | | throw new SerializationException("Error deserializing Features", e); |
| | | 301 | | } |
| | 168 | 302 | | } |
| | | 303 | | |
| | | 304 | | /// <summary> |
| | | 305 | | /// Deserializes the features from a BitReader. |
| | | 306 | | /// </summary> |
| | | 307 | | /// <param name="bitReader">The bit reader to read from.</param> |
| | | 308 | | /// <param name="length">The number of bits to read.</param> |
| | | 309 | | /// <param name="shouldPad">If the bit array should be padded.</param> |
| | | 310 | | /// <returns>The deserialized features.</returns> |
| | | 311 | | /// <exception cref="SerializationException">Error deserializing Features</exception> |
| | | 312 | | public static FeatureSet DeserializeFromBitReader(IBitReader bitReader, int length, bool shouldPad) |
| | | 313 | | { |
| | | 314 | | try |
| | | 315 | | { |
| | | 316 | | // Create a new bit array |
| | 76 | 317 | | var bitArray = new BitArray(length + (shouldPad ? 1 : 0)); |
| | 5712 | 318 | | for (var i = 0; i < length; i++) |
| | 2780 | 319 | | bitArray.Set(length - i - (shouldPad ? 0 : 1), bitReader.ReadBit()); |
| | | 320 | | |
| | 76 | 321 | | return new FeatureSet { FeatureFlags = bitArray }; |
| | | 322 | | } |
| | 0 | 323 | | catch (Exception e) |
| | | 324 | | { |
| | 0 | 325 | | throw new SerializationException("Error deserializing Features", e); |
| | | 326 | | } |
| | 76 | 327 | | } |
| | | 328 | | |
| | | 329 | | /// <summary> |
| | | 330 | | /// Combines two feature sets. |
| | | 331 | | /// </summary> |
| | | 332 | | /// <param name="first">The first feature set.</param> |
| | | 333 | | /// <param name="second">The second feature set.</param> |
| | | 334 | | /// <returns>The combined feature set.</returns> |
| | | 335 | | /// <remarks> |
| | | 336 | | /// The combined feature set is the logical OR of the two feature sets. |
| | | 337 | | /// </remarks> |
| | | 338 | | public static FeatureSet Combine(FeatureSet first, FeatureSet second) |
| | | 339 | | { |
| | 16 | 340 | | var combinedLength = Math.Max(first.FeatureFlags.Length, second.FeatureFlags.Length); |
| | 16 | 341 | | var combinedFlags = new BitArray(combinedLength); |
| | | 342 | | |
| | 1440 | 343 | | for (var i = 0; i < combinedLength; i++) |
| | 704 | 344 | | combinedFlags.Set(i, first.IsFeatureSet(i) || second.IsFeatureSet(i)); |
| | | 345 | | |
| | 16 | 346 | | return new FeatureSet { FeatureFlags = combinedFlags }; |
| | | 347 | | } |
| | | 348 | | |
| | | 349 | | public override string ToString() |
| | | 350 | | { |
| | 16 | 351 | | var sb = new StringBuilder(); |
| | 2336 | 352 | | for (var i = 0; i < FeatureFlags.Length; i++) |
| | | 353 | | { |
| | 1152 | 354 | | if (IsFeatureSet(i)) |
| | 16 | 355 | | sb.Append($"{(Feature)i}, "); |
| | | 356 | | } |
| | | 357 | | |
| | 16 | 358 | | return sb.ToString().TrimEnd(' ', ','); |
| | | 359 | | } |
| | | 360 | | |
| | | 361 | | /// <summary> |
| | | 362 | | /// Checks if all dependencies are set. |
| | | 363 | | /// </summary> |
| | | 364 | | /// <returns>true if all dependencies are set, false otherwise.</returns> |
| | | 365 | | /// <remarks> |
| | | 366 | | /// This method is used to check if all dependencies are set when a feature is set. |
| | | 367 | | /// </remarks> |
| | | 368 | | private bool AreDependenciesSet() |
| | | 369 | | { |
| | | 370 | | // Check if all known (Feature Enum) dependencies are set if the feature is set |
| | 4364 | 371 | | foreach (var feature in Enum.GetValues<Feature>()) |
| | | 372 | | { |
| | 2080 | 373 | | if (!IsFeatureSet((int)feature, false) && !IsFeatureSet((int)feature, true)) |
| | | 374 | | continue; |
| | | 375 | | |
| | 208 | 376 | | if (!s_featureDependencies.TryGetValue(feature, out var dependencies)) |
| | | 377 | | continue; |
| | | 378 | | |
| | 144 | 379 | | if (dependencies.Any(dependency => !IsFeatureSet(dependency, false) && !IsFeatureSet(dependency, true))) |
| | 4 | 380 | | return false; |
| | | 381 | | } |
| | | 382 | | |
| | 100 | 383 | | return true; |
| | | 384 | | } |
| | | 385 | | |
| | | 386 | | private void OnChanged() |
| | | 387 | | { |
| | 4764 | 388 | | Changed?.Invoke(this, EventArgs.Empty); |
| | 532 | 389 | | } |
| | | 390 | | |
| | | 391 | | private static int GetLastIndexOfOne(BitArray bitArray) |
| | | 392 | | { |
| | 880 | 393 | | for (var i = bitArray.Length - 1; i >= 0; i--) |
| | | 394 | | { |
| | 440 | 395 | | if (bitArray[i]) |
| | 304 | 396 | | return i; |
| | | 397 | | } |
| | | 398 | | |
| | 0 | 399 | | return -1; // Return -1 if no number 1 is found |
| | | 400 | | } |
| | | 401 | | } |