Skip to content

Commit

Permalink
fix(forge eip712): handle recursive types (foundry-rs#9319)
Browse files Browse the repository at this point in the history
fix(forge eip712): handle recursive types
  • Loading branch information
klkvr authored Nov 14, 2024
1 parent 31c24b0 commit a65a5b1
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 11 deletions.
23 changes: 13 additions & 10 deletions crates/forge/bin/cmd/eip712.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ impl Resolver {
/// Returns `None` if struct contains any fields that are not supported by EIP-712 (e.g.
/// mappings or function pointers).
pub fn resolve_struct_eip712(&self, id: usize) -> Result<Option<String>> {
self.resolve_eip712_inner(id, &mut Default::default(), true, None)
let mut subtypes = BTreeMap::new();
subtypes.insert(self.structs[&id].name.clone(), id);
self.resolve_eip712_inner(id, &mut subtypes, true, None)
}

fn resolve_eip712_inner(
Expand Down Expand Up @@ -205,8 +207,17 @@ impl Resolver {
// If we've already seen struct with this ID, just use assigned name.
if let Some((name, _)) = subtypes.iter().find(|(_, id)| **id == def.id) {
name.clone()
// Otherwise, try assigning a new name.
} else {
// Otherwise, assign new name.
let mut i = 0;
let mut name = def.name.clone();
while subtypes.contains_key(&name) {
i += 1;
name = format!("{}_{i}", def.name);
}

subtypes.insert(name.clone(), def.id);

// iterate over members to check if they are resolvable and to populate subtypes
for member in &def.members {
if self.resolve_type(
Expand All @@ -218,14 +229,6 @@ impl Resolver {
return Ok(None)
}
}
let mut i = 0;
let mut name = def.name.clone();
while subtypes.contains_key(&name) {
i += 1;
name = format!("{}_{i}", def.name);
}

subtypes.insert(name.clone(), def.id);
name
};

Expand Down
32 changes: 31 additions & 1 deletion crates/forge/tests/cli/eip712.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,35 @@ library Structs {
struct Complex {
Structs2.Foo foo2;
Foo[] foos;
Rec[][] recs;
}
struct Rec {
Rec[] rec;
}
}
library Structs2 {
struct Foo {
uint256 id;
}
struct Rec {
Bar[] bar;
}
struct Bar {
Rec rec;
}
struct FooBar {
Foo[] foos;
Bar[] bars;
Structs.Foo foo;
Structs.Bar bar;
Rec[] recs;
Structs.Rec rec;
}
}
"#,
)
Expand All @@ -42,10 +64,18 @@ Bar(Art art)Art(uint256 id)
Art(uint256 id)
Complex(Foo foo2,Foo_1[] foos)Art(uint256 id)Bar(Art art)Foo(uint256 id)Foo_1(Bar bar)
Complex(Foo foo2,Foo_1[] foos,Rec[][] recs)Art(uint256 id)Bar(Art art)Foo(uint256 id)Foo_1(Bar bar)Rec(Rec[] rec)
Rec(Rec[] rec)
Foo(uint256 id)
Rec(Bar[] bar)Bar(Rec rec)
Bar(Rec rec)Rec(Bar[] bar)
FooBar(Foo[] foos,Bar[] bars,Foo_1 foo,Bar_1 bar,Rec[] recs,Rec_1 rec)Art(uint256 id)Bar(Rec rec)Bar_1(Art art)Foo(uint256 id)Foo_1(Bar_1 bar)Rec(Bar[] bar)Rec_1(Rec_1[] rec)
"#]],
);
Expand Down

0 comments on commit a65a5b1

Please sign in to comment.