Skip to content

Commit

Permalink
feat(katana): genesis storage for fee token and udc (#1503)
Browse files Browse the repository at this point in the history
  • Loading branch information
kariy authored Feb 1, 2024
1 parent 042a2c0 commit 17c0b14
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 4 deletions.
24 changes: 23 additions & 1 deletion crates/katana/primitives/src/genesis/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ pub struct FeeTokenConfigJson {
/// The class hash of the fee token contract.
/// If not provided, the default fee token class is used.
pub class: Option<ClassHash>,
/// To initialize the fee token contract storage
pub storage: Option<HashMap<StorageKey, StorageValue>>,
}

#[derive(Debug, Clone, Deserialize)]
Expand All @@ -64,6 +66,8 @@ pub struct UniversalDeployerConfigJson {
/// The class hash of the universal deployer contract.
/// If not provided, the default UD class is used.
pub class: Option<ClassHash>,
/// To initialize the UD contract storage
pub storage: Option<HashMap<StorageKey, StorageValue>>,
}

#[derive(Debug, Clone, Deserialize)]
Expand Down Expand Up @@ -241,6 +245,7 @@ impl TryFrom<GenesisJsonWithBasePath> for Genesis {
decimals: value.fee_token.decimals,
address: value.fee_token.address.unwrap_or(DEFAULT_FEE_TOKEN_ADDRESS),
class_hash: value.fee_token.class.unwrap_or(DEFAULT_LEGACY_ERC20_CONTRACT_CLASS_HASH),
storage: value.fee_token.storage,
};

match value.fee_token.class {
Expand Down Expand Up @@ -273,13 +278,15 @@ impl TryFrom<GenesisJsonWithBasePath> for Genesis {
Some(UniversalDeployerConfig {
class_hash: hash,
address: config.address.unwrap_or(DEFAULT_UDC_ADDRESS),
storage: config.storage,
})
}

// if no class hash is provided, use the default UD contract parameters
None => {
let class_hash = DEFAULT_LEGACY_UDC_CLASS_HASH;
let address = config.address.unwrap_or(DEFAULT_UDC_ADDRESS);
let storage = config.storage;

let _ = classes.insert(
DEFAULT_LEGACY_UDC_CLASS_HASH,
Expand All @@ -290,7 +297,7 @@ impl TryFrom<GenesisJsonWithBasePath> for Genesis {
},
);

Some(UniversalDeployerConfig { class_hash, address })
Some(UniversalDeployerConfig { class_hash, address, storage })
}
}
} else {
Expand Down Expand Up @@ -430,6 +437,10 @@ mod tests {
assert_eq!(genesis.fee_token.symbol, String::from("ETH"));
assert_eq!(genesis.fee_token.class, Some(felt!("0x8")));
assert_eq!(genesis.fee_token.decimals, 18);
assert_eq!(
genesis.fee_token.storage,
Some(HashMap::from([(felt!("0x111"), felt!("0x1")), (felt!("0x222"), felt!("0x2"))]))
);

assert_eq!(
genesis.universal_deployer.clone().unwrap().address,
Expand All @@ -438,6 +449,10 @@ mod tests {
)))
);
assert_eq!(genesis.universal_deployer.unwrap().class, None);
assert_eq!(
genesis.fee_token.storage,
Some(HashMap::from([(felt!("0x111"), felt!("0x1")), (felt!("0x222"), felt!("0x2")),]))
);

let acc_1 = ContractAddress::from(felt!(
"0x66efb28ac62686966ae85095ff3a772e014e7fbf56d4c5f6fac5606d4dde23a"
Expand Down Expand Up @@ -564,6 +579,10 @@ mod tests {
total_supply: U256::from_str("0xD3C21BCECCEDA1000000").unwrap() * 4,
decimals: 18,
class_hash: felt!("0x8"),
storage: Some(HashMap::from([
(felt!("0x111"), felt!("0x1")),
(felt!("0x222"), felt!("0x2")),
])),
};

let acc_1 = ContractAddress::from(felt!(
Expand Down Expand Up @@ -641,6 +660,7 @@ mod tests {
address: ContractAddress::from(felt!(
"0x041a78e741e5af2fec34b695679bc6891742439f7afb8484ecd7766661ad02bf"
)),
storage: Some([(felt!("0x10"), felt!("0x100"))].into()),
}),
};

Expand Down Expand Up @@ -748,6 +768,7 @@ mod tests {
total_supply: U256::from_str("0xD3C21BCECCEDA1000000").unwrap(),
decimals: 18,
class_hash: DEFAULT_LEGACY_ERC20_CONTRACT_CLASS_HASH,
storage: None,
};

let allocations = BTreeMap::from([(
Expand Down Expand Up @@ -776,6 +797,7 @@ mod tests {
universal_deployer: Some(UniversalDeployerConfig {
class_hash: DEFAULT_LEGACY_UDC_CLASS_HASH,
address: DEFAULT_UDC_ADDRESS,
storage: None,
}),
};

Expand Down
31 changes: 30 additions & 1 deletion crates/katana/primitives/src/genesis/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ pub struct FeeTokenConfig {
/// The class hash of the fee token contract.
#[serde_as(as = "UfeHex")]
pub class_hash: ClassHash,
/// To initialize the fee token contract storage
pub storage: Option<HashMap<StorageKey, StorageValue>>,
}

#[serde_with::serde_as]
Expand All @@ -65,10 +67,15 @@ pub struct GenesisClass {
pub sierra: Option<Arc<FlattenedSierraClass>>,
}

/// The configuration of the universal deployer contract.
#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct UniversalDeployerConfig {
/// The class hash of the universal deployer contract.
pub class_hash: ClassHash,
/// The address of the universal deployer contract.
pub address: ContractAddress,
/// To initialize the UD contract storage
pub storage: Option<HashMap<StorageKey, StorageValue>>,
}

/// Genesis block configuration.
Expand Down Expand Up @@ -182,7 +189,7 @@ impl Genesis {
// TODO: put this in a separate function

// insert fee token related data
let mut fee_token_storage: HashMap<StorageKey, StorageValue> = HashMap::new();
let mut fee_token_storage = self.fee_token.storage.clone().unwrap_or_default();

let name: FieldElement = cairo_short_string_to_felt(&self.fee_token.name).unwrap();
let symbol: FieldElement = cairo_short_string_to_felt(&self.fee_token.symbol).unwrap();
Expand Down Expand Up @@ -219,7 +226,10 @@ impl Genesis {

// insert universal deployer related data
if let Some(udc) = &self.universal_deployer {
let storage = udc.storage.clone().unwrap_or_default();

states.state_updates.contract_updates.insert(udc.address, udc.class_hash);
states.state_updates.storage_updates.insert(udc.address, storage);
}

states
Expand All @@ -238,11 +248,13 @@ impl Default for Genesis {
total_supply: 0.into(),
address: DEFAULT_FEE_TOKEN_ADDRESS,
class_hash: DEFAULT_LEGACY_ERC20_CONTRACT_CLASS_HASH,
storage: None,
};

let universal_deployer = UniversalDeployerConfig {
address: DEFAULT_UDC_ADDRESS,
class_hash: DEFAULT_LEGACY_UDC_CLASS_HASH,
storage: None,
};

let classes = HashMap::from([
Expand Down Expand Up @@ -340,6 +352,10 @@ mod tests {
total_supply: U256::from_str("0xD3C21BCECCEDA1000000").unwrap(),
decimals: 18,
class_hash: DEFAULT_LEGACY_ERC20_CONTRACT_CLASS_HASH,
storage: Some(HashMap::from([
(felt!("0x111"), felt!("0x1")),
(felt!("0x222"), felt!("0x2")),
])),
};

let allocations = [
Expand Down Expand Up @@ -385,6 +401,7 @@ mod tests {
let ud = UniversalDeployerConfig {
address: ContractAddress(felt!("0xb00b1e5")),
class_hash: DEFAULT_LEGACY_UDC_CLASS_HASH,
storage: Some([(felt!("0x10"), felt!("0x100"))].into()),
};

let genesis = Genesis {
Expand Down Expand Up @@ -623,6 +640,11 @@ mod tests {
Some(&total_supply_high)
);

// check generic non-fee token specific storage

assert_eq!(fee_token_storage.get(&felt!("0x111")), Some(&felt!("0x1")));
assert_eq!(fee_token_storage.get(&felt!("0x222")), Some(&felt!("0x2")));

// check for balance
for (address, alloc) in &allocations {
let balance = alloc.balance();
Expand All @@ -639,5 +661,12 @@ mod tests {
assert_eq!(fee_token_storage.get(&low_bal_storage_var), Some(&low));
assert_eq!(fee_token_storage.get(&high_bal_storage_var), Some(&high));
}

let udc_storage =
actual_state_updates.state_updates.storage_updates.get(&ud.address).unwrap();

// check universal deployer contract storage

assert_eq!(udc_storage.get(&felt!("0x10")), Some(&felt!("0x100")));
}
}
11 changes: 9 additions & 2 deletions crates/katana/primitives/src/genesis/test-genesis.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,17 @@
"name": "ETHER",
"symbol": "ETH",
"decimals": 18,
"class": "0x8"
"class": "0x8",
"storage": {
"0x111": "0x1",
"0x222": "0x2"
}
},
"universalDeployer": {
"address": "0x041a78e741e5af2fec34b695679bc6891742439f7afb8484ecd7766661ad02bf"
"address": "0x041a78e741e5af2fec34b695679bc6891742439f7afb8484ecd7766661ad02bf",
"storage": {
"0x10": "0x100"
}
},
"accounts": {
"0x66efb28ac62686966ae85095ff3a772e014e7fbf56d4c5f6fac5606d4dde23a": {
Expand Down

0 comments on commit 17c0b14

Please sign in to comment.