Skip to content

Commit 77121b8

Browse files
authored
Merge pull request #5291 from stacks-network/feat/optimize-pox-bitmap-check
fix: use O(n) instead of mn when checking pox bitvec
2 parents fa5845c + b843273 commit 77121b8

File tree

1 file changed

+16
-12
lines changed
  • stackslib/src/chainstate/nakamoto

1 file changed

+16
-12
lines changed

stackslib/src/chainstate/nakamoto/mod.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3810,6 +3810,15 @@ impl NakamotoChainState {
38103810
active_reward_set: &RewardSet,
38113811
) -> Result<(), ChainstateError> {
38123812
if !tenure_block_commit.treatment.is_empty() {
3813+
let address_to_indeces: HashMap<_, Vec<_>> = active_reward_set
3814+
.rewarded_addresses
3815+
.iter()
3816+
.enumerate()
3817+
.fold(HashMap::new(), |mut map, (ix, addr)| {
3818+
map.entry(addr).or_insert_with(Vec::new).push(ix);
3819+
map
3820+
});
3821+
38133822
// our block commit issued a punishment, check the reward set and bitvector
38143823
// to ensure that this was valid.
38153824
for treated_addr in tenure_block_commit.treatment.iter() {
@@ -3820,24 +3829,19 @@ impl NakamotoChainState {
38203829
}
38213830
// otherwise, we need to find the indices in the rewarded_addresses
38223831
// corresponding to this address.
3823-
let address_indices = active_reward_set
3824-
.rewarded_addresses
3825-
.iter()
3826-
.enumerate()
3827-
.filter_map(|(ix, addr)| {
3828-
if addr == treated_addr.deref() {
3829-
Some(ix)
3830-
} else {
3831-
None
3832-
}
3833-
});
3832+
let empty_vec = vec![];
3833+
let address_indices = address_to_indeces
3834+
.get(treated_addr.deref())
3835+
.unwrap_or(&empty_vec);
3836+
38343837
// if any of them are 0, punishment is okay.
38353838
// if all of them are 1, punishment is not okay.
38363839
// if all of them are 0, *must* have punished
38373840
let bitvec_values: Result<Vec<_>, ChainstateError> = address_indices
3841+
.iter()
38383842
.map(
38393843
|ix| {
3840-
let ix = u16::try_from(ix)
3844+
let ix = u16::try_from(*ix)
38413845
.map_err(|_| ChainstateError::InvalidStacksBlock("Reward set index outside of u16".into()))?;
38423846
let bitvec_value = block_bitvec.get(ix)
38433847
.unwrap_or_else(|| {

0 commit comments

Comments
 (0)