From e35c6810f2efe874c9bec3f7cf73d0baea0048a8 Mon Sep 17 00:00:00 2001 From: antiyro <74653697+antiyro@users.noreply.github.com> Date: Mon, 16 Sep 2024 17:23:20 +0100 Subject: [PATCH] fix(devnet): generate a fixed set of public and private keys for devnet (#261) --- CHANGELOG.md | 1 + Cargo.lock | 1 + crates/client/devnet/Cargo.toml | 1 + crates/client/devnet/src/lib.rs | 23 ++++++++++++++++++--- crates/node/src/service/block_production.rs | 5 ++++- 5 files changed, 27 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3148614f8..ca5847353 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Next release +- 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 - ci: add coveralls report diff --git a/Cargo.lock b/Cargo.lock index 5a7a7bbfb..ca670ee12 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5472,6 +5472,7 @@ dependencies = [ "mp-transactions", "proptest", "proptest-derive", + "rand", "rstest 0.18.2", "serde_json", "starknet-core", diff --git a/crates/client/devnet/Cargo.toml b/crates/client/devnet/Cargo.toml index 063e269eb..c994c07da 100644 --- a/crates/client/devnet/Cargo.toml +++ b/crates/client/devnet/Cargo.toml @@ -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 diff --git a/crates/client/devnet/src/lib.rs b/crates/client/devnet/src/lib.rs index 69e2c338b..886ee5ffe 100644 --- a/crates/client/devnet/src/lib.rs +++ b/crates/client/devnet/src/lib.rs @@ -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; @@ -115,10 +117,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. @@ -307,7 +319,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))); diff --git a/crates/node/src/service/block_production.rs b/crates/node/src/service/block_production.rs index d0522bfb0..cd7a7a692 100644 --- a/crates/node/src/service/block_production.rs +++ b/crates/node/src/service/block_production.rs @@ -79,7 +79,10 @@ impl Service for BlockProductionService { .context("Building genesis block from devnet config")?; block_import - .add_block(genesis_block, BlockValidationContext::new(backend.chain_config().chain_id.clone())) + .add_block( + genesis_block, + BlockValidationContext::new(backend.chain_config().chain_id.clone()).trust_class_hashes(true), + ) .await .context("Importing devnet genesis block")?;