identity_iota_core/rebased/migration/
multicontroller.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Copyright 2020-2024 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use std::collections::HashMap;
use std::collections::HashSet;

use crate::rebased::iota::types::Bag;
use crate::rebased::iota::types::Number;
use identity_iota_interaction::types::base_types::ObjectID;
use identity_iota_interaction::types::collection_types::Entry;
use identity_iota_interaction::types::collection_types::VecMap;
use identity_iota_interaction::types::collection_types::VecSet;
use identity_iota_interaction::types::id::UID;
use serde::Deserialize;
use serde::Serialize;

/// A [`Multicontroller`]'s proposal for changes.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(
  try_from = "IotaProposal::<T>",
  into = "IotaProposal::<T>",
  bound(serialize = "T: Serialize + Clone")
)]
pub struct Proposal<T> {
  id: UID,
  expiration_epoch: Option<u64>,
  votes: u64,
  voters: HashSet<ObjectID>,
  pub(crate) action: T,
}

impl<T> Proposal<T> {
  /// Returns this [Proposal]'s ID.
  pub fn id(&self) -> ObjectID {
    *self.id.object_id()
  }

  /// Returns the votes received by this [`Proposal`].
  pub fn votes(&self) -> u64 {
    self.votes
  }

  pub(crate) fn votes_mut(&mut self) -> &mut u64 {
    &mut self.votes
  }

  /// Returns a reference to the action contained by this [`Proposal`].
  pub fn action(&self) -> &T {
    &self.action
  }

  /// Consumes the [`Proposal`] returning its action.
  pub fn into_action(self) -> T {
    self.action
  }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
struct IotaProposal<T> {
  id: UID,
  expiration_epoch: Option<Number<u64>>,
  votes: Number<u64>,
  voters: VecSet<ObjectID>,
  action: T,
}

impl<T> TryFrom<IotaProposal<T>> for Proposal<T> {
  type Error = <u64 as TryFrom<Number<u64>>>::Error;
  fn try_from(proposal: IotaProposal<T>) -> Result<Self, Self::Error> {
    let IotaProposal {
      id,
      expiration_epoch,
      votes,
      voters,
      action,
    } = proposal;
    let expiration_epoch = expiration_epoch.map(TryInto::try_into).transpose()?;
    let votes = votes.try_into()?;
    let voters = voters.contents.into_iter().collect();

    Ok(Self {
      id,
      expiration_epoch,
      votes,
      voters,
      action,
    })
  }
}

impl<T> From<Proposal<T>> for IotaProposal<T> {
  fn from(value: Proposal<T>) -> Self {
    let Proposal {
      id,
      expiration_epoch,
      votes,
      voters,
      action,
    } = value;
    let contents = voters.into_iter().collect();
    IotaProposal {
      id,
      expiration_epoch: expiration_epoch.map(Into::into),
      votes: votes.into(),
      voters: VecSet { contents },
      action,
    }
  }
}

/// Representation of `identity.rs`'s `multicontroller::Multicontroller` Move type.
#[derive(Debug, Serialize, Deserialize)]
#[serde(try_from = "IotaMulticontroller::<T>")]
pub struct Multicontroller<T> {
  controlled_value: T,
  controllers: HashMap<ObjectID, u64>,
  threshold: u64,
  active_proposals: HashSet<ObjectID>,
  proposals: Bag,
}

impl<T> Multicontroller<T> {
  /// Returns a reference to the value that is shared between many controllers.
  pub fn controlled_value(&self) -> &T {
    &self.controlled_value
  }

  /// Returns this [`Multicontroller`]'s threshold.
  pub fn threshold(&self) -> u64 {
    self.threshold
  }

  /// Returns the lists of active [`Proposal`]s for this [`Multicontroller`].
  pub fn proposals(&self) -> &HashSet<ObjectID> {
    &self.active_proposals
  }

  pub(crate) fn proposals_bag_id(&self) -> ObjectID {
    *self.proposals.id.object_id()
  }

  /// Returns the voting power for controller with ID `controller_cap_id`, if any.
  pub fn controller_voting_power(&self, controller_cap_id: ObjectID) -> Option<u64> {
    self.controllers.get(&controller_cap_id).copied()
  }

  /// Consumes this [`Multicontroller`], returning the wrapped value.
  pub fn into_inner(self) -> T {
    self.controlled_value
  }

  pub(crate) fn controllers(&self) -> &HashMap<ObjectID, u64> {
    &self.controllers
  }

  /// Returns `true` if `cap_id` is among this [`Multicontroller`]'s controllers' IDs.
  pub fn has_member(&self, cap_id: ObjectID) -> bool {
    self.controllers.contains_key(&cap_id)
  }
}

impl<T> TryFrom<IotaMulticontroller<T>> for Multicontroller<T> {
  type Error = <u64 as TryFrom<Number<u64>>>::Error;
  fn try_from(value: IotaMulticontroller<T>) -> Result<Self, Self::Error> {
    let IotaMulticontroller {
      controlled_value,
      controllers,
      threshold,
      active_proposals,
      proposals,
    } = value;
    let controllers = controllers
      .contents
      .into_iter()
      .map(|Entry { key: id, value: vp }| (u64::try_from(vp).map(|vp| (id, vp))))
      .collect::<Result<_, _>>()?;

    Ok(Multicontroller {
      controlled_value,
      controllers,
      threshold: threshold.try_into()?,
      active_proposals,
      proposals,
    })
  }
}

#[derive(Debug, Serialize, Deserialize)]
struct IotaMulticontroller<T> {
  controlled_value: T,
  controllers: VecMap<ObjectID, Number<u64>>,
  threshold: Number<u64>,
  active_proposals: HashSet<ObjectID>,
  proposals: Bag,
}