Skip to content

Commit

Permalink
feat(test): adding JS tests to the CI (#284)
Browse files Browse the repository at this point in the history
Co-authored-by: mohiiit <[email protected]>
  • Loading branch information
Mohiiit and mohiiit authored Oct 3, 2024
1 parent 2e1c739 commit 22f8343
Show file tree
Hide file tree
Showing 18 changed files with 499 additions and 12 deletions.
42 changes: 42 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: Task - Build Madara
on:
workflow_dispatch:
workflow_call:
jobs:
build:
permissions:
pull-requests: write
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
with:
submodules: true
- uses: Swatinem/rust-cache@v2
- uses: dtolnay/rust-toolchain@stable
with:
toolchain: 1.78.0
- name: Setup build deps
run: |
sudo apt-get update
sudo apt-get install -y clang llvm libudev-dev protobuf-compiler
- uses: rui314/setup-mold@v1
- uses: software-mansion/setup-scarb@v1
with:
scarb-version: "2.8.2"
- uses: foundry-rs/foundry-toolchain@v1
with:
version: nightly
- name: Build
run: |
cargo build --release --bin madara
- name: Cache Madara binary
uses: actions/cache@v3
with:
path: target/release/madara
key: ${{ runner.os }}-madara-${{ hashFiles('Cargo.lock') }}
- name: Cache Cairo build artifacts
uses: actions/cache@v3
with:
path: cairo/target
key: ${{ runner.os }}-madara-${{ hashFiles('Scarb.lock') }}
11 changes: 10 additions & 1 deletion .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
---
name: Workflow - Pull Request

on:
Expand Down Expand Up @@ -38,3 +37,13 @@ jobs:
uses: ./.github/workflows/coverage.yml
needs: changelog
secrets: inherit

build:
name: Build Madara
uses: ./.github/workflows/build.yml
needs: changelog

js_test:
name: Run JS Tests
uses: ./.github/workflows/starknet-js-test.yml
needs: build
32 changes: 32 additions & 0 deletions .github/workflows/starknet-js-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Starknet-js Compatibility Tests

on:
workflow_dispatch:
workflow_call:

jobs:
js-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Restore Madara binary
uses: actions/cache@v3
with:
path: target/release/madara
key: ${{ runner.os }}-madara-${{ hashFiles('Cargo.lock') }}
fail-on-cache-miss: true
- name: Restore Cairo build artifacts
uses: actions/cache@v3
with:
path: cairo/target
key: ${{ runner.os }}-madara-${{ hashFiles('Scarb.lock') }}
fail-on-cache-miss: true
- name: Setup dev chain and run tests
run: |
./target/release/madara --name madara --base-path ../madara_db --telemetry-disabled --rpc-port 9944 --rpc-cors "*" --rpc-external --devnet --preset devnet &
MADARA_PID=$!
while ! echo exit | nc localhost 9944; do sleep 1; done
cd tests/js_tests
npm install
npm test
kill $MADARA_PID
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.env
.env*

# Generated by Cargo
# will have compiled files and executables
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Next release

- test: Starknet-js basic tests added
- test: add block conversion task test
- fix(docs): updated readme and fixed launcher
- fix(ci): added gateway key to fix rate limit on tests
Expand Down
1 change: 1 addition & 0 deletions cairo/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry", tag = "v

[[target.starknet-contract]]
sierra = true
casm = true

[scripts]
test = "snforge test"
25 changes: 25 additions & 0 deletions cairo/src/hello.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#[starknet::interface]
pub trait IHelloStarknet<TContractState> {
fn increase_balance(ref self: TContractState, amount: felt252);
fn get_balance(self: @TContractState) -> felt252;
}

#[starknet::contract]
mod HelloStarknet {
#[storage]
struct Storage {
balance: felt252,
}

#[abi(embed_v0)]
impl HelloStarknetImpl of super::IHelloStarknet<ContractState> {
fn increase_balance(ref self: ContractState, amount: felt252) {
assert(amount != 0, 'Amount cannot be 0');
self.balance.write(self.balance.read() + amount);
}

fn get_balance(self: @ContractState) -> felt252 {
self.balance.read()
}
}
}
1 change: 1 addition & 0 deletions cairo/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ pub mod erc20;
pub mod udc;
pub mod account;
pub mod test_account;
pub mod hello;
33 changes: 31 additions & 2 deletions crates/client/eth/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,13 @@ pub mod eth_client_getter_test {
};
use mc_metrics::MetricsService;
use serial_test::serial;
use std::ops::Range;
use std::sync::Mutex;
use tokio;

// https://etherscan.io/tx/0xcadb202495cd8adba0d9b382caff907abf755cd42633d23c4988f875f2995d81#eventlog
// The txn we are referring to it is here ^
const L1_BLOCK_NUMBER: u64 = 20395662;
const ANVIL_PORT: u16 = 8545;
const CORE_CONTRACT_ADDRESS: &str = "0xc662c410C0ECf747543f5bA90660f6ABeBD9C8c4";
const L2_BLOCK_NUMBER: u64 = 662703;
const L2_BLOCK_HASH: &str = "563216050958639290223177746678863910249919294431961492885921903486585884664";
Expand All @@ -157,11 +159,38 @@ pub mod eth_client_getter_test {
static ref FORK_URL: String = std::env::var("ETH_FORK_URL").expect("ETH_FORK_URL not set");
}

const PORT_RANGE: Range<u16> = 19500..20000;

struct AvailablePorts<I: Iterator<Item = u16>> {
to_reuse: Vec<u16>,
next: I,
}

lazy_static::lazy_static! {
static ref AVAILABLE_PORTS: Mutex<AvailablePorts<Range<u16>>> = Mutex::new(AvailablePorts { to_reuse: vec![], next: PORT_RANGE });
}
pub struct AnvilPortNum(pub u16);
impl Drop for AnvilPortNum {
fn drop(&mut self) {
let mut guard = AVAILABLE_PORTS.lock().expect("poisoned lock");
guard.to_reuse.push(self.0);
}
}

pub fn get_port() -> AnvilPortNum {
let mut guard = AVAILABLE_PORTS.lock().expect("poisoned lock");
if let Some(el) = guard.to_reuse.pop() {
return AnvilPortNum(el);
}
AnvilPortNum(guard.next.next().expect("no more port to use"))
}

fn create_anvil_instance() -> AnvilInstance {
let port = get_port();
let anvil = Anvil::new()
.fork(FORK_URL.clone())
.fork_block_number(L1_BLOCK_NUMBER)
.port(ANVIL_PORT)
.port(port.0)
.try_spawn()
.expect("failed to spawn anvil instance");
println!("Anvil started and running at `{}`", anvil.endpoint());
Expand Down
25 changes: 19 additions & 6 deletions crates/tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::ops::Range;
use std::sync::Mutex;
use std::{
collections::HashMap,
env,
future::Future,
path::{Path, PathBuf},
process::{Child, Command, Output, Stdio},
Expand Down Expand Up @@ -64,7 +65,7 @@ impl MadaraCmd {
res.error_for_status()?;
anyhow::Ok(())
},
Duration::from_millis(1000),
Duration::from_millis(2000),
)
.await;
self.ready = true;
Expand All @@ -85,7 +86,7 @@ impl MadaraCmd {
Err(err) => bail!(err),
}
},
Duration::from_millis(5000),
Duration::from_millis(20000),
)
.await;
self
Expand Down Expand Up @@ -170,6 +171,17 @@ impl MadaraCmdBuilder {
}

pub fn run(self) -> MadaraCmd {
let output = std::process::Command::new("cargo")
.arg("locate-project")
.arg("--workspace")
.arg("--message-format=plain")
.output()
.expect("Failed to execute command");

let cargo_toml_path = String::from_utf8(output.stdout).expect("Invalid UTF-8");
let project_root = PathBuf::from(cargo_toml_path.trim()).parent().unwrap().to_path_buf();

env::set_current_dir(&project_root).expect("Failed to set working directory");
let target_bin = option_env!("COVERAGE_BIN").unwrap_or("./target/debug/madara");
let target_bin = PathBuf::from_str(target_bin).expect("target bin is not a path");
if !target_bin.exists() {
Expand Down Expand Up @@ -216,6 +228,7 @@ async fn madara_can_sync_a_few_blocks() {
use starknet_core::types::{BlockHashAndNumber, Felt};

let _ = env_logger::builder().is_test(true).try_init();

let mut cmd_builder = MadaraCmdBuilder::new().args([
"--full",
"--network",
Expand All @@ -233,14 +246,14 @@ async fn madara_can_sync_a_few_blocks() {

let mut node = cmd_builder.run();
node.wait_for_ready().await;
node.wait_for_sync_to(19).await;
node.wait_for_sync_to(9).await;

assert_eq!(
node.json_rpc().block_hash_and_number().await.unwrap(),
BlockHashAndNumber {
// https://sepolia.voyager.online/block/19
block_hash: Felt::from_hex_unchecked("0x4177d1ba942a4ab94f86a476c06f0f9e02363ad410cdf177c54064788c9bcb5"),
block_number: 19
// https://sepolia.voyager.online/block/0x4174555d24718e8225a3d536ca96d2c4cc8a31bff6a6c758ab84a16a9e92d6c
block_hash: Felt::from_hex_unchecked("0x4174555d24718e8225a3d536ca96d2c4cc8a31bff6a6c758ab84a16a9e92d6c"),
block_number: 9
}
);
}
4 changes: 2 additions & 2 deletions crates/tests/src/rpc/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1149,12 +1149,12 @@ mod test_rpc_read_calls {

let decompressed_program = decompress_to_string(contract_program.unwrap());

let mut class_program_file = File::open("../../crates/tests/src/rpc/test_utils/class_program.txt").unwrap();
let mut class_program_file = File::open("crates/tests/src/rpc/test_utils/class_program.txt").unwrap();

let mut original_program = String::new();
class_program_file.read_to_string(&mut original_program).expect("issue while reading the file");

let contract_class_file = File::open("../../crates/tests/src/rpc/test_utils/contract_class.json").unwrap();
let contract_class_file = File::open("crates/tests/src/rpc/test_utils/contract_class.json").unwrap();
let reader = BufReader::new(contract_class_file);

let expected_contract_class: ContractClass = serde_json::from_reader(reader).unwrap();
Expand Down
3 changes: 3 additions & 0 deletions tests/js_tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
lib
package-lock.json
node_modules
Loading

0 comments on commit 22f8343

Please sign in to comment.