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

refactor: make Contract generic for Compiler and add metadata to CompilerOutput #224

Merged
merged 15 commits into from
Dec 4, 2024

Conversation

elfedy
Copy link
Contributor

@elfedy elfedy commented Nov 19, 2024

Right now the compiler abstraction has two couplings that force foundry-zksync to implement it's own fork of compilers:

  1. Contract is specific to solc/EVM contracts. Era VM contracts, while requiring different fields, still could use most of the functionality of the compilers pipeline.
  2. CompilerOutput has solc specific fields. zksolc compilation has relevant information that is useful to have later on, for example when storing BuildInfo

This PR implements changes to address this. If implemented, it would allow foundry-zksync (and potentially other non EVM implementations of foundry) to get rid of all overrides and only maintain ZKsync specific data structures/trait implementations. See sample PR. Changes include:

  1. Make Compiler generic over Contract (using CompilerContract as a trait type).
  2. Add metadata field to CompilerOutput in order to add arbitrary data to compilation output.

Copy link
Member

@mattsse mattsse left a comment

Choose a reason for hiding this comment

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

I see, imo this seems reasonable if this simplifies things for zksync a lot.

the additional type/generic isn't too bad

left some doc nits,
needs another review by @klkvr

It'd be great if you could open a pr to foundry where the compilers deps a patched to this branch so we can ensure that this still works or what we need to tune

Comment on lines 14 to 27
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(transparent)]
pub struct VersionedContracts(pub FileToContractsMap<Vec<VersionedContract>>);
pub struct VersionedContracts<C: CompilerContract>(
pub FileToContractsMap<Vec<VersionedContract<C>>>,
);

impl<C> Default for VersionedContracts<C>
where
C: CompilerContract,
{
fn default() -> Self {
Self(BTreeMap::new())
}
}
Copy link
Member

Choose a reason for hiding this comment

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

these trait bounds can be relexed, we only need this for the impl block

Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed the CompilerContract bound: 2be52cf

@@ -267,6 +276,42 @@ pub trait Language:
const FILE_EXTENSIONS: &'static [&'static str];
}

pub trait CompilerContract: Serialize + Send + Sync + Debug + Clone + Eq + Sized {
Copy link
Member

Choose a reason for hiding this comment

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

could we add some docs here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added, took them from original Contract: 0a36f57

Comment on lines 212 to 213
#[serde(default)]
pub metadata: BTreeMap<String, String>,
Copy link
Member

Choose a reason for hiding this comment

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

are these really just strings or are those json objects?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed value to json: c2a6c68

Comment on lines 212 to 213
#[serde(default)]
pub metadata: BTreeMap<String, String>,
Copy link
Member

Choose a reason for hiding this comment

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

I'd like to make this an option and skip if missing because now this will always be present for solc

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Skipped serializing if empty: c2a6c68 is that good enough?

@elfedy
Copy link
Contributor Author

elfedy commented Nov 28, 2024

@mattsse thanks, added draft PR on foundry that compiles: foundry-rs/foundry#9423

Copy link
Member

@klkvr klkvr left a comment

Choose a reason for hiding this comment

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

very nice! thanks for working on this @elfedy

just have a small nit

I think it might also be useful to abstract the source eventually and have something like

trait CompilerOutput {
     type Error;
     type Contract;
     type Source;
}

but this can definitely be refactored later once we have a nice way to support generic contracts in foundry

crates/compilers/src/compilers/mod.rs Outdated Show resolved Hide resolved
@elfedy elfedy requested review from mattsse and klkvr November 28, 2024 15:05
Copy link
Member

@klkvr klkvr left a comment

Choose a reason for hiding this comment

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

lgtm!

@mattsse mattsse merged commit a087dbf into foundry-rs:main Dec 4, 2024
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants