Skip to content

Commit

Permalink
chore: upgrade sn-api version (#241)
Browse files Browse the repository at this point in the history
  • Loading branch information
MohammadNassar1 authored Jun 18, 2024
1 parent 31eacee commit 3633d98
Show file tree
Hide file tree
Showing 11 changed files with 502 additions and 212 deletions.
481 changes: 393 additions & 88 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ async-trait = "0.1.79"
axum = "0.6.12"
# TODO(YaelD, 28/5/2024): The special Blockifier version is needed in order to be aligned with the
# starknet-api version. This should be removed once we have a mono-repo.
blockifier = { git = "https://github.com/starkware-libs/blockifier.git", rev = "6babc28a", features = [
blockifier = { git = "https://github.com/starkware-libs/blockifier.git", rev = "6057646f", features = [
"testing",
] }
cairo-lang-sierra = "2.6.0"
Expand Down
6 changes: 3 additions & 3 deletions crates/gateway/src/gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::sync::Arc;
use axum::extract::State;
use axum::routing::{get, post};
use axum::{Json, Router};
use starknet_api::external_transaction::ExternalTransaction;
use starknet_api::rpc_transaction::RPCTransaction;
use starknet_api::transaction::TransactionHash;
use starknet_mempool_types::communication::SharedMempoolClient;
use starknet_mempool_types::mempool_types::{Account, MempoolInput};
Expand Down Expand Up @@ -82,7 +82,7 @@ async fn is_alive() -> GatewayResult<String> {

async fn add_tx(
State(app_state): State<AppState>,
Json(tx): Json<ExternalTransaction>,
Json(tx): Json<RPCTransaction>,
) -> GatewayResult<Json<TransactionHash>> {
let mempool_input = tokio::task::spawn_blocking(move || {
process_tx(
Expand All @@ -109,7 +109,7 @@ fn process_tx(
stateless_tx_validator: StatelessTransactionValidator,
stateful_tx_validator: &StatefulTransactionValidator,
state_reader_factory: &dyn StateReaderFactory,
tx: ExternalTransaction,
tx: RPCTransaction,
) -> GatewayResult<MempoolInput> {
// TODO(Arni, 1/5/2024): Perform congestion control.

Expand Down
6 changes: 3 additions & 3 deletions crates/gateway/src/gateway_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use axum::extract::State;
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use blockifier::context::ChainInfo;
use starknet_api::external_transaction::ExternalTransaction;
use starknet_api::rpc_transaction::RPCTransaction;
use starknet_api::transaction::TransactionHash;
use starknet_mempool::communication::create_mempool_server;
use starknet_mempool::mempool::Mempool;
Expand Down Expand Up @@ -76,10 +76,10 @@ async fn to_bytes(res: Response) -> Bytes {
res.into_body().collect().await.unwrap().to_bytes()
}

fn calculate_hash(external_tx: &ExternalTransaction) -> TransactionHash {
fn calculate_hash(external_tx: &RPCTransaction) -> TransactionHash {
assert_matches!(
external_tx,
ExternalTransaction::Invoke(_),
RPCTransaction::Invoke(_),
"Only Invoke supported for now, extend as needed."
);

Expand Down
135 changes: 62 additions & 73 deletions crates/gateway/src/starknet_api_test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ use blockifier::test_utils::{create_trivial_calldata, CairoVersion, NonceManager
use serde_json::to_string_pretty;
use starknet_api::core::{ClassHash, CompiledClassHash, ContractAddress, Nonce};
use starknet_api::data_availability::DataAvailabilityMode;
use starknet_api::external_transaction::{
ContractClass, ExternalDeclareTransaction, ExternalDeclareTransactionV3,
ExternalDeployAccountTransaction, ExternalDeployAccountTransactionV3,
ExternalInvokeTransaction, ExternalInvokeTransactionV3, ExternalTransaction,
use starknet_api::hash::StarkFelt;
use starknet_api::rpc_transaction::{
ContractClass, RPCDeclareTransaction, RPCDeclareTransactionV3, RPCDeployAccountTransaction,
RPCDeployAccountTransactionV3, RPCInvokeTransaction, RPCInvokeTransactionV3, RPCTransaction,
ResourceBoundsMapping,
};
use starknet_api::hash::StarkFelt;
use starknet_api::transaction::{
AccountDeploymentData, Calldata, ContractAddressSalt, PaymasterData, ResourceBounds, Tip,
TransactionSignature, TransactionVersion,
Expand All @@ -28,14 +27,14 @@ pub enum TransactionType {
Invoke,
}

pub fn get_sender_address(tx: &ExternalTransaction) -> ContractAddress {
pub fn get_sender_address(tx: &RPCTransaction) -> ContractAddress {
match tx {
ExternalTransaction::Declare(ExternalDeclareTransaction::V3(tx)) => tx.sender_address,
RPCTransaction::Declare(RPCDeclareTransaction::V3(tx)) => tx.sender_address,
// TODO(Mohammad): Add support for deploy account.
ExternalTransaction::DeployAccount(ExternalDeployAccountTransaction::V3(_)) => {
RPCTransaction::DeployAccount(RPCDeployAccountTransaction::V3(_)) => {
ContractAddress::default()
}
ExternalTransaction::Invoke(ExternalInvokeTransaction::V3(tx)) => tx.sender_address,
RPCTransaction::Invoke(RPCInvokeTransaction::V3(tx)) => tx.sender_address,
}
}

Expand All @@ -44,7 +43,7 @@ pub fn external_tx_for_testing(
resource_bounds: ResourceBoundsMapping,
calldata: Calldata,
signature: TransactionSignature,
) -> ExternalTransaction {
) -> RPCTransaction {
match tx_type {
TransactionType::Declare => {
let contract_class = ContractClass {
Expand Down Expand Up @@ -86,7 +85,7 @@ pub fn executable_resource_bounds_mapping() -> ResourceBoundsMapping {
)
}

pub fn invoke_tx() -> ExternalTransaction {
pub fn invoke_tx() -> RPCTransaction {
let cairo_version = CairoVersion::Cairo1;
let account_contract = FeatureContract::AccountWithoutValidations(cairo_version);
let account_address = account_contract.get_instance_address(0);
Expand Down Expand Up @@ -256,88 +255,78 @@ impl Default for DeclareTxArgs {
}
}

pub fn external_invoke_tx(invoke_args: InvokeTxArgs) -> ExternalTransaction {
pub fn external_invoke_tx(invoke_args: InvokeTxArgs) -> RPCTransaction {
match invoke_args.version {
TransactionVersion::THREE => {
starknet_api::external_transaction::ExternalTransaction::Invoke(
starknet_api::external_transaction::ExternalInvokeTransaction::V3(
ExternalInvokeTransactionV3 {
resource_bounds: invoke_args.resource_bounds,
tip: invoke_args.tip,
calldata: invoke_args.calldata,
sender_address: invoke_args.sender_address,
nonce: invoke_args.nonce,
signature: invoke_args.signature,
nonce_data_availability_mode: invoke_args.nonce_data_availability_mode,
fee_data_availability_mode: invoke_args.fee_data_availability_mode,
paymaster_data: invoke_args.paymaster_data,
account_deployment_data: invoke_args.account_deployment_data,
},
),
)
}
TransactionVersion::THREE => starknet_api::rpc_transaction::RPCTransaction::Invoke(
starknet_api::rpc_transaction::RPCInvokeTransaction::V3(RPCInvokeTransactionV3 {
resource_bounds: invoke_args.resource_bounds,
tip: invoke_args.tip,
calldata: invoke_args.calldata,
sender_address: invoke_args.sender_address,
nonce: invoke_args.nonce,
signature: invoke_args.signature,
nonce_data_availability_mode: invoke_args.nonce_data_availability_mode,
fee_data_availability_mode: invoke_args.fee_data_availability_mode,
paymaster_data: invoke_args.paymaster_data,
account_deployment_data: invoke_args.account_deployment_data,
}),
),
_ => panic!("Unsupported transaction version: {:?}.", invoke_args.version),
}
}

pub fn external_deploy_account_tx(deploy_tx_args: DeployAccountTxArgs) -> ExternalTransaction {
pub fn external_deploy_account_tx(deploy_tx_args: DeployAccountTxArgs) -> RPCTransaction {
match deploy_tx_args.version {
TransactionVersion::THREE => {
starknet_api::external_transaction::ExternalTransaction::DeployAccount(
starknet_api::external_transaction::ExternalDeployAccountTransaction::V3(
ExternalDeployAccountTransactionV3 {
resource_bounds: deploy_tx_args.resource_bounds,
tip: deploy_tx_args.tip,
contract_address_salt: deploy_tx_args.contract_address_salt,
class_hash: deploy_tx_args.class_hash,
constructor_calldata: deploy_tx_args.constructor_calldata,
nonce: deploy_tx_args.nonce,
signature: deploy_tx_args.signature,
nonce_data_availability_mode: deploy_tx_args.nonce_data_availability_mode,
fee_data_availability_mode: deploy_tx_args.fee_data_availability_mode,
paymaster_data: deploy_tx_args.paymaster_data,
},
),
)
}
TransactionVersion::THREE => starknet_api::rpc_transaction::RPCTransaction::DeployAccount(
starknet_api::rpc_transaction::RPCDeployAccountTransaction::V3(
RPCDeployAccountTransactionV3 {
resource_bounds: deploy_tx_args.resource_bounds,
tip: deploy_tx_args.tip,
contract_address_salt: deploy_tx_args.contract_address_salt,
class_hash: deploy_tx_args.class_hash,
constructor_calldata: deploy_tx_args.constructor_calldata,
nonce: deploy_tx_args.nonce,
signature: deploy_tx_args.signature,
nonce_data_availability_mode: deploy_tx_args.nonce_data_availability_mode,
fee_data_availability_mode: deploy_tx_args.fee_data_availability_mode,
paymaster_data: deploy_tx_args.paymaster_data,
},
),
),
_ => panic!("Unsupported transaction version: {:?}.", deploy_tx_args.version),
}
}

pub fn external_declare_tx(declare_tx_args: DeclareTxArgs) -> ExternalTransaction {
pub fn external_declare_tx(declare_tx_args: DeclareTxArgs) -> RPCTransaction {
match declare_tx_args.version {
TransactionVersion::THREE => {
starknet_api::external_transaction::ExternalTransaction::Declare(
starknet_api::external_transaction::ExternalDeclareTransaction::V3(
ExternalDeclareTransactionV3 {
contract_class: declare_tx_args.contract_class,
signature: declare_tx_args.signature,
sender_address: declare_tx_args.sender_address,
resource_bounds: declare_tx_args.resource_bounds,
tip: declare_tx_args.tip,
nonce_data_availability_mode: declare_tx_args.nonce_data_availability_mode,
fee_data_availability_mode: declare_tx_args.fee_data_availability_mode,
paymaster_data: declare_tx_args.paymaster_data,
account_deployment_data: declare_tx_args.account_deployment_data,
nonce: declare_tx_args.nonce,
compiled_class_hash: declare_tx_args.class_hash,
},
),
)
}
TransactionVersion::THREE => starknet_api::rpc_transaction::RPCTransaction::Declare(
starknet_api::rpc_transaction::RPCDeclareTransaction::V3(RPCDeclareTransactionV3 {
contract_class: declare_tx_args.contract_class,
signature: declare_tx_args.signature,
sender_address: declare_tx_args.sender_address,
resource_bounds: declare_tx_args.resource_bounds,
tip: declare_tx_args.tip,
nonce_data_availability_mode: declare_tx_args.nonce_data_availability_mode,
fee_data_availability_mode: declare_tx_args.fee_data_availability_mode,
paymaster_data: declare_tx_args.paymaster_data,
account_deployment_data: declare_tx_args.account_deployment_data,
nonce: declare_tx_args.nonce,
compiled_class_hash: declare_tx_args.class_hash,
}),
),
_ => panic!("Unsupported transaction version: {:?}.", declare_tx_args.version),
}
}

pub fn external_tx_to_json(tx: &ExternalTransaction) -> String {
pub fn external_tx_to_json(tx: &RPCTransaction) -> String {
let mut tx_json = serde_json::to_value(tx)
.unwrap_or_else(|tx| panic!("Failed to serialize transaction: {tx:?}"));

// Add type and version manually
let type_string = match tx {
ExternalTransaction::Declare(_) => "DECLARE",
ExternalTransaction::DeployAccount(_) => "DEPLOY_ACCOUNT",
ExternalTransaction::Invoke(_) => "INVOKE",
RPCTransaction::Declare(_) => "DECLARE",
RPCTransaction::DeployAccount(_) => "DEPLOY_ACCOUNT",
RPCTransaction::Invoke(_) => "INVOKE",
};

tx_json
Expand Down
4 changes: 2 additions & 2 deletions crates/gateway/src/stateful_transaction_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use blockifier::context::BlockContext;
use blockifier::execution::contract_class::ClassInfo;
use blockifier::state::cached_state::CachedState;
use blockifier::versioned_constants::VersionedConstants;
use starknet_api::external_transaction::ExternalTransaction;
use starknet_api::rpc_transaction::RPCTransaction;
use starknet_api::transaction::TransactionHash;

use crate::config::StatefulTransactionValidatorConfig;
Expand All @@ -25,7 +25,7 @@ impl StatefulTransactionValidator {
pub fn run_validate(
&self,
state_reader_factory: &dyn StateReaderFactory,
external_tx: &ExternalTransaction,
external_tx: &RPCTransaction,
optional_class_info: Option<ClassInfo>,
deploy_account_tx_hash: Option<TransactionHash>,
) -> StatefulTransactionValidatorResult<TransactionHash> {
Expand Down
33 changes: 15 additions & 18 deletions crates/gateway/src/stateless_transaction_validator.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use starknet_api::external_transaction::{
ExternalDeclareTransaction, ExternalDeployAccountTransaction, ExternalInvokeTransaction,
ExternalTransaction, ResourceBoundsMapping,
use starknet_api::rpc_transaction::{
RPCDeclareTransaction, RPCDeployAccountTransaction, RPCInvokeTransaction, RPCTransaction,
ResourceBoundsMapping,
};
use starknet_api::transaction::Resource;

Expand All @@ -17,22 +17,22 @@ pub struct StatelessTransactionValidator {
}

impl StatelessTransactionValidator {
pub fn validate(&self, tx: &ExternalTransaction) -> StatelessTransactionValidatorResult<()> {
pub fn validate(&self, tx: &RPCTransaction) -> StatelessTransactionValidatorResult<()> {
// TODO(Arni, 1/5/2024): Add a mechanism that validate the sender address is not blocked.
// TODO(Arni, 1/5/2024): Validate transaction version.

self.validate_resource_bounds(tx)?;
self.validate_tx_size(tx)?;

if let ExternalTransaction::Declare(declare_tx) = tx {
if let RPCTransaction::Declare(declare_tx) = tx {
self.validate_declare_tx(declare_tx)?;
}
Ok(())
}

fn validate_resource_bounds(
&self,
tx: &ExternalTransaction,
tx: &RPCTransaction,
) -> StatelessTransactionValidatorResult<()> {
let resource_bounds_mapping = tx.resource_bounds();

Expand All @@ -46,10 +46,7 @@ impl StatelessTransactionValidator {
Ok(())
}

fn validate_tx_size(
&self,
tx: &ExternalTransaction,
) -> StatelessTransactionValidatorResult<()> {
fn validate_tx_size(&self, tx: &RPCTransaction) -> StatelessTransactionValidatorResult<()> {
self.validate_tx_calldata_size(tx)?;
self.validate_tx_signature_size(tx)?;

Expand All @@ -58,17 +55,17 @@ impl StatelessTransactionValidator {

fn validate_tx_calldata_size(
&self,
tx: &ExternalTransaction,
tx: &RPCTransaction,
) -> StatelessTransactionValidatorResult<()> {
let calldata = match tx {
ExternalTransaction::Declare(_) => {
RPCTransaction::Declare(_) => {
// Declare transaction has no calldata.
return Ok(());
}
ExternalTransaction::DeployAccount(ExternalDeployAccountTransaction::V3(tx)) => {
RPCTransaction::DeployAccount(RPCDeployAccountTransaction::V3(tx)) => {
&tx.constructor_calldata
}
ExternalTransaction::Invoke(ExternalInvokeTransaction::V3(tx)) => &tx.calldata,
RPCTransaction::Invoke(RPCInvokeTransaction::V3(tx)) => &tx.calldata,
};

let calldata_length = calldata.0.len();
Expand All @@ -84,7 +81,7 @@ impl StatelessTransactionValidator {

fn validate_tx_signature_size(
&self,
tx: &ExternalTransaction,
tx: &RPCTransaction,
) -> StatelessTransactionValidatorResult<()> {
let signature = tx.signature();

Expand All @@ -101,17 +98,17 @@ impl StatelessTransactionValidator {

fn validate_declare_tx(
&self,
declare_tx: &ExternalDeclareTransaction,
declare_tx: &RPCDeclareTransaction,
) -> StatelessTransactionValidatorResult<()> {
let contract_class = match declare_tx {
ExternalDeclareTransaction::V3(tx) => &tx.contract_class,
RPCDeclareTransaction::V3(tx) => &tx.contract_class,
};
self.validate_class_length(contract_class)
}

fn validate_class_length(
&self,
contract_class: &starknet_api::external_transaction::ContractClass,
contract_class: &starknet_api::rpc_transaction::ContractClass,
) -> StatelessTransactionValidatorResult<()> {
let bytecode_size = contract_class.sierra_program.len();
if bytecode_size > self.config.max_bytecode_size {
Expand Down
2 changes: 1 addition & 1 deletion crates/gateway/src/stateless_transaction_validator_test.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use assert_matches::assert_matches;
use rstest::rstest;
use starknet_api::external_transaction::{ContractClass, ResourceBoundsMapping};
use starknet_api::hash::StarkFelt;
use starknet_api::rpc_transaction::{ContractClass, ResourceBoundsMapping};
use starknet_api::transaction::{Calldata, Resource, ResourceBounds, TransactionSignature};
use starknet_api::{calldata, stark_felt};

Expand Down
Loading

0 comments on commit 3633d98

Please sign in to comment.