-
Notifications
You must be signed in to change notification settings - Fork 155
Expand file tree
/
Copy pathproxy.rs
More file actions
160 lines (134 loc) · 5.09 KB
/
Copy pathproxy.rs
File metadata and controls
160 lines (134 loc) · 5.09 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
// This file is part of Substrate.
// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use crate::*;
use pallet_rc_migrator::types::ToPolkadotSs58;
use sp_runtime::{traits::Zero, BoundedSlice};
impl<T: Config> Pallet<T> {
pub fn do_receive_proxies(proxies: Vec<RcProxyOf<T, T::RcProxyType>>) -> Result<(), Error<T>> {
Self::deposit_event(Event::BatchReceived {
pallet: PalletEventName::ProxyProxies,
count: proxies.len() as u32,
});
let (mut count_good, mut count_bad) = (0, 0);
log::info!(target: LOG_TARGET, "Integrating batch proxies of with len {}", proxies.len());
for proxy in proxies {
match Self::do_receive_proxy(proxy) {
Ok(()) => count_good += 1,
Err(e) => {
count_bad += 1;
log::error!(target: LOG_TARGET, "Error while integrating proxy: {:?}", e);
},
}
}
Self::deposit_event(Event::BatchProcessed {
pallet: PalletEventName::ProxyProxies,
count_good,
count_bad,
});
Ok(())
}
/// Receive a single proxy and write it to storage.
pub fn do_receive_proxy(proxy: RcProxyOf<T, T::RcProxyType>) -> Result<(), Error<T>> {
log::debug!(target: LOG_TARGET, "Integrating proxy {}, deposit {:?}", proxy.delegator.to_polkadot_ss58(), proxy.deposit);
let max_proxies = <T as pallet_proxy::Config>::MaxProxies::get() as usize;
if proxy.proxies.len() > max_proxies {
log::error!(target: LOG_TARGET, "Truncating proxy list of {}", proxy.delegator.to_ss58check());
}
let proxies = proxy.proxies.into_iter().enumerate().filter_map(|(i, p)| {
let Ok(proxy_type) = T::RcToProxyType::try_convert(p.proxy_type) else {
// This is fine, eg. `Auction` proxy is not supported on AH
log::warn!(target: LOG_TARGET, "Dropping unsupported proxy at index {} for {}", i, proxy.delegator.to_polkadot_ss58());
// TODO unreserve deposit
return None;
};
let delay = T::RcToAhDelay::convert(p.delay);
log::debug!(target: LOG_TARGET, "Proxy type: {:?} delegate: {:?}", proxy_type, p.delegate.to_polkadot_ss58());
Some(pallet_proxy::ProxyDefinition {
delegate: p.delegate,
delay,
proxy_type,
})
})
.take(max_proxies)
.collect::<Vec<_>>();
let Ok(bounded_proxies) =
BoundedSlice::try_from(proxies.as_slice()).defensive_proof("unreachable")
else {
return Err(Error::TODO);
};
// The deposit was already taken by the account migration
// Add the proxies
pallet_proxy::Proxies::<T>::insert(&proxy.delegator, (bounded_proxies, proxy.deposit));
Ok(())
}
pub fn do_receive_proxy_announcements(
announcements: Vec<RcProxyAnnouncementOf<T>>,
) -> Result<(), Error<T>> {
Self::deposit_event(Event::BatchReceived {
pallet: PalletEventName::ProxyAnnouncements,
count: announcements.len() as u32,
});
let (mut count_good, mut count_bad) = (0, 0);
log::info!(target: LOG_TARGET, "Unreserving deposits for {} proxy announcements", announcements.len());
for announcement in announcements {
match Self::do_receive_proxy_announcement(announcement) {
Ok(()) => count_good += 1,
Err(e) => {
count_bad += 1;
log::error!(target: LOG_TARGET, "Error while integrating proxy announcement: {:?}", e);
},
}
}
Self::deposit_event(Event::BatchProcessed {
pallet: PalletEventName::ProxyAnnouncements,
count_good,
count_bad,
});
Ok(())
}
pub fn do_receive_proxy_announcement(
announcement: RcProxyAnnouncementOf<T>,
) -> Result<(), Error<T>> {
let before = frame_system::Account::<T>::get(&announcement.depositor);
let missing = <T as pallet_proxy::Config>::Currency::unreserve(
&announcement.depositor,
announcement.deposit,
);
if !missing.is_zero() {
log::warn!(target: LOG_TARGET, "Could not unreserve full proxy announcement deposit for {}, missing {:?}, before {:?}", announcement.depositor.to_ss58check(), missing, before);
}
Ok(())
}
}
#[cfg(feature = "std")]
impl<T: Config> crate::types::AhMigrationCheck for ProxyProxiesMigrator<T> {
type RcPrePayload = usize; // number of delegators
type AhPrePayload = (); // number of proxies
fn pre_check(_: Self::RcPrePayload) -> Self::AhPrePayload {
()
}
fn post_check(rc_pre_payload: Self::RcPrePayload, _: Self::AhPrePayload) {
let count = pallet_proxy::Proxies::<T>::iter_keys().count();
log::info!(target: LOG_TARGET, "Total number of proxies: {}", count);
// TODO: This is not necessarily correct, since some proxy types are not migrated.
// Assert storage "Proxy::Proxies::ah_post::length"
if count < rc_pre_payload {
panic!(
"Some proxies were not migrated. Expected at least {} proxies, got {}",
rc_pre_payload, count
);
}
}
}