fil_actor_miner/
termination.rs

1// Copyright 2019-2022 ChainSafe Systems
2// SPDX-License-Identifier: Apache-2.0, MIT
3
4use std::collections::BTreeMap;
5use std::ops::AddAssign;
6
7use fvm_ipld_bitfield::BitField;
8use fvm_shared::clock::ChainEpoch;
9
10#[derive(Default)]
11pub struct TerminationResult {
12    /// Sectors maps epochs at which sectors expired, to bitfields of sector numbers.
13    pub sectors: BTreeMap<ChainEpoch, BitField>,
14    pub partitions_processed: u64,
15    pub sectors_processed: u64,
16}
17
18impl AddAssign for TerminationResult {
19    #[allow(clippy::suspicious_op_assign_impl)]
20    fn add_assign(&mut self, rhs: Self) {
21        self.partitions_processed += rhs.partitions_processed;
22        self.sectors_processed += rhs.sectors_processed;
23
24        for (epoch, new_sectors) in rhs.sectors {
25            self.sectors
26                .entry(epoch)
27                .and_modify(|sectors| *sectors |= &new_sectors)
28                .or_insert(new_sectors);
29        }
30    }
31}
32
33impl TerminationResult {
34    pub fn new() -> Self {
35        Default::default()
36    }
37
38    /// Returns true if we're below the partition/sector limit. Returns false if
39    /// we're at (or above) the limit.
40    pub fn below_limit(&self, partition_limit: u64, sector_limit: u64) -> bool {
41        self.partitions_processed < partition_limit && self.sectors_processed < sector_limit
42    }
43
44    pub fn is_empty(&self) -> bool {
45        self.sectors_processed == 0
46    }
47
48    pub fn iter(&self) -> impl Iterator<Item = (ChainEpoch, &BitField)> {
49        // The btreemap is already sorted.
50        self.sectors.iter().map(|(&epoch, bf)| (epoch, bf))
51    }
52}