-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapplication.rs
More file actions
225 lines (192 loc) · 7.48 KB
/
application.rs
File metadata and controls
225 lines (192 loc) · 7.48 KB
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
use codec::{Decode, Encode, MaxEncodedLen};
use polkadot_sdk::{
frame_election_provider_support::Get,
frame_support::{DebugNoBound, dispatch::DispatchResult, traits::Currency},
polkadot_sdk_frame::prelude::BlockNumberFor,
sp_runtime::{BoundedVec, traits::Saturating},
sp_std::vec::Vec,
};
use scale_info::TypeInfo;
use crate::{
AccountIdOf, AgentApplications, BalanceOf, frame::traits::ExistenceRequirement, whitelist,
};
/// Decentralized autonomous organization application, it's used to do agent
/// operations on the network, like creating or removing, and needs to be
/// approved by other peers.
#[derive(DebugNoBound, TypeInfo, Decode, Encode, MaxEncodedLen)]
#[scale_info(skip_type_params(T))]
pub struct AgentApplication<T: crate::Config> {
pub id: u32,
pub payer_key: AccountIdOf<T>,
pub agent_key: AccountIdOf<T>,
pub data: BoundedVec<u8, T::MaxApplicationDataLength>,
pub cost: BalanceOf<T>,
pub expires_at: BlockNumberFor<T>,
pub action: ApplicationAction,
pub status: ApplicationStatus,
}
/// Possible operations are adding or removing applications.
#[derive(DebugNoBound, Decode, Encode, TypeInfo, MaxEncodedLen, PartialEq, Eq)]
pub enum ApplicationAction {
Add,
Remove,
}
#[derive(DebugNoBound, Decode, Encode, TypeInfo, MaxEncodedLen, PartialEq, Eq)]
pub enum ApplicationStatus {
Open,
/// The application was processed before expiration, can be either accepted
/// or rejected.
Resolved {
accepted: bool,
},
Expired,
}
impl<T: crate::Config> AgentApplication<T> {
/// Returns true if the application is in the Open state, i.e. not Expired
/// or Resolved, meaning it can be acted upon.
pub fn is_open(&self) -> bool {
matches!(self.status, ApplicationStatus::Open)
}
}
/// Creates a new agent application if the key is not yet whitelisted. It
/// withdraws a fee from the payer account, which is given back if the
/// application is accepted. The fee avoids actors spamming applications.
pub fn submit_application<T: crate::Config>(
payer: AccountIdOf<T>,
agent_key: AccountIdOf<T>,
data: Vec<u8>,
removing: bool,
) -> DispatchResult {
if !removing && whitelist::is_whitelisted::<T>(&agent_key) {
return Err(crate::Error::<T>::AlreadyWhitelisted.into());
} else if removing && !whitelist::is_whitelisted::<T>(&agent_key) {
return Err(crate::Error::<T>::NotWhitelisted.into());
}
let action = if removing {
ApplicationAction::Remove
} else {
ApplicationAction::Add
};
if exists_for_agent_key::<T>(&agent_key, &action) {
return Err(crate::Error::<T>::ApplicationKeyAlreadyUsed.into());
}
let config = crate::GlobalGovernanceConfig::<T>::get();
let cost = config.agent_application_cost;
<T as crate::Config>::Currency::transfer(
&payer,
&crate::DaoTreasuryAddress::<T>::get(),
cost,
ExistenceRequirement::AllowDeath,
)
.map_err(|_| crate::Error::<T>::NotEnoughBalanceToApply)?;
let data_len: u32 = data
.len()
.try_into()
.map_err(|_| crate::Error::<T>::InvalidApplicationDataLength)?;
let data_range = T::MinApplicationDataLength::get()..T::MaxApplicationDataLength::get();
if !data_range.contains(&data_len) {
return Err(crate::Error::<T>::InvalidApplicationDataLength.into());
}
let expires_at = <polkadot_sdk::frame_system::Pallet<T>>::block_number()
.saturating_add(config.agent_application_expiration);
let next_id = AgentApplications::<T>::iter()
.count()
.try_into()
.map_err(|_| crate::Error::<T>::InternalError)?;
let application = AgentApplication::<T> {
id: next_id,
payer_key: payer,
agent_key,
data: BoundedVec::truncate_from(data),
cost,
expires_at,
status: ApplicationStatus::Open,
action,
};
crate::AgentApplications::<T>::insert(next_id, application);
crate::Pallet::<T>::deposit_event(crate::Event::<T>::ApplicationCreated(next_id));
Ok(())
}
/// Accepts an agent application and executes it if it's still open, fails
/// otherwise.
pub fn accept_application<T: crate::Config>(application_id: u32) -> DispatchResult {
let application = crate::AgentApplications::<T>::get(application_id)
.ok_or(crate::Error::<T>::ApplicationNotFound)?;
if !application.is_open() {
return Err(crate::Error::<T>::ApplicationNotOpen.into());
}
match application.action {
ApplicationAction::Add => {
crate::Whitelist::<T>::insert(application.agent_key.clone(), ());
crate::Pallet::<T>::deposit_event(crate::Event::<T>::WhitelistAdded(
application.agent_key,
));
}
ApplicationAction::Remove => {
crate::Whitelist::<T>::remove(&application.agent_key);
crate::Pallet::<T>::deposit_event(crate::Event::<T>::WhitelistRemoved(
application.agent_key,
));
}
}
crate::AgentApplications::<T>::mutate(application_id, |application| {
if let Some(app) = application {
app.status = ApplicationStatus::Resolved { accepted: true };
}
});
// Give the application fee back to the payer key.
let _ = <T as crate::Config>::Currency::transfer(
&crate::DaoTreasuryAddress::<T>::get(),
&application.payer_key,
application.cost,
ExistenceRequirement::AllowDeath,
);
crate::Pallet::<T>::deposit_event(crate::Event::<T>::ApplicationAccepted(application.id));
Ok(())
}
/// Rejects an open application.
pub fn deny_application<T: crate::Config>(application_id: u32) -> DispatchResult {
let application = crate::AgentApplications::<T>::get(application_id)
.ok_or(crate::Error::<T>::ApplicationNotFound)?;
if !application.is_open() {
return Err(crate::Error::<T>::ApplicationNotOpen.into());
}
crate::AgentApplications::<T>::mutate(application_id, |application| {
if let Some(app) = application {
app.status = ApplicationStatus::Resolved { accepted: false };
}
});
crate::Pallet::<T>::deposit_event(crate::Event::<T>::ApplicationDenied(application.id));
Ok(())
}
/// Iterates through all open applications checking if the current block is
/// greater or equal to the former's expiration block. If so, marks the
/// application as Expired.
pub(crate) fn resolve_expired_applications<T: crate::Config>(current_block: BlockNumberFor<T>) {
for application in crate::AgentApplications::<T>::iter_values() {
if current_block < application.expires_at
|| !matches!(application.status, ApplicationStatus::Open)
{
continue;
}
crate::AgentApplications::<T>::mutate(application.id, |application| {
if let Some(app) = application {
if matches!(app.status, ApplicationStatus::Open) {
app.status = ApplicationStatus::Expired;
}
}
});
crate::Pallet::<T>::deposit_event(crate::Event::<T>::ApplicationExpired(application.id));
}
}
/// If any applications for this agent and action are already pending.
pub(crate) fn exists_for_agent_key<T: crate::Config>(
key: &AccountIdOf<T>,
action: &ApplicationAction,
) -> bool {
crate::AgentApplications::<T>::iter().any(|(_, application)| {
application.agent_key == *key
&& application.status == ApplicationStatus::Open
&& application.action == *action
})
}