Inserts a flashblock into the cache. Returns `true` if the flashblock was cached, `false` if it was rejected because its block number exceeds the cache-ahead limit.
(&mut self, flashblock: Flashblock)
| 51 | /// Returns `true` if the flashblock was cached, `false` if it was rejected |
| 52 | /// because its block number exceeds the cache-ahead limit. |
| 53 | pub fn insert(&mut self, flashblock: Flashblock) -> bool { |
| 54 | let block_number = flashblock.metadata.block_number; |
| 55 | if !self.is_cacheable(block_number) { |
| 56 | return false; |
| 57 | } |
| 58 | let min_block_number_to_retain = block_number.saturating_sub(MAX_CACHE_AHEAD_BLOCKS); |
| 59 | self.entries.retain(|&bn, _| bn > min_block_number_to_retain); |
| 60 | self.entries.entry(block_number).or_default().insert(flashblock.index, flashblock); |
| 61 | true |
| 62 | } |
| 63 | |
| 64 | /// Drains all cached flashblocks for the given block number, returning them |
| 65 | /// sorted by index. Returns an empty `Vec` when nothing is cached. |