Skip to content

Commit

Permalink
Merge branch 'main' into genesis3
Browse files Browse the repository at this point in the history
  • Loading branch information
ocdbytes authored Sep 17, 2024
2 parents 77c10bf + e6531e8 commit b454671
Show file tree
Hide file tree
Showing 15 changed files with 157 additions and 66 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
cargo build --bin madara --profile dev
export COVERAGE_BIN=$(realpath target/debug/madara)
rm -f target/madara-* lcov.info
cargo test --profile dev
cargo test --profile dev -- --test-threads=1
- name: Generate coverage info
run: |
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
## Next release

- fix: UDC cairo 0 migration & events logic fix
- fix: generate a fixed set of public and private keys for devnet
- fix: defaulted l1 gas price in devnet mode
- fix: fixed anvil port value in tests
- fix: flaky tests in gas price worker fixed
- ci: add coveralls report
- test: added tests for declare and deploy transactions
- fix: pending block must always be returned in rpc even if none is in db
Expand Down
42 changes: 42 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ httpmock = "0.7.0"
tempfile = "3.10.1"
env_logger = "0.11.3"
mockall = "0.13.0"
serial_test = "3.1.1"

[patch.crates-io]
starknet-core = { git = "https://github.com/kasarlabs/starknet-rs.git", branch = "fork" }
Expand Down
1 change: 1 addition & 0 deletions cairo/.tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
scarb 2.8.2
1 change: 1 addition & 0 deletions crates/client/devnet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,6 @@ starknet_api.workspace = true
# Other
anyhow.workspace = true
log.workspace = true
rand.workspace = true
serde_json.workspace = true
tokio.workspace = true
23 changes: 20 additions & 3 deletions crates/client/devnet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use mp_block::header::GasPrices;
use mp_chain_config::ChainConfig;
use mp_convert::ToFelt;
use mp_state_update::{ContractStorageDiffItem, StateDiff, StorageEntry};
use rand::rngs::StdRng;
use rand::{RngCore, SeedableRng};
use starknet_api::{core::ContractAddress, state::StorageKey};
use starknet_signers::SigningKey;
use starknet_types_core::felt::Felt;
Expand Down Expand Up @@ -117,10 +119,20 @@ impl ChainGenesisDescription {
get_storage_var_address("Account_public_key", &[])
}

pub fn from_seed(seed: u64) -> Felt {
// Use a fixed seed for deterministic RNG
let mut rng = StdRng::seed_from_u64(seed);
let mut buffer = [0u8; 32];
rng.fill_bytes(&mut buffer);

Felt::from_bytes_be_slice(&buffer)
}

DevnetKeys(
(0..n_addr)
.map(|_| {
let key = SigningKey::from_random();
.map(|addr_idx| {
let secret_scalar = from_seed(addr_idx);
let key = SigningKey::from_secret_scalar(secret_scalar);
let pubkey = key.verifying_key();

// calculating actual address w.r.t. the class hash.
Expand Down Expand Up @@ -309,7 +321,12 @@ mod tests {
println!("{:?}", block.state_diff);
tokio::runtime::Runtime::new()
.unwrap()
.block_on(importer.add_block(block, BlockValidationContext::new(chain_config.chain_id.clone())))
.block_on(
importer.add_block(
block,
BlockValidationContext::new(chain_config.chain_id.clone()).trust_class_hashes(true),
),
)
.unwrap();

log::debug!("{:?}", backend.get_block_info(&BlockId::Tag(BlockTag::Latest)));
Expand Down
1 change: 1 addition & 0 deletions crates/client/eth/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,4 @@ dotenv = { workspace = true }
prometheus = { workspace = true }
httpmock = { workspace = true }
tracing-test = "0.2.5"
serial_test = { workspace = true }
78 changes: 40 additions & 38 deletions crates/client/eth/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,50 +142,31 @@ pub mod eth_client_getter_test {
primitives::U256,
};
use mc_metrics::MetricsService;
use rstest::*;
use serial_test::serial;
use tokio;

// https://etherscan.io/tx/0xcadb202495cd8adba0d9b382caff907abf755cd42633d23c4988f875f2995d81#eventlog
// The txn we are referring to it is here ^
const L1_BLOCK_NUMBER: u64 = 20395662;
const FORK_URL: &str = "https://eth.merkle.io";
const ANVIL_PORT: u16 = 8545;
const CORE_CONTRACT_ADDRESS: &str = "0xc662c410C0ECf747543f5bA90660f6ABeBD9C8c4";
const L2_BLOCK_NUMBER: u64 = 662703;
const L2_BLOCK_HASH: &str = "563216050958639290223177746678863910249919294431961492885921903486585884664";
const L2_STATE_ROOT: &str = "1456190284387746219409791261254265303744585499659352223397867295223408682130";

#[fixture]
#[once]
fn anvil_instance() -> AnvilInstance {
fn create_anvil_instance() -> AnvilInstance {
let anvil = Anvil::new()
.fork(FORK_URL)
.fork_block_number(L1_BLOCK_NUMBER)
.port(ANVIL_PORT)
.try_spawn()
.expect("failed to spawn anvil instance");
println!("Anvil started and running at `{}`", anvil.endpoint());
anvil
}

#[rstest]
#[tokio::test]
async fn fail_create_new_client_invalid_core_contract(anvil_instance: &AnvilInstance) {
let anvil = anvil_instance;
// Sepolia core contract instead of mainnet
const INVALID_CORE_CONTRACT_ADDRESS: &str = "0xE2Bb56ee936fd6433DC0F6e7e3b8365C906AA057";

let rpc_url: Url = anvil.endpoint_url();

let core_contract_address = Address::parse_checksummed(INVALID_CORE_CONTRACT_ADDRESS, None).unwrap();
let prometheus_service = MetricsService::new(true, false, 9615).unwrap();
let l1_block_metrics = L1BlockMetrics::register(&prometheus_service.registry()).unwrap();

let new_client_result = EthereumClient::new(rpc_url, core_contract_address, l1_block_metrics).await;
assert!(new_client_result.is_err(), "EthereumClient::new should fail with an invalid core contract address");
}

pub fn create_ethereum_client(url: Option<&str>) -> EthereumClient {
let rpc_url_string = url.unwrap_or("http://localhost:8545").to_string();
let rpc_url: Url = rpc_url_string.parse().expect("issue while parsing URL");
let rpc_url: Url = url.unwrap_or("http://localhost:8545").parse().expect("issue while parsing URL");

let provider = ProviderBuilder::new().on_http(rpc_url.clone());
let address = Address::parse_checksummed(CORE_CONTRACT_ADDRESS, None).unwrap();
Expand All @@ -197,50 +178,71 @@ pub mod eth_client_getter_test {
EthereumClient { provider: Arc::new(provider), l1_core_contract: contract.clone(), l1_block_metrics }
}

#[fixture]
#[once]
pub fn eth_client(anvil_instance: &AnvilInstance) -> EthereumClient {
create_ethereum_client(Some(&anvil_instance.endpoint()))
#[serial]
#[tokio::test]
async fn fail_create_new_client_invalid_core_contract() {
let anvil = create_anvil_instance();
// Sepolia core contract instead of mainnet
const INVALID_CORE_CONTRACT_ADDRESS: &str = "0xE2Bb56ee936fd6433DC0F6e7e3b8365C906AA057";

let rpc_url: Url = anvil.endpoint_url();

let core_contract_address = Address::parse_checksummed(INVALID_CORE_CONTRACT_ADDRESS, None).unwrap();
let prometheus_service = MetricsService::new(true, false, 9615).unwrap();
let l1_block_metrics = L1BlockMetrics::register(&prometheus_service.registry()).unwrap();

let new_client_result = EthereumClient::new(rpc_url, core_contract_address, l1_block_metrics).await;
assert!(new_client_result.is_err(), "EthereumClient::new should fail with an invalid core contract address");
}

#[rstest]
#[serial]
#[tokio::test]
async fn get_latest_block_number_works(eth_client: &EthereumClient) {
async fn get_latest_block_number_works() {
let anvil = create_anvil_instance();
let eth_client = create_ethereum_client(Some(anvil.endpoint().as_str()));
let block_number =
eth_client.provider.get_block_number().await.expect("issue while fetching the block number").as_u64();
assert_eq!(block_number, L1_BLOCK_NUMBER, "provider unable to get the correct block number");
}

#[rstest]
#[serial]
#[tokio::test]
async fn get_last_event_block_number_works(eth_client: &EthereumClient) {
async fn get_last_event_block_number_works() {
let anvil = create_anvil_instance();
let eth_client = create_ethereum_client(Some(anvil.endpoint().as_str()));
let block_number = eth_client
.get_last_event_block_number::<StarknetCoreContract::LogStateUpdate>()
.await
.expect("issue while getting the last block number with given event");
assert_eq!(block_number, L1_BLOCK_NUMBER, "block number with given event not matching");
}

#[rstest]
#[serial]
#[tokio::test]
async fn get_last_verified_block_hash_works(eth_client: &EthereumClient) {
async fn get_last_verified_block_hash_works() {
let anvil = create_anvil_instance();
let eth_client = create_ethereum_client(Some(anvil.endpoint().as_str()));
let block_hash =
eth_client.get_last_verified_block_hash().await.expect("issue while getting the last verified block hash");
let expected = u256_to_felt(U256::from_str_radix(L2_BLOCK_HASH, 10).unwrap()).unwrap();
assert_eq!(block_hash, expected, "latest block hash not matching");
}

#[rstest]
#[serial]
#[tokio::test]
async fn get_last_state_root_works(eth_client: &EthereumClient) {
async fn get_last_state_root_works() {
let anvil = create_anvil_instance();
let eth_client = create_ethereum_client(Some(anvil.endpoint().as_str()));
let state_root = eth_client.get_last_state_root().await.expect("issue while getting the state root");
let expected = u256_to_felt(U256::from_str_radix(L2_STATE_ROOT, 10).unwrap()).unwrap();
assert_eq!(state_root, expected, "latest block state root not matching");
}

#[rstest]
#[serial]
#[tokio::test]
async fn get_last_verified_block_number_works(eth_client: &EthereumClient) {
async fn get_last_verified_block_number_works() {
let anvil = create_anvil_instance();
let eth_client = create_ethereum_client(Some(anvil.endpoint().as_str()));
let block_number = eth_client.get_last_verified_block_number().await.expect("issue");
assert_eq!(block_number, L2_BLOCK_NUMBER, "verified block number not matching");
}
Expand Down
Loading

0 comments on commit b454671

Please sign in to comment.