Skip to content

Commit be7e9a5

Browse files
authored
docs: Add docs to revm-bytecode crate (#2108)
* docs: Bytecode crate * fix test * fmt and doc * Doc * add comments for EOF
1 parent b3300a1 commit be7e9a5

File tree

22 files changed

+283
-164
lines changed

22 files changed

+283
-164
lines changed

bins/revme/src/cmd/bytecode.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ impl Cmd {
3939
/// Runs statetest command.
4040
pub fn run(&self) {
4141
let container_kind = if self.eof_initcode {
42-
Some(CodeType::ReturnContract)
42+
Some(CodeType::Initcode)
4343
} else if self.eof_runtime {
44-
Some(CodeType::ReturnOrStop)
44+
Some(CodeType::Runtime)
4545
} else {
4646
None
4747
};

bins/revme/src/cmd/eofvalidation.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ pub fn run_test(path: &Path) -> Result<(), Error> {
7777
}
7878
test_sum += 1;
7979
let kind = if test_vector.container_kind.is_some() {
80-
Some(CodeType::ReturnContract)
80+
Some(CodeType::Initcode)
8181
} else {
82-
Some(CodeType::ReturnOrStop)
82+
Some(CodeType::Runtime)
8383
};
8484
// In future this can be generalized to cover multiple forks, Not just Osaka.
8585
let Some(test_result) = test_vector.results.get("Osaka") else {

crates/bytecode/src/bytecode.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use core::fmt::Debug;
77
use primitives::{keccak256, Address, Bytes, B256, KECCAK_EMPTY};
88
use std::sync::Arc;
99

10-
/// State of the [`Bytecode`] analysis
10+
/// Main bytecode structure with all variants.
1111
#[derive(Clone, Debug, PartialEq, Eq, Hash, Ord, PartialOrd)]
1212
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
1313
pub enum Bytecode {
@@ -81,7 +81,7 @@ impl Bytecode {
8181
///
8282
/// # Panics
8383
///
84-
/// Panics if bytecode is in incorrect format.
84+
/// Panics if bytecode is in incorrect format. If you want to handle errors use [`Self::new_raw_checked`].
8585
#[inline]
8686
pub fn new_raw(bytecode: Bytes) -> Self {
8787
Self::new_raw_checked(bytecode).expect("Expect correct EOF bytecode")
@@ -114,15 +114,10 @@ impl Bytecode {
114114

115115
/// Create new checked bytecode.
116116
///
117-
/// # Safety
117+
/// # Panics
118118
///
119-
/// Bytecode needs to end with `STOP` (`0x00`) opcode as checked bytecode assumes
120-
/// that it is safe to iterate over bytecode without checking lengths.
121-
pub unsafe fn new_analyzed(
122-
bytecode: Bytes,
123-
original_len: usize,
124-
jump_table: JumpTable,
125-
) -> Self {
119+
/// For possible panics see [`LegacyAnalyzedBytecode::new`].
120+
pub fn new_analyzed(bytecode: Bytes, original_len: usize, jump_table: JumpTable) -> Self {
126121
Self::LegacyAnalyzed(LegacyAnalyzedBytecode::new(
127122
bytecode,
128123
original_len,
@@ -156,7 +151,7 @@ impl Bytecode {
156151
self.bytes_ref().clone()
157152
}
158153

159-
/// Returns bytes.
154+
/// Returns raw bytes reference.
160155
#[inline]
161156
pub fn bytes_ref(&self) -> &Bytes {
162157
match self {
@@ -166,13 +161,13 @@ impl Bytecode {
166161
}
167162
}
168163

169-
/// Returns bytes slice.
164+
/// Returns raw bytes slice.
170165
#[inline]
171166
pub fn bytes_slice(&self) -> &[u8] {
172167
self.bytes_ref()
173168
}
174169

175-
/// Returns a reference to the original bytecode.
170+
/// Returns the original bytecode.
176171
#[inline]
177172
pub fn original_bytes(&self) -> Bytes {
178173
match self {

crates/bytecode/src/eof.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
mod body;
2+
mod code_info;
23
mod decode_helpers;
34
mod header;
45
pub mod printer;
5-
mod types_section;
66
pub mod verification;
77

88
pub use body::EofBody;
9+
pub use code_info::CodeInfo;
910
pub use header::EofHeader;
10-
pub use types_section::TypesSection;
1111
pub use verification::*;
1212

1313
use core::cmp::min;
@@ -39,7 +39,7 @@ impl Default for Eof {
3939
fn default() -> Self {
4040
let body = EofBody {
4141
// Types section with zero inputs, zero outputs and zero max stack size.
42-
types_section: vec![TypesSection::default()],
42+
code_info: vec![CodeInfo::default()],
4343
code_section: vec![1],
4444
// One code section with a STOP byte.
4545
code: Bytes::from_static(&[0x00]),
@@ -136,10 +136,10 @@ pub enum EofDecodeError {
136136
MissingBodyWithoutData,
137137
/// Body size is more than specified in the header
138138
DanglingData,
139-
/// Invalid types section data
140-
InvalidTypesSection,
141-
/// Invalid types section size
142-
InvalidTypesSectionSize,
139+
/// Invalid code info data
140+
InvalidCodeInfo,
141+
/// Invalid code info size
142+
InvalidCodeInfoSize,
143143
/// Invalid EOF magic number
144144
InvalidEOFMagicNumber,
145145
/// Invalid EOF version
@@ -154,8 +154,8 @@ pub enum EofDecodeError {
154154
InvalidDataKind,
155155
/// Invalid kind after code
156156
InvalidKindAfterCode,
157-
/// Mismatch of code and types sizes
158-
MismatchCodeAndTypesSize,
157+
/// Mismatch of code and info sizes
158+
MismatchCodeAndInfoSize,
159159
/// There should be at least one size
160160
NonSizes,
161161
/// Missing size
@@ -178,16 +178,16 @@ impl fmt::Display for EofDecodeError {
178178
Self::MissingInput => "Short input while processing EOF",
179179
Self::MissingBodyWithoutData => "Short body while processing EOF",
180180
Self::DanglingData => "Body size is more than specified in the header",
181-
Self::InvalidTypesSection => "Invalid types section data",
182-
Self::InvalidTypesSectionSize => "Invalid types section size",
181+
Self::InvalidCodeInfo => "Invalid types section data",
182+
Self::InvalidCodeInfoSize => "Invalid types section size",
183183
Self::InvalidEOFMagicNumber => "Invalid EOF magic number",
184184
Self::InvalidEOFVersion => "Invalid EOF version",
185185
Self::InvalidTypesKind => "Invalid number for types kind",
186186
Self::InvalidCodeKind => "Invalid number for code kind",
187187
Self::InvalidTerminalByte => "Invalid terminal code",
188188
Self::InvalidDataKind => "Invalid data kind",
189189
Self::InvalidKindAfterCode => "Invalid kind after code",
190-
Self::MismatchCodeAndTypesSize => "Mismatch of code and types sizes",
190+
Self::MismatchCodeAndInfoSize => "Mismatch of code and types sizes",
191191
Self::NonSizes => "There should be at least one size",
192192
Self::ShortInputForSizes => "Missing size",
193193
Self::ZeroSize => "Size cant be zero",

crates/bytecode/src/eof/body.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{Eof, EofDecodeError, EofHeader, TypesSection};
1+
use super::{CodeInfo, Eof, EofDecodeError, EofHeader};
22
use primitives::Bytes;
33
use std::vec::Vec;
44

@@ -11,7 +11,7 @@ use std::vec::Vec;
1111
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
1212
pub struct EofBody {
1313
/// Code information
14-
pub types_section: Vec<TypesSection>,
14+
pub code_info: Vec<CodeInfo>,
1515
/// Index of the last byte of each code section
1616
pub code_section: Vec<usize>,
1717
pub code: Bytes,
@@ -37,7 +37,7 @@ impl EofBody {
3737
pub fn into_eof(self) -> Eof {
3838
let mut prev_value = 0;
3939
let header = EofHeader {
40-
types_size: self.types_section.len() as u16 * 4,
40+
types_size: self.code_info.len() as u16 * 4,
4141
code_sizes: self
4242
.code_section
4343
.iter()
@@ -76,8 +76,8 @@ impl EofBody {
7676

7777
/// Encodes this body into the given buffer.
7878
pub fn encode(&self, buffer: &mut Vec<u8>) {
79-
for types_section in &self.types_section {
80-
types_section.encode(buffer);
79+
for code_info in &self.code_info {
80+
code_info.encode(buffer);
8181
}
8282

8383
buffer.extend_from_slice(&self.code);
@@ -108,9 +108,9 @@ impl EofBody {
108108

109109
let mut types_input = &input[header_len..];
110110
for _ in 0..header.types_count() {
111-
let (types_section, local_input) = TypesSection::decode(types_input)?;
111+
let (code_info, local_input) = CodeInfo::decode(types_input)?;
112112
types_input = local_input;
113-
body.types_section.push(types_section);
113+
body.code_info.push(code_info);
114114
}
115115

116116
// Extract code section

crates/bytecode/src/eof/types_section.rs renamed to crates/bytecode/src/eof/code_info.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const EOF_NON_RETURNING_FUNCTION: u8 = 0x80;
1010
/// Types section that contains stack information for matching code section
1111
#[derive(Debug, Clone, Default, Hash, PartialEq, Eq, Copy, PartialOrd, Ord)]
1212
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
13-
pub struct TypesSection {
13+
pub struct CodeInfo {
1414
/// `inputs` - 1 byte - `0x00-0x7F`
1515
///
1616
/// Number of stack elements the code section consumes
@@ -25,8 +25,8 @@ pub struct TypesSection {
2525
pub max_stack_size: u16,
2626
}
2727

28-
impl TypesSection {
29-
/// Returns new `TypesSection` with the given inputs, outputs, and max_stack_size.
28+
impl CodeInfo {
29+
/// Returns new `CodeInfo` with the given inputs, outputs, and max_stack_size.
3030
pub fn new(inputs: u8, outputs: u8, max_stack_size: u16) -> Self {
3131
Self {
3232
inputs,
@@ -72,10 +72,10 @@ impl TypesSection {
7272
/// Validates the section.
7373
pub fn validate(&self) -> Result<(), EofDecodeError> {
7474
if self.inputs > 0x7f || self.outputs > 0x80 || self.max_stack_size > 0x03FF {
75-
return Err(EofDecodeError::InvalidTypesSection);
75+
return Err(EofDecodeError::InvalidCodeInfo);
7676
}
7777
if self.inputs as u16 > self.max_stack_size {
78-
return Err(EofDecodeError::InvalidTypesSection);
78+
return Err(EofDecodeError::InvalidCodeInfo);
7979
}
8080
Ok(())
8181
}

crates/bytecode/src/eof/decode_helpers.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
use super::EofDecodeError;
22

3-
/// Consumes a u8 from the input.
3+
/// Consumes a single byte from the input slice and returns a tuple containing the remaining input slice
4+
/// and the consumed byte as a u8.
5+
///
6+
/// Returns `EofDecodeError::MissingInput` if the input slice is empty.
47
#[inline]
58
pub(crate) fn consume_u8(input: &[u8]) -> Result<(&[u8], u8), EofDecodeError> {
69
if input.is_empty() {
@@ -10,6 +13,8 @@ pub(crate) fn consume_u8(input: &[u8]) -> Result<(&[u8], u8), EofDecodeError> {
1013
}
1114

1215
/// Consumes a u16 from the input.
16+
///
17+
/// Returns `EofDecodeError::MissingInput` if the input slice is less than 2 bytes.
1318
#[inline]
1419
pub(crate) fn consume_u16(input: &[u8]) -> Result<(&[u8], u16), EofDecodeError> {
1520
if input.len() < 2 {

crates/bytecode/src/eof/header.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use super::{
44
};
55
use std::vec::Vec;
66

7-
/// EOF Header containing
7+
/// EOF header structure that contains section sizes and metadata
88
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, Ord, PartialOrd)]
99
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
1010
pub struct EofHeader {
@@ -22,14 +22,14 @@ pub struct EofHeader {
2222
pub container_sizes: Vec<u16>,
2323
/// EOF data size
2424
pub data_size: u16,
25-
/// Sum code sizes
25+
/// Sum of code sizes
2626
pub sum_code_sizes: usize,
27-
/// Sum container sizes
27+
/// Sum of container sizes
2828
pub sum_container_sizes: usize,
2929
}
3030

3131
const KIND_TERMINAL: u8 = 0;
32-
const KIND_TYPES: u8 = 1;
32+
const KIND_CODE_INFO: u8 = 1;
3333
const KIND_CODE: u8 = 2;
3434
const KIND_CONTAINER: u8 = 3;
3535
const KIND_DATA: u8 = 4;
@@ -113,7 +113,7 @@ impl EofHeader {
113113
// `version` 1 byte 0x01 EOF version
114114
buffer.push(0x01);
115115
// `kind_types` 1 byte 0x01 kind marker for types size section
116-
buffer.push(KIND_TYPES);
116+
buffer.push(KIND_CODE_INFO);
117117
// `types_size` 2 bytes 0x0004-0xFFFF
118118
buffer.extend_from_slice(&self.types_size.to_be_bytes());
119119
// `kind_code` 1 byte 0x02 kind marker for code size section
@@ -159,8 +159,8 @@ impl EofHeader {
159159
}
160160

161161
// `kind_types` 1 byte 0x01 kind marker for types size section
162-
let (input, kind_types) = consume_u8(input)?;
163-
if kind_types != KIND_TYPES {
162+
let (input, kind_code_info) = consume_u8(input)?;
163+
if kind_code_info != KIND_CODE_INFO {
164164
return Err(EofDecodeError::InvalidTypesKind);
165165
}
166166

@@ -170,12 +170,12 @@ impl EofHeader {
170170
header.types_size = types_size;
171171

172172
if header.types_size % 4 != 0 {
173-
return Err(EofDecodeError::InvalidTypesSection);
173+
return Err(EofDecodeError::InvalidCodeInfo);
174174
}
175175

176176
// `kind_code` 1 byte 0x02 kind marker for code size section
177-
let (input, kind_types) = consume_u8(input)?;
178-
if kind_types != KIND_CODE {
177+
let (input, kind_code) = consume_u8(input)?;
178+
if kind_code != KIND_CODE {
179179
return Err(EofDecodeError::InvalidCodeKind);
180180
}
181181

@@ -192,7 +192,7 @@ impl EofHeader {
192192
}
193193

194194
if sizes.len() != (types_size / 4) as usize {
195-
return Err(EofDecodeError::MismatchCodeAndTypesSize);
195+
return Err(EofDecodeError::MismatchCodeAndInfoSize);
196196
}
197197

198198
header.code_sizes = sizes;

0 commit comments

Comments
 (0)