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

chore: improve unlinked bytecode deserde error #484

Merged
merged 3 commits into from
Jan 16, 2024
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
46 changes: 28 additions & 18 deletions crates/json-abi/src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,20 +529,39 @@ impl<'de> Visitor<'de> for ContractObjectVisitor {
f.write_str("an ABI sequence or contract object")
}

#[inline]
fn visit_seq<A: SeqAccess<'de>>(self, seq: A) -> Result<Self::Value, A::Error> {
JsonAbiVisitor.visit_seq(seq).map(|abi| ContractObject {
abi: Some(abi),
bytecode: None,
deployed_bytecode: None,
})
}

fn visit_map<A: MapAccess<'de>>(self, mut map: A) -> Result<Self::Value, A::Error> {
#[derive(Deserialize)]
#[serde(untagged)]
enum Bytecode {
Bytes(Bytes),
Object { object: Bytes },
Unlinked(String),
UnlinkedObject { object: String },
}

impl Bytecode {
#[allow(clippy::missing_const_for_fn)]
#[inline(always)]
fn bytes(self) -> Bytes {
let (Self::Object { object: bytes } | Self::Bytes(bytes)) = self;
bytes
fn ensure_bytes<E: serde::de::Error>(self) -> Result<Bytes, E> {
match self {
Bytecode::Bytes(bytes) | Bytecode::Object { object: bytes } => Ok(bytes),
Bytecode::Unlinked(unlinked)
| Bytecode::UnlinkedObject { object: unlinked } => {
if let Some((_, unlinked)) = unlinked.split_once("__$") {
if let Some((addr, _)) = unlinked.split_once("$__") {
return Err(E::custom(format!("expected bytecode, found unlinked bytecode with placeholder: {addr}")));
}
}
Err(E::custom("invalid contract bytecode"))
}
}
}
}

Expand All @@ -564,18 +583,18 @@ impl<'de> Visitor<'de> for ContractObjectVisitor {
"evm" => {
let evm = map.next_value::<EvmObj>()?;
if let Some(bytes) = evm.bytecode {
set_if_none!(@serde bytecode, bytes.bytes());
set_if_none!(@serde bytecode, bytes.ensure_bytes()?);
}
if let Some(bytes) = evm.deployed_bytecode {
set_if_none!(@serde deployed_bytecode, bytes.bytes());
set_if_none!(@serde deployed_bytecode, bytes.ensure_bytes()?);
}
}
"bytecode" | "bin" => {
set_if_none!(@serde bytecode, map.next_value::<Bytecode>()?.bytes());
set_if_none!(@serde bytecode, map.next_value::<Bytecode>()?.ensure_bytes()?);
}
"deployedBytecode" | "deployedbytecode" | "deployed_bytecode" | "runtimeBin"
| "runtimebin" | "runtime " => {
set_if_none!(@serde deployed_bytecode, map.next_value::<Bytecode>()?.bytes());
set_if_none!(@serde deployed_bytecode, map.next_value::<Bytecode>()?.ensure_bytes()?);
}
_ => {
map.next_value::<serde::de::IgnoredAny>()?;
Expand All @@ -585,13 +604,4 @@ impl<'de> Visitor<'de> for ContractObjectVisitor {

Ok(ContractObject { abi, bytecode, deployed_bytecode })
}

#[inline]
fn visit_seq<A: SeqAccess<'de>>(self, seq: A) -> Result<Self::Value, A::Error> {
JsonAbiVisitor.visit_seq(seq).map(|abi| ContractObject {
abi: Some(abi),
bytecode: None,
deployed_bytecode: None,
})
}
}
16 changes: 15 additions & 1 deletion crates/json-abi/tests/abi.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use alloy_json_abi::{AbiItem, EventParam, JsonAbi, Param};
use alloy_json_abi::{AbiItem, ContractObject, EventParam, JsonAbi, Param};
use pretty_assertions::assert_eq;
use std::{
collections::HashMap,
Expand All @@ -10,6 +10,8 @@ use std::{

const JSON_PATH: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/abi");

const TESTDATA_PATH: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/testdata");

static UPDATED: AtomicBool = AtomicBool::new(false);

#[test]
Expand Down Expand Up @@ -283,3 +285,15 @@ fn get_solc_version() -> Option<(u16, u16, u16)> {
let patch = iter.next().unwrap();
Some((major, minor, patch))
}

// <https://github.com/foundry-rs/foundry/issues/6815>
#[test]
#[cfg_attr(miri, ignore = "no fs")]
#[cfg(all(feature = "std", feature = "serde_json"))]
fn parse_unlinked_contract() {
// unlinked placeholder __$7233c33f2e1e35848c685b0eb24649959e$__
let content = fs::read_to_string(Path::new(TESTDATA_PATH).join("UnlinkedNouns.json")).unwrap();
let res = serde_json::from_str::<ContractObject>(&content);
let err = res.unwrap_err();
assert!(err.to_string().contains("expected bytecode, found unlinked bytecode with placeholder: 7233c33f2e1e35848c685b0eb24649959e"));
}
Loading