Skip to content

Commit

Permalink
Merge pull request #3 from lambdaclass/ci-quality-checks
Browse files Browse the repository at this point in the history
CI: Add a QA workflow
  • Loading branch information
jpcenteno committed Sep 15, 2023
2 parents 063631d + f0ffd81 commit e7c74fc
Show file tree
Hide file tree
Showing 12 changed files with 158 additions and 48 deletions.
90 changes: 90 additions & 0 deletions .github/workflows/quality.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
name: Quality checks
on:
push:
branches: [main]
pull_request:
branches: ["*"]

jobs:

cargo_check:
name: Cargo Check
runs-on: ubuntu-latest
steps:

- name: Checkout sources
uses: actions/checkout@v3

- name: Rustup toolchain install
uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable

- uses: Swatinem/rust-cache@v2
with:
cache-on-failure: true

- name: Run cargo check
uses: dtolnay/rust-toolchain@stable
with:
command: check

format:
name: Check format
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v3

- uses: Swatinem/rust-cache@v2
with:
cache-on-failure: true

- name: Install stable toolchain
uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable
components: rustfmt

- name: Run cargo fmt
run: cargo fmt --all -- --check

clippy:
name: Clippy
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v3

- uses: Swatinem/rust-cache@v2
with:
cache-on-failure: true

- name: Install stable toolchain
uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable
components: clippy

- name: Run clippy
run: cargo clippy --all-targets --all-features -- -D warnings

test:
name: Test
runs-on: ubuntu-latest
env:
CARGO_TERM_COLOR: always

steps:
- name: Checkout sources
uses: actions/checkout@v3

- uses: Swatinem/rust-cache@v2
with:
cache-on-failure: true

- name: Install stable toolchain
uses: dtolnay/rust-toolchain@stable

- name: Run tests
run: cargo test
7 changes: 3 additions & 4 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::commands::{
account_balance, call, compile, deploy, encode, get_bridge_contracts, get_bytecode_by_hash,
get_contract, get_transaction, pay, selector, get_confirmed_tokens, get_l1_batch_details, get_l2_to_l1_proof, main_contract,
get_confirmed_tokens, get_contract, get_l1_batch_details, get_l2_to_l1_proof, get_transaction,
main_contract, pay, selector,
};
use clap::{command, Args, Parser, Subcommand};

Expand Down Expand Up @@ -57,9 +58,7 @@ pub async fn start() -> eyre::Result<()> {
ZKSyncCommand::Encode(args) => encode::run(args).await?,
ZKSyncCommand::Selector(args) => selector::run(args).await?,
ZKSyncCommand::GetBridgeContracts => get_bridge_contracts::run(config).await?,
ZKSyncCommand::GetBytecodeByHash(args) => {
get_bytecode_by_hash::run(args, config).await?
}
ZKSyncCommand::GetBytecodeByHash(args) => get_bytecode_by_hash::run(args, config).await?,
ZKSyncCommand::ConfirmedTokens(args) => get_confirmed_tokens::run(args, config).await?,
ZKSyncCommand::L1BatchDetails(args) => get_l1_batch_details::run(args, config).await?,
ZKSyncCommand::L2ToL1LogProof(args) => get_l2_to_l1_proof::run(args, config).await?,
Expand Down
14 changes: 7 additions & 7 deletions src/commands/call.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::cli::ZKSyncConfig;
use clap::Args as ClapArgs;
use eyre::eyre;
use zksync_web3_rs::abi::{decode, ParamType, Tokenize};
use zksync_web3_rs::providers::Middleware;
use zksync_web3_rs::signers::LocalWallet;
Expand Down Expand Up @@ -46,20 +47,19 @@ pub(crate) async fn run(args: Args, config: ZKSyncConfig) -> eyre::Result<()> {
.data(data);
let transaction: TypedTransaction = request.into();
let encoded_output = Middleware::call(&provider, &transaction, None).await?;
let decoded_output = if let Some(output_types) = args.output_types {
if let Some(output_types) = args.output_types {
let parsed_param_types: Vec<ParamType> = output_types
.iter()
.map(|output_type| match output_type.as_str() {
"uint256" => ParamType::Uint(256),
"sint256" => ParamType::Int(256),
_ => todo!(),
"uint256" => Ok(ParamType::Uint(256)),
"sint256" => Ok(ParamType::Int(256)),
other => Err(eyre!("Unable to parse output type: {other}")),
})
.collect();
.collect::<eyre::Result<Vec<ParamType>>>()?;
decode(&parsed_param_types, &encoded_output)?
} else {
encoded_output.into_tokens()
};
decoded_output
}
} else {
ZKSProvider::call(&provider, args.contract, function_signature, args.args).await?
};
Expand Down
2 changes: 0 additions & 2 deletions src/commands/compile/constants.rs

This file was deleted.

2 changes: 0 additions & 2 deletions src/commands/compile/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use clap::Args as ClapArgs;
use std::{env, path::PathBuf};

pub mod constants;
pub mod errors;
pub mod output;
pub mod project;

pub use constants::*;
pub use errors::*;
pub use output::*;
pub use project::*;
Expand Down
5 changes: 1 addition & 4 deletions src/commands/compile/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ impl ZKSProject {
.args(source_files(self.base_project.root()));

let command_output = command.output().map_err(|e| {
ZKCompilerError::CompilationError(format!(
"failed to execute zksolc: {}",
e.to_string()
))
ZKCompilerError::CompilationError(format!("failed to execute zksolc: {e}"))
})?;

let compilation_output = String::from_utf8_lossy(&command_output.stdout)
Expand Down
14 changes: 6 additions & 8 deletions src/commands/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::str::FromStr;
use crate::cli::ZKSyncConfig;
use crate::commands::compile::ZKSProject;
use clap::Args as ClapArgs;
use eyre::eyre;
use eyre::ContextCompat;
use zksync_web3_rs::abi::Token;
use zksync_web3_rs::signers::LocalWallet;
Expand Down Expand Up @@ -59,14 +60,11 @@ pub(crate) async fn run(args: Args, config: ZKSyncConfig) -> eyre::Result<()> {

let compilation_output = project.compile()?;
let artifact = compilation_output
.find_contract(
ContractInfo::from_str(&format!(
"{contract_path}:{contract_name}",
contract_name = args.contract_name.context("no contract name provided")?,
))
.unwrap(),
)
.unwrap();
.find_contract(ContractInfo::from_str(&format!(
"{contract_path}:{contract_name}",
contract_name = args.contract_name.context("no contract name provided")?,
))?)
.ok_or(eyre!("Artifact not found"))?;
let compiled_bytecode = artifact.bin.clone().context("no bytecode")?;
let compiled_abi = artifact.abi.clone().context("no abi")?;

Expand Down
15 changes: 11 additions & 4 deletions src/commands/encode.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use clap::Args as ClapArgs;
use eyre::eyre;
use std::str::FromStr;
use zksync_web3_rs::{
abi::{encode, HumanReadableParser, Token, Tokenizable},
Expand All @@ -20,10 +21,7 @@ pub(crate) async fn run(args: Args) -> eyre::Result<()> {
.arguments
.iter()
.zip(args.types.iter())
.map(|(arg, t)| match t.as_str() {
"uint256" => U256::from_str(&arg).map(Tokenizable::into_token),
_ => todo!(),
})
.map(|(arg, t)| parse_token(arg, t))
.collect::<Result<Vec<Token>, _>>()?;

let encoded = if let Some(function_signature) = args.function {
Expand All @@ -40,3 +38,12 @@ pub(crate) async fn run(args: Args) -> eyre::Result<()> {

Ok(())
}

fn parse_token(arg: &str, t: &str) -> eyre::Result<Token> {
let x = match t {
"uint256" => Ok(U256::from_str(arg).map(Tokenizable::into_token)),
other => Err(eyre!("Could not parse type: {other}")),
}??;

Ok(x)
}
6 changes: 2 additions & 4 deletions src/commands/get_confirmed_tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub(crate) struct Args {
#[clap(long, name = "FROM")]
from: u32,
#[clap(long, name = "LIMIT")]
limit:u8
limit: u8,
}

pub(crate) async fn run(args: Args, config: ZKSyncConfig) -> eyre::Result<()> {
Expand All @@ -18,9 +18,7 @@ pub(crate) async fn run(args: Args, config: ZKSyncConfig) -> eyre::Result<()> {
port = config.port
))?
.interval(std::time::Duration::from_millis(10));
let confirmed_tokens = provider
.get_confirmed_tokens(args.from, args.limit)
.await?;
let confirmed_tokens = provider.get_confirmed_tokens(args.from, args.limit).await?;
log::info!("{:#?}", confirmed_tokens);
Ok(())
}
4 changes: 1 addition & 3 deletions src/commands/get_l1_batch_details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ pub(crate) async fn run(args: Args, config: ZKSyncConfig) -> eyre::Result<()> {
port = config.port
))?
.interval(std::time::Duration::from_millis(10));
let l1_batch_details = provider
.get_l1_batch_details(args.batch)
.await?;
let l1_batch_details = provider.get_l1_batch_details(args.batch).await?;
log::info!("{:#?}", l1_batch_details);
Ok(())
}
39 changes: 33 additions & 6 deletions src/commands/get_l2_to_l1_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::cli::ZKSyncConfig;
use clap::Args as ClapArgs;
use eyre::ContextCompat;
use zksync_web3_rs::providers::Provider;
use zksync_web3_rs::types::{H256, U64, Address};
use zksync_web3_rs::types::{Address, H256, U64};
use zksync_web3_rs::zks_provider::ZKSProvider;

#[derive(ClapArgs)]
Expand All @@ -11,15 +11,42 @@ pub(crate) struct Args {
transaction: H256,
#[clap(long, name = "L2_TO_L1_LOG_INDEX")]
log_index: Option<u64>,
#[clap(long, action, conflicts_with = "msg_proof, block, sender, msg", group = "log", name = "LOG_PROOF")]
#[clap(
long,
action,
conflicts_with = "msg_proof, block, sender, msg",
group = "log",
name = "LOG_PROOF"
)]
log_proof: bool,
#[clap(long, action, conflicts_with = "log_proof, l2_to_l1_log_index", group = "msg", name = "MESSAGE_PROOF")]
#[clap(
long,
action,
conflicts_with = "log_proof, l2_to_l1_log_index",
group = "msg",
name = "MESSAGE_PROOF"
)]
msg_proof: bool,
#[clap(long, conflicts_with = "log_proof, l2_to_l1_log_index", group = "msg", name = "MESSAGE_BLOCK")]
#[clap(
long,
conflicts_with = "log_proof, l2_to_l1_log_index",
group = "msg",
name = "MESSAGE_BLOCK"
)]
block: U64,
#[clap(long, conflicts_with = "log_proof, l2_to_l1_log_index", group = "msg", name = "MESSAGE_SENDER")]
#[clap(
long,
conflicts_with = "log_proof, l2_to_l1_log_index",
group = "msg",
name = "MESSAGE_SENDER"
)]
sender: Address,
#[clap(long, conflicts_with = "log_proof, l2_to_l1_log_index", group = "msg", name = "MESSAGE")]
#[clap(
long,
conflicts_with = "log_proof, l2_to_l1_log_index",
group = "msg",
name = "MESSAGE"
)]
msg: H256,
}

Expand Down
8 changes: 4 additions & 4 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ pub(crate) mod deploy;
pub(crate) mod encode;
pub(crate) mod get_bridge_contracts;
pub(crate) mod get_bytecode_by_hash;
pub(crate) mod get_contract;
pub(crate) mod get_transaction;
pub(crate) mod pay;
pub(crate) mod selector;
pub(crate) mod get_confirmed_tokens;
pub(crate) mod get_contract;
pub(crate) mod get_l1_batch_details;
pub(crate) mod get_l2_to_l1_proof;
pub(crate) mod get_transaction;
pub(crate) mod main_contract;
pub(crate) mod pay;
pub(crate) mod selector;

// It is set so that the transaction is replay-protected (EIP-155)
// https://era.zksync.io/docs/api/hardhat/testing.html#connect-wallet-to-local-nodes
Expand Down

0 comments on commit e7c74fc

Please sign in to comment.