datahaven/contracts/deployments
Ahmad Kaouk 162247bfbd
refactor(rewards): Optimize reward calculation (#408)
### Summary

Optimizes `award_session_performance_points` by batching all validator
rewards into a single storage mutation instead of performing individual
mutations inside the loop.

### Problem

The `award_session_performance_points` function, called during session
rotation via `SessionManager::end_session`, was calling `reward_by_ids`
inside the validator loop for each validator individually:

```rust
for validator in validators.iter() {
    // ... calculate points ...
    Self::reward_by_ids([(validator.clone(), points)].into_iter());
}
```

Each call to `reward_by_ids` performs a `StorageMap::mutate` on
`RewardPointsForEra`, which reads and writes the entire
`EraRewardPoints` structure (a `BTreeMap` containing up to N validator
entries). With N validators, this results in N separate
read-modify-write cycles of an O(N)-sized structure, leading to O(N²)
total storage I/O.

### Solution

Collect all reward points first, then perform a single batched call to
`reward_by_ids`:

```rust
let mut rewards = Vec::new();

for validator in validators.iter() {
    // ... calculate points ...
    rewards.push((validator.clone(), points));
}

if !rewards.is_empty() {
    Self::reward_by_ids(rewards.into_iter());
}
```

This reduces the complexity from O(N²) to O(N) by performing only one
storage mutation that processes all validators at once.

### Why This Matters

Session rotation hooks are mandatory—they execute regardless of block
weight limits. While `pallet_session::on_initialize` returns `max_block`
weight during rotation (preventing user transactions), the actual
execution time still matters. With a large validator set, O(N²) storage
operations could exceed the block time target, potentially causing block
production delays.

### Test Plan

- [x] Existing unit tests pass (`cargo test -p
pallet-external-validators-rewards`)
2026-01-22 18:40:12 -03:00
..
anvil-rewards-info.json refactor: cleanup old rewards model (#383) 2026-01-09 15:25:49 +01:00
anvil.json refactor: Remove eigenlayer-middleware and flatten ServiceManagerBase (#389) 2026-01-13 15:03:10 +01:00
datahaven-logo.jpg feat: support updating the AVS dashboard metadata (#136) 2025-09-02 15:54:47 +02:00
datahaven-logo.png feat: support updating the AVS dashboard metadata (#136) 2025-09-02 15:54:47 +02:00
hoodi.json feat: Datahaven contracts deployment on public testnet (#123) 2025-08-21 10:02:31 +00:00
metadata.json refactor(contracts): Harden DataHavenServiceManager with input validation and code cleanup (#395) 2026-01-20 10:32:32 +00:00
state-diff.checksum refactor(rewards): Optimize reward calculation (#408) 2026-01-22 18:40:12 -03:00
state-diff.json refactor(rewards): Optimize reward calculation (#408) 2026-01-22 18:40:12 -03:00