Repository: bitcoin/bitcoin (language: cpp)

Issue:
Error when parsing captured p2p messages using message-capture-parser.py
I am unable to read the p2p messages saved in `msgs_sent.dat` and `msgs_recv.dat` files.

Steps to reproduce:

1. Run `bitcoind` with `capturemessages=1`
2. Wait for a few minutes, ensure there are some peers and corresponding directories created in `~/.bitcoin/message_capture`
3. Run message capture python script with: `python message-capture-parser.py -o out.json ~/.bitcoin/message_capture/<IP_ADDRESS>_<PORT>/msgs_sent.dat`

I am getting this error for sent/recv dat files of all peers:

```
python message-capture-parser.py -o out.json /home/test/.bitcoin/signet/message_capture/95.217.184.148_38333/msgs_sent.dat 
[ ######################################################################################################################           ]  92%Traceback (most recent call last):
  File "/home/test/Downloads/bitcoin/contrib/message-capture/message-capture-parser.py", line 215, in <module>
    main()
  File "/home/test/Downloads/bitcoin/contrib/message-capture/message-capture-parser.py", line 200, in main
    process_file(str(capture), messages, "recv" in capture.stem, progress_bar)
  File "/home/test/Downloads/bitcoin/contrib/message-capture/message-capture-parser.py", line 160, in process_file
    msg_dict["body"] = to_jsonable(msg)
  File "/home/test/Downloads/bitcoin/contrib/message-capture/message-capture-parser.py", line 83, in to_jsonable
    assert all(isinstance(a, int) for a in val)
AssertionError
```

---

Message parsing script: https://github.com/bitcoin/bitcoin/blob/master/contrib/message-capture/message-capture-parser.py
Doc: https://github.com/bitcoin/bitcoin/blob/master/contrib/message-capture/message-capture-docs.md

 


Discussion / hints:
Try removing headers from HASH_INT_VECTORS. I think the deser_uint256_vector requirement is covered by the fact that hashPrevBlock and hashMerkleRoot are in the HASH_INTS list. But someone else should probably confirm this.

Fix patch:
diff --git a/contrib/message-capture/message-capture-parser.py b/contrib/message-capture/message-capture-parser.py
index eefd22a60e031..33759ee71377a 100755
--- a/contrib/message-capture/message-capture-parser.py
+++ b/contrib/message-capture/message-capture-parser.py
@@ -79,8 +79,7 @@ def to_jsonable(obj: Any) -> Any:
             val = getattr(obj, slot, None)
             if slot in HASH_INTS and isinstance(val, int):
                 ret[slot] = ser_uint256(val).hex()
-            elif slot in HASH_INT_VECTORS:
-                assert all(isinstance(a, int) for a in val)
+            elif slot in HASH_INT_VECTORS and all(isinstance(a, int) for a in val):
                 ret[slot] = [ser_uint256(a).hex() for a in val]
             else:
                 ret[slot] = to_jsonable(val)


Test patch: