Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pre-forking state refactor #329

Merged
merged 72 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from 65 commits
Commits
Show all changes
72 commits
Select commit Hold shift + click to select a range
299b819
Remove redundant state definitions (usage remaining) [skip ci]
FabijanC Jan 16, 2024
1378c73
Replace: StateExtractor->StateReader, StateChanger->State [WIP] [skip…
FabijanC Jan 17, 2024
dfae767
Committing stash for easier main-merge [skip ci]
FabijanC Jan 24, 2024
9ee4a79
Merge branch 'main' into state-refactor
FabijanC Jan 24, 2024
b0f6c16
Use create_trace in simulate_transactions
FabijanC Jan 24, 2024
ce64d0d
Fix format and clippy script
FabijanC Jan 24, 2024
cb152f1
Remove unnecessary extraction to variable [skip ci]
FabijanC Jan 24, 2024
ebecbf8
Merge branch 'refactor-trace-simulate' into state-refactor
FabijanC Jan 24, 2024
002309c
Merge branch 'main' into state-refactor
FabijanC Jan 24, 2024
79a6bf0
In trace creation, read from state instead of map
FabijanC Jan 25, 2024
4925e00
Merge branch 'read-from-state' into state-refactor
FabijanC Jan 25, 2024
ff8812d
Merge branch 'main' into state-refactor
FabijanC Jan 25, 2024
18aff70
Remove apply_state and clear_dirty_state; add CustomStateReader+Custo…
FabijanC Jan 26, 2024
416b013
Implement CustomState+CustomStateReader for StarknetState [skip ci]
FabijanC Jan 29, 2024
51b20c9
Mostly state refactoring; add TODOs [skip ci]
FabijanC Jan 29, 2024
714d73e
Finish test state setup [skip ci]
FabijanC Jan 30, 2024
44ece00
Make state mutable where needed [skip ci]
FabijanC Jan 30, 2024
defebb5
Resolve unused warnings [skip ci]
FabijanC Jan 30, 2024
f69ab6e
Improve state diff calculation and class committing [skip ci]
FabijanC Jan 30, 2024
397690f
Add deployment assertion [skip ci]
FabijanC Jan 30, 2024
cba783a
Remove todo-test; fix class hash storing [skip ci]
FabijanC Jan 31, 2024
dfdad5f
Add missing mut modifier
FabijanC Jan 31, 2024
394aa54
Add TODOs; minor refactor of failing test - still failing [skip ci]
FabijanC Jan 31, 2024
928874b
Remove todo, rename state_readers file [skip ci]
FabijanC Feb 1, 2024
53906cb
Merge branch 'main' into state-refactor [skip ci]
FabijanC Feb 1, 2024
a05236a
Remove unnecessary mut in call [skip ci]
FabijanC Feb 2, 2024
fef8a52
Merge branch 'main' into state-refactor
FabijanC Feb 5, 2024
5978486
Avoid state cloning in call [skip ci]
FabijanC Feb 5, 2024
6573d18
Use StarknetState::deploy_contract in Deployed [skip ci]
FabijanC Feb 5, 2024
1f6f3e8
Fix checking if declared [skip ci]
FabijanC Feb 5, 2024
91dc9c2
Fix casm write
FabijanC Feb 5, 2024
ff0dca3
Fix tx estimate (msg fee remaining); currently returning wrong nonce …
FabijanC Feb 6, 2024
6765f8b
Merge branch 'main' into state-refactor [skip ci]
FabijanC Feb 7, 2024
325e317
Use transactional state for tx estimation [skip ci]
FabijanC Feb 7, 2024
4d93f8e
Use reference in account class selection [skip ci]
FabijanC Feb 7, 2024
57ccb3d
Start fixing tx simulation [WIP] [skip ci]
FabijanC Feb 7, 2024
46982e2
Merge branch 'main' into state-refactor [skip ci]
FabijanC Feb 8, 2024
40aee27
Finish fixing tx simulation [skip ci]
FabijanC Feb 8, 2024
fec53ab
Fix estimate_message_fee; remove last state clone [skip ci]
FabijanC Feb 8, 2024
3618ae6
Fix warnings (getting and saving state; saving still on todo)
FabijanC Feb 8, 2024
8813a0d
Expand simulation test
FabijanC Feb 8, 2024
bac411b
Merge branch 'main' into state-refactor
FabijanC Feb 8, 2024
f9175b0
Disable tests with state archive
FabijanC Feb 8, 2024
3b8cbe1
Fix estimation - simulation pending [skip ci]
FabijanC Feb 9, 2024
fd24b24
Fix tx simulation
FabijanC Feb 12, 2024
f1d9e8a
Merge branch 'main' into state-refactor
FabijanC Feb 13, 2024
6d460f4
Fix tests: deploy_account, declare, get_class_impls [skip ci]
FabijanC Feb 13, 2024
4fb4523
Remove obsolete test comment [skip ci]
FabijanC Feb 14, 2024
896a786
Predeploy to DictState [skip ci]
FabijanC Feb 14, 2024
e857414
Merge branch 'main' into state-refactor [skip ci]
FabijanC Feb 14, 2024
a822479
Merge branch 'main' into state-refactor
FabijanC Feb 15, 2024
96cfe08
Fix predeclaring [skip ci]
FabijanC Feb 15, 2024
70b3151
Fix storage setting in predeployment
FabijanC Feb 15, 2024
c62f9ae
Adapt tests
FabijanC Feb 15, 2024
318a1ae
Error if state-archive selected; add comments
FabijanC Feb 16, 2024
1004f07
Remove TODOs, fix typo
FabijanC Feb 16, 2024
daefe35
Impl TryFrom<PatriciaKey> for blockifier::StorageKey
FabijanC Feb 16, 2024
1417829
Ignore remaining full state archive tests
FabijanC Feb 16, 2024
e99a288
Merge branch 'main' into state-refactor
FabijanC Feb 22, 2024
ae936fa
Merge branch 'main' into state-refactor
FabijanC Feb 22, 2024
121825b
Merge branch 'main' into state-refactor
FabijanC Feb 26, 2024
1b3c448
Add successive declaration test
FabijanC Feb 27, 2024
5b4881a
Rename generate_commit and commit_full_state_and_get_diff
FabijanC Feb 27, 2024
c40cb23
Merge branch 'main' into state-refactor
FabijanC Mar 1, 2024
5a84633
Merge branch 'main' into state-refactor [skip ci]
FabijanC Mar 1, 2024
1999c72
Reduce mutability
FabijanC Mar 4, 2024
7a7d020
Old state support (#353)
FabijanC Mar 6, 2024
0083e8f
Remove redundant deployment assertion
FabijanC Mar 6, 2024
ac7329e
Return DevnetResult<bool> from is_contract_deployed
FabijanC Mar 6, 2024
ee01d66
Warn if add_visited_pcs called [skip ci]
FabijanC Mar 6, 2024
6696dc6
remove mut impl State [skip ci]
marioiordanov Mar 6, 2024
41fa197
Move StateDict methods to custom impl
FabijanC Mar 6, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 30 additions & 37 deletions crates/starknet-devnet-core/src/account.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::sync::Arc;

use blockifier::state::state_api::{State, StateReader};
use starknet_api::core::{calculate_contract_address, PatriciaKey};
use starknet_api::hash::{StarkFelt, StarkHash};
use starknet_api::transaction::{Calldata, ContractAddressSalt};
Expand All @@ -17,7 +18,8 @@ use crate::constants::{
CHARGEABLE_ACCOUNT_PUBLIC_KEY,
};
use crate::error::DevnetResult;
use crate::traits::{Accounted, Deployed, StateChanger, StateExtractor};
use crate::state::{CustomState, StarknetState};
use crate::traits::{Accounted, Deployed};
use crate::utils::get_storage_var_address;

/// data taken from https://github.com/0xSpaceShard/starknet-devnet/blob/fb96e0cc3c1c31fb29892ecefd2a670cf8a32b51/starknet_devnet/account.py
Expand Down Expand Up @@ -114,19 +116,21 @@ impl Account {
}

impl Deployed for Account {
fn deploy(&self, state: &mut (impl StateChanger + StateExtractor)) -> DevnetResult<()> {
// declare if not declared
if !state.is_contract_declared(&self.class_hash) {
state.declare_contract_class(self.class_hash, self.contract_class.clone())?;
}
fn deploy(&self, state: &mut StarknetState) -> DevnetResult<()> {
self.declare_if_undeclared(state, self.class_hash, &self.contract_class)?;

// deploy
state.deploy_contract(self.account_address, self.class_hash)?;
state.predeploy_contract(self.account_address, self.class_hash)?;

// set public key
// set public key directly in the most underlying state
let public_key_storage_var = get_storage_var_address("Account_public_key", &[])?;
let storage_key = ContractStorageKey::new(self.account_address, public_key_storage_var);
state.change_storage(storage_key, self.public_key)?;
state.state.state.set_storage_at(
self.account_address.try_into()?,
public_key_storage_var.try_into()?,
self.public_key.into(),
)?;

// set balance directly in the most underlying state
self.set_initial_balance(&mut state.state.state)?;

Ok(())
}
Expand All @@ -137,29 +141,31 @@ impl Deployed for Account {
}

impl Accounted for Account {
fn set_initial_balance(&self, state: &mut impl StateChanger) -> DevnetResult<()> {
fn set_initial_balance(&self, state: &mut impl State) -> DevnetResult<()> {
let storage_var_address =
get_storage_var_address("ERC20_balances", &[Felt::from(self.account_address)])?;

for fee_token_address in [self.eth_fee_token_address, self.strk_fee_token_address] {
let storage_key = ContractStorageKey::new(fee_token_address, storage_var_address);

state.change_storage(storage_key, self.initial_balance)?;
state.set_storage_at(
fee_token_address.try_into()?,
storage_var_address.try_into()?,
self.initial_balance.into(),
)?;
}

Ok(())
}

fn get_balance(
&self,
state: &mut impl StateExtractor,
token: FeeToken,
) -> DevnetResult<Balance> {
fn get_balance(&self, state: &mut impl StateReader, token: FeeToken) -> DevnetResult<Balance> {
let balance_storage_key = match token {
FeeToken::ETH => self.eth_balance_storage_key()?,
FeeToken::STRK => self.strk_balance_storage_key()?,
};
state.get_storage(balance_storage_key)
let balance = state.get_storage_at(
(*balance_storage_key.get_contract_address()).try_into()?,
(*balance_storage_key.get_storage_key()).try_into()?,
)?;
Ok(balance.into())
}
}

Expand All @@ -172,8 +178,8 @@ mod tests {
use super::Account;
use crate::account::FeeToken;
use crate::constants::CAIRO_1_ERC20_CONTRACT_CLASS_HASH;
use crate::state::StarknetState;
use crate::traits::{Accounted, Deployed, StateChanger};
use crate::state::{CustomState, StarknetState};
use crate::traits::{Accounted, Deployed};
use crate::utils::exported_test_utils::dummy_cairo_0_contract_class;
use crate::utils::get_storage_var_address;
use crate::utils::test_utils::{dummy_contract_address, dummy_felt};
Expand Down Expand Up @@ -253,25 +259,12 @@ mod tests {
assert!(account.deploy(&mut state).is_ok());
}

#[test]
fn account_get_balance_should_return_zero_because_balance_was_not_set() {
let (account, mut state) = setup();

account.deploy(&mut state).unwrap();
let balance = account.get_balance(&mut state, FeeToken::ETH).unwrap();
assert_eq!(balance, Felt::from(0));

let balance = account.get_balance(&mut state, FeeToken::STRK).unwrap();
assert_eq!(balance, Felt::from(0));
}

#[test]
fn account_get_balance_should_return_correct_value() {
let (mut account, mut state) = setup();
let expected_balance = Felt::from(100);
account.initial_balance = expected_balance;
account.deploy(&mut state).unwrap();
account.set_initial_balance(&mut state).unwrap();
let generated_balance = account.get_balance(&mut state, FeeToken::ETH).unwrap();

assert_eq!(expected_balance, generated_balance);
Expand Down Expand Up @@ -301,7 +294,7 @@ mod tests {

// deploy the erc20 contract
state
.deploy_contract(
.predeploy_contract(
fee_token_address,
Felt::from_prefixed_hex_str(CAIRO_1_ERC20_CONTRACT_CLASS_HASH).unwrap(),
)
Expand Down
5 changes: 3 additions & 2 deletions crates/starknet-devnet-core/src/blocks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ impl StarknetBlocks {
self.last_block_hash = Some(hash);
}

pub fn save_state_at(&mut self, block_number: BlockNumber, state: StarknetState) {
self.num_to_state.insert(block_number, state);
pub fn save_state_at(&mut self, _block_number: BlockNumber, _state: &StarknetState) {
// self.num_to_state.insert(block_number, state.clone());
todo!("we also need to store block context or somehow reconstruct it when needed");
}

pub fn get_by_block_id(&self, block_id: &BlockId) -> Option<&StarknetBlock> {
Expand Down
52 changes: 30 additions & 22 deletions crates/starknet-devnet-core/src/starknet/add_declare_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,15 @@ pub fn add_declare_transaction_v1(

#[cfg(test)]
mod tests {
use blockifier::state::state_api::StateReader;
use nonzero_ext::nonzero;
use starknet_api::block::BlockNumber;
use starknet_api::core::CompiledClassHash;
use starknet_api::hash::StarkHash;
use starknet_api::transaction::Fee;
use starknet_rs_core::types::{TransactionExecutionStatus, TransactionFinalityStatus};
use starknet_rs_core::types::{
BlockId, BlockTag, TransactionExecutionStatus, TransactionFinalityStatus,
};
use starknet_types::contract_address::ContractAddress;
use starknet_types::contract_class::{Cairo0Json, ContractClass};
use starknet_types::felt::Felt;
Expand All @@ -143,7 +148,8 @@ mod tests {
};
use crate::starknet::predeployed::create_erc20_at_address;
use crate::starknet::{predeployed, Starknet};
use crate::traits::{Accounted, Deployed, HashIdentifiedMut, StateExtractor};
use crate::state::CustomStateReader;
use crate::traits::{Deployed, HashIdentifiedMut};
use crate::utils::exported_test_utils::dummy_cairo_0_contract_class;
use crate::utils::test_utils::{
convert_broadcasted_declare_v2_to_v3, dummy_broadcasted_declare_transaction_v2,
Expand Down Expand Up @@ -248,7 +254,7 @@ mod tests {
// check if txn is with status accepted
assert_eq!(tx.finality_status, TransactionFinalityStatus::AcceptedOnL2);
assert_eq!(tx.execution_result.status(), TransactionExecutionStatus::Succeeded);
assert!(starknet.state.contract_classes.get(&class_hash).is_some());
starknet.state.get_rpc_contract_class(&class_hash).unwrap();
}

#[test]
Expand All @@ -262,14 +268,17 @@ mod tests {
let tx = starknet.transactions.get_by_hash_mut(&tx_hash).unwrap();

// check if generated class hash is expected one
assert_eq!(
class_hash,
ContractClass::Cairo1(declare_txn.contract_class).generate_hash().unwrap()
);
let generated_hash =
ContractClass::Cairo1(declare_txn.contract_class.clone()).generate_hash().unwrap();
assert_eq!(class_hash, generated_hash);

// check if txn is with status accepted
assert_eq!(tx.finality_status, TransactionFinalityStatus::AcceptedOnL2);
assert_eq!(tx.execution_result.status(), TransactionExecutionStatus::Succeeded);
assert!(starknet.state.contract_classes.get(&class_hash).is_some());
assert_eq!(
starknet.get_class(&BlockId::Tag(BlockTag::Latest), class_hash).unwrap(),
declare_txn.contract_class.into()
);
}

#[test]
Expand All @@ -281,15 +290,12 @@ mod tests {
let expected_compiled_class_hash = declare_txn.compiled_class_hash;

// check if contract is not declared
assert!(!starknet.state.is_contract_declared(&expected_class_hash));
assert!(
!starknet
.state
.state
.state
.class_hash_to_compiled_class
.contains_key(&expected_compiled_class_hash)
assert!(!starknet.state.is_contract_declared(expected_class_hash));
assert_eq!(
starknet.state.get_compiled_class_hash(expected_class_hash.into()).unwrap(),
CompiledClassHash(StarkHash::ZERO)
);
assert!(starknet.get_class(&BlockId::Tag(BlockTag::Latest), expected_class_hash).is_err());

let (tx_hash, retrieved_class_hash) =
starknet.add_declare_transaction_v2(declare_txn).unwrap();
Expand All @@ -301,7 +307,11 @@ mod tests {
// check if txn is with status accepted
assert_eq!(retrieved_txn.finality_status, TransactionFinalityStatus::AcceptedOnL2);
assert_eq!(retrieved_txn.execution_result.status(), TransactionExecutionStatus::Succeeded);
assert!(starknet.state.is_contract_declared(&expected_class_hash));
assert!(starknet.state.is_contract_declared(expected_class_hash));
assert_eq!(
starknet.state.get_compiled_class_hash(expected_class_hash.into()).unwrap(),
expected_compiled_class_hash.into()
);
}

#[test]
Expand Down Expand Up @@ -374,7 +384,7 @@ mod tests {
assert_eq!(tx.finality_status, TransactionFinalityStatus::AcceptedOnL2);
assert_eq!(tx.execution_result.status(), TransactionExecutionStatus::Succeeded);
// check if contract is successfully declared
assert!(starknet.state.is_contract_declared(&class_hash));
assert!(starknet.state.is_contract_declared(class_hash));
// check if pending block is resetted
assert!(starknet.pending_block().get_transactions().is_empty());
// check if there is generated block
Expand All @@ -400,7 +410,7 @@ mod tests {

let expected_class_hash = declare_txn.contract_class.generate_hash().unwrap();
// check if contract is not declared
assert!(!starknet.state.is_contract_declared(&expected_class_hash));
assert!(!starknet.state.is_contract_declared(expected_class_hash));

let (tx_hash, class_hash) = starknet.add_declare_transaction_v1(declare_txn).unwrap();

Expand All @@ -411,7 +421,7 @@ mod tests {
assert_eq!(tx.execution_result.status(), TransactionExecutionStatus::Succeeded);

// check if contract is declared
assert!(starknet.state.is_contract_declared(&class_hash));
assert!(starknet.state.is_contract_declared(class_hash));
}

/// Initializes starknet with 1 account - account without validations
Expand Down Expand Up @@ -442,9 +452,7 @@ mod tests {
.unwrap();

acc.deploy(&mut starknet.state).unwrap();
acc.set_initial_balance(&mut starknet.state).unwrap();

starknet.state.clear_dirty_state();
starknet.block_context = Starknet::init_block_context(
nonzero!(1u128),
constants::ETH_ERC20_CONTRACT_ADDRESS,
Expand Down
Loading