Skip to content

Commit

Permalink
refactor(katana): include class artifact in genesis file + base64 enc…
Browse files Browse the repository at this point in the history
…oding (#1508)
  • Loading branch information
kariy authored Feb 2, 2024
1 parent 15c71cc commit dfe390a
Show file tree
Hide file tree
Showing 9 changed files with 5,073 additions and 163 deletions.
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.

20 changes: 16 additions & 4 deletions bin/katana/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::path::PathBuf;

use katana_primitives::genesis::json::GenesisJsonWithBasePath;
use katana_primitives::genesis::json::GenesisJson;
use katana_primitives::genesis::Genesis;

pub fn parse_seed(seed: &str) -> [u8; 32] {
Expand All @@ -15,8 +15,20 @@ pub fn parse_seed(seed: &str) -> [u8; 32] {
}
}

pub fn parse_genesis(path: &str) -> Result<Genesis, anyhow::Error> {
let path = PathBuf::from(shellexpand::full(path)?.into_owned());
let genesis = Genesis::try_from(GenesisJsonWithBasePath::new(path)?)?;
/// Used as clap value parser for [Genesis].
pub fn parse_genesis(value: &str) -> Result<Genesis, anyhow::Error> {
let path = PathBuf::from(shellexpand::full(value)?.into_owned());
let genesis = Genesis::try_from(GenesisJson::load(path)?)?;
Ok(genesis)
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn parse_genesis_file() {
let path = "./tests/test-data/genesis.json";
parse_genesis(path).unwrap();
}
}
42 changes: 42 additions & 0 deletions bin/katana/tests/test-data/genesis.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"number": 0,
"parentHash": "0x999",
"timestamp": 5123512314,
"stateRoot": "0x99",
"sequencerAddress": "0x100",
"gasPrices": {
"ETH": 1111,
"STRK": 2222
},
"feeToken": {
"address": "0x55",
"name": "ETHER",
"symbol": "ETH",
"decimals": 18,
"storage": {
"0x111": "0x1",
"0x222": "0x2"
}
},
"universalDeployer": {
"address": "0x041a78e741e5af2fec34b695679bc6891742439f7afb8484ecd7766661ad02bf",
"storage": {
"0x10": "0x100"
}
},
"accounts": {
"0x66efb28ac62686966ae85095ff3a772e014e7fbf56d4c5f6fac5606d4dde23a": {
"publicKey": "0x1",
"balance": "0xD3C21BCECCEDA1000000",
"nonce": "0x1",
"storage": {
"0x1": "0x1",
"0x2": "0x2"
}
}
},
"contracts": {
},
"classes": [
]
}
1 change: 1 addition & 0 deletions crates/katana/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ version.workspace = true

[dependencies]
anyhow.workspace = true
base64.workspace = true
cairo-vm.workspace = true
derive_more.workspace = true
lazy_static = "1.4.0"
Expand Down
12 changes: 6 additions & 6 deletions crates/katana/primitives/src/genesis/allocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use derive_more::{Deref, DerefMut};
use ethers::types::U256;
use rand::rngs::SmallRng;
use rand::{RngCore, SeedableRng};
use serde::Serialize;
use serde::{Deserialize, Serialize};
use starknet::core::serde::unsigned_field_element::UfeHex;
use starknet::core::utils::get_contract_address;
use starknet::signers::SigningKey;
Expand All @@ -15,7 +15,7 @@ use crate::contract::{ClassHash, ContractAddress, StorageKey, StorageValue};
use crate::FieldElement;

/// Represents a contract allocation in the genesis block.
#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(untagged)]
pub enum GenesisAllocation {
/// Account contract
Expand Down Expand Up @@ -67,7 +67,7 @@ impl GenesisAllocation {
}

/// Genesis allocation for account contract.
#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(untagged)]
pub enum GenesisAccountAlloc {
/// Account contract with hidden private key.
Expand Down Expand Up @@ -123,7 +123,7 @@ impl GenesisAccountAlloc {

/// A generic non-account contract.
#[serde_with::serde_as]
#[derive(Debug, Default, Clone, Serialize, PartialEq, Eq)]
#[derive(Debug, Default, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct GenesisContractAlloc {
/// The class hash of the contract.
#[serde_as(as = "UfeHex")]
Expand All @@ -141,7 +141,7 @@ pub struct GenesisContractAlloc {
/// Used mainly for development purposes where the account info including the
/// private key is printed to the console.
#[serde_with::serde_as]
#[derive(Debug, Default, Clone, Serialize, PartialEq, Eq, Deref, DerefMut)]
#[derive(Debug, Default, Clone, Serialize, Deserialize, PartialEq, Eq, Deref, DerefMut)]
pub struct DevGenesisAccount {
/// The private key associated with the public key of the account.
#[serde_as(as = "UfeHex")]
Expand Down Expand Up @@ -175,7 +175,7 @@ impl DevGenesisAccount {

/// Account contract allocated in the genesis block.
#[serde_with::serde_as]
#[derive(Debug, Default, Clone, Serialize, PartialEq, Eq)]
#[derive(Debug, Default, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct GenesisAccount {
/// The public key associated with the account for validation.
#[serde_as(as = "UfeHex")]
Expand Down
Loading

0 comments on commit dfe390a

Please sign in to comment.