Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check if message can be handled before attempting to deserialize #5927

Merged
merged 1 commit into from
Mar 12, 2024

Conversation

thephez
Copy link
Collaborator

@thephez thephez commented Mar 7, 2024

Issue being fixed or feature implemented

Currently message-capture-parser.py crashes when encountering certain messages (e.g. mnauth). This at least makes it possible to run the script without crashing. There may be better options for solving this.

What was done?

Check if the dictionary is going to return None before we attempt to do something further with it. Hide whitespace changes to see the few lines that were added: https://github.com/dashpay/dash/pull/5927/files?diff=unified&w=1

How Has This Been Tested?

Running script locally

Breaking Changes

N/A

Checklist:

Go over all the following points, and put an x in all the boxes that apply.

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have added or updated relevant unit/integration/functional/e2e tests
  • I have made corresponding changes to the documentation
  • I have assigned this pull request to a milestone (for repository code-owners and collaborators only)

@thephez thephez requested review from knst and UdjinM6 March 7, 2024 20:38
@UdjinM6
Copy link

UdjinM6 commented Mar 8, 2024

How about a slightly different approach?

diff --git a/contrib/message-capture/message-capture-parser.py b/contrib/message-capture/message-capture-parser.py
index 33759ee71..c47685730 100755
--- a/contrib/message-capture/message-capture-parser.py
+++ b/contrib/message-capture/message-capture-parser.py
@@ -122,8 +122,8 @@ def process_file(path: str, messages: List[Any], recv: bool, progress_bar: Optio
             msg_ser = BytesIO(f_in.read(length))
 
             # Determine message type
-            if msgtype not in MESSAGEMAP:
-                # Unrecognized message type
+            if msgtype not in MESSAGEMAP or MESSAGEMAP[msgtype] is None:
+                # Unrecognized or unhandled message type
                 try:
                     msgtype_tmp = msgtype.decode()
                     if not msgtype_tmp.isprintable():
@@ -131,10 +131,11 @@ def process_file(path: str, messages: List[Any], recv: bool, progress_bar: Optio
                     msg_dict["msgtype"] = msgtype_tmp
                 except UnicodeDecodeError:
                     msg_dict["msgtype"] = "UNREADABLE"
+                err_str = "Unrecognized" if msgtype not in MESSAGEMAP else "Unhandled"
                 msg_dict["body"] = msg_ser.read().hex()
-                msg_dict["error"] = "Unrecognized message type."
+                msg_dict["error"] = f"{err_str} message type"
                 messages.append(msg_dict)
-                print(f"WARNING - Unrecognized message type {msgtype} in {path}", file=sys.stderr)
+                print(f"WARNING - {msg_dict['error']} {msgtype} in {path}", file=sys.stderr)
                 continue
 
             # Deserialize the message

This way you'd get unhandled messages in the list too. Their body field would be an unparsed hex string but it's still better than nothing imo.

Example:

  {
    "direction": "sent",
    "time": 1709896338971830,
    "size": 1,
    "msgtype": "senddsq",
    "body": "01",
    "error": "Unhandled message type"
  },

@UdjinM6 UdjinM6 added this to the 21 milestone Mar 8, 2024
@thephez
Copy link
Collaborator Author

thephez commented Mar 11, 2024

How about a slightly different approach?

Thanks 👍 Your solution is preferable imo 🙂

Copy link
Member

@PastaPastaPasta PastaPastaPasta left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utACK for squash merge

Copy link

@UdjinM6 UdjinM6 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK

Co-authored-by: UdjinM6 <UdjinM6@users.noreply.github.com>
@PastaPastaPasta PastaPastaPasta merged commit 1cbaa7b into dashpay:develop Mar 12, 2024
5 checks passed
@thephez thephez deleted the message-parser-fix branch March 12, 2024 20:37
PastaPastaPasta added a commit to PastaPastaPasta/dash that referenced this pull request Apr 3, 2024
…pting to deserialize

afbae06 fix: check if message can be handled before attempting to deserialize (thephez)

Pull request description:

  ## Issue being fixed or feature implemented
  Currently `message-capture-parser.py` crashes when encountering certain messages (e.g. mnauth). This at least makes it possible to run the script without crashing. There may be better options for solving this.

  ## What was done?
  Check if the dictionary is going to return `None` before we attempt to do something further with it. Hide whitespace changes to see the few lines that were added: https://github.com/dashpay/dash/pull/5927/files?diff=unified&w=1

  ## How Has This Been Tested?
  Running script locally

  ## Breaking Changes
  N/A

  ## Checklist:
    _Go over all the following points, and put an `x` in all the boxes that apply._
  - [x] I have performed a self-review of my own code
  - [ ] I have commented my code, particularly in hard-to-understand areas
  - [ ] I have added or updated relevant unit/integration/functional/e2e tests
  - [ ] I have made corresponding changes to the documentation
  - [ ] I have assigned this pull request to a milestone _(for repository code-owners and collaborators only)_

Top commit has no ACKs.

Tree-SHA512: 041af57afcfd1d93487fd41d34a50e3a99f7fa129563dfe1e1cf2498974c8e658bd6acb9c810887c841160074056ba999e9b6607ac9336b98b9d42806682c607
PastaPastaPasta added a commit to PastaPastaPasta/dash that referenced this pull request Apr 3, 2024
…pting to deserialize

afbae06 fix: check if message can be handled before attempting to deserialize (thephez)

Pull request description:

  ## Issue being fixed or feature implemented
  Currently `message-capture-parser.py` crashes when encountering certain messages (e.g. mnauth). This at least makes it possible to run the script without crashing. There may be better options for solving this.

  ## What was done?
  Check if the dictionary is going to return `None` before we attempt to do something further with it. Hide whitespace changes to see the few lines that were added: https://github.com/dashpay/dash/pull/5927/files?diff=unified&w=1

  ## How Has This Been Tested?
  Running script locally

  ## Breaking Changes
  N/A

  ## Checklist:
    _Go over all the following points, and put an `x` in all the boxes that apply._
  - [x] I have performed a self-review of my own code
  - [ ] I have commented my code, particularly in hard-to-understand areas
  - [ ] I have added or updated relevant unit/integration/functional/e2e tests
  - [ ] I have made corresponding changes to the documentation
  - [ ] I have assigned this pull request to a milestone _(for repository code-owners and collaborators only)_

Top commit has no ACKs.

Tree-SHA512: 041af57afcfd1d93487fd41d34a50e3a99f7fa129563dfe1e1cf2498974c8e658bd6acb9c810887c841160074056ba999e9b6607ac9336b98b9d42806682c607
@UdjinM6 UdjinM6 modified the milestones: 21, 20.1.1 Apr 3, 2024
PastaPastaPasta added a commit that referenced this pull request Apr 3, 2024
b96b202 chore: bump version to 20.1.1 (pasta)
83cac77 docs: add v20.1.1 release notes (pasta)
e58c7c4 Merge #5970: guix: exclude debug symbols for apple from list of hashes due to its non-determinism (pasta)
2bde1dd Merge #5927: fix: check if message can be handled before attempting to deserialize (pasta)
1637fa5 Merge #5968: docs: add v20.0.4 release notes (pasta)
fd46c4c Merge #5962: fix: deadlock over cs_main and contributionsCacheCs in dkssessionmgr (pasta)

Pull request description:

  ## Issue being fixed or feature implemented
  Backport to v20.1.x and release v20.1.1

  ## What was done?
  Backports and release

  ## How Has This Been Tested?

  ## Breaking Changes
  None

  ## Checklist:
    _Go over all the following points, and put an `x` in all the boxes that apply._
  - [ ] I have performed a self-review of my own code
  - [ ] I have commented my code, particularly in hard-to-understand areas
  - [ ] I have added or updated relevant unit/integration/functional/e2e tests
  - [ ] I have made corresponding changes to the documentation
  - [x] I have assigned this pull request to a milestone _(for repository code-owners and collaborators only)_

Top commit has no ACKs.

Tree-SHA512: 40574df3349bf1c653a50b4efbf78d1654eb048eeeb483eb657eec5f2af102f95cff8f978a98c174925d2672d56426238a1b7ec3e8cd2a53ec28c0fca42c1293
PastaPastaPasta added a commit that referenced this pull request Apr 9, 2024
b96b202 chore: bump version to 20.1.1 (pasta)
83cac77 docs: add v20.1.1 release notes (pasta)
e58c7c4 Merge #5970: guix: exclude debug symbols for apple from list of hashes due to its non-determinism (pasta)
2bde1dd Merge #5927: fix: check if message can be handled before attempting to deserialize (pasta)
1637fa5 Merge #5968: docs: add v20.0.4 release notes (pasta)
fd46c4c Merge #5962: fix: deadlock over cs_main and contributionsCacheCs in dkssessionmgr (pasta)

Pull request description:

  Merge master into develop

Top commit has no ACKs.

Tree-SHA512: e65ffefcb7d0e92367ff322cab533f8505f0e9d00ab136bccb1dbf86ad64d6b710b5c9f9943594c544827d2eb69f5d0ba185962a2ac0b16c6b776a94f44cfb42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants