diff --git a/zebra-network/src/address_book.rs b/zebra-network/src/address_book.rs index 07d40a6c..9e15dce0 100644 --- a/zebra-network/src/address_book.rs +++ b/zebra-network/src/address_book.rs @@ -113,8 +113,20 @@ impl AddressBook { /// Returns an iterator that drains entries from the address book, removing /// them in order from most recent to least recent. - pub fn drain_recent<'a>(&'a mut self) -> impl Iterator + 'a { - Drain { book: self } + pub fn drain_newest<'a>(&'a mut self) -> impl Iterator + 'a { + Drain { + book: self, + newest_first: true, + } + } + + /// Returns an iterator that drains entries from the address book, removing + /// them in order from most recent to least recent. + pub fn drain_oldest<'a>(&'a mut self) -> impl Iterator + 'a { + Drain { + book: self, + newest_first: false, + } } } @@ -131,18 +143,23 @@ impl Extend for AddressBook { struct Drain<'a> { book: &'a mut AddressBook, + newest_first: bool, } impl<'a> Iterator for Drain<'a> { type Item = MetaAddr; fn next(&mut self) -> Option { - let most_recent = self.book.by_time.iter().next()?.clone(); - self.book.by_time.remove(&most_recent); + let next_item = if self.newest_first { + self.book.by_time.iter().next()?.clone() + } else { + self.book.by_time.iter().rev().next()?.clone() + }; + self.book.by_time.remove(&next_item); self.book .by_addr - .remove(&most_recent.addr) + .remove(&next_item.addr) .expect("cannot have by_time entry without by_addr entry"); - Some(most_recent) + Some(next_item) } } diff --git a/zebrad/src/commands/connect.rs b/zebrad/src/commands/connect.rs index 6bc5893f..5da2876c 100644 --- a/zebrad/src/commands/connect.rs +++ b/zebrad/src/commands/connect.rs @@ -139,7 +139,7 @@ impl ConnectCmd { ); } - let addrs = all_addrs.drain_recent().collect::>(); + let addrs = all_addrs.drain_newest().collect::>(); info!(addrs.len = addrs.len(), ab.len = all_addrs.peers().count()); let mut head = Vec::new();