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(cast): decode external lib sigs from cached selectors #9399

Merged
merged 3 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
96 changes: 96 additions & 0 deletions crates/cast/tests/cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1713,3 +1713,99 @@ Transaction successfully executed.

"#]]);
});

// tests cast can decode external libraries traces with project cached selectors
forgetest_async!(decode_external_libraries_with_cached_selectors, |prj, cmd| {
let (api, handle) = anvil::spawn(NodeConfig::test()).await;

foundry_test_utils::util::initialize(prj.root());
prj.add_source(
"ExternalLib",
r#"
import "./CounterInExternalLib.sol";

library ExternalLib {
function updateCounterInExternalLib(CounterInExternalLib.Info storage counterInfo, uint256 counter) public {
counterInfo.counter = counter + 1;
}
}
"#,
)
.unwrap();
prj.add_source(
"CounterInExternalLib",
r#"
import "./ExternalLib.sol";

contract CounterInExternalLib {
struct Info {
uint256 counter;
}

Info info;

constructor() {
ExternalLib.updateCounterInExternalLib(info, 100);
}
}
"#,
)
.unwrap();
prj.add_script(
"CounterInExternalLibScript",
r#"
import "forge-std/Script.sol";
import {CounterInExternalLib} from "../src/CounterInExternalLib.sol";

contract CounterInExternalLibScript is Script {
function run() public {
vm.startBroadcast();
new CounterInExternalLib();
vm.stopBroadcast();
}
}
"#,
)
.unwrap();

cmd.args([
"script",
"--private-key",
"0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80",
"--rpc-url",
&handle.http_endpoint(),
"--broadcast",
"CounterInExternalLibScript",
])
.assert_success();

let tx_hash = api
.transaction_by_block_number_and_index(BlockNumberOrTag::Latest, Index::from(0))
.await
.unwrap()
.unwrap()
.tx_hash();

// Cache project selectors.
cmd.forge_fuse().set_current_dir(prj.root());
cmd.forge_fuse().args(["selectors", "cache"]).assert_success();

// Assert cast with local artifacts can decode external lib signature.
cmd.cast_fuse().set_current_dir(prj.root());
cmd.cast_fuse()
.args(["run", format!("{tx_hash}").as_str(), "--rpc-url", &handle.http_endpoint()])
.assert_success()
.stdout_eq(str![[r#"
...
Traces:
[37739] → new <unknown>@0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512
├─ [22411] 0x1Bc4A44b22E17b81A5cD2d1f2E8F0E2F3621c939::updateCounterInExternalLib(0, 100) [delegatecall]
│ └─ ← [Stop]
└─ ← [Return] 62 bytes of code


Transaction successfully executed.
[GAS]

"#]]);
});
9 changes: 9 additions & 0 deletions crates/cli/src/utils/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,15 @@ pub fn cache_local_signatures(output: &ProjectCompileOutput, cache_path: PathBuf
.events
.insert(event.selector().to_string(), event.full_signature());
}
// External libraries doesn't have functions included in abi, but `methodIdentifiers`.
if let Some(method_identifiers) = &artifact.method_identifiers {
method_identifiers.iter().for_each(|(signature, selector)| {
cached_signatures
.functions
.entry(format!("0x{selector}"))
.or_insert(signature.to_string());
});
}
}
});

Expand Down
7 changes: 5 additions & 2 deletions crates/evm/traces/src/decoder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -689,10 +689,13 @@ fn reconstruct_params(event: &Event, decoded: &DecodedEvent) -> Vec<DynSolValue>
let mut unindexed = 0;
let mut inputs = vec![];
for input in event.inputs.iter() {
if input.indexed {
// Prevent panic of event `Transfer(from, to)` decoded with a signature
// `Transfer(address indexed from, address indexed to, uint256 indexed tokenId)` by making
// sure the event inputs is not higher than decoded indexed / un-indexed values.
if input.indexed && indexed < decoded.indexed.len() {
Comment on lines +692 to +695
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah nice find

inputs.push(decoded.indexed[indexed].clone());
indexed += 1;
} else {
} else if unindexed < decoded.body.len() {
inputs.push(decoded.body[unindexed].clone());
unindexed += 1;
}
Expand Down