-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Embed runtime version as a custom section #8688
Changes from 16 commits
217a874
259d06a
03eea2b
d4a3038
ceced97
422363a
76f6c37
53ffc97
866b13f
0f13caa
93f2036
78fb99e
84c63c0
d0ec418
1621d6c
689116a
bc41d06
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -27,6 +27,17 @@ pub struct RuntimeBlob { | |||||||||
} | ||||||||||
|
||||||||||
impl RuntimeBlob { | ||||||||||
/// Create `RuntimeBlob` from the given wasm code. Will attempt to decompress the code before | ||||||||||
/// deserializing it. | ||||||||||
/// | ||||||||||
/// See [`sp_maybe_compressed_blob`] for details about decompression. | ||||||||||
pub fn uncompress_if_needed(wasm_code: &[u8]) -> Result<Self, WasmError> { | ||||||||||
use sp_maybe_compressed_blob::CODE_BLOB_BOMB_LIMIT; | ||||||||||
let wasm_code = sp_maybe_compressed_blob::decompress(wasm_code, CODE_BLOB_BOMB_LIMIT) | ||||||||||
.map_err(|e| WasmError::Other(format!("Decompression error: {:?}", e)))?; | ||||||||||
Self::new(&wasm_code) | ||||||||||
} | ||||||||||
|
||||||||||
/// Create `RuntimeBlob` from the given wasm code. | ||||||||||
/// | ||||||||||
/// Returns `Err` if the wasm code cannot be deserialized. | ||||||||||
|
@@ -85,9 +96,24 @@ impl RuntimeBlob { | |||||||||
}) | ||||||||||
} | ||||||||||
|
||||||||||
/// Scans the wasm blob for the first section with the name that matches the given. Returns the | ||||||||||
/// contents of the custom section if found or `None` otherwise. | ||||||||||
pub fn custom_section_contents(&self, section_name: &str) -> Option<&[u8]> { | ||||||||||
self.raw_module | ||||||||||
.custom_sections() | ||||||||||
.filter(|cs| cs.name() == section_name) | ||||||||||
.next() | ||||||||||
.map(|cs| cs.payload()) | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not entirely sure how would that work. Currently, this code takes an iterator of custom sections, leaves only sections that we are interested in, takes the first one which gives us an option and then we narrow the contents of this section to only its payload. But in the proposed version the result seems to be not an option There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe I can introduce you to: https://doc.rust-lang.org/std/primitive.bool.html#method.then ;) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TIL. This however misses the point, if I understand you correctly. The problem is the expression in the proposed code is typed as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
We can simplify the code to We could use a |
||||||||||
} | ||||||||||
|
||||||||||
/// Consumes this runtime blob and serializes it. | ||||||||||
pub fn serialize(self) -> Vec<u8> { | ||||||||||
serialize(self.raw_module) | ||||||||||
.expect("serializing into a vec should succeed; qed") | ||||||||||
} | ||||||||||
|
||||||||||
/// Destructure this structure into the underlying parity-wasm Module. | ||||||||||
pub fn into_inner(self) -> RawModule { | ||||||||||
self.raw_module | ||||||||||
} | ||||||||||
} |
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.
Maybe
new
should do this? IMHO the name indicates that this is done on an already existing reference, while actually we create an instance with this methodThere 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.
You mean because it's
uncompress
but returnsSelf
? I don't think it is unheard of, even though thatnew_*
,from_*
orwith_*
are the most common. For example, there isTcpStream::connect
which returnsSelf
.I also had an idea about putting it in
new
, but in the end I decided to go withuncompress
to indicate at the call sites , mainly in sc-executor, that the code may be compressed at that point. If compression were hidden innew
, it would be even harder to find out.I do agree that it is a small thing and perhaps is not that useful, so I can tuck this logic under
new
if you wish so.