Skip to content

Commit

Permalink
Elin/merge main mempool into main (#286)
Browse files Browse the repository at this point in the history
<!-- Reviewable:start -->
This change is [<img src="https://reviewable.io/review_button.svg"
height="34" align="absmiddle"
alt="Reviewable"/>](https://reviewable.io/reviews/starkware-libs/starknet-api/286)
<!-- Reviewable:end -->
  • Loading branch information
elintul authored Jun 27, 2024
2 parents 6a76d87 + eb5697b commit a78c47e
Show file tree
Hide file tree
Showing 4 changed files with 207 additions and 180 deletions.
153 changes: 0 additions & 153 deletions src/external_transaction.rs

This file was deleted.

2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ pub mod core;
pub mod crypto;
pub mod data_availability;
pub mod deprecated_contract_class;
pub mod external_transaction;
pub mod hash;
pub mod internal_transaction;
pub mod rpc_transaction;
pub mod serde_utils;
pub mod state;
pub mod transaction;
Expand Down
180 changes: 180 additions & 0 deletions src/rpc_transaction.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
#[cfg(test)]
#[path = "rpc_transaction_test.rs"]
mod rpc_transaction_test;

use std::collections::BTreeMap;

use serde::{Deserialize, Serialize};
use starknet_types_core::felt::Felt;

use crate::core::{ClassHash, CompiledClassHash, ContractAddress, Nonce};
use crate::data_availability::DataAvailabilityMode;
use crate::state::EntryPoint;
use crate::transaction::{
AccountDeploymentData, Calldata, ContractAddressSalt, PaymasterData, Resource, ResourceBounds,
Tip, TransactionSignature,
};

/// Transactions that are ready to be broadcasted to the network through RPC and are not included in
/// a block.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(tag = "type")]
#[serde(deny_unknown_fields)]
pub enum RPCTransaction {
#[serde(rename = "DECLARE")]
Declare(RPCDeclareTransaction),
#[serde(rename = "DEPLOY_ACCOUNT")]
DeployAccount(RPCDeployAccountTransaction),
#[serde(rename = "INVOKE")]
Invoke(RPCInvokeTransaction),
}

macro_rules! implement_ref_getters {
($(($member_name:ident, $member_type:ty)), *) => {
$(pub fn $member_name(&self) -> &$member_type {
match self {
RPCTransaction::Declare(
RPCDeclareTransaction::V3(tx)
) => &tx.$member_name,
RPCTransaction::DeployAccount(
RPCDeployAccountTransaction::V3(tx)
) => &tx.$member_name,
RPCTransaction::Invoke(
RPCInvokeTransaction::V3(tx)
) => &tx.$member_name
}
})*
};
}

impl RPCTransaction {
implement_ref_getters!(
(nonce, Nonce),
(resource_bounds, ResourceBoundsMapping),
(signature, TransactionSignature),
(tip, Tip)
);
}

/// A RPC declare transaction.
///
/// This transaction is equivalent to the component DECLARE_TXN in the
/// [`Starknet specs`] with a contract class (DECLARE_TXN allows having
/// either a contract class or a class hash).
///
/// [`Starknet specs`]: https://github.com/starkware-libs/starknet-specs/blob/master/api/starknet_api_openrpc.json
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(tag = "version")]
pub enum RPCDeclareTransaction {
#[serde(rename = "0x3")]
V3(RPCDeclareTransactionV3),
}

/// A RPC deploy account transaction.
///
/// This transaction is equivalent to the component DEPLOY_ACCOUNT_TXN in the
/// [`Starknet specs`].
///
/// [`Starknet specs`]: https://github.com/starkware-libs/starknet-specs/blob/master/api/starknet_api_openrpc.json
#[derive(Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
#[serde(tag = "version")]
pub enum RPCDeployAccountTransaction {
#[serde(rename = "0x3")]
V3(RPCDeployAccountTransactionV3),
}

/// A RPC invoke transaction.
///
/// This transaction is equivalent to the component INVOKE_TXN in the
/// [`Starknet specs`].
///
/// [`Starknet specs`]: https://github.com/starkware-libs/starknet-specs/blob/master/api/starknet_api_openrpc.json
#[derive(Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
#[serde(tag = "version")]
pub enum RPCInvokeTransaction {
#[serde(rename = "0x3")]
V3(RPCInvokeTransactionV3),
}

/// A declare transaction of a Cairo-v1 contract class that can be added to Starknet through the
/// RPC.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct RPCDeclareTransactionV3 {
// TODO: Check with Shahak why we need to keep the DeclareType.
// pub r#type: DeclareType,
pub sender_address: ContractAddress,
pub compiled_class_hash: CompiledClassHash,
pub signature: TransactionSignature,
pub nonce: Nonce,
pub contract_class: ContractClass,
pub resource_bounds: ResourceBoundsMapping,
pub tip: Tip,
pub paymaster_data: PaymasterData,
pub account_deployment_data: AccountDeploymentData,
pub nonce_data_availability_mode: DataAvailabilityMode,
pub fee_data_availability_mode: DataAvailabilityMode,
}

/// A deploy account transaction that can be added to Starknet through the RPC.
#[derive(Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
pub struct RPCDeployAccountTransactionV3 {
pub signature: TransactionSignature,
pub nonce: Nonce,
pub class_hash: ClassHash,
pub contract_address_salt: ContractAddressSalt,
pub constructor_calldata: Calldata,
pub resource_bounds: ResourceBoundsMapping,
pub tip: Tip,
pub paymaster_data: PaymasterData,
pub nonce_data_availability_mode: DataAvailabilityMode,
pub fee_data_availability_mode: DataAvailabilityMode,
}

/// An invoke account transaction that can be added to Starknet through the RPC.
#[derive(Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
pub struct RPCInvokeTransactionV3 {
pub sender_address: ContractAddress,
pub calldata: Calldata,
pub signature: TransactionSignature,
pub nonce: Nonce,
pub resource_bounds: ResourceBoundsMapping,
pub tip: Tip,
pub paymaster_data: PaymasterData,
pub account_deployment_data: AccountDeploymentData,
pub nonce_data_availability_mode: DataAvailabilityMode,
pub fee_data_availability_mode: DataAvailabilityMode,
}

// The contract class in SN_API state doesn't have `contract_class_version`, not following the spec.
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
pub struct ContractClass {
pub sierra_program: Vec<Felt>,
pub contract_class_version: String,
pub entry_points_by_type: EntryPointByType,
pub abi: String,
}

#[derive(Debug, Clone, Default, Eq, PartialEq, Deserialize, Serialize)]
pub struct EntryPointByType {
#[serde(rename = "CONSTRUCTOR")]
pub constructor: Vec<EntryPoint>,
#[serde(rename = "EXTERNAL")]
pub external: Vec<EntryPoint>,
#[serde(rename = "L1_HANDLER")]
pub l1handler: Vec<EntryPoint>,
}

// The serialization of the struct in transaction is in capital letters, not following the spec.
#[derive(Clone, Debug, Default, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
pub struct ResourceBoundsMapping {
pub l1_gas: ResourceBounds,
pub l2_gas: ResourceBounds,
}

impl From<ResourceBoundsMapping> for crate::transaction::ResourceBoundsMapping {
fn from(mapping: ResourceBoundsMapping) -> crate::transaction::ResourceBoundsMapping {
let map =
BTreeMap::from([(Resource::L1Gas, mapping.l1_gas), (Resource::L2Gas, mapping.l2_gas)]);
crate::transaction::ResourceBoundsMapping(map)
}
}
Loading

0 comments on commit a78c47e

Please sign in to comment.