Skip to content

Signing bytes need to encoded with encode_length_delimited#69

Merged
liamsi merged 1 commit intocometbft:lite_impl_simple_merkle_mergedfrom
yihuang:fix-vote-signing-byte
Nov 19, 2019
Merged

Signing bytes need to encoded with encode_length_delimited#69
liamsi merged 1 commit intocometbft:lite_impl_simple_merkle_mergedfrom
yihuang:fix-vote-signing-byte

Conversation

@yihuang
Copy link
Contributor

@yihuang yihuang commented Nov 18, 2019

So we have identical signing bytes as golang version to verify signatures.

Copy link
Contributor

@liamsi liamsi left a comment

Choose a reason for hiding this comment

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

Thanks!

fn sign_bytes(&self) -> Vec<u8> {
AminoMessage::bytes_vec(&self.vote.to_owned())
let mut buf = Vec::with_capacity(self.vote.encoded_len() + 4);
self.vote.encode_length_delimited(&mut buf).unwrap();
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks! You're right, this has to be length_delimited!


fn sign_bytes(&self) -> Vec<u8> {
AminoMessage::bytes_vec(&self.vote.to_owned())
let mut buf = Vec::with_capacity(self.vote.encoded_len() + 4);
Copy link
Contributor

Choose a reason for hiding this comment

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

4 bytes should be correct but it would still be cleaner to take the length and its encoded length. Prost provides a method for that. As far as I remember it was called encoded_len_varint. Let me double check.

Copy link
Contributor

@liamsi liamsi left a comment

Choose a reason for hiding this comment

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

See suggestion (and comment). Otherwise LGTM


fn sign_bytes(&self) -> Vec<u8> {
AminoMessage::bytes_vec(&self.vote.to_owned())
let mut buf = Vec::with_capacity(self.vote.encoded_len() + 4);
Copy link
Contributor

@liamsi liamsi Nov 18, 2019

Choose a reason for hiding this comment

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

This should work (given the right imports):

Suggested change
let mut buf = Vec::with_capacity(self.vote.encoded_len() + 4);
let vote_len = self.vote.encoded_len();
let len_delimiter_len =
prost_amino::encoding::encoded_len_varint(vote_len.try_into().unwrap());
let mut buf = Vec::with_capacity(self.vote.encoded_len() + len_delimiter_len);

I think it would be better to add a method to the AminoMessage trait. Sth. like length_delimited_bytes_vec. But OK to merge with above changes 👍 (you would still need to fix the imports).

@yihuang yihuang force-pushed the fix-vote-signing-byte branch 2 times, most recently from 41431be to bc4ff7c Compare November 18, 2019 22:19
@yihuang yihuang requested a review from liamsi November 18, 2019 22:19
Copy link
Contributor

@liamsi liamsi left a comment

Choose a reason for hiding this comment

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

🎉 Thanks!

Self: Sized,
{
let len = self.encoded_len();
let mut res = Vec::with_capacity(len + encoded_len_varint(len as u64));
Copy link
Contributor

Choose a reason for hiding this comment

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

Awesome, thanks!

Why did you decide against len.try_into().unwrap() (as suggested in #69 (comment)) instead of len as u64? I think, the latter can silently overflow while the first would panic.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice trick, re-commited.

Copy link
Contributor

Choose a reason for hiding this comment

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

Awesome, thanks!

@yihuang yihuang force-pushed the fix-vote-signing-byte branch from bc4ff7c to 6ca48b5 Compare November 19, 2019 09:20
@liamsi liamsi merged commit ef7410c into cometbft:lite_impl_simple_merkle_merged Nov 19, 2019
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