fn push_unconfirmed(&mut self, unconfirmed: Unconfirmed) -> Result<usize, String> {
        // most high position depend index
        let mut depend_index: Option<usize> = None;
        while let Some(tx) = self.txs.streaming().next() {
            if unconfirmed.depend_hashs.contains(&tx.hash) {
                let index = self.txs.position(&tx.hash).unwrap();
                depend_index = Some(index);
            }
            if unconfirmed.hash == tx.hash {
                return Err("already inserted tx".to_owned());
            }
        }

        // most low position required index
        let mut required_index = None;
        let mut disturbs = Vec::new();
        while let Some(tx) = self.txs.streaming().rev().next() {
            if tx.depend_hashs.contains(&unconfirmed.hash) {
                let index = self.txs.position(&tx.hash).unwrap();
                required_index = Some(index);
                // check absolute condition: depend_index < required_index
                if depend_index.is_some() && depend_index.unwrap() >= index {
                    disturbs.push(tx.hash.clone());
                }
            }
        }

        // exception: with disturbs
        if 0 < disturbs.len() {
            // 1. remove disturbs
            let mut deleted: Vec<Unconfirmed> = Vec::new();
            for disturb in disturbs {
                self.remove_with_depend_myself(&disturb, &mut deleted);
            }

            // 2. push original (not disturbed)
            let hash = unconfirmed.hash.clone();
            assert!(self.push_unconfirmed(unconfirmed).is_ok());

            // 3. push deleted disturbs
            for tx in deleted {
                assert!(self.push_unconfirmed(tx).is_ok());
            }

            // 4. find original position
            let position = self.txs.position(&hash);
            return Ok(position.unwrap());
        }

        // normal: without disturbs
        // find best relative condition
        let mut best_index: Option<usize> = None;
        while let Some(tx) = self.txs.streaming().next() {
            let index = self.txs.position(&tx.hash).unwrap();
            // absolute conditions
            // ex
            //        0 1 2 3 4 5
            // vec = [a,b,c,d,e,f]
            //
            // You can insert positions(2,3,4) when you depend on b(1) and required by e(4)
            if depend_index.is_some() && index <= depend_index.unwrap() {
                continue;
            }
            if required_index.is_some() && index > required_index.unwrap() {
                continue;
            }

            // relative conditions
            if unconfirmed.gas_price < tx.gas_price {
                continue;
            } else if unconfirmed.gas_price == tx.gas_price {
                if unconfirmed.time >= tx.time {
                    continue;
                }
            }
            // find
            if best_index.is_none() {
                best_index = Some(index);
                break;
            }
        }

        // minimum index is required_index (or None)
        if best_index.is_none() {
            best_index = required_index.clone();
        }

        // insert
        match best_index {
            Some(best_index) => {
                // println!("best {} {:?} {:?}", best_index, depend_index, required_index);
                self.txs.insert(best_index, unconfirmed);
                Ok(best_index)
            },
            None => {
                // println!("last {:?} {:?}", depend_index, required_index);
                self.txs.push(unconfirmed);
                Ok(self.txs.len() - 1)
            },
        }
    }
/// push unconfirmed tx with dependency check
/// return inserted tx's index
push unconfirmed tx with dependency check
return inserted tx's index
push unconfirmed tx with dependency check return inserted tx ' s index
fn push_unconfirmed(&mut self, unconfirmed: Unconfirmed) -> Result<usize, String> {
        // most high position depend index
        let mut depend_index: Option<usize> = None;
        while let Some(tx) = self.txs.streaming().next() {
            if unconfirmed.depend_hashs.contains(&tx.hash) {
                let index = self.txs.position(&tx.hash).unwrap();
                depend_index = Some(index);
            }
            if unconfirmed.hash == tx.hash {
                return Err("already inserted tx".to_owned());
            }
        }
        // most low position required index
        let mut required_index = None;
        let mut disturbs = Vec::new();
        while let Some(tx) = self.txs.streaming().rev().next() {
            if tx.depend_hashs.contains(&unconfirmed.hash) {
                let index = self.txs.position(&tx.hash).unwrap();
                required_index = Some(index);
                // check absolute condition: depend_index < required_index
                if depend_index.is_some() && depend_index.unwrap() >= index {
                    disturbs.push(tx.hash.clone());
                }
            }
        }
        // exception: with disturbs
        if 0 < disturbs.len() {
            // 1. remove disturbs
            let mut deleted: Vec<Unconfirmed> = Vec::new();
            for disturb in disturbs {
                self.remove_with_depend_myself(&disturb, &mut deleted);
            }
            // 2. push original (not disturbed)
            let hash = unconfirmed.hash.clone();
            assert!(self.push_unconfirmed(unconfirmed).is_ok());
            // 3. push deleted disturbs
            for tx in deleted {
                assert!(self.push_unconfirmed(tx).is_ok());
            }
            // 4. find original position
            let position = self.txs.position(&hash);
            return Ok(position.unwrap());
        }
        // normal: without disturbs
        // find best relative condition
        let mut best_index: Option<usize> = None;
        while let Some(tx) = self.txs.streaming().next() {
            let index = self.txs.position(&tx.hash).unwrap();
            // absolute conditions
            // ex
            //        0 1 2 3 4 5
            // vec = [a,b,c,d,e,f]
            //
            // You can insert positions(2,3,4) when you depend on b(1) and required by e(4)
            if depend_index.is_some() && index <= depend_index.unwrap() {
                continue;
            }
            if required_index.is_some() && index > required_index.unwrap() {
                continue;
            }
            // relative conditions
            if unconfirmed.gas_price < tx.gas_price {
                continue;
            } else if unconfirmed.gas_price == tx.gas_price {
                if unconfirmed.time >= tx.time {
                    continue;
                }
            }
            // find
            if best_index.is_none() {
                best_index = Some(index);
                break;
            }
        }
        // minimum index is required_index (or None)
        if best_index.is_none() {
            best_index = required_index.clone();
        }
        // insert
        match best_index {
            Some(best_index) => {
                // println!("best {} {:?} {:?}", best_index, depend_index, required_index);
                self.txs.insert(best_index, unconfirmed);
                Ok(best_index)
            },
            None => {
                // println!("last {:?} {:?}", depend_index, required_index);
                self.txs.push(unconfirmed);
                Ok(self.txs.len() - 1)
            },
        }
    }
fn push_unconfirmed ( & mut self , unconfirmed : Unconfirmed ) -> Result < usize , String > { // most high position depend index let mut depend_index : Option < usize > = None ; while let Some ( tx ) = self . txs . streaming ( ) . next ( ) { if unconfirmed . depend_hashs . contains ( & tx . hash ) { let index = self . txs . position ( & tx . hash ) . unwrap ( ) ; depend_index = Some ( index ) ; } if unconfirmed . hash == tx . hash { return Err ( " " . to_owned ( ) ) ; } } // most low position required index let mut required_index = None ; let mut disturbs = Vec :: new ( ) ; while let Some ( tx ) = self . txs . streaming ( ) . rev ( ) . next ( ) { if tx . depend_hashs . contains ( & unconfirmed . hash ) { let index = self . txs . position ( & tx . hash ) . unwrap ( ) ; required_index = Some ( index ) ; // check absolute condition: depend_index < required_index if depend_index . is_some ( ) && depend_index . unwrap ( ) >= index { disturbs . push ( tx . hash . clone ( ) ) ; } } } // exception: with disturbs if 0 < disturbs . len ( ) { // 1. remove disturbs let mut deleted : Vec < Unconfirmed > = Vec :: new ( ) ; for disturb in disturbs { self . remove_with_depend_myself ( & disturb , & mut deleted ) ; } // 2. push original (not disturbed) let hash = unconfirmed . hash . clone ( ) ; assert ! ( self push_unconfirmed ( unconfirmed ) is_ok ( ) ) ; // 3. push deleted disturbs for tx in deleted { assert ! ( self push_unconfirmed ( tx ) is_ok ( ) ) ; } // 4. find original position let position = self . txs . position ( & hash ) ; return Ok ( position . unwrap ( ) ) ; } // normal: without disturbs // find best relative condition let mut best_index : Option < usize > = None ; while let Some ( tx ) = self . txs . streaming ( ) . next ( ) { let index = self . txs . position ( & tx . hash ) . unwrap ( ) ; // absolute conditions // ex //        0 1 2 3 4 5 // vec = [a,b,c,d,e,f] // // You can insert positions(2,3,4) when you depend on b(1) and required by e(4) if depend_index . is_some ( ) && index <= depend_index . unwrap ( ) { continue ; } if required_index . is_some ( ) && index > required_index . unwrap ( ) { continue ; } // relative conditions if unconfirmed . gas_price < tx . gas_price { continue ; } else if unconfirmed . gas_price == tx . gas_price { if unconfirmed . time >= tx . time { continue ; } } // find if best_index . is_none ( ) { best_index = Some ( index ) ; break ; } } // minimum index is required_index (or None) if best_index . is_none ( ) { best_index = required_index . clone ( ) ; } // insert match best_index { Some ( best_index ) => { // println!("best {} {:?} {:?}", best_index, depend_index, required_index); self . txs . insert ( best_index , unconfirmed ) ; Ok ( best_index ) } , None => { // println!("last {:?} {:?}", depend_index, required_index); self . txs . push ( unconfirmed ) ; Ok ( self . txs . len ( ) - 1 ) } , } }
push unconfirmed tx with dependency check
return inserted tx's index
push unconfirmed tx with dependency check return inserted tx ' s index
&mut self
unconfirmed
Unconfirmed
&mut self
unconfirmed
Unconfirmed