1
0

lnutil.LnFeatures: impl and use "supports" method for feature-bit-tests

Note that for a required feature, BOLT-09 allows setting either:
- only the REQ bit
- both the REQ bit and the OPT bit

Hence, when checking if a feature is supported by e.g. an invoice, both
bits should be checked.

Note that in lnpeer.py, in self.features specifically, REQ implies OPT,
as it is set by ln_compare_features.
This commit is contained in:
SomberNight
2021-02-22 19:53:01 +01:00
parent 0369829e5e
commit 4aab843f17
5 changed files with 96 additions and 12 deletions

View File

@@ -1013,6 +1013,23 @@ class LnFeatures(IntFlag):
features |= (1 << flag)
return features
def supports(self, feature: 'LnFeatures') -> bool:
"""Returns whether given feature is enabled.
Helper function that tries to hide the complexity of even/odd bits.
For example, instead of:
bool(myfeatures & LnFeatures.VAR_ONION_OPT or myfeatures & LnFeatures.VAR_ONION_REQ)
you can do:
myfeatures.supports(LnFeatures.VAR_ONION_OPT)
"""
enabled_bits = list_enabled_bits(feature)
if len(enabled_bits) != 1:
raise ValueError(f"'feature' cannot be a combination of features: {feature}")
flag = enabled_bits[0]
our_flags = set(list_enabled_bits(self))
return (flag in our_flags
or get_ln_flag_pair_of_bit(flag) in our_flags)
del LNFC # name is ambiguous without context