-
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.
feat(mempool): implement priority queuedata structure
- Loading branch information
1 parent
72a2b06
commit eca5cf1
Showing
6 changed files
with
172 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,17 @@ | ||
[package] | ||
name = "starknet_mempool" | ||
version.workspace = true | ||
edition.workspace = true | ||
repository.workspace = true | ||
license.workspace = true | ||
|
||
[lints] | ||
workspace = true | ||
|
||
[dependencies] | ||
derive_more.workspace = true | ||
starknet_api.workspace = true | ||
|
||
[dev-dependencies] | ||
assert_matches.workspace = true | ||
tokio.workspace = true |
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,2 @@ | ||
// TODO: change to pub(crate) once this is used by the (not yet implemented) mempool struct. | ||
pub mod priority_queue; |
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,75 @@ | ||
#[cfg(test)] | ||
#[path = "priority_queue_test.rs"] | ||
pub mod priority_queue_test; | ||
|
||
use std::{cmp::Ordering, collections::BTreeSet}; | ||
|
||
use starknet_api::{ | ||
internal_transaction::InternalTransaction, | ||
transaction::{DeclareTransaction, DeployAccountTransaction, InvokeTransaction, Tip}, | ||
}; | ||
|
||
// Assumption: for the MVP only one transaction from the same contract class can be in the mempool | ||
// at a time. When this changes, saving the transactions themselves on the queu might no longer be | ||
// appropriate, because we'll also need to stores transactions without indexing them. For example, | ||
// transactions with future nonces will need to be stored, and potentially indexed on block commits. | ||
#[derive(Clone, Debug, Default, derive_more::Deref, derive_more::DerefMut)] | ||
pub struct PriorityQueue(BTreeSet<PQTransaction>); | ||
|
||
impl PriorityQueue { | ||
pub fn push(&mut self, tx: InternalTransaction) { | ||
let mempool_tx = PQTransaction(tx); | ||
self.insert(mempool_tx); | ||
} | ||
|
||
// Removes and returns the transaction with the highest tip. | ||
pub fn pop(&mut self) -> Option<InternalTransaction> { | ||
self.pop_last().map(|tx| tx.0) | ||
} | ||
} | ||
|
||
#[derive(Clone, Debug, derive_more::Deref)] | ||
pub struct PQTransaction(pub InternalTransaction); | ||
|
||
impl PQTransaction { | ||
fn tip(&self) -> Tip { | ||
match &self.0 { | ||
InternalTransaction::Declare(declare_tx) => match &declare_tx.tx { | ||
DeclareTransaction::V3(tx_v3) => tx_v3.tip, | ||
_ => unimplemented!(), | ||
}, | ||
InternalTransaction::DeployAccount(deploy_account_tx) => match &deploy_account_tx.tx { | ||
DeployAccountTransaction::V3(tx_v3) => tx_v3.tip, | ||
_ => unimplemented!(), | ||
}, | ||
InternalTransaction::Invoke(invoke_tx) => match &invoke_tx.tx { | ||
InvokeTransaction::V3(tx_v3) => tx_v3.tip, | ||
_ => unimplemented!(), | ||
}, | ||
} | ||
} | ||
} | ||
|
||
// Compare transactions based on their tip only, which implies `Eq`, because `tip` is uint. | ||
impl PartialEq for PQTransaction { | ||
fn eq(&self, other: &PQTransaction) -> bool { | ||
self.tip() == other.tip() | ||
} | ||
} | ||
|
||
/// Marks PQTransaction as capable of strict equality comparisons, signaling to the compiler it | ||
/// adheres to equality semantics. | ||
// Note: this depends on the implementation of `PartialEq`, see its docstring. | ||
impl Eq for PQTransaction {} | ||
|
||
impl Ord for PQTransaction { | ||
fn cmp(&self, other: &Self) -> Ordering { | ||
self.tip().cmp(&other.tip()) | ||
} | ||
} | ||
|
||
impl PartialOrd for PQTransaction { | ||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { | ||
Some(self.cmp(other)) | ||
} | ||
} |
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,62 @@ | ||
use starknet_api::hash::StarkFelt; | ||
use starknet_api::{ | ||
data_availability::DataAvailabilityMode, | ||
internal_transaction::{InternalInvokeTransaction, InternalTransaction}, | ||
transaction::{ | ||
InvokeTransaction, InvokeTransactionV3, ResourceBounds, ResourceBoundsMapping, Tip, | ||
TransactionHash, | ||
}, | ||
}; | ||
|
||
use crate::priority_queue::PriorityQueue; | ||
|
||
pub fn create_tx_for_testing(tip: Tip, tx_hash: TransactionHash) -> InternalTransaction { | ||
let tx = InvokeTransactionV3 { | ||
resource_bounds: ResourceBoundsMapping::try_from(vec![ | ||
( | ||
starknet_api::transaction::Resource::L1Gas, | ||
ResourceBounds::default(), | ||
), | ||
( | ||
starknet_api::transaction::Resource::L2Gas, | ||
ResourceBounds::default(), | ||
), | ||
]) | ||
.expect("Resource bounds mapping has unexpected structure."), | ||
signature: Default::default(), | ||
nonce: Default::default(), | ||
sender_address: Default::default(), | ||
calldata: Default::default(), | ||
nonce_data_availability_mode: DataAvailabilityMode::L1, | ||
fee_data_availability_mode: DataAvailabilityMode::L1, | ||
paymaster_data: Default::default(), | ||
account_deployment_data: Default::default(), | ||
tip, | ||
}; | ||
|
||
InternalTransaction::Invoke(InternalInvokeTransaction { | ||
tx: InvokeTransaction::V3(tx), | ||
tx_hash, | ||
only_query: false, | ||
}) | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_priority_queue() { | ||
let tx_hash_50 = TransactionHash(StarkFelt::ONE); | ||
let tx_hash_100 = TransactionHash(StarkFelt::TWO); | ||
let tx_hash_10 = TransactionHash(StarkFelt::THREE); | ||
|
||
let tx_tip_50 = create_tx_for_testing(Tip(50), tx_hash_50); | ||
let tx_tip_100 = create_tx_for_testing(Tip(100), tx_hash_100); | ||
let tx_tip_10 = create_tx_for_testing(Tip(10), tx_hash_10); | ||
|
||
let mut pq = PriorityQueue::default(); | ||
pq.push(tx_tip_50.clone()); | ||
pq.push(tx_tip_100.clone()); | ||
pq.push(tx_tip_10.clone()); | ||
|
||
assert_eq!(pq.pop().unwrap(), tx_tip_100); | ||
assert_eq!(pq.pop().unwrap(), tx_tip_50); | ||
assert_eq!(pq.pop().unwrap(), tx_tip_10); | ||
} |