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

refactor(p2p): JSON encoding to use protobuf instead #2061

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

LePremierHomme
Copy link
Contributor

@LePremierHomme LePremierHomme commented Jan 11, 2021

Closing opendexnetwork/opendex.network#34.

Changes:

  • p2p packets' checksum to utilize their protobuf encoding instead of generating a JSON string.
  • SessionInit p2p packet fields needed for signing to be encoded via the same protobuf's scheme of the entire packet, instead of JSON. Using a subset of the fields isn't a problem with protobuf - the used fields will be encoded while the remaining ones won't exist on the binary representation.

BREAKING CHANGE: JSON encoding to use protobuf instead

@LePremierHomme LePremierHomme requested review from a user and sangaman January 11, 2021 17:56
@LePremierHomme LePremierHomme self-assigned this Jan 12, 2021
@raladev raladev self-requested a review January 12, 2021 15:29
raladev
raladev previously approved these changes Jan 12, 2021
Copy link
Collaborator

@sangaman sangaman left a comment

Choose a reason for hiding this comment

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

Looks good, just a few questions.

lib/p2p/packets/Packet.ts Outdated Show resolved Hide resolved
@@ -215,6 +214,7 @@
"grpc-tools": "^1.10.0",
"grpc_tools_node_protoc_ts": "^5.1.0",
"jest": "^26.6.3",
"json-stable-stringify": "^1.0.1",
Copy link
Collaborator

Choose a reason for hiding this comment

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

It looks like this is only being used for the Parser.spec.ts suite, but it doesn't make much sense to me to use it for tests when it's not used in the actual functioning of xud. I think we can just change the line that expects the stringify outputs to match to instead check that the packet checksums match, and then get rid of this package entirely, right? If you'd like though I could do this in a follow-up PR instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was thinking of this as well, but there’s a small problem - protobuf scheme (in which checksum now relies on) is fixed (unlike JSON), and so it will only compare between the define fields, while in some of the negative test cases these are just a subset, because an additional (undefined in protobuf) field is added (e.g., reqId).
Also, there’s a small benefit in comparing between the constructed POJO, and not the binary representation, which is an intermediate stage. I tried to use the built-inJSON.stringify, but the output isn’t deterministic and so many tests fail.

So the usage of stringify here isn’t related to its previous usage in xud. It’s just a tool for comparing between nested objects reliably.
Let me know what you agree. If not, I can just change some of the negative tests.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks for the explanation. That makes sense and I think it's OK to leave it for now, it's not a big deal to have as a dev dependency. I might take another look at this test suite some time to see if there's a sensible way to run it without this stringify function but no need to hold this PR up for that.

Copy link
Collaborator

@sangaman sangaman left a comment

Choose a reason for hiding this comment

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

Looks good, just need to squash the commit and merge whenever we are ready for the breaking change this introduces.

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.

3 participants