|
| 1 | +pub mod data; |
| 2 | + |
| 3 | +use std::{ |
| 4 | + io::Result, |
| 5 | + sync::{Arc, RwLock}, |
| 6 | +}; |
| 7 | + |
| 8 | +use avalanche_types::ids; |
| 9 | +use tokio::sync::broadcast; |
| 10 | + |
| 11 | +use crate::chain::tx::tx::Transaction; |
| 12 | + |
| 13 | +use self::data::{Data, Entry}; |
| 14 | + |
| 15 | +pub struct Mempool { |
| 16 | + data: Arc<RwLock<Data>>, |
| 17 | + pending_tx: broadcast::Sender<()>, |
| 18 | + new_txs: Vec<Transaction>, |
| 19 | +} |
| 20 | + |
| 21 | +impl Mempool { |
| 22 | + pub fn new(max_size: usize) -> Self { |
| 23 | + // initialize broadcast channel |
| 24 | + let (pending_tx, _rx): (broadcast::Sender<()>, broadcast::Receiver<()>) = |
| 25 | + tokio::sync::broadcast::channel(1); |
| 26 | + Self { |
| 27 | + data: Arc::new(RwLock::new(Data::new(max_size))), |
| 28 | + /// Channel of length one, which the mempool ensures has an item on |
| 29 | + /// it as long as there is an unissued transaction remaining in [txs]. |
| 30 | + pending_tx, |
| 31 | + new_txs: Vec::new(), |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + /// Returns a broadcast receiver for the pending tx channel. |
| 36 | + pub fn subscribe_pending(&self) -> broadcast::Receiver<()> { |
| 37 | + self.pending_tx.subscribe() |
| 38 | + } |
| 39 | + |
| 40 | + /// Returns Tx from Id if it exists. |
| 41 | + pub fn get(&self, id: &ids::Id) -> Result<Option<Transaction>> { |
| 42 | + let data = self.data.read().unwrap(); |
| 43 | + if let Some(entry) = data.get(id)? { |
| 44 | + if let Some(tx) = entry.tx { |
| 45 | + return Ok(Some(tx)); |
| 46 | + } |
| 47 | + } |
| 48 | + Ok(None) |
| 49 | + } |
| 50 | + |
| 51 | + /// Adds a Tx Entry to mempool and writes to the pending channel. |
| 52 | + pub fn add(&mut self, tx: Transaction) -> Result<bool> { |
| 53 | + let tx_id = &tx.id; |
| 54 | + |
| 55 | + let mut data = self.data.write().unwrap(); |
| 56 | + if data.has(tx_id)? { |
| 57 | + return Ok(false); |
| 58 | + } |
| 59 | + let old_len = data.len(); |
| 60 | + |
| 61 | + let entry = &Entry { |
| 62 | + id: tx_id.to_owned(), |
| 63 | + tx: Some(tx.clone()), |
| 64 | + index: old_len, |
| 65 | + }; |
| 66 | + |
| 67 | + // Optimistically add tx to mempool |
| 68 | + data.push(entry)?; |
| 69 | + |
| 70 | + self.new_txs.push(tx); |
| 71 | + |
| 72 | + self.add_pending(); |
| 73 | + |
| 74 | + Ok(true) |
| 75 | + } |
| 76 | + |
| 77 | + /// Returns len of mempool data. |
| 78 | + pub fn len(&self) -> usize { |
| 79 | + let data = self.data.read().unwrap(); |
| 80 | + data.len() |
| 81 | + } |
| 82 | + |
| 83 | + pub fn is_empty(&self) -> bool { |
| 84 | + let data = self.data.read().unwrap(); |
| 85 | + data.is_empty() |
| 86 | + } |
| 87 | + |
| 88 | + /// Prunes any Ids not included in valid hashes set. |
| 89 | + pub fn prune(&self, valid_hashes: ids::Set) { |
| 90 | + let mut to_remove: Vec<ids::Id> = Vec::with_capacity(valid_hashes.len()); |
| 91 | + |
| 92 | + let data = self.data.write().unwrap(); |
| 93 | + |
| 94 | + for entry in data.items.iter() { |
| 95 | + if let Some(tx) = &entry.tx { |
| 96 | + if !valid_hashes.contains(&tx.id) { |
| 97 | + to_remove.push(entry.id); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + // drop write lock |
| 102 | + drop(data); |
| 103 | + |
| 104 | + for id in to_remove.iter() { |
| 105 | + log::debug!("attempting to prune id: {}", id); |
| 106 | + if self.remove(id.to_owned()).is_some() { |
| 107 | + log::debug!("id deleted: {}", id); |
| 108 | + } else { |
| 109 | + log::debug!("failed to delete id: {}: not found", id); |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + /// Removes Tx entry from mempool data if it exists. |
| 115 | + pub fn remove(&self, id: ids::Id) -> Option<Transaction> { |
| 116 | + let mut data = self.data.write().unwrap(); |
| 117 | + |
| 118 | + // TODO: try to optimize. |
| 119 | + // find the position of the entry in vec and remove |
| 120 | + match data.items.iter().position(|e| e.id == id) { |
| 121 | + Some(index) => { |
| 122 | + data.items.remove(index); |
| 123 | + } |
| 124 | + None => return None, |
| 125 | + } |
| 126 | + |
| 127 | + // remove entry from lookup |
| 128 | + match data.lookup.remove(&id) { |
| 129 | + Some(entry) => entry.tx, |
| 130 | + None => { |
| 131 | + // should not happen |
| 132 | + log::error!("failed to remove id: {}: mempool is out of balance", id); |
| 133 | + None |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + fn add_pending(&self) { |
| 139 | + self.pending_tx.send(()).unwrap(); |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +#[tokio::test] |
| 144 | +async fn test_mempool() { |
| 145 | + use crate::chain::tx::{tx::TransactionType, unsigned}; |
| 146 | + |
| 147 | + // init mempool |
| 148 | + let mut mempool = Mempool::new(10); |
| 149 | + let mut pending_rx = mempool.subscribe_pending(); |
| 150 | + |
| 151 | + // create tx_1 |
| 152 | + let tx_data_1 = unsigned::TransactionData { |
| 153 | + typ: TransactionType::Bucket, |
| 154 | + bucket: "foo".to_string(), |
| 155 | + key: "".to_string(), |
| 156 | + value: vec![], |
| 157 | + }; |
| 158 | + let resp = tx_data_1.decode(); |
| 159 | + assert!(resp.is_ok()); |
| 160 | + let utx_1 = resp.unwrap(); |
| 161 | + let tx_1 = Transaction::new(utx_1); |
| 162 | + |
| 163 | + // add tx_1 to mempool |
| 164 | + let tx_1_id = tx_1.id; |
| 165 | + assert_eq!(mempool.add(tx_1).unwrap(), true); |
| 166 | + // drain channel |
| 167 | + let resp = pending_rx.recv().await; |
| 168 | + assert!(resp.is_ok()); |
| 169 | + assert_eq!(mempool.len(), 1); |
| 170 | + |
| 171 | + // add tx_1 as valid |
| 172 | + let mut valid_txs = ids::new_set(2); |
| 173 | + valid_txs.insert(tx_1_id); |
| 174 | + |
| 175 | + // create tx_2 |
| 176 | + let tx_data_2 = unsigned::TransactionData { |
| 177 | + typ: TransactionType::Bucket, |
| 178 | + bucket: "bar".to_string(), |
| 179 | + key: "".to_string(), |
| 180 | + value: vec![], |
| 181 | + }; |
| 182 | + let resp = tx_data_2.decode(); |
| 183 | + assert!(resp.is_ok()); |
| 184 | + let utx_2 = resp.unwrap(); |
| 185 | + let mut tx_2 = Transaction::new(utx_2); |
| 186 | + tx_2.id = ids::Id::from_slice("sup".as_bytes()); |
| 187 | + |
| 188 | + // add tx_2 to mempool |
| 189 | + assert_eq!(mempool.add(tx_2).unwrap(), true); |
| 190 | + assert_eq!(mempool.len(), 2); |
| 191 | + |
| 192 | + // drain channel |
| 193 | + let resp = pending_rx.recv().await; |
| 194 | + assert!(resp.is_ok()); |
| 195 | + |
| 196 | + // prune tx_2 as invalid |
| 197 | + mempool.prune(valid_txs); |
| 198 | + |
| 199 | + // verify one tx entry removed |
| 200 | + assert_eq!(mempool.len(), 1); |
| 201 | + |
| 202 | + // verify tx_1 exists |
| 203 | + let resp = mempool.get(&tx_1_id); |
| 204 | + assert!(resp.is_ok()); |
| 205 | + |
| 206 | + assert_eq!(resp.unwrap().unwrap().id, tx_1_id); |
| 207 | +} |
0 commit comments