-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathadmin.rs
More file actions
314 lines (278 loc) · 9.22 KB
/
Copy pathadmin.rs
File metadata and controls
314 lines (278 loc) · 9.22 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
use soroban_sdk::{panic_with_error, Address, BytesN, Env, String};
use crate::errors::Error;
use crate::events as evt;
use crate::storage;
use crate::types::{PendingAdmin, PendingUpgrade};
const PENDING_ADMIN_TTL_LEDGERS: u32 = 120_960;
pub(crate) const MAX_FEE_BPS: u32 = 1_000;
#[cfg(not(feature = "testnet"))]
const UPGRADE_TIMELOCK_LEDGERS: u32 = 17_280;
#[cfg(feature = "testnet")]
const UPGRADE_TIMELOCK_LEDGERS: u32 = 0;
const PENDING_UPGRADE_TTL_LEDGERS: u32 = 518_400;
pub const INITIAL_VERSION: &str = "1.1.0";
// ============================================================
// INITIALIZATION
// ============================================================
pub fn initialize(
env: &Env,
admin: Address,
fee_account: Address,
fee_bps: u32,
profile_contract: Address,
) {
if env.storage().instance().has(&crate::types::DataKey::Admin) {
panic_with_error!(env, Error::AlreadyInitialized);
}
if fee_bps > MAX_FEE_BPS {
panic_with_error!(env, Error::InvalidFeeBps);
}
storage::set_admin(env, &admin);
storage::set_fee_account(env, &fee_account);
storage::set_fee_bps(env, fee_bps);
storage::set_profile_contract(env, &profile_contract);
storage::set_deployment_seq(env, env.ledger().sequence());
storage::set_paused(env, false);
storage::set_version(env, &String::from_str(env, INITIAL_VERSION));
storage::touch_instance(env);
evt::AdminUpdated {
new_admin: admin.clone(),
}
.publish(env);
evt::FeeAccountUpdated {
new_account: fee_account,
}
.publish(env);
evt::FeeBpsUpdated { new_bps: fee_bps }.publish(env);
evt::ProfileContractUpdated {
new_addr: profile_contract,
}
.publish(env);
}
// ============================================================
// ADMIN ROTATION (two-step)
// ============================================================
pub fn set_admin(env: &Env, new_admin: Address) -> Result<(), Error> {
require_admin(env)?;
let expires_at = env
.ledger()
.sequence()
.saturating_add(PENDING_ADMIN_TTL_LEDGERS);
let pending = PendingAdmin {
target: new_admin.clone(),
expires_at_ledger: expires_at,
};
storage::set_pending_admin(env, &pending);
storage::touch_instance(env);
evt::PendingAdminSet { target: new_admin }.publish(env);
Ok(())
}
pub fn accept_admin(env: &Env) -> Result<(), Error> {
let pending = storage::get_pending_admin(env).ok_or(Error::PendingAdminMismatch)?;
if env.ledger().sequence() > pending.expires_at_ledger {
storage::clear_pending_admin(env);
storage::touch_instance(env);
return Err(Error::PendingAdminExpired);
}
pending.target.require_auth();
storage::set_admin(env, &pending.target);
storage::clear_pending_admin(env);
storage::touch_instance(env);
evt::AdminUpdated {
new_admin: pending.target,
}
.publish(env);
Ok(())
}
// ============================================================
// FEE CONFIG
// ============================================================
pub fn set_fee_bps(env: &Env, new_bps: u32) -> Result<(), Error> {
require_admin(env)?;
if new_bps > MAX_FEE_BPS {
return Err(Error::InvalidFeeBps);
}
storage::set_fee_bps(env, new_bps);
storage::touch_instance(env);
evt::FeeBpsUpdated { new_bps }.publish(env);
Ok(())
}
pub fn set_fee_account(env: &Env, new_account: Address) -> Result<(), Error> {
require_admin(env)?;
storage::set_fee_account(env, &new_account);
storage::touch_instance(env);
evt::FeeAccountUpdated {
new_account: new_account.clone(),
}
.publish(env);
Ok(())
}
// ============================================================
// PROFILE CONTRACT BINDING
// ============================================================
pub fn set_profile_contract(env: &Env, new_addr: Address) -> Result<(), Error> {
require_admin(env)?;
storage::set_profile_contract(env, &new_addr);
storage::touch_instance(env);
evt::ProfileContractUpdated {
new_addr: new_addr.clone(),
}
.publish(env);
Ok(())
}
// ============================================================
// PAUSE
// ============================================================
pub fn pause(env: &Env) -> Result<(), Error> {
require_admin(env)?;
storage::set_paused(env, true);
storage::touch_instance(env);
evt::Paused {}.publish(env);
Ok(())
}
pub fn unpause(env: &Env) -> Result<(), Error> {
require_admin(env)?;
storage::set_paused(env, false);
storage::touch_instance(env);
evt::Unpaused {}.publish(env);
Ok(())
}
// ============================================================
// UPGRADE (timelocked; H6)
// ============================================================
pub fn propose_upgrade(
env: &Env,
new_wasm_hash: BytesN<32>,
new_version: String,
) -> Result<(), Error> {
require_admin(env)?;
if new_version.is_empty() {
return Err(Error::InvalidPillar);
}
let now = env.ledger().sequence();
let available_at = now.saturating_add(UPGRADE_TIMELOCK_LEDGERS);
let expires_at = now.saturating_add(PENDING_UPGRADE_TTL_LEDGERS);
let pending = PendingUpgrade {
wasm_hash: new_wasm_hash.clone(),
new_version: new_version.clone(),
proposed_at_ledger: now,
available_at_ledger: available_at,
expires_at_ledger: expires_at,
};
storage::set_pending_upgrade(env, &pending);
storage::touch_instance(env);
evt::PendingUpgradeProposed {
wasm_hash: new_wasm_hash,
new_version,
available_at_ledger: available_at,
expires_at_ledger: expires_at,
}
.publish(env);
Ok(())
}
pub fn apply_upgrade(env: &Env) -> Result<(), Error> {
require_admin(env)?;
let pending = storage::get_pending_upgrade(env).ok_or(Error::UpgradeNotProposed)?;
let now = env.ledger().sequence();
if now > pending.expires_at_ledger {
return Err(Error::UpgradeProposalExpired);
}
if now < pending.available_at_ledger {
return Err(Error::UpgradeTimelockNotElapsed);
}
storage::touch_instance(env);
env.deployer()
.update_current_contract_wasm(pending.wasm_hash.clone());
storage::set_version(env, &pending.new_version);
storage::clear_pending_upgrade(env);
evt::UpgradeApplied {
wasm_hash: pending.wasm_hash.clone(),
new_version: pending.new_version.clone(),
}
.publish(env);
evt::Upgraded {
new_wasm_hash: pending.wasm_hash,
}
.publish(env);
Ok(())
}
pub fn cancel_pending_upgrade(env: &Env) -> Result<(), Error> {
require_admin(env)?;
if storage::get_pending_upgrade(env).is_none() {
return Err(Error::UpgradeNotProposed);
}
storage::clear_pending_upgrade(env);
storage::touch_instance(env);
evt::PendingUpgradeCancelled {
cancelled_at_ledger: env.ledger().sequence(),
}
.publish(env);
Ok(())
}
// ============================================================
// MIGRATE (post-upgrade one-shot; H6)
// ============================================================
pub fn migrate(env: &Env) -> Result<(), Error> {
require_admin(env)?;
let current = storage::get_version(env).ok_or(Error::NotInitialized)?;
let already = storage::get_migrated_to_version(env);
if let Some(m) = already.clone() {
if m == current {
return Err(Error::MigrationAlreadyApplied);
}
}
let from_version = already.unwrap_or_else(|| String::from_str(env, "0.0.0"));
// ============================================================
// PER-(from -> to) MIGRATION DISPATCH
// ============================================================
storage::set_migrated_to_version(env, ¤t);
storage::touch_instance(env);
evt::Migrated {
from_version,
to_version: current,
}
.publish(env);
Ok(())
}
// ============================================================
// READS
// ============================================================
pub fn get_admin(env: &Env) -> Address {
storage::get_admin(env).unwrap_or_else(|_| panic_with_error!(env, Error::NotInitialized))
}
pub fn get_fee_bps(env: &Env) -> u32 {
storage::get_fee_bps(env)
}
pub fn get_fee_account(env: &Env) -> Address {
storage::get_fee_account(env)
}
pub fn get_profile_contract(env: &Env) -> Address {
storage::get_profile_contract(env)
}
pub fn is_paused(env: &Env) -> bool {
storage::is_paused(env)
}
pub fn get_version(env: &Env) -> String {
storage::get_version(env).unwrap_or_else(|| panic_with_error!(env, Error::NotInitialized))
}
pub fn get_pending_upgrade(env: &Env) -> Option<PendingUpgrade> {
storage::get_pending_upgrade(env)
}
pub fn get_migrated_to_version(env: &Env) -> Option<String> {
storage::get_migrated_to_version(env)
}
// ============================================================
// AUTH GUARDS
// ============================================================
pub fn require_admin(env: &Env) -> Result<(), Error> {
let admin = storage::get_admin(env)?;
admin.require_auth();
Ok(())
}
pub fn require_not_paused(env: &Env) -> Result<(), Error> {
storage::touch_instance(env);
if storage::is_paused(env) {
return Err(Error::Paused);
}
Ok(())
}