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

Elin/merge main mempool into main #286

Merged
merged 5 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
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
178 changes: 178 additions & 0 deletions src/rpc_transaction.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
#[cfg(test)]
#[path = "rpc_transaction_test.rs"]
mod rpc_transaction_test;

use std::collections::BTreeMap;

use serde::{Deserialize, Serialize};

use crate::core::{ClassHash, CompiledClassHash, ContractAddress, Nonce};
use crate::data_availability::DataAvailabilityMode;
use crate::hash::StarkFelt;
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!(
(resource_bounds, ResourceBoundsMapping),
(signature, TransactionSignature)
);
}

/// 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<StarkFelt>,
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
Loading