Skip to content

Commit

Permalink
docs: update to include scarb & contribution notes
Browse files Browse the repository at this point in the history
  • Loading branch information
cwkang1998 committed May 2, 2024
1 parent 596ab73 commit 289d854
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 2 deletions.
111 changes: 111 additions & 0 deletions HOW_IT_WORKS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# How it works

## Documentation

### Overview

Cairo 1 adopts a crate/module-centric approach, inspired by Rust's design, to group related items into coherent units, facilitating the organization of code within a project. To declare the root of a crate, Cairo uses a `lib.cairo` file, which attaches all the modules to the module tree, making them accessible to the rest of the project.

The challenge in designing this contract verifier is that, to verify a contract within a project, we need to provide the compiler with the entire project, as it must resolve all the modules declared in the `lib.cairo` file and their submodules. However, to improve performance, we want to minimize the number of files sent to our backend.

To verify a contract, we only need to send the contract itself and its dependent modules. However, the compiler requires a valid Cairo project to compile. Therefore, we must send a Cairo crate, with a `lib.cairo` file that declares all the modules on which the contract depends, including their submodules. To accomplish this, we follow these steps:

- Gather the list of imports used by each module in the crate.
- Resolve the file path of each module, ignoring virtual file modules generated by compiler plugins.
- Create a directed graph that links module files together based on their dependencies.
- Generate a new crate that includes only the required files.
- Generate the "attachment" files that link the modules to the module tree up to the top-level lib.rs file.

As an example, let's consider the following project structure:

```
.
├── cairo_ds
│   ├── Scarb.toml
│   └── src
│   ├── contracts
│   │   └── erc20.cairo
│   ├── contracts.cairo
│   ├── lib.cairo
│   ├── tests
│   │   └── test_erc20.cairo
│   └── tests.cairo
└── dependency
├── Scarb.toml
└── src
├── lib.cairo
└── main.cairo
```

To verify the erc20.cairo contract that imports `use dependency::main::foo;`, we generate a new crate that contains only the required files.
In this case, our contract depends on the external `dependency dependency::main::foo`. The resolver generates two crates: one for the cairo_ds project and one for the dependency project. Both crates contain only the necessary files.

### Output

Here is the generated minimal `cairo_ds` crate that can be sent to our backend for verification:

```
.
├── cairo_ds
│   ├── Scarb.toml
│   └── src
│   ├── contracts
│   │   └── ERC20.cairo
│   ├── contracts.cairo
│   └── lib.cairo
└── dependency
├── Scarb.toml
└── src
├── lib.cairo
└── main.cairo
```

You will notice here that everything related to `tests` is gone, as it is not required to compile the `erc20.cairo` contract.

This reduced project is generated under the `voyager-verify` directory, which is created in the root of the project.

### Scarb

Our verifier will use a `tool` section inside the Scarb manifest file to know which contracts it should verify.
Users will create a section `tool.voyager`, under which they will declare the name of the contracts they want
to verify, followed by the path to the contract file and the address the associated on-chain address.

```toml
[tool.voyager]
ERC20 ={path= "contracts/ERC20.cairo", address = "0x12345"}
```

### Sequence Diagram

```mermaid
sequenceDiagram
User ->> CLI: voyager-verify [args]
CLI ->> Resolver: compile(workspace)
Resolver ->> CairoCompiler: RootDatabase.[...]build()
CairoCompiler --> Resolver: db
Resolver ->> Scarb: read_scarb_metadata(manifest_path)
Scarb -->> Resolver: metadata
Resolver ->> CairoCompiler: update_crate_roots_from_metadata(metadata)
Resolver ->> CairoCompiler: get_main_crate_ids_from_project(db, config)
CairoCompiler -->> Resolver: Vec<CrateId>
loop crate_id
CairoCompiler-->>Resolver: crate_root_dir
CairoCompiler-->>Resolver: main_file_path
Resolver ->> Queries: extract_crate_modules(db, crate_id)
loop crate_modules
Queries -->> Queries: get_module_file(db, module_id)
Queries -->> Queries: extract_file_imports(db, module_id, file)
Queries -->> Queries: CairoModule{...}
end
Queries -->> Resolver: crate_modules
end
Resolver ->> Graph: create_graph(all_modules)
Graph -->> Resolver: graph
Resolver ->> Graph: get_reduced_project(graph, modules_to_verify)
Graph -->> Resolver: (required_modules_paths, attachment_modules_data)
Resolver ->> Filesystem: create_attachment_files()
Resolver ->> Filesystem: copy_required_files()
Resolver ->> Filesystem: generate_scarb_updated_files
```
26 changes: 24 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,30 @@

`starknet-contract-verifier` is a contract class verification cli that allows you to verify your starknet classes on a block explorer.

The list of the block explorer we currently support are:
#### The list of the block explorer we currently support are:
- [Voyager Starknet block explorer](https://voyager.online).


#### We currently support the following cairo version & scarb version.
- [ ] Cairo 2.0.2 (Scarb v0.5.2)
- [ ] Cairo 2.1.1 (Scarb v0.6.2)
- [ ] Cairo 2.2.0 (Scarb v0.7.0)
- [ ] Cairo & Scarb 2.3.1
- [x] Cairo & Scarb 2.4.3
- [x] Cairo & Scarb 2.5.4
- [ ] Cairo & Scarb 2.6.3


## Getting started

### Prerequisite

#### Installing Scarb

This CLI relies upon Scarb for dependencies resolving during compilation and thus require you to have Scarb installed for it to work properly. You can install Scarb following the instruction on their documentation at https://docs.swmansion.com/scarb.

Note that CLI version that you install should follow the version of the Scarb you have installed for it to work as expected.

#### Getting an api key

The verification CLI uses the public API of the block explorer under the hood, as such you will have to obtain your API key in order to start using the verifier.
Expand Down Expand Up @@ -85,4 +100,11 @@ To build the project, simply do
cargo build
```

and the project should start building.
and the project should start building.


## Contributing

We welcome any form of contribution to this project!

To start, you can take a look at the issues that's available for taking and work on whichever you might be interested in. Do leave a comment so we can assign the issue to you!

0 comments on commit 289d854

Please sign in to comment.