Skip to content

Commit

Permalink
stake-contract: optimized get_provisioners
Browse files Browse the repository at this point in the history
  • Loading branch information
miloszm committed Mar 8, 2024
1 parent 104fdbe commit 70a9ae2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
2 changes: 2 additions & 0 deletions contracts/stake/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- Improved performance of get_provisioners [#1447]
- Change dependencies declarations enforce bytecheck [#1371]
- Removed 'phoenix-core' dependency [#1138]

## [0.7.0] - 2023-12-15

[#1447]: https://github.com/dusk-network/rusk/issues/1447
[#1371]: https://github.com/dusk-network/rusk/issues/1371
[#1138]: https://github.com/dusk-network/rusk/issues/1138
19 changes: 9 additions & 10 deletions contracts/stake/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use transfer_contract_types::*;
/// valid stake.
#[derive(Debug, Default, Clone)]
pub struct StakeState {
stakes: BTreeMap<[u8; PublicKey::SIZE], StakeData>,
stakes: BTreeMap<[u8; PublicKey::SIZE], (StakeData, PublicKey)>,
slashed_amount: u64,
}

Expand Down Expand Up @@ -156,17 +156,18 @@ impl StakeState {

/// Gets a reference to a stake.
pub fn get_stake(&self, key: &PublicKey) -> Option<&StakeData> {
self.stakes.get(&key.to_bytes())
self.stakes.get(&key.to_bytes()).map(|(s, _)| s)
}

/// Gets a mutable reference to a stake.
pub fn get_stake_mut(&mut self, key: &PublicKey) -> Option<&mut StakeData> {
self.stakes.get_mut(&key.to_bytes())
self.stakes.get_mut(&key.to_bytes()).map(|(s, _)| s)
}

/// Pushes the given `stake` onto the state for a given `public_key`.
pub fn insert_stake(&mut self, public_key: PublicKey, stake: StakeData) {
self.stakes.insert(public_key.to_bytes(), stake);
self.stakes
.insert(public_key.to_bytes(), (stake, public_key));
}

/// Gets a mutable reference to the stake of a given key. If said stake
Expand All @@ -180,11 +181,11 @@ impl StakeState {

if is_missing {
let stake = StakeData::default();
self.stakes.insert(pk.to_bytes(), stake);
self.stakes.insert(pk.to_bytes(), (stake, *pk));
}

// SAFETY: unwrap is ok since we're sure we inserted an element
self.stakes.get_mut(&pk.to_bytes()).unwrap()
self.stakes.get_mut(&pk.to_bytes()).map(|(s, _)| s).unwrap()
}

/// Rewards a `public_key` with the given `value`. If a stake does not exist
Expand Down Expand Up @@ -259,10 +260,8 @@ impl StakeState {

/// Feeds the host with the stakes.
pub fn stakes(&self) {
for (k, v) in self.stakes.iter() {
let pk = PublicKey::from_bytes(k).unwrap();
let stake_data = v.clone();
rusk_abi::feed((pk, stake_data));
for (stake_data, pk) in self.stakes.values() {
rusk_abi::feed((*pk, stake_data.clone()));
}
}
}

0 comments on commit 70a9ae2

Please sign in to comment.