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