Skip to content

Commit

Permalink
Fix some TODOs in header.rs (#2981)
Browse files Browse the repository at this point in the history
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
tomaka and mergify[bot] committed Nov 11, 2022
1 parent a203816 commit 621e0f5
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@
//! assert_eq!(reencoded, scale_encoded_header);
//! ```

// TODO: consider rewriting the encoding/decoding into a more legible style
// TODO: consider nom for decoding
// TODO: consider rewriting the encoding into a more legible style

use crate::{trie, util};

Expand Down Expand Up @@ -137,7 +136,6 @@ pub fn decode(scale_encoded: &[u8], block_number_bytes: usize) -> Result<HeaderR
///
/// Contrary to [`decode`], doesn't return an error if the slice is too long but returns the
/// remainder.
// TODO: use block_number_bytes
pub fn decode_partial(
mut scale_encoded: &[u8],
block_number_bytes: usize,
Expand Down Expand Up @@ -265,9 +263,15 @@ impl<'a> HeaderRef<'a> {

/// Equivalent to [`HeaderRef::scale_encoding`] but returns the data in a `Vec`.
pub fn scale_encoding_vec(&self, block_number_bytes: usize) -> Vec<u8> {
// TODO: Vec::with_capacity?
// We assume that an average header should be less than this size.
// At the time of writing of this comment, the average Westend/Polkadot/Kusama block
// header is 288 bytes, and the average parachain block is 186 bytes. Some blocks
// (the epoch transition blocks in particular) have a much larger header, but considering
// that they are the vast minority we don't care so much about that.
const CAP: usize = 1024;

self.scale_encoding(block_number_bytes)
.fold(Vec::new(), |mut a, b| {
.fold(Vec::with_capacity(CAP), |mut a, b| {
a.extend_from_slice(b.as_ref());
a
})
Expand Down

0 comments on commit 621e0f5

Please sign in to comment.