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

Allow decoding Events containing BitVecs #408

Merged
merged 4 commits into from
Jan 27, 2022
Merged

Allow decoding Events containing BitVecs #408

merged 4 commits into from
Jan 27, 2022

Conversation

jsdw
Copy link
Collaborator

@jsdw jsdw commented Jan 25, 2022

Also a little refactoring to make it easier to test the core decode logic, and remove the need to re-encode.

closes #405

@jsdw
Copy link
Collaborator Author

jsdw commented Jan 25, 2022

I wonder whether it's worth trying to simplify the decode logic; if bitvec contains its byte length, I could just ignore the order and store things entirely since I no longer need to try and re-encode it (I haven't checked yet, but should do so before committing to this).

@jsdw
Copy link
Collaborator Author

jsdw commented Jan 26, 2022

I had a look into the encode impl and reworked the handling to cover more cases and not care about ordering

@jsdw jsdw requested review from ascjones and a team January 26, 2022 11:51
Copy link
Contributor

@ascjones ascjones left a comment

Choose a reason for hiding this comment

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

Awesome!

decode_and_consume_type(type_id, &self.metadata.runtime_metadata().types, input)?;
// count how many bytes were consumed based on remaining length:
let consumed_len = all_bytes.len() - input.len();
// move those consumed bytes to the output vec unaltered:
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice, much more efficient 💪


// Given a type Id and a type registry, attempt to consume the bytes
// corresponding to that type from our input.
fn decode_and_consume_type(
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

TODO: If we are happy and merge this, we could create a new "good first issue" to review the tests, perhaps adding some like the bitvec ones to test this method more specifically?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes this would be a good method to unit test


// We just need to consume the correct number of bytes. Roughly, we encode this
// as a Compact<u32> length, and then a slice of T of that length, where T is the
// bit store type. So, we ignore the bit order and only care that the bit store type
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// bit store type. So, we ignore the bit order and only care that the bit store type
// bit store type. We ignore the bit order and only care that the bit store type

@@ -404,6 +449,19 @@ mod tests {
EventsDecoder::<DefaultConfig>::new(metadata)
}

fn decode_and_consume_type_consumes_all_bytes<
Copy link
Member

Choose a reason for hiding this comment

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

👍

@jsdw jsdw merged commit d1494b5 into master Jan 27, 2022
@jsdw jsdw deleted the jsdw-bitvec-decoding branch January 27, 2022 16:02
0623forbidden pushed a commit to DEIPworld/substrate-subxt that referenced this pull request Feb 15, 2022
* Handle bitvec decoding

* clippy

* rework bitvec decoding; don't care about order and can handle other store primitives

* cargo fmt
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.

Handle BitVec decoding in Events
3 participants