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

Multiaddr refactor #217

Merged
merged 17 commits into from
Jan 28, 2022
Merged

Conversation

Nashatyrev
Copy link
Collaborator

@Nashatyrev Nashatyrev commented Jan 25, 2022

  • Refactor Multiaddr to be represented by a list of MultiaddrComponent instead of Pair
  • Refactor Protocol enum implementation. Add component value validation
  • Refactor/clean up Multiaddr methods/constructors
  • Piggy tailing minor NoiseXXSecureChannel update to make it more extendable

Note: breaking API changes

src/main/kotlin/io/libp2p/core/Network.kt Outdated Show resolved Hide resolved
src/main/kotlin/io/libp2p/core/multiformats/Protocol.kt Outdated Show resolved Hide resolved
src/main/kotlin/io/libp2p/core/multiformats/Protocol.kt Outdated Show resolved Hide resolved
Comment on lines 129 to 132
private val NO_PARSER: (Protocol, String) -> ByteArray = { protocol, _ ->
throw IllegalArgumentException("No value serializer for protocol $protocol")
}
private val NO_DESERIALIZER: (Protocol, ByteArray) -> String = { protocol, _ ->
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm confused about the PARSER vs DESERIALIZER here. In my mind a PARSER goes from String to internal representation and a SERIALIZER goes from internal to String. A PARSER and DESERIALIZER would then be the same thing.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Oh, that was also breaking my mind.
I used to term 'serializer/deserializer' for converting 'bytes' from/to internal representation
'parser' in my mind is 'string => internal representation' converter.
If we assume that 'bytes' is internal form, then it should be something opposite to 'parser'. 'Stringifier', 'Composer', 'Unparser'? What term do you think is the most adequate?

Just don't want to confuse it with the 'binary serializer'. If take the Multiaddr class for example then it clearly has 3 representations: binary, string and internal. And they are all distinct

Copy link
Collaborator Author

@Nashatyrev Nashatyrev Jan 27, 2022

Choose a reason for hiding this comment

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

In other words: what should be the name of a substance who does toString()? 😄

Copy link
Contributor

Choose a reason for hiding this comment

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

oof 3 meaningful states makes it hard. While it's not valid English, Stringifier is probably a good name as it's very clear what it does. Or possibly parser becomes STRING_TO_BYTES and the current deserialiser becomes BYTES_TO_STRING?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I've stick with stringifier: I found a number of existing classes with this name. Even IDEA vocabulary knows this term! 🎉
STRING_TO_BYTES sounds more rigorously but it would ideally be STRING_TO_BYTES_CONVERTER which is already too long

addr.toByteArray(StandardCharsets.UTF_8)
}
private val UTF8_DESERIALIZER: (Protocol, ByteArray) -> String = { _, bytes ->
String(bytes, StandardCharsets.UTF_8)
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this will reject invalid characters (just replace them with ?). If we want to fully validate we could use StandardCharsets.UTF_8.newDecoder().onMalformedInput(CodingErrorAction.REPORT).onUnmappableCharacter(CodingErrorAction.REPORT).decode() but that adds a bunch of complexity and I'm not entirely sure it's worth it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Right, better be safe. However I've added a UTF-8 validator instead: 6375f2a

Copy link
Contributor

@ajsutton ajsutton left a comment

Choose a reason for hiding this comment

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

LGTM. Just the naming of parsers/deserializers/whatevers - not sure there's a "right" answer there so go with what you think and we can always change it if we think of something better later.

Comment on lines 129 to 132
private val NO_PARSER: (Protocol, String) -> ByteArray = { protocol, _ ->
throw IllegalArgumentException("No value serializer for protocol $protocol")
}
private val NO_DESERIALIZER: (Protocol, ByteArray) -> String = { protocol, _ ->
Copy link
Contributor

Choose a reason for hiding this comment

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

oof 3 meaningful states makes it hard. While it's not valid English, Stringifier is probably a good name as it's very clear what it does. Or possibly parser becomes STRING_TO_BYTES and the current deserialiser becomes BYTES_TO_STRING?

@Nashatyrev Nashatyrev merged commit 01b5473 into libp2p:develop Jan 28, 2022
@Nashatyrev Nashatyrev deleted the feature/multiaddr-refactor branch February 2, 2022 12:09
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.

2 participants