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

✨ feat: --truncate-calldata flag #145

Merged
merged 1 commit into from
Sep 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions heimdall/src/decode/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ pub struct DecodeArgs {
/// When prompted, always select the default value.
#[clap(long, short)]
pub default: bool,

/// Whether to truncate nonstandard sized calldata.
#[clap(long, short)]
pub truncate_calldata: bool,
}

#[allow(deprecated)]
Expand Down Expand Up @@ -90,7 +94,7 @@ pub fn decode(args: DecodeArgs) {
}

// normalize
let calldata = calldata.replacen("0x", "", 1);
let mut calldata = calldata.replacen("0x", "", 1);

// check if the calldata length is a standard length
if calldata.len() % 2 != 0 {
Expand All @@ -99,8 +103,20 @@ pub fn decode(args: DecodeArgs) {
}

// if calldata isn't a multiple of 64, it may be harder to decode.
if calldata[8..].len() % 64 != 0 {
if (calldata[8..].len() % 64 != 0) && !args.truncate_calldata {
logger.warn("calldata is not a standard size. decoding may fail since each word is not exactly 32 bytes long.");
logger.warn("if decoding fails, try using the --truncate-calldata flag to truncate the calldata to a standard size.");
} else if args.truncate_calldata {
logger.warn("calldata is not a standard size. truncating the calldata to a standard size.");

// get the selector
let selector = calldata[0..8].to_owned();

// truncate calldata to a standard size, removing the selector
calldata = calldata[8..][..calldata[8..].len() - (calldata[8..].len() % 64)].to_owned();

// add the selector back
calldata = selector + &calldata;
}

// parse the two parts of calldata, inputs and selector
Expand Down
6 changes: 6 additions & 0 deletions heimdall/tests/test_decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ mod benchmark {
openai_api_key: String::from(""),
explain: false,
default: true,
truncate_calldata: false,
};
heimdall::decode::decode(args)
}
Expand All @@ -32,6 +33,7 @@ mod benchmark {
openai_api_key: String::from(""),
explain: false,
default: true,
truncate_calldata: false,
};
heimdall::decode::decode(args)
}
Expand All @@ -49,6 +51,7 @@ mod benchmark {
openai_api_key: String::from(""),
explain: false,
default: true,
truncate_calldata: false,
};
heimdall::decode::decode(args)
}
Expand All @@ -66,6 +69,7 @@ mod benchmark {
openai_api_key: String::from(""),
explain: false,
default: true,
truncate_calldata: false,
};
heimdall::decode::decode(args)
}
Expand All @@ -82,6 +86,7 @@ mod benchmark {
openai_api_key: String::from(""),
explain: false,
default: true,
truncate_calldata: false,
};
heimdall::decode::decode(args);
assert!(true)
Expand All @@ -96,6 +101,7 @@ mod benchmark {
openai_api_key: String::from(""),
explain: false,
default: true,
truncate_calldata: false,
};
heimdall::decode::decode(args);
assert!(true)
Expand Down
Loading