fixed comparison logic
@@ -47,12 +47,23 @@ use std::cmp::{Ord, PartialEq, PartialOrd, Eq, Ordering};
impl Ord for PoolRecord {
fn cmp(&self, other: &Self) -> Ordering {
- if self.fee < other.fee {
+ if self.account_id == other.account_id && self.nonce == other.nonce {
+ // replace by a new nonce, handle fee on another level
+ return Ordering::Equal;
+ } else if self.account_id == other.account_id {
+ if self.nonce < other.nonce {
+ return Ordering::Less;
+ } else {
return Ordering::Greater;
- } else if self.fee < other.fee {
+ }
+ }
+
+ if self.fee >= other.fee {
+ // sort by fee by default
return Ordering::Less;
+ } else {
+ return Ordering::Greater;
}
- Ordering::Equal
}
}
@@ -273,3 +284,25 @@ pub fn start_mem_pool(mut mem_pool: MemPool, rx_for_tx: Receiver<TransferTx>, tx
mem_pool.run(rx_for_tx, tx_for_blocks);
});
}
+
+
+#[test]
+fn test_set_insert() {
+ let pool_record_0 = PoolRecord {
+ fee: 0u128,
+ nonce: 0,
+ account_id: 2
+ };
+
+ let pool_record_1 = PoolRecord {
+ fee: 0u128,
+ nonce: 1,
+ account_id: 2
+ };
+
+ let mut set = OrdSet::new();
+ let r0 = set.insert(pool_record_0);
+ let r1 = set.insert(pool_record_1);
+ let len = set.len();
+ assert_eq!(len, 2);
+}
\ No newline at end of file
