-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathdelete.rs
More file actions
303 lines (265 loc) · 10.7 KB
/
delete.rs
File metadata and controls
303 lines (265 loc) · 10.7 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
use crate::{
error::DoubleZeroError,
pda::get_accesspass_pda,
serializer::{try_acc_close, try_acc_write},
state::{
accesspass::{AccessPass, AccessPassStatus},
device::Device,
globalstate::GlobalState,
tenant::Tenant,
user::*,
},
};
use borsh::BorshSerialize;
use borsh_incremental::BorshDeserializeIncremental;
use core::fmt;
use solana_program::{
account_info::{next_account_info, AccountInfo},
entrypoint::ProgramResult,
msg,
program_error::ProgramError,
pubkey::Pubkey,
};
use std::net::Ipv4Addr;
use super::resource_onchain_helpers;
#[derive(BorshSerialize, BorshDeserializeIncremental, PartialEq, Clone, Default)]
pub struct UserDeleteArgs {
/// Number of DzPrefixBlock accounts passed for on-chain deallocation.
/// When 0, legacy behavior (Deleting status). When > 0, atomic delete+deallocate+close.
#[incremental(default = 0)]
pub dz_prefix_count: u8,
/// Whether MulticastPublisherBlock account is passed (1 = yes, 0 = no).
#[incremental(default = 0)]
pub multicast_publisher_count: u8,
}
impl fmt::Debug for UserDeleteArgs {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"dz_prefix_count: {}, multicast_publisher_count: {}",
self.dz_prefix_count, self.multicast_publisher_count
)
}
}
pub fn process_delete_user(
program_id: &Pubkey,
accounts: &[AccountInfo],
value: &UserDeleteArgs,
) -> ProgramResult {
let accounts_iter = &mut accounts.iter();
let user_account = next_account_info(accounts_iter)?;
let accesspass_account = next_account_info(accounts_iter)?;
let globalstate_account = next_account_info(accounts_iter)?;
// Optional: additional accounts for atomic deallocation (between globalstate and payer)
// Account layout WITH deallocation (dz_prefix_count > 0):
// [user, accesspass, globalstate, device, user_tunnel_block, multicast_publisher_block?, device_tunnel_ids, dz_prefix_0..N, optional_tenant, owner, payer, system]
// Account layout WITHOUT (legacy, dz_prefix_count == 0):
// [user, accesspass, globalstate, payer, system]
let deallocation_accounts = if value.dz_prefix_count > 0 {
let device_account = next_account_info(accounts_iter)?;
let user_tunnel_block_ext = next_account_info(accounts_iter)?;
let multicast_publisher_block_ext = if value.multicast_publisher_count > 0 {
Some(next_account_info(accounts_iter)?)
} else {
None
};
let device_tunnel_ids_ext = next_account_info(accounts_iter)?;
let mut dz_prefix_accounts = Vec::with_capacity(value.dz_prefix_count as usize);
for _ in 0..value.dz_prefix_count {
dz_prefix_accounts.push(next_account_info(accounts_iter)?);
}
Some((
device_account,
user_tunnel_block_ext,
multicast_publisher_block_ext,
device_tunnel_ids_ext,
dz_prefix_accounts,
))
} else {
None
};
// For atomic path: check if user has a tenant (we'll peek at user data)
// For legacy path: no tenant handling needed
let tenant_account = if value.dz_prefix_count > 0 {
// Peek at user to check tenant
let user_peek = User::try_from(user_account)?;
if user_peek.tenant_pk != Pubkey::default() {
Some(next_account_info(accounts_iter)?)
} else {
None
}
} else {
None
};
// For atomic path, owner account is needed for close
let owner_account = if value.dz_prefix_count > 0 {
Some(next_account_info(accounts_iter)?)
} else {
None
};
let payer_account = next_account_info(accounts_iter)?;
let system_program = next_account_info(accounts_iter)?;
#[cfg(test)]
msg!("process_delete_user({:?})", value);
// Check if the payer is a signer
assert!(payer_account.is_signer, "Payer must be a signer");
// Check the owner of the accounts
assert_eq!(user_account.owner, program_id, "Invalid PDA Account Owner");
if accesspass_account.data_is_empty() {
return Err(DoubleZeroError::AccessPassNotFound.into());
}
assert_eq!(
globalstate_account.owner, program_id,
"Invalid GlobalState Account Owner"
);
assert_eq!(
accesspass_account.owner, program_id,
"Invalid AccessPass Account Owner"
);
assert_eq!(
*system_program.unsigned_key(),
solana_system_interface::program::ID,
"Invalid System Program Account Owner"
);
// Check if the account is writable
assert!(user_account.is_writable, "PDA Account is not writable");
let user: User = User::try_from(user_account)?;
let globalstate = GlobalState::try_from(globalstate_account)?;
// Allow delete if payer is: the user owner, the access pass user_payer,
// or on the foundation allowlist.
let accesspass_user_payer = if !accesspass_account.data_is_empty() {
AccessPass::try_from(accesspass_account)
.map(|ap| ap.user_payer)
.ok()
} else {
None
};
let is_authorized = globalstate.foundation_allowlist.contains(payer_account.key)
|| user.owner == *payer_account.key
|| accesspass_user_payer == Some(*payer_account.key);
if !is_authorized {
return Err(DoubleZeroError::NotAllowed.into());
}
// Accept access pass from either user.owner or payer
let (owner_pda, _) = get_accesspass_pda(program_id, &user.client_ip, &user.owner);
let (owner_dynamic_pda, _) =
get_accesspass_pda(program_id, &Ipv4Addr::UNSPECIFIED, &user.owner);
let (payer_pda, _) = get_accesspass_pda(program_id, &user.client_ip, payer_account.key);
let (payer_dynamic_pda, _) =
get_accesspass_pda(program_id, &Ipv4Addr::UNSPECIFIED, payer_account.key);
assert!(
accesspass_account.key == &owner_pda
|| accesspass_account.key == &owner_dynamic_pda
|| accesspass_account.key == &payer_pda
|| accesspass_account.key == &payer_dynamic_pda,
"Invalid AccessPass PDA",
);
if !accesspass_account.data_is_empty() {
// Read Access Pass
let mut accesspass = AccessPass::try_from(accesspass_account)?;
if accesspass.is_dynamic() && accesspass.client_ip == Ipv4Addr::UNSPECIFIED {
accesspass.client_ip = user.client_ip; // lock to the first used IP
}
if accesspass.client_ip != user.client_ip && !accesspass.allow_multiple_ip() {
msg!(
"Invalid client_ip accesspass.{{client_ip: {}}} = {{ client_ip: {} }}",
accesspass.client_ip,
user.client_ip
);
return Err(DoubleZeroError::Unauthorized.into());
}
accesspass.connection_count = accesspass.connection_count.saturating_sub(1);
accesspass.status = if accesspass.connection_count > 0 {
AccessPassStatus::Connected
} else {
AccessPassStatus::Disconnected
};
if accesspass.connection_count == 0 && accesspass.allow_multiple_ip() {
accesspass.client_ip = Ipv4Addr::UNSPECIFIED; // reset to allow multiple IPs
}
try_acc_write(&accesspass, accesspass_account, payer_account, accounts)?;
}
if value.dz_prefix_count == 0 {
// legacy activator path: no behavior change
if matches!(user.status, UserStatus::Deleting | UserStatus::Updating) {
return Err(DoubleZeroError::InvalidStatus.into());
}
}
if !user.publishers.is_empty() || !user.subscribers.is_empty() {
msg!("{:?}", user);
return Err(DoubleZeroError::ReferenceCountNotZero.into());
}
if let Some((
device_account,
user_tunnel_block_ext,
multicast_publisher_block_ext,
device_tunnel_ids_ext,
dz_prefix_accounts,
)) = deallocation_accounts
{
let owner_account = owner_account.unwrap();
// Validate additional accounts
assert_eq!(
device_account.owner, program_id,
"Invalid Device Account Owner"
);
if user.device_pk != *device_account.key {
return Err(ProgramError::InvalidAccountData);
}
if user.owner != *owner_account.key {
return Err(ProgramError::InvalidAccountData);
}
// Deallocate resources via helper (checks feature flag, validates PDAs)
resource_onchain_helpers::validate_and_deallocate_user_resources(
program_id,
&user,
user_tunnel_block_ext,
multicast_publisher_block_ext.as_ref().map(|a| *a),
device_tunnel_ids_ext,
&dz_prefix_accounts,
&globalstate,
)?;
// Decrement tenant reference count if user has tenant assigned
if let Some(tenant_acc) = tenant_account {
assert_eq!(
tenant_acc.key, &user.tenant_pk,
"Tenant account doesn't match user's tenant"
);
assert_eq!(tenant_acc.owner, program_id, "Invalid Tenant Account Owner");
assert!(tenant_acc.is_writable, "Tenant Account is not writable");
let mut tenant = Tenant::try_from(tenant_acc)?;
tenant.reference_count = tenant.reference_count.saturating_sub(1);
try_acc_write(&tenant, tenant_acc, payer_account, accounts)?;
}
// Decrement device counters
let mut device = Device::try_from(device_account)?;
device.reference_count = device.reference_count.saturating_sub(1);
device.users_count = device.users_count.saturating_sub(1);
match user.user_type {
UserType::Multicast => {
if !user.publishers.is_empty() {
device.multicast_publishers_count =
device.multicast_publishers_count.saturating_sub(1);
} else {
device.multicast_subscribers_count =
device.multicast_subscribers_count.saturating_sub(1);
}
}
_ => {
device.unicast_users_count = device.unicast_users_count.saturating_sub(1);
}
}
try_acc_write(&device, device_account, payer_account, accounts)?;
try_acc_close(user_account, owner_account)?;
#[cfg(test)]
msg!("DeleteUser (atomic): User deallocated and closed");
} else {
// Legacy path: just mark as Deleting
let mut user: User = User::try_from(user_account)?;
user.status = UserStatus::Deleting;
try_acc_write(&user, user_account, payer_account, accounts)?;
#[cfg(test)]
msg!("Deleting: {:?}", user);
}
Ok(())
}