AztecProtocol / aztec-packages

Apache License 2.0
203 stars 235 forks source link

Artifact Generation broken when you have the Token Contract as a Dependency in Nargo.toml #8532

Closed Cheetah0x closed 1 month ago

Cheetah0x commented 2 months ago

Bug Report: Incorrect Contract Storage Layout Generated with Token Dependency

Aztec Version: v0.54.0 Repo for Replication: aztec-artifact-bug

Bug Description

When adding the token contract as a dependency in Nargo.toml, without actually importing or using it in the contract, the generated Contract Storage Layout reflects the storage of the token contract instead of the contract being worked on.

In the provided example, the simple counter contract should have the following storage layout in the generated TypeScript artifact:

public static get storage(): ContractStorageLayout<'counters'> {
  return {
    counters: {
      slot: new Fr(1n),
    },
  } as ContractStorageLayout<'counters'>;
}

However, when adding the token contract as a dependency in Nargo.toml, without importing it in the contract, the storage layout that is generated incorrectly maps to the storage slots of the token contract:

public static get storage(): ContractStorageLayout<'admin' | 'minters' | 'balances' | 'total_supply' | 'pending_shields' | 'public_balances' | 'symbol' | 'name' | 'decimals'> {
  return {
    admin: {
      slot: new Fr(1n),
    },
    minters: {
      slot: new Fr(2n),
    },
    balances: {
      slot: new Fr(3n),
    },
    total_supply: {
      slot: new Fr(4n),
    },
    pending_shields: {
      slot: new Fr(5n),
    },
    public_balances: {
      slot: new Fr(6n),
    },
    symbol: {
      slot: new Fr(7n),
    },
    name: {
      slot: new Fr(8n),
    },
    decimals: {
      slot: new Fr(9n),
    },
  } as ContractStorageLayout<'admin' | 'minters' | 'balances' | 'total_supply' | 'pending_shields' | 'public_balances' | 'symbol' | 'name' | 'decimals'>;
}

Steps to Reproduce

Clone the repo: aztec-artifact-bug. Check the Nargo.toml file where the token contract is added as a dependency but not imported in the contract.

Compile the contract using:

aztec-nargo compile
aztec codegen -o src/artifacts target

Observe the incorrect storage layout in the generated TypeScript artifact.

Expected Behavior The generated storage layout should match the contract being worked on (e.g., the simple counter contract) and not the token contract when it’s merely a dependency in Nargo.toml but not actually imported.

benesjan commented 1 month ago

This is related to aztec macros and we are soon about to migrate them all to meta programming (should be by next week). Once that is in master I will investigate whether this bug pertains.

@Cheetah0x Thanks for reporting the bug!

benesjan commented 1 month ago

I confirm that the bug pertains after the macro migration. Updated the reproduction repo with the new version in this fork.

The reproduction was very easy to follow. Thanks again @Cheetah0x