-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
03ce42f
commit d4c80f0
Showing
3 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod mempool; | ||
pub(crate) mod priority_queue; | ||
pub mod staging_area; | ||
pub mod transaction_store; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
use std::cmp::Ordering; | ||
use std::collections::HashMap; | ||
|
||
use starknet_api::core::{ContractAddress, Nonce}; | ||
use starknet_api::transaction::TransactionHash; | ||
use starknet_mempool_types::errors::MempoolError; | ||
use starknet_mempool_types::mempool_types::ThinTransaction; | ||
|
||
#[derive(Clone, Debug, Default)] | ||
pub struct StagingTransaction { | ||
pub tx_hash: TransactionHash, | ||
pub address: ContractAddress, | ||
pub nonce: Nonce, | ||
} | ||
|
||
impl PartialEq for StagingTransaction { | ||
fn eq(&self, other: &StagingTransaction) -> bool { | ||
self.address == other.address && self.nonce == other.nonce | ||
} | ||
} | ||
|
||
impl Eq for StagingTransaction {} | ||
|
||
impl Ord for StagingTransaction { | ||
fn cmp(&self, other: &Self) -> Ordering { | ||
self.address.cmp(&other.address).then_with(|| self.nonce.cmp(&other.nonce)) | ||
} | ||
} | ||
|
||
impl PartialOrd for StagingTransaction { | ||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { | ||
Some(self.cmp(other)) | ||
} | ||
} | ||
|
||
#[derive(Clone, Debug, Default, derive_more::Deref, derive_more::DerefMut)] | ||
pub struct StagingQueue(HashMap<TransactionHash, StagingTransaction>); | ||
impl StagingQueue { | ||
pub fn insert(&mut self, tx: StagingTransaction) -> Result<(), MempoolError> { | ||
if self.0.contains_key(&tx.tx_hash) { | ||
return Err(MempoolError::DuplicateTransaction { tx_hash: tx.tx_hash }); | ||
} | ||
self.0.insert(tx.tx_hash, tx); | ||
|
||
Ok(()) | ||
} | ||
|
||
pub fn remove( | ||
&mut self, | ||
tx_hash: &TransactionHash, | ||
) -> Result<StagingTransaction, MempoolError> { | ||
match self.0.remove(tx_hash) { | ||
Some(tx) => Ok(tx), | ||
None => Err(MempoolError::TransactionNotFound { tx_hash: *tx_hash }), | ||
} | ||
} | ||
} | ||
|
||
impl From<ThinTransaction> for StagingTransaction { | ||
fn from(tx: ThinTransaction) -> Self { | ||
StagingTransaction { address: tx.sender_address, nonce: tx.nonce, tx_hash: tx.tx_hash } | ||
} | ||
} |