Page MenuHomePhabricator

test_framework: improve the syntax of serialize() methods
ClosedPublic

Authored by PiRK on Thu, Mar 20, 08:27.

Details

Reviewers
Fabien
Group Reviewers
Restricted Project
Commits
rABCefe279d43307: test_framework: improve the syntax of serialize() methods
Summary

These changes do not affect behavior.

Don't unnecessarily initialize an empty bytes object before appending to it.
Don't use an intermediate variable before returning in trivial cases.
Add a few typehints when they are not obvious to the reader (e.g. when *string* actually means *bytes* because of python2 legacy)

Test Plan

ninja check-functional-extended

Diff Detail

Repository
rABC Bitcoin ABC
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

PiRK requested review of this revision.Thu, Mar 20, 08:27
PiRK planned changes to this revision.Thu, Mar 20, 08:30

back-off unrelated changes

This whole diff is a test on how Grok v3 can helpful to do semi-automated changes on a large amount of code.
It worked quite well, I just had to revert a few changes msg_avaproofs.serialize and msg_avaproofsreq.serialize, which imo were not trivial enough to justify removing the intermadiate var and all the comments to make it a one-liner.

Some unrelated blank line changes (annoying) and indentation changes (fixed by the linters).

test/functional/test_framework/messages.py
241 ↗(On Diff #53198)

this is manually changed. Grok's solution was worse imo

def serialize(self, *, with_time=True) -> bytes:
    """Serialize in addrv1 format (pre-BIP155)"""
    assert self.net == self.NET_IPV4
    if with_time:
        # VERSION messages serialize CAddress objects without time
        return (
            struct.pack("<I", self.time)
            + struct.pack("<Q", self.nServices)
            + b"\x00" * 10
            + b"\xff" * 2
            + socket.inet_aton(self.ip)
            + struct.pack(">H", self.port)
        )
    return (
        struct.pack("<Q", self.nServices)
        + b"\x00" * 10
        + b"\xff" * 2
        + socket.inet_aton(self.ip)
        + struct.pack(">H", self.port)
    )
This revision is now accepted and ready to land.Thu, Mar 20, 09:42