lnonion: add OnionFailureCode.from_int() helper
This commit is contained in:
@@ -505,10 +505,7 @@ class OnionRoutingFailure(Exception):
|
|||||||
@classmethod
|
@classmethod
|
||||||
def from_bytes(cls, failure_msg: bytes):
|
def from_bytes(cls, failure_msg: bytes):
|
||||||
failure_code = int.from_bytes(failure_msg[:2], byteorder='big')
|
failure_code = int.from_bytes(failure_msg[:2], byteorder='big')
|
||||||
try:
|
failure_code = OnionFailureCode.from_int(failure_code) # convert to enum, if known code
|
||||||
failure_code = OnionFailureCode(failure_code)
|
|
||||||
except ValueError:
|
|
||||||
pass # unknown failure code
|
|
||||||
failure_data = failure_msg[2:]
|
failure_data = failure_msg[2:]
|
||||||
return OnionRoutingFailure(failure_code, failure_data)
|
return OnionRoutingFailure(failure_code, failure_data)
|
||||||
|
|
||||||
@@ -640,6 +637,14 @@ class OnionFailureCode(IntEnum):
|
|||||||
TRAMPOLINE_FEE_INSUFFICIENT = NODE | 51
|
TRAMPOLINE_FEE_INSUFFICIENT = NODE | 51
|
||||||
TRAMPOLINE_EXPIRY_TOO_SOON = NODE | 52
|
TRAMPOLINE_EXPIRY_TOO_SOON = NODE | 52
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_int(cls, code: int) -> Union[int, 'OnionFailureCode']:
|
||||||
|
try:
|
||||||
|
code = OnionFailureCode(code)
|
||||||
|
except ValueError:
|
||||||
|
pass # unknown failure code
|
||||||
|
return code
|
||||||
|
|
||||||
|
|
||||||
# don't use these elsewhere, the names are ambiguous without context
|
# don't use these elsewhere, the names are ambiguous without context
|
||||||
del BADONION; del PERM; del NODE; del UPDATE
|
del BADONION; del PERM; del NODE; del UPDATE
|
||||||
|
|||||||
Reference in New Issue
Block a user