Skip to content

Commit 6bedee2

Browse files
committed
documentation
1 parent d6aff66 commit 6bedee2

File tree

22 files changed

+210
-1
lines changed

22 files changed

+210
-1
lines changed

crates/core/src/error.rs

+8
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
// TODO: after all errors are fixed, remove most instances of Generic for
22
// specific errors (e.g. ParseError, FilesystemError, etc.)
3+
/// Error type for the Core module
34
#[derive(Debug, thiserror::Error)]
45
pub enum Error {
6+
/// Error when serializing or deserializing JSON data
57
#[error("Json error: {0}")]
68
SerdeError(#[from] serde_json::Error),
9+
/// Error when accessing data out of bounds
710
#[error("BoundsError")]
811
BoundsError,
12+
/// Error when decoding data
913
#[error("DecodeError")]
1014
DecodeError,
15+
/// Error when interacting with an RPC endpoint
1116
#[error("RPCError: {0}")]
1217
RpcError(String),
18+
/// Generic error with a message
1319
#[error("Error: {0}")]
1420
Generic(String),
21+
/// Error when transforming data structures
1522
#[error("TransposeError: {0}")]
1623
TransposeError(String),
24+
/// Error when parsing data
1725
#[error("Parse error: {0}")]
1826
ParseError(String),
1927
}

crates/core/src/lib.rs

+8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1+
//! The Core module serves as the central integration point for all Heimdall's
2+
//! functionality, providing access to various analysis tools for Ethereum smart contracts.
3+
//!
4+
//! This module re-exports the public interfaces of all the tool-specific crates,
5+
//! making it easier to use Heimdall's capabilities in other projects.
6+
7+
/// Error types for the core module
18
pub mod error;
29

10+
// Re-export all tool-specific modules
311
pub use heimdall_cfg;
412
pub use heimdall_decoder;
513
pub use heimdall_decompiler;

crates/decode/src/core/mod.rs

+20
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,37 @@ use crate::{
2323
};
2424

2525
#[derive(Debug, Clone)]
26+
/// Result of a successful decode operation
27+
///
28+
/// Contains the decoded function signature and parameters, as well as
29+
/// a trace factory for displaying the result in a formatted way.
2630
pub struct DecodeResult {
31+
/// The resolved function with its decoded inputs
2732
pub decoded: ResolvedFunction,
2833
_trace: TraceFactory,
2934
}
3035

3136
impl DecodeResult {
37+
/// Displays the decoded function signature and parameters in a formatted way
3238
pub fn display(&self) {
3339
self._trace.display();
3440
}
3541
}
3642

43+
/// Decodes EVM calldata into human-readable function signatures and parameters
44+
///
45+
/// This function attempts to identify the function being called based on the function
46+
/// selector in the calldata, and then decodes the remaining data according to the
47+
/// function's parameter types. If no matching function is found, it will attempt
48+
/// to infer the parameter types from the raw calldata.
49+
///
50+
/// # Arguments
51+
///
52+
/// * `args` - Configuration parameters for the decode operation
53+
///
54+
/// # Returns
55+
///
56+
/// A DecodeResult containing the resolved function and its decoded parameters
3757
pub async fn decode(mut args: DecodeArgs) -> Result<DecodeResult, Error> {
3858
let start_time = Instant::now();
3959

crates/decode/src/error.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
/// Error type for the Decoder module
12
#[derive(Debug, thiserror::Error)]
23
pub enum Error {
4+
/// Error when fetching data from external sources
35
#[error("Fetch error: {0}")]
46
FetchError(String),
7+
/// Generic internal error
58
#[error("Internal error: {0}")]
69
Eyre(#[from] eyre::Report),
10+
/// Error when accessing data out of bounds
711
#[error("Bounds error")]
812
BoundsError,
913
}

crates/decode/src/interfaces/args.rs

+12
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ use heimdall_config::parse_url_arg;
1010
after_help = "For more information, read the wiki: https://jbecker.dev/r/heimdall-rs/wiki",
1111
override_usage = "heimdall decode <TARGET> [OPTIONS]"
1212
)]
13+
/// Arguments for the decode operation
14+
///
15+
/// This struct contains all the configuration parameters needed to decode
16+
/// calldata into human-readable function signatures and parameters.
1317
pub struct DecodeArgs {
1418
/// The target to decode, either a transaction hash or string of bytes.
1519
#[clap(required = true)]
@@ -59,12 +63,20 @@ pub struct DecodeArgs {
5963
}
6064

6165
impl DecodeArgs {
66+
/// Retrieves the calldata from the specified target
67+
///
68+
/// This method fetches the calldata from a transaction hash, raw hex string,
69+
/// or directly from the provided target, depending on the configuration options.
70+
///
71+
/// # Returns
72+
/// The raw calldata as a vector of bytes
6273
pub async fn get_calldata(&self) -> Result<Vec<u8>> {
6374
get_calldata_from_target(&self.target, self.raw, &self.rpc_url).await
6475
}
6576
}
6677

6778
impl DecodeArgsBuilder {
79+
/// Creates a new DecodeArgsBuilder with default values
6880
pub fn new() -> Self {
6981
Self {
7082
target: Some(String::new()),

crates/decode/src/lib.rs

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
//! The Decode module provides functionality to decode EVM calldata into
2+
//! human-readable function signatures and parameters.
3+
//!
4+
//! This module enables the analysis of raw transaction data by identifying the
5+
//! function being called and properly parsing its parameters.
6+
7+
/// Error types for the decoder module
18
pub mod error;
29

310
mod core;

crates/decompile/src/core/mod.rs

+19
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,30 @@ use crate::{
3939
use tracing::{debug, info, warn};
4040

4141
#[derive(Debug, Clone)]
42+
/// Result of a successful decompile operation
43+
///
44+
/// Contains the decompiled source code (if requested) and the reconstructed ABI
45+
/// of the contract.
4246
pub struct DecompileResult {
47+
/// The decompiled source code in Solidity or Yul format (if requested)
4348
pub source: Option<String>,
49+
/// The reconstructed JSON ABI of the contract
4450
pub abi: JsonAbi,
4551
}
4652

53+
/// Decompiles EVM bytecode into higher-level Solidity-like code
54+
///
55+
/// This function analyzes the bytecode of a contract through symbolic execution
56+
/// and attempts to reconstruct the original source code or a functionally equivalent
57+
/// representation. It also generates an ABI for the contract.
58+
///
59+
/// # Arguments
60+
///
61+
/// * `args` - Configuration parameters for the decompile operation
62+
///
63+
/// # Returns
64+
///
65+
/// A DecompileResult containing the decompiled source (if requested) and the ABI
4766
pub async fn decompile(args: DecompilerArgs) -> Result<DecompileResult, Error> {
4867
// init
4968
let start_time = Instant::now();

crates/decompile/src/error.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
/// Error type for the Decompiler module
12
#[derive(Debug, thiserror::Error)]
23
pub enum Error {
4+
/// Error when fetching data from external sources
35
#[error("Fetch error: {0}")]
46
FetchError(String),
7+
/// Error during the disassembly process
58
#[error("Disassembly error: {0}")]
69
DisassemblyError(#[from] heimdall_disassembler::Error),
10+
/// Generic internal error
711
#[error("Internal error: {0}")]
812
Eyre(#[from] eyre::Report),
913
}

crates/decompile/src/interfaces/args.rs

+12
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ use heimdall_config::parse_url_arg;
1010
after_help = "For more information, read the wiki: https://jbecker.dev/r/heimdall-rs/wiki",
1111
override_usage = "heimdall decompile <TARGET> [OPTIONS]"
1212
)]
13+
/// Arguments for the decompile operation
14+
///
15+
/// This struct contains all the configuration parameters needed to decompile
16+
/// bytecode into human-readable source code and ABI.
1317
pub struct DecompilerArgs {
1418
/// The target to decompile, either a file, bytecode, contract address, or ENS name.
1519
#[clap(required = true)]
@@ -62,12 +66,20 @@ pub struct DecompilerArgs {
6266
}
6367

6468
impl DecompilerArgs {
69+
/// Retrieves the bytecode for the specified target
70+
///
71+
/// This method fetches the bytecode from a file, address, or directly from a hex string,
72+
/// depending on the target type provided in the arguments.
73+
///
74+
/// # Returns
75+
/// The raw bytecode as a vector of bytes
6576
pub async fn get_bytecode(&self) -> Result<Vec<u8>> {
6677
get_bytecode_from_target(&self.target, &self.rpc_url).await
6778
}
6879
}
6980

7081
impl DecompilerArgsBuilder {
82+
/// Creates a new DecompilerArgsBuilder with default values
7183
pub fn new() -> Self {
7284
Self {
7385
target: Some(String::new()),

crates/decompile/src/lib.rs

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
//! The Decompile module provides functionality to convert EVM bytecode
2+
//! into higher-level Solidity-like code.
3+
//!
4+
//! This module enables the analysis of compiled smart contracts by reconstructing
5+
//! the original source code structure, making bytecode more human-readable and
6+
//! understandable.
7+
8+
/// Error types for the decompiler module
19
mod error;
210

311
mod core;

crates/disassemble/src/core/mod.rs

+13
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,19 @@ use heimdall_common::utils::strings::encode_hex;
66
use heimdall_vm::core::opcodes::opcode_name;
77
use tracing::{debug, info};
88

9+
/// Disassembles EVM bytecode into readable assembly instructions
10+
///
11+
/// This function takes the bytecode of a contract and converts it into a string
12+
/// representation of the equivalent EVM assembly code. It handles special cases
13+
/// like PUSH operations which consume additional bytes as data.
14+
///
15+
/// # Arguments
16+
///
17+
/// * `args` - Arguments specifying the target and disassembly options
18+
///
19+
/// # Returns
20+
///
21+
/// A string containing the disassembled bytecode in assembly format
922
pub async fn disassemble(args: DisassemblerArgs) -> Result<String, Error> {
1023
// init
1124
let start_time = Instant::now();

crates/disassemble/src/error.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
/// Error type for the Disassembler module
12
#[derive(Debug, thiserror::Error)]
23
pub enum Error {
4+
/// Generic internal error that may occur during disassembly
35
#[error("Internal error: {0}")]
46
Eyre(#[from] eyre::Report),
57
}

crates/disassemble/src/interfaces/args.rs

+25
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ use heimdall_config::parse_url_arg;
99
after_help = "For more information, read the wiki: https://jbecker.dev/r/heimdall-rs/wiki",
1010
override_usage = "heimdall disassemble <TARGET> [OPTIONS]"
1111
)]
12+
/// Arguments for the disassembly operation
13+
///
14+
/// This struct contains all the configuration parameters needed to disassemble
15+
/// a contract's bytecode into human-readable assembly.
1216
pub struct DisassemblerArgs {
1317
/// The target to disassemble, either a file, bytecode, contract address, or ENS name.
1418
#[clap(required = true)]
@@ -33,6 +37,10 @@ pub struct DisassemblerArgs {
3337
}
3438

3539
#[derive(Debug, Clone)]
40+
/// Builder for DisassemblerArgs
41+
///
42+
/// This struct provides a builder pattern for creating DisassemblerArgs instances
43+
/// with a fluent API.
3644
pub struct DisassemblerArgsBuilder {
3745
/// The target to disassemble, either a file, bytecode, contract address, or ENS name.
3846
target: Option<String>,
@@ -51,6 +59,13 @@ pub struct DisassemblerArgsBuilder {
5159
}
5260

5361
impl DisassemblerArgs {
62+
/// Retrieves the bytecode for the specified target
63+
///
64+
/// This method fetches the bytecode from a file, address, or directly from a hex string,
65+
/// depending on the target type provided in the arguments.
66+
///
67+
/// # Returns
68+
/// The raw bytecode as a vector of bytes
5469
pub async fn get_bytecode(&self) -> Result<Vec<u8>> {
5570
get_bytecode_from_target(&self.target, &self.rpc_url).await
5671
}
@@ -63,6 +78,7 @@ impl Default for DisassemblerArgsBuilder {
6378
}
6479

6580
impl DisassemblerArgsBuilder {
81+
/// Creates a new DisassemblerArgsBuilder with default values
6682
pub fn new() -> Self {
6783
Self {
6884
target: Some(String::new()),
@@ -73,31 +89,40 @@ impl DisassemblerArgsBuilder {
7389
}
7490
}
7591

92+
/// Sets the target for disassembly (address, file, or bytecode)
7693
pub fn target(&mut self, target: String) -> &mut Self {
7794
self.target = Some(target);
7895
self
7996
}
8097

98+
/// Sets the RPC URL for fetching bytecode if the target is an address
8199
pub fn rpc_url(&mut self, rpc_url: String) -> &mut Self {
82100
self.rpc_url = Some(rpc_url);
83101
self
84102
}
85103

104+
/// Sets whether to use decimal (true) or hexadecimal (false) for program counter
86105
pub fn decimal_counter(&mut self, decimal_counter: bool) -> &mut Self {
87106
self.decimal_counter = Some(decimal_counter);
88107
self
89108
}
90109

110+
/// Sets the name for the output file
91111
pub fn name(&mut self, name: String) -> &mut Self {
92112
self.name = Some(name);
93113
self
94114
}
95115

116+
/// Sets the output directory or 'print' to print to console
96117
pub fn output(&mut self, output: String) -> &mut Self {
97118
self.output = Some(output);
98119
self
99120
}
100121

122+
/// Builds the DisassemblerArgs from the builder
123+
///
124+
/// # Returns
125+
/// A Result containing the built DisassemblerArgs or an error if required fields are missing
101126
pub fn build(&self) -> eyre::Result<DisassemblerArgs> {
102127
Ok(DisassemblerArgs {
103128
target: self.target.clone().ok_or_else(|| eyre::eyre!("target is required"))?,

crates/disassemble/src/lib.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
mod error;
1+
//! The Disassembler module provides functionality to convert EVM bytecode
2+
//! into human-readable assembly instructions.
3+
//!
4+
//! This module enables the translation of raw bytecode into meaningful operations,
5+
//! which is a critical step for understanding and analyzing smart contracts.
6+
7+
/// Error types for the disassembler module
8+
pub mod error;
29

310
mod core;
411
mod interfaces;

crates/dump/src/core/mod.rs

+12
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@ use tracing::{debug, info};
1616

1717
use crate::{error::Error, interfaces::DumpArgs};
1818

19+
/// Dumps the storage slots for a contract
20+
///
21+
/// This function retrieves storage slots from a contract by analyzing state differences
22+
/// across multiple blocks. It uses parallel processing to efficiently handle large block ranges.
23+
///
24+
/// # Arguments
25+
///
26+
/// * `args` - Configuration parameters for the dump operation
27+
///
28+
/// # Returns
29+
///
30+
/// A HashMap containing the storage slots (keys) and their values
1931
pub async fn dump(args: DumpArgs) -> Result<HashMap<FixedBytes<32>, FixedBytes<32>>, Error> {
2032
let start_time = Instant::now();
2133
let storage = Arc::new(Mutex::new(HashMap::new()));

crates/dump/src/error.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// TODO: after all errors are fixed, remove most instances of Generic for
22
// specific errors (e.g. ParseError, FilesystemError, etc.)
3+
/// Generic error type for the Dump Module
34
#[derive(Debug, thiserror::Error)]
45
pub enum Error {
6+
/// Generic internal error
57
#[error("Internal error: {0}")]
68
Eyre(#[from] eyre::Report),
79
}

crates/dump/src/interfaces/args.rs

+5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ use heimdall_config::parse_url_arg;
88
after_help = "For more information, read the wiki: https://jbecker.dev/r/heimdall-rs/wiki",
99
override_usage = "heimdall dump <TARGET> [OPTIONS]"
1010
)]
11+
/// Arguments for the dump operation
12+
///
13+
/// This struct contains all the configuration parameters needed to perform
14+
/// a storage slot dump for a target contract.
1115
pub struct DumpArgs {
1216
/// The target to find and dump the storage slots of.
1317
#[clap(required = true)]
@@ -40,6 +44,7 @@ pub struct DumpArgs {
4044
}
4145

4246
impl DumpArgsBuilder {
47+
/// Creates a new DumpArgsBuilder with default values
4348
pub fn new() -> Self {
4449
Self {
4550
target: Some(String::new()),

0 commit comments

Comments
 (0)