safe_nd/rewards.rs
1// Copyright 2020 MaidSafe.net limited.
2//
3// This SAFE Network Software is licensed to you under The General Public License (GPL), version 3.
4// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
5// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
6// KIND, either express or implied. Please review the Licences for the specific language governing
7// permissions and limitations relating to use of the SAFE Network Software.
8
9use crate::Money;
10use serde::{Deserialize, Serialize};
11
12/// The representation of the smallest unit of work.
13/// This is strictly incrementing (i.e. accumulated)
14/// during the network lifetime of the worker.
15pub type Work = u64;
16
17///
18#[derive(Clone, Eq, Hash, PartialEq, PartialOrd, Debug, Ord, Serialize, Deserialize)]
19pub struct RewardCounter {
20 /// Accumulated rewards.
21 /// This is reset every time the
22 /// reward is paid out to the worker.
23 pub reward: Money,
24 /// Accumulated work.
25 /// This is strictly incrementing during
26 /// the network lifetime of the worker.
27 pub work: Work,
28}
29
30impl RewardCounter {
31 ///
32 pub fn add(&self, reward: Money) -> Option<Self> {
33 let sum = match self.reward.checked_add(reward) {
34 Some(s) => s,
35 None => return None,
36 };
37 Some(Self {
38 work: self.work + 1,
39 reward: sum,
40 })
41 }
42}
43
44impl Default for RewardCounter {
45 fn default() -> Self {
46 Self {
47 work: 0,
48 reward: Money::zero(),
49 }
50 }
51}