Skip to content

Commit

Permalink
feat: add genesis loader utility to runtimes (keep-starknet-strange#913)
Browse files Browse the repository at this point in the history
  • Loading branch information
greged93 authored and a-moreira committed Aug 2, 2023
1 parent ecb0f7d commit 06235f3
Show file tree
Hide file tree
Showing 17 changed files with 821 additions and 419 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
## Next release

- ci: add `CHANGELOG.md` and enforce it is edited for each PR on `main`
- feat: add a `genesis_loader` for the node and mocking
4 changes: 2 additions & 2 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
Expand Up @@ -163,7 +163,7 @@ futures = { version = "0.3.28", default-features = false }
futures-timer = { version = "3.0.2", default-features = false }
serde = { version = "1.0.175", default-features = false }
serde_json = { version = "1.0.100", default-features = false }
serde_with = "2.3.3"
serde_with = { version = "2.3.3", default-features = false }
bitvec = { version = "1", default-features = false }
thiserror = "1.0.44"
thiserror-no-std = "2.0.2"
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ Get started with our comprehensive documentation, which covers everything from
project structure and architecture to benchmarking and running Madara:

- [Architecture Overview](./docs/architecture.md)
- [Chain Genesis Information](./docs/genesis.md)
- [Project Structure](./docs/project-structure.md)
- [Getting Started Guide](./docs/getting-started.md)
- [Run benchmark yourself](./benchmarking/README.md)
Expand Down
194 changes: 6 additions & 188 deletions crates/node/src/chain_spec.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
use std::fs;
use std::path::PathBuf;

use madara_runtime::{AuraConfig, EnableManualSeal, GenesisConfig, GrandpaConfig, SystemConfig, WASM_BINARY};
use mp_starknet::execution::types::Felt252Wrapper;
use mp_starknet::starknet_serde::get_contract_class;
use pallet_starknet::types::ContractStorageKeyWrapper;
use pallet_starknet::genesis_loader::{read_file_to_string, GenesisLoader};
use sc_service::ChainType;
use serde::{Deserialize, Serialize};
use sp_consensus_aura::sr25519::AuthorityId as AuraId;
use sp_consensus_grandpa::AuthorityId as GrandpaId;
use sp_core::storage::Storage;
use sp_core::{Pair, Public, H256};
use sp_core::{Pair, Public};
use sp_state_machine::BasicExternalities;
use starknet_core::types::FieldElement;
use starknet_core::utils::get_storage_var_address;

use super::constants::*;

/// Specialized `ChainSpec`. This is a specialization of the general Substrate ChainSpec type.
pub type ChainSpec = sc_service::GenericChainSpec<GenesisConfig>;
Expand Down Expand Up @@ -136,103 +127,15 @@ pub fn local_testnet_config() -> Result<ChainSpec, String> {
))
}

/// Returns the storage key for a given storage name, keys and offset.
/// Calculates pedersen(sn_keccak(storage_name), keys) + storage_key_offset which is the key in the
/// starknet contract for storage_name(key_1, key_2, ..., key_n).
/// https://docs.starknet.io/documentation/architecture_and_concepts/Contracts/contract-storage/#storage_variables
pub fn get_storage_key(
address: &Felt252Wrapper,
storage_name: &str,
keys: &[Felt252Wrapper],
storage_key_offset: u64,
) -> ContractStorageKeyWrapper {
let storage_key_offset = H256::from_low_u64_be(storage_key_offset);
let mut storage_key = get_storage_var_address(
storage_name,
keys.iter().map(|x| FieldElement::from(*x)).collect::<Vec<_>>().as_slice(),
)
.unwrap();
storage_key += FieldElement::from_bytes_be(&storage_key_offset.to_fixed_bytes()).unwrap();
(*address, storage_key.into())
}

fn read_file_to_string(path: &str) -> String {
let cargo_dir = String::from(env!("CARGO_MANIFEST_DIR"));
let path: PathBuf = [cargo_dir + "/" + path].iter().collect();
fs::read_to_string(path).unwrap()
}

/// Configure initial storage state for FRAME modules.
fn testnet_genesis(
wasm_binary: &[u8],
initial_authorities: Vec<(AuraId, GrandpaId)>,
_enable_println: bool,
) -> GenesisConfig {
// ACCOUNT CONTRACT
let no_validate_account_class =
get_contract_class(&read_file_to_string("../../cairo-contracts/build/NoValidateAccount.json"), 0);
let no_validate_account_class_hash = Felt252Wrapper::from_hex_be(NO_VALIDATE_ACCOUNT_CLASS_HASH).unwrap();
let no_validate_account_address = Felt252Wrapper::from_hex_be(NO_VALIDATE_ACCOUNT_ADDRESS).unwrap();

// ARGENT ACCOUNT CONTRACT
let argent_account_class =
get_contract_class(&read_file_to_string("../../cairo-contracts/build/ArgentAccount.json"), 0);
let argent_account_class_hash = Felt252Wrapper::from_hex_be(ARGENT_ACCOUNT_CLASS_HASH).unwrap();
let argent_account_address = Felt252Wrapper::from_hex_be(ARGENT_ACCOUNT_ADDRESS).unwrap();
let argent_proxy_class = get_contract_class(&read_file_to_string("../../cairo-contracts/build/Proxy.json"), 0);
let argent_proxy_class_hash = Felt252Wrapper::from_hex_be(ARGENT_PROXY_CLASS_HASH).unwrap();

// BRAAVOS ACCOUNT CONTRACT
let braavos_account_class = get_contract_class(&read_file_to_string("src/genesis_assets/Account.json"), 0);
let braavos_account_class_hash = Felt252Wrapper::from_hex_be(BRAAVOS_ACCOUNT_CLASS_HASH).unwrap();
let braavos_account_base_impl_class =
get_contract_class(&read_file_to_string("src/genesis_assets/AccountBaseImpl.json"), 0);
let braavos_account_base_impl_class_hash =
Felt252Wrapper::from_hex_be(BRAAVOS_ACCOUNT_BASE_IMPL_CLASS_HASH).unwrap();
let braavos_call_aggregator_class =
get_contract_class(&read_file_to_string("src/genesis_assets/CallAggregator.json"), 0);
let braavos_call_aggregator_class_hash = Felt252Wrapper::from_hex_be(BRAAVOS_CALL_AGGREGATOR_CLASS_HASH).unwrap();
let braavos_proxy_class = get_contract_class(&read_file_to_string("../../cairo-contracts/build/Proxy.json"), 0);
let braavos_proxy_class_hash = Felt252Wrapper::from_hex_be(BRAAVOS_PROXY_CLASS_HASH).unwrap();

// OZ ACCOUNT CONTRACT
let oz_account_class =
get_contract_class(&read_file_to_string("../../cairo-contracts/build/OpenzeppelinAccount.json"), 0);
let oz_account_class_hash = Felt252Wrapper::from_hex_be(OZ_ACCOUNT_CLASS_HASH).unwrap();
let oz_account_address = Felt252Wrapper::from_hex_be(OZ_ACCOUNT_ADDRESS).unwrap();

// CAIRO 1 ACCOUNT CONTRACT
let cairo_1_no_validate_account_class =
get_contract_class(&read_file_to_string("../../cairo-contracts/build/cairo_1/NoValidateAccount.casm.json"), 1);
let cairo_1_no_validate_account_class_hash =
Felt252Wrapper::from_hex_be(CAIRO_1_NO_VALIDATE_ACCOUNT_CLASS_HASH).unwrap();
let cairo_1_no_validate_account_address = Felt252Wrapper::from_hex_be(CAIRO_1_NO_VALIDATE_ACCOUNT_ADDRESS).unwrap();

// TEST CONTRACT
let test_contract_class = get_contract_class(&read_file_to_string("../../cairo-contracts/build/test.json"), 0);
let test_contract_class_hash = Felt252Wrapper::from_hex_be(TEST_CONTRACT_CLASS_HASH).unwrap();
let test_contract_address = Felt252Wrapper::from_hex_be(TEST_CONTRACT_ADDRESS).unwrap();

// Fee token
let fee_token_address = Felt252Wrapper::from_hex_be(FEE_TOKEN_ADDRESS).unwrap();
let fee_token_class_hash = Felt252Wrapper::from_hex_be(FEE_TOKEN_CLASS_HASH).unwrap();

// ERC20 CONTRACT
let erc20_class = get_contract_class(&read_file_to_string("../../cairo-contracts/build/ERC20.json"), 0);
let token_class_hash = Felt252Wrapper::from_hex_be(ERC20_CLASS_HASH).unwrap();
let token_contract_address = Felt252Wrapper::from_hex_be(ERC20_ADDRESS).unwrap();

// ERC721 CONTRACT
let erc721_class = get_contract_class(&read_file_to_string("../../cairo-contracts/build/ERC721.json"), 0);
let nft_class_hash = Felt252Wrapper::from_hex_be(ERC721_CLASS_HASH).unwrap();
let nft_contract_address = Felt252Wrapper::from_hex_be(ERC721_ADDRESS).unwrap();

// UDC CONTRACT
let udc_class = get_contract_class(&read_file_to_string("../../cairo-contracts/build/UniversalDeployer.json"), 0);
let udc_class_hash = Felt252Wrapper::from_hex_be(UDC_CLASS_HASH).unwrap();
let udc_contract_address = Felt252Wrapper::from_hex_be(UDC_CONTRACT_ADDRESS).unwrap();

let public_key = Felt252Wrapper::from_hex_be(PUBLIC_KEY).unwrap();
let genesis: GenesisLoader =
serde_json::from_str(&read_file_to_string("crates/node/src/genesis_assets/genesis.json")).unwrap();
let starknet_genesis: madara_runtime::pallet_starknet::GenesisConfig<_> = genesis.into();

GenesisConfig {
system: SystemConfig {
Expand All @@ -244,91 +147,6 @@ fn testnet_genesis(
// Deterministic finality mechanism used for block finalization
grandpa: GrandpaConfig { authorities: initial_authorities.iter().map(|x| (x.1.clone(), 1)).collect() },
/// Starknet Genesis configuration.
starknet: madara_runtime::pallet_starknet::GenesisConfig {
contracts: vec![
(no_validate_account_address, no_validate_account_class_hash),
(cairo_1_no_validate_account_address, cairo_1_no_validate_account_class_hash),
(test_contract_address, test_contract_class_hash),
(token_contract_address, token_class_hash),
(nft_contract_address, nft_class_hash),
(fee_token_address, fee_token_class_hash),
(argent_account_address, argent_account_class_hash),
(oz_account_address, oz_account_class_hash),
(udc_contract_address, udc_class_hash),
],
contract_classes: vec![
(no_validate_account_class_hash, no_validate_account_class),
(cairo_1_no_validate_account_class_hash, cairo_1_no_validate_account_class),
(argent_account_class_hash, argent_account_class),
(oz_account_class_hash, oz_account_class),
(argent_proxy_class_hash, argent_proxy_class),
(test_contract_class_hash, test_contract_class),
(token_class_hash, erc20_class.clone()),
(fee_token_class_hash, erc20_class),
(nft_class_hash, erc721_class),
(udc_class_hash, udc_class),
(braavos_account_class_hash, braavos_account_class),
(braavos_account_base_impl_class_hash, braavos_account_base_impl_class),
(braavos_call_aggregator_class_hash, braavos_call_aggregator_class),
(braavos_proxy_class_hash, braavos_proxy_class),
],
storage: vec![
(
get_storage_key(&fee_token_address, "ERC20_balances", &[no_validate_account_address], 0),
Felt252Wrapper::from(u128::MAX),
),
(
get_storage_key(&fee_token_address, "ERC20_balances", &[no_validate_account_address], 1),
Felt252Wrapper::from(u128::MAX),
),
(
get_storage_key(&fee_token_address, "ERC20_balances", &[cairo_1_no_validate_account_address], 0),
Felt252Wrapper::from(u128::MAX),
),
(
get_storage_key(&fee_token_address, "ERC20_balances", &[cairo_1_no_validate_account_address], 1),
Felt252Wrapper::from(u128::MAX),
),
(
get_storage_key(&fee_token_address, "ERC20_balances", &[oz_account_address], 0),
Felt252Wrapper::from(u128::MAX),
),
(
get_storage_key(&fee_token_address, "ERC20_balances", &[oz_account_address], 1),
Felt252Wrapper::from(u128::MAX),
),
(
get_storage_key(&fee_token_address, "ERC20_balances", &[argent_account_address], 0),
Felt252Wrapper::from(u128::MAX),
),
(
get_storage_key(&token_contract_address, "ERC20_balances", &[no_validate_account_address], 0),
Felt252Wrapper::from(u128::MAX),
),
(
get_storage_key(&token_contract_address, "ERC20_balances", &[no_validate_account_address], 1),
Felt252Wrapper::from(u128::MAX),
),
(
get_storage_key(&fee_token_address, "ERC20_balances", &[public_key], 0),
Felt252Wrapper::from(u128::MAX),
),
(
get_storage_key(&argent_account_address, "_signer", &[], 0),
Felt252Wrapper::from_hex_be(PUBLIC_KEY).unwrap(),
),
(
get_storage_key(&oz_account_address, "Account_public_key", &[], 0),
Felt252Wrapper::from_hex_be(PUBLIC_KEY).unwrap(),
),
(
get_storage_key(&nft_contract_address, "Ownable_owner", &[], 0),
Felt252Wrapper::from_hex_be(NO_VALIDATE_ACCOUNT_ADDRESS).unwrap(),
),
],
fee_token_address,
_phantom: Default::default(),
seq_addr_updated: true,
},
starknet: starknet_genesis,
}
}
30 changes: 0 additions & 30 deletions crates/node/src/constants.rs

This file was deleted.

Loading

0 comments on commit 06235f3

Please sign in to comment.