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

Update spec reader tests #657

Open
wants to merge 47 commits into
base: json-rpc-v0.8.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
a552d1d
changed files
marioiordanov Nov 27, 2024
1779527
added spec instructions
marioiordanov Nov 27, 2024
14accab
estimate fee fix
marioiordanov Dec 2, 2024
0f9d2ed
fix data from spec
marioiordanov Dec 2, 2024
54a14b2
added todos
marioiordanov Dec 2, 2024
587926d
added l2_gas_price
marioiordanov Dec 2, 2024
b1d00d9
progress
marioiordanov Dec 2, 2024
65eeba7
progress
marioiordanov Dec 3, 2024
647ba27
changes to spec reader procedure
marioiordanov Dec 4, 2024
7f4996b
change getCompiledCasm request based on tests
marioiordanov Dec 4, 2024
369a958
add versioned constants when generating data for gas usage in traces
marioiordanov Dec 5, 2024
f7aaa1e
fix tuple generator
marioiordanov Dec 5, 2024
2e20d3d
add versioned constants
marioiordanov Dec 5, 2024
b9a1382
spec files
marioiordanov Dec 6, 2024
0a3b581
edit ws file
marioiordanov Dec 6, 2024
40afd35
edit spec
marioiordanov Dec 10, 2024
3cc1efd
renamed block to block_id
marioiordanov Dec 10, 2024
be027d8
SubscriptionNotification refactored, renamed SubscriptionNotification…
marioiordanov Dec 10, 2024
17a5050
changed block to block_id
marioiordanov Dec 10, 2024
b8c1c6b
changed block to block_id
marioiordanov Dec 10, 2024
4a0eb65
fix change
marioiordanov Dec 10, 2024
4793568
fix test
marioiordanov Dec 10, 2024
650a70e
make block_hash and block_number optional as part of emitted event. I…
marioiordanov Dec 10, 2024
ff9e3a8
added Deserialize support
marioiordanov Dec 10, 2024
270f95f
edit spec
marioiordanov Dec 10, 2024
2525f02
edit spec reader tests
marioiordanov Dec 10, 2024
143148c
Merge branch 'json-rpc-v0.8.0' into json-rpc-v0.8.0-spec-reader
marioiordanov Dec 10, 2024
b327704
fmt
marioiordanov Dec 10, 2024
d53d373
fixes after merge
marioiordanov Dec 10, 2024
9d82830
fixes
marioiordanov Dec 10, 2024
e72bede
clippy fix
marioiordanov Dec 10, 2024
4349848
remove 0.7.1 files
marioiordanov Dec 10, 2024
b427f79
remove a test and ignore some other
marioiordanov Dec 10, 2024
599b2d6
replace spec version
marioiordanov Dec 10, 2024
d25a023
edit version is spec
marioiordanov Dec 11, 2024
6327376
address todos
marioiordanov Dec 11, 2024
a88285b
renamings
marioiordanov Dec 11, 2024
80c5193
comment part of the code
marioiordanov Dec 12, 2024
4cce222
serde fix
marioiordanov Dec 12, 2024
a3f9cac
import instead of fully qualify
marioiordanov Dec 18, 2024
0658f67
rename does_block_number_matches_criteria to is_block_number_in_range
marioiordanov Dec 18, 2024
71424eb
remove wrong filter
marioiordanov Dec 18, 2024
a8e7514
added comment
marioiordanov Dec 18, 2024
b567418
remove comment
marioiordanov Dec 18, 2024
a9b4bdf
edit comment
marioiordanov Dec 18, 2024
b2e2207
remove variable
marioiordanov Dec 18, 2024
11f9079
remove ignore attribute
marioiordanov Dec 18, 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
86 changes: 57 additions & 29 deletions crates/starknet-devnet-core/src/blocks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ impl StarknetBlocks {
// used IndexMap to keep elements in the order of the keys
let mut filtered_blocks: IndexMap<Felt, &StarknetBlock> = IndexMap::new();

let pending_block_number = self.pending_block.block_number();

let starting_block = if let Some(block_id) = from {
// If the value for block number provided is not correct it will return None
// So we have to return an error
Expand All @@ -141,23 +143,45 @@ impl StarknetBlocks {
None
};

fn is_block_number_in_range(
current_block_number: BlockNumber,
starting_block: Option<BlockNumber>,
ending_block: Option<BlockNumber>,
) -> bool {
match (starting_block, ending_block) {
(None, None) => true,
(Some(start), None) => current_block_number >= start,
(None, Some(end)) => current_block_number <= end,
(Some(start), Some(end)) => {
current_block_number >= start && current_block_number <= end
}
}
}

let mut insert_pending_block_in_final_result = true;
// iterate over the blocks and apply the filter
// then insert the filtered blocks into the index map
self.num_to_hash
.iter()
.filter(|(current_block_number, _)| match (starting_block, ending_block) {
(None, None) => true,
(Some(start), None) => **current_block_number >= start,
(None, Some(end)) => **current_block_number <= end,
(Some(start), Some(end)) => {
**current_block_number >= start && **current_block_number <= end
}
.filter(|(current_block_number, _)| {
is_block_number_in_range(**current_block_number, starting_block, ending_block)
})
.for_each(|(_, block_hash)| {
.for_each(|(block_number, block_hash)| {
if *block_number == pending_block_number {
insert_pending_block_in_final_result = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When will it be false? Only when one of the blocks in num_to_hash has the same number as pending_block? And when should that be the case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly in this case. num_to_hash apart from being in the same object as pending_block has no relation to it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creating pending block have to be changed. It should advance the block number, but this is happening not in StarknetBlocks file, but in Starknet. Thats why this case is covered

}
filtered_blocks.insert(*block_hash, &self.hash_to_block[block_hash]);
});

Ok(filtered_blocks.into_values().collect())
let mut result: Vec<&StarknetBlock> = filtered_blocks.into_values().collect();

if is_block_number_in_range(pending_block_number, starting_block, ending_block)
&& insert_pending_block_in_final_result
{
result.push(&self.pending_block);
}

Ok(result)
}

pub fn next_block_number(&self) -> BlockNumber {
Expand Down Expand Up @@ -187,6 +211,7 @@ impl From<&StarknetBlock> for TypesPendingBlockHeader {
price_in_fri: value.header.l1_data_gas_price.price_in_fri.0.into(),
price_in_wei: value.header.l1_data_gas_price.price_in_wei.0.into(),
},
l2_gas_price: ResourcePrice { price_in_fri: Felt::ZERO, price_in_wei: Felt::ZERO },
l1_da_mode: value.header.l1_da_mode,
}
}
Expand All @@ -210,6 +235,7 @@ impl From<&StarknetBlock> for TypesBlockHeader {
price_in_fri: value.header.l1_data_gas_price.price_in_fri.0.into(),
price_in_wei: value.header.l1_data_gas_price.price_in_wei.0.into(),
},
l2_gas_price: ResourcePrice { price_in_fri: Felt::ZERO, price_in_wei: Felt::ZERO },
l1_da_mode: value.header.l1_da_mode,
}
}
Expand Down Expand Up @@ -327,30 +353,32 @@ mod tests {
block_to_insert.header.block_hash =
starknet_api::block::BlockHash(Felt::from(block_number as u128));
blocks.insert(block_to_insert, StateDiff::default());
blocks.pending_block.header.block_number = BlockNumber(block_number).unchecked_next();
}

let expected_block_numbers = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let block_numbers: Vec<u64> = blocks
.get_blocks(None, None)
.unwrap()
.iter()
.map(|block| block.block_number().0)
.collect();
assert_eq!(vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], block_numbers);

for _ in 0..10 {
let block_numbers: Vec<u64> = blocks
.get_blocks(None, None)
.unwrap()
.iter()
.map(|block| block.block_number().0)
.collect();
assert_eq!(expected_block_numbers, block_numbers);
}
let block_numbers: Vec<u64> = blocks
.get_blocks(Some(BlockId::Number(7)), None)
.unwrap()
.iter()
.map(|block| block.block_number().0)
.collect();
assert_eq!(vec![7, 8, 9, 10, 11], block_numbers);

let expected_block_numbers = vec![7, 8, 9, 10];
for _ in 0..10 {
let block_numbers: Vec<u64> = blocks
.get_blocks(Some(BlockId::Number(7)), None)
.unwrap()
.iter()
.map(|block| block.block_number().0)
.collect();
assert_eq!(expected_block_numbers, block_numbers);
}
let block_numbers: Vec<u64> = blocks
.get_blocks(Some(BlockId::Number(7)), Some(BlockId::Tag(BlockTag::Latest)))
.unwrap()
.iter()
.map(|block| block.block_number().0)
.collect();
assert_eq!(vec![7, 8, 9, 10], block_numbers);
}

#[test]
Expand Down
11 changes: 7 additions & 4 deletions crates/starknet-devnet-core/src/starknet/estimations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,14 @@ fn estimate_transaction_fee<S: StateReader>(
),
};

// TODO: change l2 fields logic
Ok(FeeEstimateWrapper {
gas_consumed: Felt::from(gas_vector.l1_gas),
data_gas_consumed: Felt::from(gas_vector.l1_data_gas),
gas_price: Felt::from(gas_price),
data_gas_price: Felt::from(data_gas_price),
l1_gas_consumed: Felt::from(gas_vector.l1_gas),
l1_data_gas_consumed: Felt::from(gas_vector.l1_data_gas),
l1_gas_price: Felt::from(gas_price),
l1_data_gas_price: Felt::from(data_gas_price),
l2_gas_consumed: Felt::ZERO,
l2_gas_price: Felt::ZERO,
overall_fee: Felt::from(total_fee.0),
unit,
})
Expand Down
17 changes: 13 additions & 4 deletions crates/starknet-devnet-core/src/starknet/events.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use starknet_api::block::BlockStatus;
use starknet_rs_core::types::{BlockId, Felt};
use starknet_types::contract_address::ContractAddress;
use starknet_types::emitted_event::{EmittedEvent, Event};
Expand Down Expand Up @@ -61,11 +62,16 @@ pub(crate) fn get_events(
return Ok((events, true));
}
}
let (block_hash, block_number) = if block.status() == &BlockStatus::Pending {
(None, None)
} else {
(Some(block.block_hash()), Some(block.block_number()))
};

let emitted_event = EmittedEvent {
transaction_hash: *transaction_hash,
block_hash: block.block_hash(),
block_number: block.block_number(),
block_hash,
block_number,
keys: transaction_event.keys,
from_address: transaction_event.from_address,
data: transaction_event.data,
Expand Down Expand Up @@ -125,7 +131,7 @@ where
#[cfg(test)]
mod tests {
use blockifier::execution::call_info::CallInfo;
use starknet_rs_core::types::{BlockId, Felt};
use starknet_rs_core::types::{BlockId, BlockTag, Felt};
use starknet_types::contract_address::ContractAddress;
use starknet_types::emitted_event::Event;
use starknet_types::rpc::transactions::TransactionWithHash;
Expand Down Expand Up @@ -405,7 +411,10 @@ mod tests {
starknet.handle_accepted_transaction(transaction.clone(), txn_info).unwrap();
}

assert_eq!(starknet.blocks.get_blocks(None, None).unwrap().len(), 6);
assert_eq!(
starknet.blocks.get_blocks(None, Some(BlockId::Tag(BlockTag::Latest))).unwrap().len(),
6
);
for idx in 0..5 {
starknet.transactions.get_by_hash(Felt::from(idx as u128 + 100)).unwrap();
}
Expand Down
5 changes: 2 additions & 3 deletions crates/starknet-devnet-core/src/starknet/get_class_impls.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use blockifier::state::state_api::StateReader;
use cairo_lang_starknet_classes::casm_contract_class::CasmContractClass;
use starknet_api::block::BlockStatus;
use starknet_rs_core::types::BlockId;
use starknet_rs_core::types::{BlockId, BlockTag};
use starknet_types::compile_sierra_contract;
use starknet_types::contract_address::ContractAddress;
use starknet_types::contract_class::ContractClass;
Expand Down Expand Up @@ -57,10 +57,9 @@ pub fn get_class_at_impl(

pub fn get_compiled_casm_impl(
starknet: &Starknet,
block_id: &BlockId,
class_hash: ClassHash,
) -> DevnetResult<CasmContractClass> {
let contract_class = get_class_impl(starknet, block_id, class_hash)?;
let contract_class = get_class_impl(starknet, &BlockId::Tag(BlockTag::Latest), class_hash)?;
match contract_class {
ContractClass::Cairo1(sierra_contract_class) => {
Ok(compile_sierra_contract(&sierra_contract_class)?)
Expand Down
10 changes: 4 additions & 6 deletions crates/starknet-devnet-core/src/starknet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ impl Starknet {
transaction.get_type(),
&tx_info,
state_diff.clone().into(),
self.block_context.versioned_constants(),
)?;
let transaction_to_add = StarknetTransaction::create_accepted(&transaction, tx_info, trace);

Expand Down Expand Up @@ -600,12 +601,8 @@ impl Starknet {
get_class_impls::get_class_at_impl(self, block_id, contract_address)
}

pub fn get_compiled_casm(
&self,
block_id: &BlockId,
class_hash: ClassHash,
) -> DevnetResult<CasmContractClass> {
get_class_impls::get_compiled_casm_impl(self, block_id, class_hash)
pub fn get_compiled_casm(&self, class_hash: ClassHash) -> DevnetResult<CasmContractClass> {
get_class_impls::get_compiled_casm_impl(self, class_hash)
}

pub fn call(
Expand Down Expand Up @@ -1190,6 +1187,7 @@ impl Starknet {
transaction_type,
&tx_execution_info,
state_diff,
block_context.versioned_constants(),
)?;
transactions_traces.push(trace);
}
Expand Down
29 changes: 22 additions & 7 deletions crates/starknet-devnet-core/src/starknet/transaction_trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use blockifier::execution::call_info::CallInfo;
use blockifier::state::cached_state::CachedState;
use blockifier::state::state_api::StateReader;
use blockifier::transaction::objects::TransactionExecutionInfo;
use blockifier::versioned_constants::VersionedConstants;
use starknet_types::rpc::state::ThinStateDiff;
use starknet_types::rpc::transaction_receipt::ExecutionResources;
use starknet_types::rpc::transactions::{
Expand All @@ -15,11 +16,14 @@ use crate::error::{DevnetResult, Error};
fn get_execute_call_info<S: StateReader>(
state: &mut CachedState<S>,
execution_info: &TransactionExecutionInfo,
versioned_constants: &VersionedConstants,
) -> DevnetResult<ExecutionInvocation> {
Ok(match &execution_info.execute_call_info {
Some(call_info) => match call_info.execution.failed {
false => ExecutionInvocation::Succeeded(FunctionInvocation::try_from_call_info(
call_info, state,
call_info,
state,
versioned_constants,
)?),
true => ExecutionInvocation::Reverted(starknet_types::rpc::transactions::Reversion {
revert_reason: execution_info
Expand All @@ -46,9 +50,10 @@ fn get_execute_call_info<S: StateReader>(
fn get_call_info_invocation<S: StateReader>(
state: &mut CachedState<S>,
call_info_invocation: &Option<CallInfo>,
versioned_constants: &VersionedConstants,
) -> DevnetResult<Option<FunctionInvocation>> {
Ok(if let Some(call_info) = call_info_invocation {
Some(FunctionInvocation::try_from_call_info(call_info, state)?)
Some(FunctionInvocation::try_from_call_info(call_info, state, versioned_constants)?)
} else {
None
})
Expand All @@ -59,13 +64,18 @@ pub(crate) fn create_trace<S: StateReader>(
tx_type: TransactionType,
execution_info: &TransactionExecutionInfo,
state_diff: ThinStateDiff,
versioned_constants: &VersionedConstants,
) -> DevnetResult<TransactionTrace> {
let state_diff = Some(state_diff);
let validate_invocation = get_call_info_invocation(state, &execution_info.validate_call_info)?;
let validate_invocation =
get_call_info_invocation(state, &execution_info.validate_call_info, versioned_constants)?;
let execution_resources = ExecutionResources::from(execution_info);

let fee_transfer_invocation =
get_call_info_invocation(state, &execution_info.fee_transfer_call_info)?;
let fee_transfer_invocation = get_call_info_invocation(
state,
&execution_info.fee_transfer_call_info,
versioned_constants,
)?;

match tx_type {
TransactionType::Declare => Ok(TransactionTrace::Declare(DeclareTransactionTrace {
Expand All @@ -80,6 +90,7 @@ pub(crate) fn create_trace<S: StateReader>(
constructor_invocation: get_call_info_invocation(
state,
&execution_info.execute_call_info,
versioned_constants,
)?,
fee_transfer_invocation,
state_diff,
Expand All @@ -88,13 +99,17 @@ pub(crate) fn create_trace<S: StateReader>(
}
TransactionType::Invoke => Ok(TransactionTrace::Invoke(InvokeTransactionTrace {
validate_invocation,
execute_invocation: get_execute_call_info(state, execution_info)?,
execute_invocation: get_execute_call_info(state, execution_info, versioned_constants)?,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There already is a util function get_versioned_constants. Perhaps we should unify the way these versioned constants are obtained, meaning either always use the existing util function or remove it.

fee_transfer_invocation,
state_diff,
execution_resources,
})),
TransactionType::L1Handler => {
match get_call_info_invocation(state, &execution_info.execute_call_info)? {
match get_call_info_invocation(
state,
&execution_info.execute_call_info,
versioned_constants,
)? {
Some(function_invocation) => {
Ok(TransactionTrace::L1Handler(L1HandlerTransactionTrace {
function_invocation,
Expand Down
2 changes: 2 additions & 0 deletions crates/starknet-devnet-core/src/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ impl StarknetTransaction {
#[cfg(test)]
mod tests {
use blockifier::transaction::objects::TransactionExecutionInfo;
use blockifier::versioned_constants;
use starknet_rs_core::types::{TransactionExecutionStatus, TransactionFinalityStatus};
use starknet_types::rpc::transactions::{TransactionTrace, TransactionWithHash};

Expand All @@ -302,6 +303,7 @@ mod tests {
tx.get_type(),
&Default::default(),
Default::default(),
versioned_constants::VersionedConstants::latest_constants(),
)
.unwrap()
}
Expand Down
9 changes: 3 additions & 6 deletions crates/starknet-devnet-server/src/api/json_rpc/endpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,15 +217,12 @@ impl JsonRpcHandler {
}
}

pub async fn get_compiled_casm(
&self,
block_id: BlockId,
class_hash: ClassHash,
) -> StrictRpcResult {
/// starknet_getCompiledCasm
pub async fn get_compiled_casm(&self, class_hash: ClassHash) -> StrictRpcResult {
// starknet_getCompiledCasm compiles sierra to casm the same way it is done in
// starknet_addDeclareTransaction, so if during starknet_addDeclareTransaction compilation
// does not fail, so it will not fail during this endpoint execution
match self.api.starknet.lock().await.get_compiled_casm(block_id.as_ref(), class_hash) {
match self.api.starknet.lock().await.get_compiled_casm(class_hash) {
Ok(compiled_casm) => Ok(StarknetResponse::CompiledCasm(compiled_casm).into()),
Err(e) => Err(match e {
Error::NoBlock => ApiError::BlockNotFound,
Expand Down
Loading