1#![cfg_attr(not(feature = "std"), no_std)]
17
18extern crate alloc;
19
20use alloc::string::String;
21use core::ops::{Deref, DerefMut};
22use core::sync::atomic::{AtomicU8, Ordering};
23use core::cell::UnsafeCell;
24use core::marker::PhantomData;
25use core::fmt;
26
27#[derive(Debug, PartialEq, Eq, Clone, Copy)]
31#[repr(u8)]
32pub enum SovereignState {
33 Domestic = 0,
34 Exiled = 1,
35}
36
37pub struct Sovereign<T> {
41 inner: UnsafeCell<T>,
42 state: AtomicU8,
43}
44
45#[derive(Debug, Clone, PartialEq, Eq)]
47pub enum SovereigntyError {
48 ForeignJurisdiction,
50}
51
52impl fmt::Display for SovereigntyError {
53 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54 match self {
55 SovereigntyError::ForeignJurisdiction => write!(f, "SOVEREIGNTY VIOLATION: Resource is under foreign jurisdiction."),
56 }
57 }
58}
59
60#[cfg(feature = "std")]
61impl std::error::Error for SovereigntyError {}
62
63impl<T> Sovereign<T> {
64 pub fn new(value: T) -> Self {
66 Self {
67 inner: UnsafeCell::new(value),
68 state: AtomicU8::new(SovereignState::Domestic as u8),
69 }
70 }
71
72 #[must_use]
77 pub fn annex(&self) -> Result<(), AnnexError> {
78 let current = self.state.load(Ordering::SeqCst);
79 if current == SovereignState::Exiled as u8 {
80 return Err(AnnexError::AlreadyExiled);
81 }
82
83 self.state.store(SovereignState::Exiled as u8, Ordering::SeqCst);
85 Ok(())
86 }
87
88 pub fn inner_ref(&self) -> &T {
94 unsafe { &*self.inner.get() }
97 }
98
99 pub fn state(&self) -> SovereignState {
101 match self.state.load(Ordering::SeqCst) {
102 0 => SovereignState::Domestic,
103 _ => SovereignState::Exiled,
104 }
105 }
106
107 pub fn try_get(&self) -> Result<&T, SovereigntyError> {
109 if self.state.load(Ordering::SeqCst) == SovereignState::Exiled as u8 {
110 return Err(SovereigntyError::ForeignJurisdiction);
111 }
112 unsafe { Ok(&*self.inner.get()) }
114 }
115
116 pub fn try_get_mut(&mut self) -> Result<&mut T, SovereigntyError> {
118 if self.state.load(Ordering::SeqCst) == SovereignState::Exiled as u8 {
119 return Err(SovereigntyError::ForeignJurisdiction);
120 }
121 unsafe { Ok(&mut *self.inner.get()) }
123 }
124
125 fn verify_jurisdiction(&self) {
127 if self.state.load(Ordering::SeqCst) == SovereignState::Exiled as u8 {
128 panic!("SOVEREIGNTY VIOLATION: Resource is under foreign jurisdiction.");
129 }
130 }
131}
132
133impl<T> Deref for Sovereign<T> {
134 type Target = T;
135
136 fn deref(&self) -> &Self::Target {
137 self.verify_jurisdiction();
138 unsafe { &*self.inner.get() }
140 }
141}
142
143impl<T> DerefMut for Sovereign<T> {
144 fn deref_mut(&mut self) -> &mut Self::Target {
145 self.verify_jurisdiction();
146 unsafe { &mut *self.inner.get() }
148 }
149}
150
151unsafe impl<T: Send> Send for Sovereign<T> {}
154unsafe impl<T: Sync> Sync for Sovereign<T> {}
155
156pub trait CheckProtocol {
158 fn enforce_law(&self);
160}
161
162#[derive(Debug)]
172pub struct ProofCarrying<T> {
173 pub value: T,
175 _proof: PhantomData<()>,
177}
178
179impl<T> ProofCarrying<T> {
180 #[doc(hidden)]
184 pub fn new_unchecked(value: T) -> Self {
185 Self {
186 value,
187 _proof: PhantomData,
188 }
189 }
190
191 pub fn into_inner(self) -> T {
193 self.value
194 }
195}
196
197impl<T: Clone> Clone for ProofCarrying<T> {
198 fn clone(&self) -> Self {
199 Self {
200 value: self.value.clone(),
201 _proof: PhantomData,
202 }
203 }
204}
205
206#[derive(Debug, Clone, PartialEq, Eq)]
208pub enum AnnexError {
209 AlreadyExiled,
211 VerificationFailed(String),
213 ProverError(String),
215}
216
217impl fmt::Display for AnnexError {
218 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
219 match self {
220 AnnexError::AlreadyExiled => write!(f, "Resource is already under foreign jurisdiction"),
221 AnnexError::VerificationFailed(msg) => write!(f, "Verification failed: {}", msg),
222 AnnexError::ProverError(msg) => write!(f, "Prover error: {}", msg),
223 }
224 }
225}
226
227#[cfg(feature = "std")]
228impl std::error::Error for AnnexError {}
229
230#[derive(Debug, Clone, PartialEq, Eq)]
232pub enum LeaseError {
233 AlreadyLeased,
235 ForeignJurisdiction,
237}
238
239pub struct Lease<T> {
241 pub holder: u128,
243 pub duration: core::time::Duration,
245 _phantom: PhantomData<T>,
247}
248
249impl<T> Lease<T> {
250 pub fn new(holder: u128, duration: core::time::Duration) -> Self {
252 Self {
253 holder,
254 duration,
255 _phantom: PhantomData,
256 }
257 }
258
259 pub fn duration(&self) -> core::time::Duration {
261 self.duration
262 }
263}
264
265pub trait DistributedBorrow<T> {
267 #[must_use]
269 fn try_hire(&self, candidate_id: u128, term: core::time::Duration) -> Result<Lease<T>, LeaseError>;
270}
271
272impl<T> DistributedBorrow<T> for Sovereign<T> {
273 fn try_hire(&self, candidate_id: u128, term: core::time::Duration) -> Result<Lease<T>, LeaseError> {
274 let current = self.state.load(Ordering::SeqCst);
275 if current == SovereignState::Exiled as u8 {
276 return Err(LeaseError::AlreadyLeased);
277 }
278
279 self.state.store(SovereignState::Exiled as u8, Ordering::SeqCst);
281 Ok(Lease::<T>::new(candidate_id, term))
282 }
283}
284
285pub trait VerifiedAnnex<T> {
290 fn annex_verified(&self) -> Result<ProofCarrying<()>, AnnexError>;
314}
315
316#[cfg(test)]
323mod tests {
324 use super::*;
325
326 #[test]
327 fn test_sovereign_new() {
328 let s = Sovereign::new(42i32);
329 assert_eq!(s.state(), SovereignState::Domestic);
330 }
331
332 #[test]
333 fn test_sovereign_deref() {
334 let s = Sovereign::new(42i32);
335 assert_eq!(*s, 42);
336 }
337
338 #[test]
339 fn test_sovereign_deref_mut() {
340 let mut s = Sovereign::new(42i32);
341 *s = 100;
342 assert_eq!(*s, 100);
343 }
344
345 #[test]
346 fn test_sovereign_annex() {
347 let s = Sovereign::new(42i32);
348 assert!(s.annex().is_ok());
349 assert_eq!(s.state(), SovereignState::Exiled);
350 }
351
352 #[test]
353 fn test_sovereign_double_annex() {
354 let s = Sovereign::new(42i32);
355 assert!(s.annex().is_ok());
356 assert!(s.annex().is_err());
357 }
358
359 #[test]
360 #[should_panic(expected = "SOVEREIGNTY VIOLATION")]
361 fn test_sovereignty_violation() {
362 let s = Sovereign::new(42i32);
363 s.annex().unwrap();
364 let _ = *s; }
366
367 #[test]
368 fn test_proof_carrying() {
369 let proof = ProofCarrying::new_unchecked(42i32);
370 assert_eq!(proof.value, 42);
371 assert_eq!(proof.into_inner(), 42);
372 }
373
374 #[test]
375 fn test_annex_error_display() {
376 let e = AnnexError::AlreadyExiled;
377 assert!(e.to_string().contains("foreign jurisdiction"));
378
379 let e = AnnexError::VerificationFailed("test".to_string());
380 assert!(e.to_string().contains("test"));
381 }
382}