1#[cfg(feature = "dev-context-only-utils")]
4use qualifier_attr::qualifiers;
5use {
6 crate::{Account, AccountSharedData},
7 solana_account_info::{debug_account_data::*, AccountInfo},
8 solana_clock::Epoch,
9 solana_instruction_error::LamportsError,
10 solana_pubkey::Pubkey,
11 std::{cell::RefCell, fmt, mem::MaybeUninit, ops::Deref, ptr, rc::Rc, sync::Arc},
12};
13
14#[cfg(feature = "bincode")]
15mod bincode;
16
17pub fn accounts_equal<T: ReadableAccount, U: ReadableAccount>(me: &T, other: &U) -> bool {
32 me.lamports() == other.lamports()
33 && me.executable() == other.executable()
34 && me.rent_epoch() == other.rent_epoch()
35 && me.owner() == other.owner()
36 && me.data() == other.data()
37}
38
39impl From<AccountSharedData> for Account {
40 fn from(mut other: AccountSharedData) -> Self {
41 let account_data = Arc::make_mut(&mut other.data);
42 Self {
43 lamports: other.lamports,
44 data: std::mem::take(account_data),
45 owner: other.owner,
46 executable: other.executable,
47 rent_epoch: other.rent_epoch,
48 }
49 }
50}
51
52impl From<Account> for AccountSharedData {
53 fn from(other: Account) -> Self {
54 Self {
55 lamports: other.lamports,
56 data: Arc::new(other.data),
57 owner: other.owner,
58 executable: other.executable,
59 rent_epoch: other.rent_epoch,
60 }
61 }
62}
63
64impl Default for Account {
65 fn default() -> Self {
66 Self {
67 lamports: 0,
68 data: Vec::new(),
69 owner: Pubkey::default(),
70 executable: false,
71 rent_epoch: Epoch::MAX,
72 }
73 }
74}
75
76impl Default for AccountSharedData {
77 fn default() -> Self {
78 Self {
79 lamports: 0,
80 data: Arc::default(),
81 owner: Pubkey::default(),
82 executable: false,
83 rent_epoch: Epoch::MAX,
84 }
85 }
86}
87
88pub trait WritableAccount: ReadableAccount {
89 fn set_lamports(&mut self, lamports: u64);
90 fn checked_add_lamports(&mut self, lamports: u64) -> Result<(), LamportsError> {
91 self.set_lamports(
92 self.lamports()
93 .checked_add(lamports)
94 .ok_or(LamportsError::ArithmeticOverflow)?,
95 );
96 Ok(())
97 }
98 fn checked_sub_lamports(&mut self, lamports: u64) -> Result<(), LamportsError> {
99 self.set_lamports(
100 self.lamports()
101 .checked_sub(lamports)
102 .ok_or(LamportsError::ArithmeticUnderflow)?,
103 );
104 Ok(())
105 }
106 fn saturating_add_lamports(&mut self, lamports: u64) {
107 self.set_lamports(self.lamports().saturating_add(lamports))
108 }
109 fn saturating_sub_lamports(&mut self, lamports: u64) {
110 self.set_lamports(self.lamports().saturating_sub(lamports))
111 }
112 fn data_as_mut_slice(&mut self) -> &mut [u8];
113 fn set_owner(&mut self, owner: Pubkey);
114 fn copy_into_owner_from_slice(&mut self, source: &[u8]);
115 fn set_executable(&mut self, executable: bool);
116 #[deprecated(since = "5.3.0", note = "`rent_epoch` is always `u64::MAX`")]
117 fn set_rent_epoch(&mut self, epoch: Epoch);
118}
119
120pub trait ReadableAccount: Sized {
121 fn lamports(&self) -> u64;
122 fn data(&self) -> &[u8];
123 fn owner(&self) -> &Pubkey;
124 fn executable(&self) -> bool;
125 fn rent_epoch(&self) -> Epoch;
126}
127
128impl<T> ReadableAccount for T
129where
130 T: Deref,
131 T::Target: ReadableAccount,
132{
133 fn lamports(&self) -> u64 {
134 self.deref().lamports()
135 }
136 fn data(&self) -> &[u8] {
137 self.deref().data()
138 }
139 fn owner(&self) -> &Pubkey {
140 self.deref().owner()
141 }
142 fn executable(&self) -> bool {
143 self.deref().executable()
144 }
145 fn rent_epoch(&self) -> Epoch {
146 self.deref().rent_epoch()
147 }
148}
149
150impl ReadableAccount for Account {
151 fn lamports(&self) -> u64 {
152 self.lamports
153 }
154 fn data(&self) -> &[u8] {
155 &self.data
156 }
157 fn owner(&self) -> &Pubkey {
158 &self.owner
159 }
160 fn executable(&self) -> bool {
161 self.executable
162 }
163 fn rent_epoch(&self) -> Epoch {
164 self.rent_epoch
165 }
166}
167
168impl WritableAccount for Account {
169 fn set_lamports(&mut self, lamports: u64) {
170 self.lamports = lamports;
171 }
172 fn data_as_mut_slice(&mut self) -> &mut [u8] {
173 &mut self.data
174 }
175 fn set_owner(&mut self, owner: Pubkey) {
176 self.owner = owner;
177 }
178 fn copy_into_owner_from_slice(&mut self, source: &[u8]) {
179 self.owner.as_mut().copy_from_slice(source);
180 }
181 fn set_executable(&mut self, executable: bool) {
182 self.executable = executable;
183 }
184 fn set_rent_epoch(&mut self, epoch: Epoch) {
185 self.rent_epoch = epoch;
186 }
187}
188
189impl WritableAccount for AccountSharedData {
190 fn set_lamports(&mut self, lamports: u64) {
191 self.lamports = lamports;
192 }
193 fn data_as_mut_slice(&mut self) -> &mut [u8] {
194 &mut self.data_mut()[..]
195 }
196 fn set_owner(&mut self, owner: Pubkey) {
197 self.owner = owner;
198 }
199 fn copy_into_owner_from_slice(&mut self, source: &[u8]) {
200 self.owner.as_mut().copy_from_slice(source);
201 }
202 fn set_executable(&mut self, executable: bool) {
203 self.executable = executable;
204 }
205 fn set_rent_epoch(&mut self, epoch: Epoch) {
206 self.rent_epoch = epoch;
207 }
208}
209
210impl ReadableAccount for AccountSharedData {
211 fn lamports(&self) -> u64 {
212 self.lamports
213 }
214 fn data(&self) -> &[u8] {
215 &self.data
216 }
217 fn owner(&self) -> &Pubkey {
218 &self.owner
219 }
220 fn executable(&self) -> bool {
221 self.executable
222 }
223 fn rent_epoch(&self) -> Epoch {
224 self.rent_epoch
225 }
226}
227
228fn debug_fmt<T: ReadableAccount>(item: &T, f: &mut fmt::Formatter<'_>) -> fmt::Result {
229 let mut f = f.debug_struct("Account");
230
231 f.field("lamports", &item.lamports())
232 .field("data.len", &item.data().len())
233 .field("owner", &item.owner())
234 .field("executable", &item.executable())
235 .field("rent_epoch", &item.rent_epoch());
236 debug_account_data(item.data(), &mut f);
237
238 f.finish()
239}
240
241impl fmt::Debug for Account {
242 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
243 debug_fmt(self, f)
244 }
245}
246
247impl fmt::Debug for AccountSharedData {
248 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
249 debug_fmt(self, f)
250 }
251}
252
253impl Account {
254 #[deprecated(
255 since = "5.2.0",
256 note = "Use `empty`, `new_with_data`, or `state_traits::StateMutWincode::{new_data, new_data_with_space}` instead"
257 )]
258 pub fn new(lamports: u64, space: usize, owner: &Pubkey) -> Self {
259 Account {
260 lamports,
261 data: vec![0; space],
262 owner: *owner,
263 executable: false,
264 rent_epoch: Epoch::MAX,
265 }
266 }
267 pub fn new_with_data(lamports: u64, data: Vec<u8>, owner: &Pubkey) -> Self {
271 Account {
272 lamports,
273 data,
274 owner: *owner,
275 executable: false,
276 rent_epoch: Epoch::MAX,
277 }
278 }
279 pub fn empty(lamports: u64, owner: &Pubkey) -> Self {
281 Account {
282 lamports,
283 data: Vec::new(),
284 owner: *owner,
285 executable: false,
286 rent_epoch: Epoch::MAX,
287 }
288 }
289 #[allow(deprecated, reason = "`new` and `new_ref` are deprecated together")]
290 #[deprecated(
291 since = "5.2.0",
292 note = "Use `empty`, `new_with_data`, or `state_traits::StateMutWincode::{new_data, new_data_with_space}`, then wrap in `Rc<RefCell<_>>`"
293 )]
294 pub fn new_ref(lamports: u64, space: usize, owner: &Pubkey) -> Rc<RefCell<Self>> {
295 Rc::new(RefCell::new(Account::new(lamports, space, owner)))
296 }
297 #[deprecated(since = "5.2.0", note = "Use `new_with_data` instead")]
298 pub fn new_rent_epoch(lamports: u64, space: usize, owner: &Pubkey, rent_epoch: Epoch) -> Self {
299 Account {
300 lamports,
301 data: vec![0; space],
302 owner: *owner,
303 executable: false,
304 rent_epoch,
305 }
306 }
307}
308
309impl AccountSharedData {
310 pub fn is_shared(&self) -> bool {
311 Arc::strong_count(&self.data) > 1
312 }
313
314 pub fn reserve(&mut self, additional: usize) {
315 if let Some(data) = Arc::get_mut(&mut self.data) {
316 data.reserve(additional)
317 } else {
318 let mut data = Vec::with_capacity(self.data.len().saturating_add(additional));
319 data.extend_from_slice(&self.data);
320 self.data = Arc::new(data);
321 }
322 }
323
324 pub fn capacity(&self) -> usize {
325 self.data.capacity()
326 }
327
328 pub fn data_clone(&self) -> Arc<Vec<u8>> {
329 Arc::clone(&self.data)
330 }
331
332 fn data_mut(&mut self) -> &mut Vec<u8> {
333 Arc::make_mut(&mut self.data)
334 }
335
336 pub fn resize(&mut self, new_len: usize, value: u8) {
337 self.data_mut().resize(new_len, value)
338 }
339
340 pub fn extend_from_slice(&mut self, data: &[u8]) {
341 self.data_mut().extend_from_slice(data)
342 }
343
344 pub fn set_data_from_slice(&mut self, new_data: &[u8]) {
345 let Some(data) = Arc::get_mut(&mut self.data) else {
347 return self.set_data(new_data.to_vec());
350 };
351
352 let new_len = new_data.len();
353
354 data.reserve(new_len.saturating_sub(data.len()));
369
370 #[allow(clippy::uninit_vec)]
375 unsafe {
377 data.set_len(0);
378 ptr::copy_nonoverlapping(new_data.as_ptr(), data.as_mut_ptr(), new_len);
379 data.set_len(new_len);
380 };
381 }
382
383 #[cfg_attr(feature = "dev-context-only-utils", qualifiers(pub))]
384 fn set_data(&mut self, data: Vec<u8>) {
385 self.data = Arc::new(data);
386 }
387
388 pub fn spare_data_capacity_mut(&mut self) -> &mut [MaybeUninit<u8>] {
389 self.data_mut().spare_capacity_mut()
390 }
391
392 #[deprecated(
393 since = "5.2.0",
394 note = "Use `empty`, `new_with_data`, or `state_traits::StateMutWincode::{new_data, new_data_with_space}` instead"
395 )]
396 pub fn new(lamports: u64, space: usize, owner: &Pubkey) -> Self {
397 AccountSharedData {
398 lamports,
399 data: Arc::new(vec![0u8; space]),
400 owner: *owner,
401 executable: false,
402 rent_epoch: Epoch::MAX,
403 }
404 }
405 pub fn new_with_data(lamports: u64, data: Vec<u8>, owner: &Pubkey) -> Self {
412 AccountSharedData {
413 lamports,
414 data: Arc::new(data),
415 owner: *owner,
416 executable: false,
417 rent_epoch: Epoch::MAX,
418 }
419 }
420 pub fn empty(lamports: u64, owner: &Pubkey) -> Self {
422 AccountSharedData {
423 lamports,
424 data: Arc::new(Vec::new()),
425 owner: *owner,
426 executable: false,
427 rent_epoch: Epoch::MAX,
428 }
429 }
430 #[allow(deprecated, reason = "`new` and `new_ref` are deprecated together")]
431 #[deprecated(
432 since = "5.2.0",
433 note = "Use `empty`, `new_with_data`, or `state_traits::StateMutWincode::{new_data, new_data_with_space}`, then wrap in `Rc<RefCell<_>>`"
434 )]
435 pub fn new_ref(lamports: u64, space: usize, owner: &Pubkey) -> Rc<RefCell<Self>> {
436 Rc::new(RefCell::new(AccountSharedData::new(lamports, space, owner)))
437 }
438 #[deprecated(since = "5.2.0", note = "Use `empty` or `new_with_data` instead")]
439 pub fn new_rent_epoch(lamports: u64, space: usize, owner: &Pubkey, rent_epoch: Epoch) -> Self {
440 AccountSharedData {
441 lamports,
442 data: Arc::new(vec![0; space]),
443 owner: *owner,
444 executable: false,
445 rent_epoch,
446 }
447 }
448 pub fn new_with_shared_data(
450 lamports: u64,
451 data: Arc<Vec<u8>>,
452 owner: &Pubkey,
453 executable: bool,
454 ) -> AccountSharedData {
455 AccountSharedData {
456 lamports,
457 data,
458 owner: *owner,
459 executable,
460 rent_epoch: Epoch::MAX,
461 }
462 }
463 #[deprecated(since = "5.3.0", note = "Use `new_with_shared_data` instead")]
464 pub fn create_from_existing_shared_data(
465 lamports: u64,
466 data: Arc<Vec<u8>>,
467 owner: Pubkey,
468 executable: bool,
469 rent_epoch: Epoch,
470 ) -> AccountSharedData {
471 AccountSharedData {
472 lamports,
473 data,
474 owner,
475 executable,
476 rent_epoch,
477 }
478 }
479}
480
481#[deprecated(
482 since = "5.3.0",
483 note = "Not in use anymore; `rent_epoch` is always `u64::MAX`"
484)]
485pub type InheritableAccountFields = (u64, Epoch);
486#[deprecated(
487 since = "5.3.0",
488 note = "Not in use anymore; `rent_epoch` is always `u64::MAX`"
489)]
490#[allow(deprecated)]
491pub const DUMMY_INHERITABLE_ACCOUNT_FIELDS: InheritableAccountFields = (1, Epoch::MAX);
492
493impl solana_account_info::Account for Account {
496 fn get(&mut self) -> (&mut u64, &mut [u8], &Pubkey, bool) {
497 (
498 &mut self.lamports,
499 &mut self.data,
500 &self.owner,
501 self.executable,
502 )
503 }
504}
505
506pub fn create_is_signer_account_infos<'a>(
508 accounts: &'a mut [(&'a Pubkey, bool, &'a mut Account)],
509) -> Vec<AccountInfo<'a>> {
510 accounts
511 .iter_mut()
512 .map(|(key, is_signer, account)| {
513 AccountInfo::new(
514 key,
515 *is_signer,
516 false,
517 &mut account.lamports,
518 &mut account.data,
519 &account.owner,
520 account.executable,
521 )
522 })
523 .collect()
524}
525
526#[cfg(test)]
527pub mod tests {
528 use super::*;
529
530 fn make_two_accounts(key: &Pubkey) -> (Account, AccountSharedData) {
531 let mut account1 = Account::new_with_data(1, vec![3, 4], key);
532 account1.executable = true;
533 account1.rent_epoch = 4;
534 let mut account2 = AccountSharedData::new_with_data(1, vec![3, 4], key);
535 account2.executable = true;
536 account2.rent_epoch = 4;
537 assert!(accounts_equal(&account1, &account2));
538 (account1, account2)
539 }
540
541 #[test]
542 fn test_account_empty() {
543 let key = Pubkey::new_unique();
544 let account1 = Account::empty(1, &key);
545 let account2 = AccountSharedData::empty(1, &key);
546 assert!(accounts_equal(&account1, &account2));
547 }
548
549 #[test]
550 fn test_account_shared_data_new_with_data() {
551 let key = Pubkey::new_unique();
552
553 let account = AccountSharedData::new_with_data(1, vec![1, 2, 3], &key);
554 assert!(accounts_equal(
555 &account,
556 &Account::new_with_data(1, vec![1, 2, 3], &key)
557 ));
558 assert_eq!(account.lamports(), 1);
559 assert_eq!(account.owner(), &key);
560 assert_eq!(account.data(), &[1, 2, 3]);
561 assert!(!account.executable());
562 assert_eq!(account.rent_epoch(), Epoch::MAX);
563 assert!(!account.is_shared());
564 }
565
566 #[test]
567 fn test_account_constructors_rent_epoch() {
568 let key = Pubkey::new_unique();
569
570 assert_eq!(Account::default().rent_epoch(), Epoch::MAX);
571 assert_eq!(AccountSharedData::default().rent_epoch(), Epoch::MAX);
572
573 assert_eq!(AccountSharedData::empty(1, &key).rent_epoch(), Epoch::MAX);
574
575 assert_eq!(
576 Account::new_with_data(1, vec![1], &key).rent_epoch(),
577 Epoch::MAX
578 );
579 assert_eq!(
580 AccountSharedData::new_with_data(1, vec![1], &key).rent_epoch(),
581 Epoch::MAX
582 );
583 assert_eq!(
584 AccountSharedData::new_with_shared_data(1, Arc::new(vec![1]), &key, true).rent_epoch(),
585 Epoch::MAX
586 );
587
588 assert_eq!(
589 Account::new_data(1, &1u8, &key).unwrap().rent_epoch(),
590 Epoch::MAX
591 );
592 assert_eq!(
593 AccountSharedData::new_data(1, &1u8, &key)
594 .unwrap()
595 .rent_epoch(),
596 Epoch::MAX
597 );
598 }
599
600 #[test]
601 fn test_account_shared_data_new_with_shared_data() {
602 let key = Pubkey::new_unique();
603 let data = Arc::new(vec![1, 2, 3]);
604 let account = AccountSharedData::new_with_shared_data(7, Arc::clone(&data), &key, true);
605 assert_eq!(account.lamports(), 7);
606 assert_eq!(account.owner(), &key);
607 assert_eq!(account.data(), &[1, 2, 3]);
608 assert!(account.executable());
609 assert_eq!(account.rent_epoch(), Epoch::MAX);
610 assert!(account.is_shared());
611 assert!(Arc::ptr_eq(&account.data_clone(), &data));
612 }
613
614 #[test]
615 fn test_account_data_copy_as_slice() {
616 let key = Pubkey::new_unique();
617 let key2 = Pubkey::new_unique();
618 let (mut account1, mut account2) = make_two_accounts(&key);
619 account1.copy_into_owner_from_slice(key2.as_ref());
620 account2.copy_into_owner_from_slice(key2.as_ref());
621 assert!(accounts_equal(&account1, &account2));
622 assert_eq!(account1.owner(), &key2);
623 }
624
625 #[test]
626 fn test_account_set_data_from_slice() {
627 let key = Pubkey::new_unique();
628 let (_, mut account) = make_two_accounts(&key);
629 assert_eq!(account.data(), &vec![3, 4]);
630 account.set_data_from_slice(&[1, 2]);
631 assert_eq!(account.data(), &vec![1, 2]);
632 account.set_data_from_slice(&[1, 2, 3]);
633 assert_eq!(account.data(), &vec![1, 2, 3]);
634 account.set_data_from_slice(&[4, 5, 6]);
635 assert_eq!(account.data(), &vec![4, 5, 6]);
636 account.set_data_from_slice(&[4, 5, 6, 0]);
637 assert_eq!(account.data(), &vec![4, 5, 6, 0]);
638 account.set_data_from_slice(&[]);
639 assert_eq!(account.data().len(), 0);
640 account.set_data_from_slice(&[44]);
641 assert_eq!(account.data(), &vec![44]);
642 account.set_data_from_slice(&[44]);
643 assert_eq!(account.data(), &vec![44]);
644 }
645
646 #[test]
647 fn test_account_data_set_data() {
648 let key = Pubkey::new_unique();
649 let (_, mut account) = make_two_accounts(&key);
650 assert_eq!(account.data(), &vec![3, 4]);
651 account.set_data(vec![1, 2]);
652 assert_eq!(account.data(), &vec![1, 2]);
653 account.set_data(vec![]);
654 assert_eq!(account.data().len(), 0);
655 }
656
657 #[test]
658 #[should_panic(
659 expected = "called `Result::unwrap()` on an `Err` value: Io(Kind(UnexpectedEof))"
660 )]
661 fn test_account_deserialize() {
662 let key = Pubkey::new_unique();
663 let (account1, _account2) = make_two_accounts(&key);
664 account1.deserialize_data::<String>().unwrap();
665 }
666
667 #[test]
668 #[should_panic(expected = "called `Result::unwrap()` on an `Err` value: SizeLimit")]
669 fn test_account_serialize() {
670 let key = Pubkey::new_unique();
671 let (mut account1, _account2) = make_two_accounts(&key);
672 account1.serialize_data(&"hello world").unwrap();
673 }
674
675 #[test]
676 #[should_panic(
677 expected = "called `Result::unwrap()` on an `Err` value: Io(Kind(UnexpectedEof))"
678 )]
679 fn test_account_shared_data_deserialize() {
680 let key = Pubkey::new_unique();
681 let (_account1, account2) = make_two_accounts(&key);
682 account2.deserialize_data::<String>().unwrap();
683 }
684
685 #[test]
686 #[should_panic(expected = "called `Result::unwrap()` on an `Err` value: SizeLimit")]
687 fn test_account_shared_data_serialize() {
688 let key = Pubkey::new_unique();
689 let (_account1, mut account2) = make_two_accounts(&key);
690 account2.serialize_data(&"hello world").unwrap();
691 }
692
693 #[test]
694 fn test_account_shared_data() {
695 let key = Pubkey::new_unique();
696 let (account1, account2) = make_two_accounts(&key);
697 assert!(accounts_equal(&account1, &account2));
698 let account = account1;
699 assert_eq!(account.lamports, 1);
700 assert_eq!(account.lamports(), 1);
701 assert_eq!(account.data.len(), 2);
702 assert_eq!(account.data().len(), 2);
703 assert_eq!(account.owner, key);
704 assert_eq!(account.owner(), &key);
705 assert!(account.executable);
706 assert!(account.executable());
707 assert_eq!(account.rent_epoch, 4);
708 assert_eq!(account.rent_epoch(), 4);
709 let account = account2;
710 assert_eq!(account.lamports, 1);
711 assert_eq!(account.lamports(), 1);
712 assert_eq!(account.data.len(), 2);
713 assert_eq!(account.data().len(), 2);
714 assert_eq!(account.owner, key);
715 assert_eq!(account.owner(), &key);
716 assert!(account.executable);
717 assert!(account.executable());
718 assert_eq!(account.rent_epoch, 4);
719 assert_eq!(account.rent_epoch(), 4);
720 }
721
722 fn test_equal(
724 should_be_equal: bool,
725 account1: &Account,
726 account2: &AccountSharedData,
727 account_expected: &Account,
728 ) {
729 assert_eq!(should_be_equal, accounts_equal(account1, account2));
730 if should_be_equal {
731 assert!(accounts_equal(account_expected, account2));
732 }
733 assert_eq!(
734 accounts_equal(account_expected, account1),
735 accounts_equal(account_expected, &account1.clone())
736 );
737 assert_eq!(
738 accounts_equal(account_expected, account2),
739 accounts_equal(account_expected, &account2.clone())
740 );
741 assert_eq!(
742 accounts_equal(account_expected, account1),
743 accounts_equal(account_expected, &AccountSharedData::from(account1.clone()))
744 );
745 assert_eq!(
746 accounts_equal(account_expected, account2),
747 accounts_equal(account_expected, &Account::from(account2.clone()))
748 );
749 }
750
751 #[test]
752 fn test_account_add_sub_lamports() {
753 let key = Pubkey::new_unique();
754 let (mut account1, mut account2) = make_two_accounts(&key);
755 assert!(accounts_equal(&account1, &account2));
756 account1.checked_add_lamports(1).unwrap();
757 account2.checked_add_lamports(1).unwrap();
758 assert!(accounts_equal(&account1, &account2));
759 assert_eq!(account1.lamports(), 2);
760 account1.checked_sub_lamports(2).unwrap();
761 account2.checked_sub_lamports(2).unwrap();
762 assert!(accounts_equal(&account1, &account2));
763 assert_eq!(account1.lamports(), 0);
764 }
765
766 #[test]
767 #[should_panic(expected = "Overflow")]
768 fn test_account_checked_add_lamports_overflow() {
769 let key = Pubkey::new_unique();
770 let (mut account1, _account2) = make_two_accounts(&key);
771 account1.checked_add_lamports(u64::MAX).unwrap();
772 }
773
774 #[test]
775 #[should_panic(expected = "Underflow")]
776 fn test_account_checked_sub_lamports_underflow() {
777 let key = Pubkey::new_unique();
778 let (mut account1, _account2) = make_two_accounts(&key);
779 account1.checked_sub_lamports(u64::MAX).unwrap();
780 }
781
782 #[test]
783 #[should_panic(expected = "Overflow")]
784 fn test_account_checked_add_lamports_overflow2() {
785 let key = Pubkey::new_unique();
786 let (_account1, mut account2) = make_two_accounts(&key);
787 account2.checked_add_lamports(u64::MAX).unwrap();
788 }
789
790 #[test]
791 #[should_panic(expected = "Underflow")]
792 fn test_account_checked_sub_lamports_underflow2() {
793 let key = Pubkey::new_unique();
794 let (_account1, mut account2) = make_two_accounts(&key);
795 account2.checked_sub_lamports(u64::MAX).unwrap();
796 }
797
798 #[test]
799 fn test_account_saturating_add_lamports() {
800 let key = Pubkey::new_unique();
801 let (mut account, _) = make_two_accounts(&key);
802
803 let remaining = 22;
804 account.set_lamports(u64::MAX - remaining);
805 account.saturating_add_lamports(remaining * 2);
806 assert_eq!(account.lamports(), u64::MAX);
807 }
808
809 #[test]
810 fn test_account_saturating_sub_lamports() {
811 let key = Pubkey::new_unique();
812 let (mut account, _) = make_two_accounts(&key);
813
814 let remaining = 33;
815 account.set_lamports(remaining);
816 account.saturating_sub_lamports(remaining * 2);
817 assert_eq!(account.lamports(), 0);
818 }
819
820 #[test]
821 fn test_account_shared_data_all_fields() {
822 let key = Pubkey::new_unique();
823 let key2 = Pubkey::new_unique();
824 let key3 = Pubkey::new_unique();
825 let (mut account1, mut account2) = make_two_accounts(&key);
826 assert!(accounts_equal(&account1, &account2));
827
828 let mut account_expected = account1.clone();
829 assert!(accounts_equal(&account1, &account_expected));
830 assert!(accounts_equal(&account1, &account2.clone())); for field_index in 0..5 {
833 for pass in 0..4 {
834 if field_index == 0 {
835 if pass == 0 {
836 account1.checked_add_lamports(1).unwrap();
837 } else if pass == 1 {
838 account_expected.checked_add_lamports(1).unwrap();
839 account2.set_lamports(account2.lamports + 1);
840 } else if pass == 2 {
841 account1.set_lamports(account1.lamports + 1);
842 } else if pass == 3 {
843 account_expected.checked_add_lamports(1).unwrap();
844 account2.checked_add_lamports(1).unwrap();
845 }
846 } else if field_index == 1 {
847 if pass == 0 {
848 account1.data[0] += 1;
849 } else if pass == 1 {
850 account_expected.data[0] += 1;
851 account2.data_as_mut_slice()[0] = account2.data[0] + 1;
852 } else if pass == 2 {
853 account1.data_as_mut_slice()[0] = account1.data[0] + 1;
854 } else if pass == 3 {
855 account_expected.data[0] += 1;
856 account2.data_as_mut_slice()[0] += 1;
857 }
858 } else if field_index == 2 {
859 if pass == 0 {
860 account1.owner = key2;
861 } else if pass == 1 {
862 account_expected.owner = key2;
863 account2.set_owner(key2);
864 } else if pass == 2 {
865 account1.set_owner(key3);
866 } else if pass == 3 {
867 account_expected.owner = key3;
868 account2.owner = key3;
869 }
870 } else if field_index == 3 {
871 if pass == 0 {
872 account1.executable = !account1.executable;
873 } else if pass == 1 {
874 account_expected.executable = !account_expected.executable;
875 account2.set_executable(!account2.executable);
876 } else if pass == 2 {
877 account1.set_executable(!account1.executable);
878 } else if pass == 3 {
879 account_expected.executable = !account_expected.executable;
880 account2.executable = !account2.executable;
881 }
882 } else if field_index == 4 {
883 if pass == 0 {
884 account1.rent_epoch += 1;
885 } else if pass == 1 {
886 account_expected.rent_epoch += 1;
887 #[allow(deprecated)]
888 account2.set_rent_epoch(account2.rent_epoch + 1);
889 } else if pass == 2 {
890 #[allow(deprecated)]
891 account1.set_rent_epoch(account1.rent_epoch + 1);
892 } else if pass == 3 {
893 account_expected.rent_epoch += 1;
894 account2.rent_epoch += 1;
895 }
896 }
897
898 let should_be_equal = pass == 1 || pass == 3;
899 test_equal(should_be_equal, &account1, &account2, &account_expected);
900
901 if should_be_equal {
902 {
903 let account1_with_data = Account::new_data(
905 account_expected.lamports(),
906 &account_expected.data()[0],
907 account_expected.owner(),
908 )
909 .unwrap();
910 let account2_with_data = AccountSharedData::new_data(
911 account_expected.lamports(),
912 &account_expected.data()[0],
913 account_expected.owner(),
914 )
915 .unwrap();
916
917 assert!(accounts_equal(&account1_with_data, &account2_with_data));
918 assert_eq!(
919 account1_with_data.deserialize_data::<u8>().unwrap(),
920 account2_with_data.deserialize_data::<u8>().unwrap()
921 );
922 }
923
924 assert!(accounts_equal(
926 &Account::new_data_with_space(
927 account_expected.lamports(),
928 &account_expected.data()[0],
929 1,
930 account_expected.owner()
931 )
932 .unwrap(),
933 &AccountSharedData::new_data_with_space(
934 account_expected.lamports(),
935 &account_expected.data()[0],
936 1,
937 account_expected.owner()
938 )
939 .unwrap()
940 ));
941
942 assert!(accounts_equal(
944 &Account::new_ref_data(
945 account_expected.lamports(),
946 &account_expected.data()[0],
947 account_expected.owner()
948 )
949 .unwrap()
950 .borrow(),
951 &AccountSharedData::new_ref_data(
952 account_expected.lamports(),
953 &account_expected.data()[0],
954 account_expected.owner()
955 )
956 .unwrap()
957 .borrow()
958 ));
959
960 assert!(accounts_equal(
962 &Account::new_ref_data_with_space(
963 account_expected.lamports(),
964 &account_expected.data()[0],
965 1,
966 account_expected.owner()
967 )
968 .unwrap()
969 .borrow(),
970 &AccountSharedData::new_ref_data_with_space(
971 account_expected.lamports(),
972 &account_expected.data()[0],
973 1,
974 account_expected.owner()
975 )
976 .unwrap()
977 .borrow()
978 ));
979 }
980 }
981 }
982 }
983}