Skip to content

Commit

Permalink
fix(devnet): generate a fixed set of public and private keys for devn…
Browse files Browse the repository at this point in the history
…et (#261)
  • Loading branch information
antiyro authored Sep 16, 2024
1 parent 0ac1f62 commit e35c681
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 4 deletions.
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

- 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
Expand Down
1 change: 1 addition & 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 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 @@ -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.
Expand Down Expand Up @@ -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)));
Expand Down
5 changes: 4 additions & 1 deletion crates/node/src/service/block_production.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")?;

Expand Down

0 comments on commit e35c681

Please sign in to comment.