-
Notifications
You must be signed in to change notification settings - Fork 49
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
Represent XDR strings as bytes #48
Conversation
XDR strings are equivalent to XDR opaque fields. An XDR string is a variable length list of bytes with no restrictions on the contents of the bytes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not very familiar with this code, but the change makes sense. I don't see any other way we can ensure strings do not become mutated in Java by changing encodings.
One thing below that I think we're missing.
@tamirms can you attach an example of how a generated String class will look like? It's hard to understand from the generator. I think we should add a |
checkout lightsail-network/java-stellar-sdk#259 to see what the generated output looks like. The Java sdk converts the bytes to a string. If the bytes are not valid utf 8 the Java String class will convert the bytes to valid utf8. |
@tomerweller please check the most recent commit |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks really good. The XdrString
is a great idea. 👏
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call on introducing a new XDR String class that handles encoding/decoding. (Much more elegant that my proposed solution)
I think this is a great contract, and also very readable code. So this is approved.
One small comment inline, but that's a matter of style and up to you.
|
||
public class XdrString implements XdrElement { | ||
private byte[] bytes; | ||
private String text; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not a fan of keeping the same state in two variables. I'd get rid of the text
member and just keep the bytes
, as that's the closest representation to what's on the wire.
|
||
@Override | ||
public String toString() { | ||
return this.text; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Following on the previous comment, you can construct a new String here from bytes
, instead of keeping text
around.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! Tested in java-stellar-sdk (after changing the code to be compatible) and works like a charm.
@Override | ||
public void encode(XdrDataOutputStream stream) throws IOException { | ||
stream.writeInt(this.bytes.length); | ||
stream.write(this.bytes, 0, this.bytes.length); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably add a maxSize
check here too.
According to stellar/go#2022 XDR strings are equivalent to XDR opaque fields. The Java XDR code generator represents XDR strings as Java Strings. This representation is incorrect because an XDR string has no restrictions on the byte format. It is possible for an XDR string to contain invalid ASCII or invalid unicode bytes. The safest representation of an XDR string is a byte array.