Skip to content

Commit

Permalink
feat(starknet_api): add sierra version to class info
Browse files Browse the repository at this point in the history
  • Loading branch information
AvivYossef-starkware committed Dec 2, 2024
1 parent 111c6fd commit 8062001
Show file tree
Hide file tree
Showing 12 changed files with 203 additions and 35 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.

10 changes: 5 additions & 5 deletions crates/blockifier/src/transaction/test_utils.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use rstest::fixture;
use starknet_api::abi::abi_utils::get_fee_token_var_address;
use starknet_api::block::{FeeType, GasPrice};
use starknet_api::contract_class::{ClassInfo, ContractClass};
use starknet_api::contract_class::{ClassInfo, ContractClass, SierraVersion};
use starknet_api::core::{ClassHash, ContractAddress, Nonce};
use starknet_api::execution_resources::GasAmount;
use starknet_api::test_utils::deploy_account::DeployAccountTxArgs;
Expand Down Expand Up @@ -348,11 +348,11 @@ pub fn create_all_resource_bounds(
}

pub fn calculate_class_info_for_testing(contract_class: ContractClass) -> ClassInfo {
let sierra_program_length = match contract_class {
ContractClass::V0(_) => 0,
ContractClass::V1(_) => 100,
let (sierra_program_length, sierra_version) = match contract_class {
ContractClass::V0(_) => (0, SierraVersion::zero()),
ContractClass::V1(_) => (100, SierraVersion::latest()),
};
ClassInfo::new(&contract_class, sierra_program_length, 100).unwrap()
ClassInfo::new(&contract_class, sierra_program_length, 100, sierra_version).unwrap()
}

pub fn emit_n_events_tx(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use blockifier::test_utils::MAX_FEE;
use blockifier::transaction::transaction_execution::Transaction as BlockifierTransaction;
use papyrus_execution::DEPRECATED_CONTRACT_SIERRA_SIZE;
use starknet_api::block::{BlockHash, BlockNumber};
use starknet_api::contract_class::ClassInfo;
use starknet_api::contract_class::{ClassInfo, SierraVersion};
use starknet_api::core::ClassHash;
use starknet_api::transaction::{Transaction, TransactionHash};
use starknet_core::types::ContractClass as StarknetContractClass;
Expand All @@ -22,7 +22,15 @@ pub trait ReexecutionStateReader {
StarknetContractClass::Sierra(sierra) => {
let abi_length = sierra.abi.len();
let sierra_length = sierra.sierra_program.len();
Ok(ClassInfo::new(&sierra_to_contact_class_v1(sierra)?, sierra_length, abi_length)?)

let sierra_version = SierraVersion::extract_from_program(&sierra.sierra_program)?;

Ok(ClassInfo::new(
&sierra_to_contact_class_v1(sierra)?,
sierra_length,
abi_length,
sierra_version,
)?)
}
StarknetContractClass::Legacy(legacy) => {
let abi_length =
Expand All @@ -31,6 +39,7 @@ pub trait ReexecutionStateReader {
&legacy_to_contract_class_v0(legacy)?,
DEPRECATED_CONTRACT_SIERRA_SIZE,
abi_length,
SierraVersion::zero(),
)?)
}
}
Expand Down
6 changes: 5 additions & 1 deletion crates/native_blockifier/src/py_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use blockifier::transaction::transaction_types::TransactionType;
use pyo3::exceptions::PyValueError;
use pyo3::prelude::*;
use starknet_api::block::GasPrice;
use starknet_api::contract_class::{ClassInfo, ContractClass};
use starknet_api::contract_class::{ClassInfo, ContractClass, SierraVersion};
use starknet_api::executable_transaction::AccountTransaction as ExecutableTransaction;
use starknet_api::execution_resources::GasAmount;
use starknet_api::transaction::fields::{
Expand Down Expand Up @@ -164,6 +164,7 @@ pub struct PyClassInfo {
raw_contract_class: String,
sierra_program_length: usize,
abi_length: usize,
sierra_version: (u64, u64, u64),
}

impl PyClassInfo {
Expand All @@ -181,10 +182,13 @@ impl PyClassInfo {
ContractClass::V1(serde_json::from_str(&py_class_info.raw_contract_class)?)
}
};
let (major, minor, patch) = py_class_info.sierra_version;

let class_info = ClassInfo::new(
&contract_class,
py_class_info.sierra_program_length,
py_class_info.abi_length,
SierraVersion::new(major, minor, patch),
)?;
Ok(class_info)
}
Expand Down
85 changes: 65 additions & 20 deletions crates/papyrus_execution/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
mod execution_test;
pub mod execution_utils;
mod state_reader;

#[cfg(test)]
mod test_utils;
#[cfg(any(feature = "testing", test))]
Expand Down Expand Up @@ -58,7 +57,7 @@ use starknet_api::block::{
NonzeroGasPrice,
StarknetVersion,
};
use starknet_api::contract_class::{ClassInfo, EntryPointType};
use starknet_api::contract_class::{ClassInfo, EntryPointType, SierraVersion};
use starknet_api::core::{ChainId, ClassHash, ContractAddress, EntryPointSelector};
use starknet_api::data_availability::L1DataAvailabilityMode;
use starknet_api::deprecated_contract_class::ContractClass as DeprecatedContractClass;
Expand Down Expand Up @@ -433,8 +432,22 @@ pub enum ExecutableTransactionInput {
// todo(yair): Do we need to support V0?
DeclareV0(DeclareTransactionV0V1, DeprecatedContractClass, AbiSize, OnlyQuery),
DeclareV1(DeclareTransactionV0V1, DeprecatedContractClass, AbiSize, OnlyQuery),
DeclareV2(DeclareTransactionV2, CasmContractClass, SierraSize, AbiSize, OnlyQuery),
DeclareV3(DeclareTransactionV3, CasmContractClass, SierraSize, AbiSize, OnlyQuery),
DeclareV2(
DeclareTransactionV2,
CasmContractClass,
SierraSize,
AbiSize,
OnlyQuery,
SierraVersion,
),
DeclareV3(
DeclareTransactionV3,
CasmContractClass,
SierraSize,
AbiSize,
OnlyQuery,
SierraVersion,
),
DeployAccount(DeployAccountTransaction, OnlyQuery),
L1Handler(L1HandlerTransaction, Fee, OnlyQuery),
}
Expand Down Expand Up @@ -490,27 +503,49 @@ impl ExecutableTransactionInput {
sierra_program_length,
abi_length,
only_query,
sierra_version,
) => {
let as_transaction = Transaction::Declare(DeclareTransaction::V2(tx));
let res = func(&as_transaction, only_query);
let Transaction::Declare(DeclareTransaction::V2(tx)) = as_transaction else {
unreachable!("Should be declare v2 transaction.")
};
(Self::DeclareV2(tx, class, sierra_program_length, abi_length, only_query), res)
(
Self::DeclareV2(
tx,
class,
sierra_program_length,
abi_length,
only_query,
sierra_version,
),
res,
)
}
ExecutableTransactionInput::DeclareV3(
tx,
class,
sierra_program_length,
abi_length,
only_query,
sierra_version,
) => {
let as_transaction = Transaction::Declare(DeclareTransaction::V3(tx));
let res = func(&as_transaction, only_query);
let Transaction::Declare(DeclareTransaction::V3(tx)) = as_transaction else {
unreachable!("Should be declare v3 transaction.")
};
(Self::DeclareV3(tx, class, sierra_program_length, abi_length, only_query), res)
(
Self::DeclareV3(
tx,
class,
sierra_program_length,
abi_length,
only_query,
sierra_version,
),
res,
)
}
ExecutableTransactionInput::DeployAccount(tx, only_query) => {
let as_transaction = Transaction::DeployAccount(tx);
Expand Down Expand Up @@ -800,6 +835,7 @@ fn to_blockifier_tx(
&deprecated_class.into(),
DEPRECATED_CONTRACT_SIERRA_SIZE,
abi_length,
SierraVersion::zero(),
)
.map_err(|err| ExecutionError::BadDeclareTransaction {
tx: DeclareTransaction::V0(declare_tx.clone()),
Expand All @@ -826,6 +862,7 @@ fn to_blockifier_tx(
&deprecated_class.into(),
DEPRECATED_CONTRACT_SIERRA_SIZE,
abi_length,
SierraVersion::zero(),
)
.map_err(|err| ExecutionError::BadDeclareTransaction {
tx: DeclareTransaction::V1(declare_tx.clone()),
Expand All @@ -847,14 +884,18 @@ fn to_blockifier_tx(
sierra_program_length,
abi_length,
only_query,
sierra_version,
) => {
let class_info =
ClassInfo::new(&compiled_class.into(), sierra_program_length, abi_length).map_err(
|err| ExecutionError::BadDeclareTransaction {
tx: DeclareTransaction::V2(declare_tx.clone()),
err,
},
)?;
let class_info = ClassInfo::new(
&compiled_class.into(),
sierra_program_length,
abi_length,
sierra_version,
)
.map_err(|err| ExecutionError::BadDeclareTransaction {
tx: DeclareTransaction::V2(declare_tx.clone()),
err,
})?;
BlockifierTransaction::from_api(
Transaction::Declare(DeclareTransaction::V2(declare_tx)),
tx_hash,
Expand All @@ -871,14 +912,18 @@ fn to_blockifier_tx(
sierra_program_length,
abi_length,
only_query,
sierra_version,
) => {
let class_info =
ClassInfo::new(&compiled_class.into(), sierra_program_length, abi_length).map_err(
|err| ExecutionError::BadDeclareTransaction {
tx: DeclareTransaction::V3(declare_tx.clone()),
err,
},
)?;
let class_info = ClassInfo::new(
&compiled_class.into(),
sierra_program_length,
abi_length,
sierra_version,
)
.map_err(|err| ExecutionError::BadDeclareTransaction {
tx: DeclareTransaction::V3(declare_tx.clone()),
err,
})?;
BlockifierTransaction::from_api(
Transaction::Declare(DeclareTransaction::V3(declare_tx)),
tx_hash,
Expand Down
2 changes: 2 additions & 0 deletions crates/papyrus_execution/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use starknet_api::block::{
GasPrice,
GasPricePerToken,
};
use starknet_api::contract_class::SierraVersion;
use starknet_api::core::{
ChainId,
ClassHash,
Expand Down Expand Up @@ -312,6 +313,7 @@ impl TxsScenarioBuilder {
DUMMY_SIERRA_SIZE,
0,
false,
SierraVersion::latest(),
);
self.txs.push(tx);
self
Expand Down
19 changes: 15 additions & 4 deletions crates/papyrus_rpc/src/v0_8/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use papyrus_storage::state::StateStorageReader;
use papyrus_storage::StorageTxn;
use serde::{Deserialize, Serialize};
use starknet_api::block::{BlockHashAndNumber, BlockNumber};
use starknet_api::contract_class::SierraVersion;
use starknet_api::core::{ClassHash, ContractAddress, Nonce};
use starknet_api::deprecated_contract_class::{
ContractClass as StarknetApiDeprecatedContractClass,
Expand Down Expand Up @@ -379,14 +380,15 @@ pub(crate) fn stored_txn_to_executable_txn(
value.class_hash
))
})?;
let (sierra_program_length, abi_length) =
let (sierra_program_length, abi_length, sierra_version) =
get_class_lengths(storage_txn, state_number, value.class_hash)?;
Ok(ExecutableTransactionInput::DeclareV2(
value,
casm,
sierra_program_length,
abi_length,
false,
sierra_version,
))
}
starknet_api::transaction::Transaction::Declare(
Expand All @@ -401,14 +403,15 @@ pub(crate) fn stored_txn_to_executable_txn(
value.class_hash
))
})?;
let (sierra_program_length, abi_length) =
let (sierra_program_length, abi_length, sierra_version) =
get_class_lengths(storage_txn, state_number, value.class_hash)?;
Ok(ExecutableTransactionInput::DeclareV3(
value,
casm,
sierra_program_length,
abi_length,
false,
sierra_version,
))
}
starknet_api::transaction::Transaction::Deploy(_) => {
Expand Down Expand Up @@ -453,9 +456,10 @@ fn get_class_lengths(
storage_txn: &StorageTxn<'_, RO>,
state_number: StateNumber,
class_hash: ClassHash,
) -> Result<(SierraSize, AbiSize), ErrorObjectOwned> {
) -> Result<(SierraSize, AbiSize, SierraVersion), ErrorObjectOwned> {
let state_number_after_block =
StateNumber::unchecked_right_after_block(state_number.block_after());

storage_txn
.get_state_reader()
.map_err(internal_server_error)?
Expand All @@ -464,7 +468,14 @@ fn get_class_lengths(
.ok_or_else(|| {
internal_server_error(format!("Missing deprecated class definition of {class_hash}."))
})
.map(|contract_class| (contract_class.sierra_program.len(), contract_class.abi.len()))
.and_then(|contract_class| {
let sierra_program_len = contract_class.sierra_program.len();
let abi_len = contract_class.abi.len();
let sierra_program =
SierraVersion::extract_from_program(&contract_class.sierra_program)
.map_err(internal_server_error)?;
Ok((sierra_program_len, abi_len, sierra_program))
})
}

impl TryFrom<BroadcastedDeclareTransaction> for ExecutableTransactionInput {
Expand Down
1 change: 1 addition & 0 deletions crates/starknet_api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ num-bigint.workspace = true
papyrus_proc_macros.workspace = true
pretty_assertions.workspace = true
primitive-types = { workspace = true, features = ["serde"] }
semver.workspace = true
serde = { workspace = true, features = ["derive", "rc"] }
serde_json.workspace = true
sha3.workspace = true
Expand Down
Loading

0 comments on commit 8062001

Please sign in to comment.