Skip to content

Commit

Permalink
dev: refactor kakarot_rpc_core::test_utils to a kakarot_test_utils cr…
Browse files Browse the repository at this point in the history
…ate (#553)

* dev: refactor kakarot_rpc_core::test_utils to a kakarot_test_utils crate

* dev: minor changes, delete duplicate manifest file

* dev: use workspace=true for serde dependencies

* dev: formatting of Cargo.toml

---------

Co-authored-by: Harsh Bajpai <harshbajpai@Harshs-MBP.bbrouter>
  • Loading branch information
bajpai244 and Harsh Bajpai authored Sep 12, 2023
1 parent 0c87dc9 commit 46cddc6
Show file tree
Hide file tree
Showing 21 changed files with 139 additions and 60 deletions.
30 changes: 30 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]
members = ["crates/eth-rpc", "crates/core", "crates/hive-utils"]
members = ["crates/eth-rpc", "crates/core", "crates/hive-utils", "crates/test-utils"]
resolver = "2"

[workspace.package]
Expand Down
10 changes: 2 additions & 8 deletions crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,5 @@ starknet-crypto = { workspace = true }
toml = "0.7.5"
tracing = "0.1.37"
tracing-subscriber = "0.3.17"

[features]
dump = ["git2"]

[[bin]]
name = "dump-katana"
path = "src/test_utils/bin/dump-katana.rs"
required-features = ["dump"]
kakarot-test-utils = { path = "../test-utils" }
kakarot-rpc-core = { path = "." }
48 changes: 27 additions & 21 deletions crates/core/src/contracts/tests/account.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,33 @@
use rstest::*;
use starknet::core::types::{BlockId, BlockTag};
#[cfg(test)]
mod tests {

use crate::client::api::KakarotStarknetApi;
use crate::contracts::account::{Account, KakarotAccount};
use crate::test_utils::deploy_helpers::{get_contract, get_contract_deployed_bytecode, KakarotTestEnvironmentContext};
use crate::test_utils::fixtures::kakarot_test_env_ctx;
use kakarot_rpc_core::client::api::KakarotStarknetApi;
use kakarot_test_utils::deploy_helpers::{
get_contract, get_contract_deployed_bytecode, KakarotTestEnvironmentContext,
};
use kakarot_test_utils::fixtures::kakarot_test_env_ctx;
use rstest::*;
use starknet::core::types::{BlockId, BlockTag};

#[rstest]
#[tokio::test(flavor = "multi_thread")]
async fn test_bytecode(kakarot_test_env_ctx: KakarotTestEnvironmentContext) {
// Given
let contract_name = "Counter";
let counter_starknet_address = kakarot_test_env_ctx.evm_contract(contract_name).addresses.starknet_address;
let counter_contract = get_contract(contract_name);
let expected_bytecode = get_contract_deployed_bytecode(counter_contract);
use crate::contracts::account::{Account, KakarotAccount};

let starknet_block_id = BlockId::Tag(BlockTag::Latest);
let starknet_provider = kakarot_test_env_ctx.client().starknet_provider();
let counter_contract_account = KakarotAccount::new(counter_starknet_address, starknet_provider.as_ref());
#[rstest]
#[tokio::test(flavor = "multi_thread")]
async fn test_bytecode(kakarot_test_env_ctx: KakarotTestEnvironmentContext) {
// Given
let contract_name = "Counter";
let counter_starknet_address = kakarot_test_env_ctx.evm_contract(contract_name).addresses.starknet_address;
let counter_contract = get_contract(contract_name);
let expected_bytecode = get_contract_deployed_bytecode(counter_contract);

// When
let actual_bytecode = counter_contract_account.bytecode(&starknet_block_id).await.unwrap();
let starknet_block_id = BlockId::Tag(BlockTag::Latest);
let starknet_provider = kakarot_test_env_ctx.client().starknet_provider();
let counter_contract_account = KakarotAccount::new(counter_starknet_address, starknet_provider.as_ref());

// Then
assert_eq!(expected_bytecode.to_vec(), actual_bytecode.to_vec());
// When
let actual_bytecode = counter_contract_account.bytecode(&starknet_block_id).await.unwrap();

// Then
assert_eq!(expected_bytecode.to_vec(), actual_bytecode.to_vec());
}
}
1 change: 0 additions & 1 deletion crates/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@ pub mod client;
pub mod contracts;
pub mod mock;
pub mod models;
pub mod test_utils;
8 changes: 4 additions & 4 deletions crates/core/tests/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ mod tests {
use kakarot_rpc_core::mock::constants::ACCOUNT_ADDRESS_EVM;
use kakarot_rpc_core::models::balance::{TokenBalance, TokenBalances};
use kakarot_rpc_core::models::felt::Felt252Wrapper;
use kakarot_rpc_core::test_utils::constants::EOA_RECEIVER_ADDRESS;
use kakarot_rpc_core::test_utils::deploy_helpers::KakarotTestEnvironmentContext;
use kakarot_rpc_core::test_utils::execution_helpers::{execute_eth_transfer_tx, execute_eth_tx};
use kakarot_rpc_core::test_utils::fixtures::kakarot_test_env_ctx;
use kakarot_test_utils::constants::EOA_RECEIVER_ADDRESS;
use kakarot_test_utils::deploy_helpers::KakarotTestEnvironmentContext;
use kakarot_test_utils::execution_helpers::{execute_eth_transfer_tx, execute_eth_tx};
use kakarot_test_utils::fixtures::kakarot_test_env_ctx;
use reth_primitives::{Address, BlockId, BlockNumberOrTag, Bytes, H256, U256};
use reth_rpc_types::{Filter, FilterBlockOption, FilterChanges, Log, ValueOrArray};
use rstest::*;
Expand Down
2 changes: 2 additions & 0 deletions crates/eth-rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ tracing-subscriber = { version = "0.3.3", features = ["env-filter"] }
# for cross-compiling
openssl = { version = "0.10", features = ["vendored"] }

kakarot-test-utils = { path = "../test-utils" }

[dev-dependencies]
cargo-husky = { workspace = true }
ethers = { workspace = true }
Expand Down
6 changes: 3 additions & 3 deletions crates/eth-rpc/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use jsonrpsee::server::ServerHandle;
use kakarot_rpc_core::client::api::KakarotStarknetApi;
use kakarot_rpc_core::client::config::{Network, StarknetConfig};
use kakarot_rpc_core::client::KakarotClient;
use kakarot_rpc_core::test_utils::deploy_helpers::KakarotTestEnvironmentContext;
use kakarot_test_utils::deploy_helpers::KakarotTestEnvironmentContext;
use starknet::providers::jsonrpc::HttpTransport;
use starknet::providers::JsonRpcClient;

Expand Down Expand Up @@ -51,9 +51,9 @@ use crate::run_server;
/// async fn test_case(kakarot_test_env_ctx: KakarotTestEnvironmentContext) {
/// // Set up the Kakarot RPC integration environment.
/// let (server_addr, server_handle) = start_kakarot_rpc_server(&kakarot_test_env_ctx).await.unwrap();
///
///
/// // Query whatever eth_rpc endpoints
///
///
/// // Dont forget to close server at the end.
/// server_handle.stop().expect("Failed to stop the server");
///
Expand Down
4 changes: 2 additions & 2 deletions crates/eth-rpc/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ mod integration_tests {
use dotenv::dotenv;
use ethers::prelude::{Block as EthersBlock, Http as EthersHttp, H256 as EthersH256};
use kakarot_rpc::test_utils::start_kakarot_rpc_server;
use kakarot_rpc_core::test_utils::deploy_helpers::KakarotTestEnvironmentContext;
use kakarot_rpc_core::test_utils::fixtures::kakarot_test_env_ctx;
use kakarot_test_utils::deploy_helpers::KakarotTestEnvironmentContext;
use kakarot_test_utils::fixtures::kakarot_test_env_ctx;
use reth_primitives::U64;
use rstest::*;

Expand Down
2 changes: 1 addition & 1 deletion crates/eth-rpc/tests/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use starknet::providers::JsonRpcClient;
///
/// Example :
/// ```ignore
/// use kakarot_rpc::test_utils::setup_rpc_server;
/// use kakarot_test_utils::setup_rpc_server;
///
/// #[tokio::test]
/// async fn test_case() {
Expand Down
2 changes: 2 additions & 0 deletions crates/hive-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ starknet_api = { workspace = true }
tokio = { workspace = true }
url = { workspace = true }

kakarot-test-utils = { path = "../test-utils" }

[dev-dependencies]
cargo-husky = { workspace = true }
katana-core = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion crates/hive-utils/src/hive/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::path::Path;
use eyre::Result;
use kakarot_rpc_core::client::constants::STARKNET_NATIVE_TOKEN;
use kakarot_rpc_core::models::felt::Felt252Wrapper;
use kakarot_rpc_core::test_utils::deploy_helpers::compute_kakarot_contracts_class_hash;
use kakarot_test_utils::deploy_helpers::compute_kakarot_contracts_class_hash;
use lazy_static::lazy_static;
use reth_primitives::{Address, Bytes, H256, U256, U64};
use serde::{Deserialize, Serialize};
Expand Down
4 changes: 2 additions & 2 deletions crates/hive-utils/src/kakarot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ pub fn compute_starknet_address(
#[cfg(test)]
mod tests {
use kakarot_rpc_core::mock::constants::ACCOUNT_ADDRESS;
use kakarot_rpc_core::test_utils::deploy_helpers::KakarotTestEnvironmentContext;
use kakarot_rpc_core::test_utils::fixtures::kakarot_test_env_ctx;
use kakarot_test_utils::deploy_helpers::KakarotTestEnvironmentContext;
use kakarot_test_utils::fixtures::kakarot_test_env_ctx;
use rstest::*;
use starknet::core::types::{BlockId, BlockTag};

Expand Down
4 changes: 2 additions & 2 deletions crates/hive-utils/src/madara/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ mod tests {
use kakarot_rpc_core::contracts::account::Account;
use kakarot_rpc_core::contracts::contract_account::ContractAccount;
use kakarot_rpc_core::mock::constants::ACCOUNT_ADDRESS;
use kakarot_rpc_core::test_utils::deploy_helpers::KakarotTestEnvironmentContext;
use kakarot_rpc_core::test_utils::fixtures::kakarot_test_env_ctx;
use kakarot_test_utils::deploy_helpers::KakarotTestEnvironmentContext;
use kakarot_test_utils::fixtures::kakarot_test_env_ctx;
use reth_primitives::U256;
use rstest::rstest;
use starknet::core::types::{BlockId as StarknetBlockId, BlockTag, FieldElement};
Expand Down
46 changes: 46 additions & 0 deletions crates/test-utils/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
[package]
name = "kakarot-test-utils"
version.workspace = true
edition.workspace = true
authors.workspace = true
description.workspace = true
homepage.workspace = true
repository.workspace = true
readme.workspace = true
license.workspace = true

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
kakarot-rpc-core={ path = "../core" }

dojo-test-utils = { workspace = true }
katana-core = { workspace = true }
reth-primitives = { workspace = true }
starknet = { workspace = true }
starknet-crypto = { workspace = true }
url = { workspace = true }

lazy_static = { workspace = true }
tokio = { workspace = true }

bytes = "1"
dotenv = { workspace = true }
ethers = { workspace = true }
ethers-solc = { workspace = true }
foundry-config = { git = "https://github.com/foundry-rs/foundry", branch = "master" }
rstest = { workspace = true }
git2 = { workspace = true, optional = true }

futures = "0.3.26"
serde = { workspace = true }
serde_json = { workspace = true, features = ["preserve_order"] }
serde_with = { workspace = true }

[features]
dump = ["git2"]

[[bin]]
name = "dump-katana"
path = "src/bin/dump-katana.rs"
required-features = ["dump"]
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::collections::HashMap;
use ethers::abi::Token;
use git2::{Repository, SubmoduleIgnore};
use kakarot_rpc_core::client::api::KakarotStarknetApi;
use kakarot_rpc_core::test_utils::constants::STARKNET_DEPLOYER_ACCOUNT_PRIVATE_KEY;
use kakarot_rpc_core::test_utils::deploy_helpers::{
use kakarot_test_utils::constants::STARKNET_DEPLOYER_ACCOUNT_PRIVATE_KEY;
use kakarot_test_utils::deploy_helpers::{
ContractDeploymentArgs, DeployerAccount, KakarotTestEnvironmentContext, TestContext,
};
use starknet::accounts::Account;
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ use ethers::signers::{LocalWallet as EthersLocalWallet, Signer};
use ethers::types::Address as EthersAddress;
use ethers_solc::artifacts::CompactContractBytecode;
use foundry_config::utils::{find_project_root_path, load_config};
use kakarot_rpc_core::client::api::KakarotStarknetApi;
use kakarot_rpc_core::client::config::{Network, StarknetConfig as StarknetClientConfig};
use kakarot_rpc_core::client::constants::{CHAIN_ID, DEPLOY_FEE, STARKNET_NATIVE_TOKEN};
use kakarot_rpc_core::client::waiter::TransactionWaiter;
use kakarot_rpc_core::client::KakarotClient;
use kakarot_rpc_core::contracts::kakarot::KakarotContract;
use kakarot_rpc_core::models::felt::Felt252Wrapper;
use katana_core::db::serde::state::SerializableState;
use reth_primitives::{
sign_message, Address, Bytes, Transaction, TransactionKind, TransactionSigned, TxEip1559, H256, U256,
Expand All @@ -32,14 +39,7 @@ use url::Url;

use super::constants::{EVM_CONTRACTS, STARKNET_DEPLOYER_ACCOUNT_PRIVATE_KEY};
use super::execution_helpers::execute_and_wait_for_tx;
use crate::client::api::KakarotStarknetApi;
use crate::client::config::{Network, StarknetConfig as StarknetClientConfig};
use crate::client::constants::{CHAIN_ID, DEPLOY_FEE, STARKNET_NATIVE_TOKEN};
use crate::client::waiter::TransactionWaiter;
use crate::client::KakarotClient;
use crate::contracts::kakarot::KakarotContract;
use crate::models::felt::Felt252Wrapper;
use crate::test_utils::constants::EOA_WALLET;
use crate::constants::EOA_WALLET;

/// Macro to find the root path of the project.
///
Expand Down Expand Up @@ -129,7 +129,7 @@ pub fn get_contract(filename: &str) -> CompactContractBytecode {
/// # Example
///
/// ```no_run
/// # use kakarot_rpc_core::test_utils::deploy_helpers::{get_contract, encode_contract, get_contract_abi, get_contract_bytecode};
/// # use kakarot_test_utils::deploy_helpers::{get_contract, encode_contract, get_contract_abi, get_contract_bytecode};
/// # use ethers::abi::Abi;
/// let contract = get_contract("MyContract");
/// let abi = get_contract_abi(&contract);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::sync::Arc;

use ethers::signers::{LocalWallet, Signer};
use kakarot_rpc_core::client::api::KakarotEthApi;
use kakarot_rpc_core::client::waiter::TransactionWaiter;
use reth_primitives::{Address, BlockId, H256, U256};
use starknet::accounts::{Account, Call, ConnectedAccount, SingleOwnerAccount};
use starknet::core::types::InvokeTransactionResult;
Expand All @@ -9,8 +11,6 @@ use starknet::providers::JsonRpcClient;
use starknet::signers::LocalWallet as StarknetLocalWallet;

use super::deploy_helpers::{create_eth_transfer_tx, create_raw_ethereum_tx, KakarotTestEnvironmentContext};
use crate::client::api::KakarotEthApi;
use crate::client::waiter::TransactionWaiter;

pub async fn execute_and_wait_for_tx(
account: &SingleOwnerAccount<JsonRpcClient<HttpTransport>, StarknetLocalWallet>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use futures::executor::block_on;
use rstest::*;

use crate::test_utils::deploy_helpers::KakarotTestEnvironmentContext;
use crate::deploy_helpers::KakarotTestEnvironmentContext;

#[fixture]
pub fn kakarot_test_env_ctx() -> KakarotTestEnvironmentContext {
Expand Down
File renamed without changes.

0 comments on commit 46cddc6

Please sign in to comment.