account_serialization.rs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // SPDX-License-Identifier: Apache-2.0
  2. use crate::borsh_encoding::BorshToken;
  3. use crate::{account_new, build_solidity, AccountMeta, AccountState, Pubkey};
  4. #[test]
  5. fn deserialize_duplicate_account() {
  6. let mut vm = build_solidity(
  7. r#"
  8. contract Testing {
  9. function check_deserialization(address my_address) public view {
  10. assert(tx.accounts[1].key == tx.accounts[2].key);
  11. assert(tx.accounts[1].is_signer == tx.accounts[2].is_signer);
  12. assert(tx.accounts[1].is_writable == tx.accounts[2].is_writable);
  13. assert(my_address == tx.program_id);
  14. }
  15. }
  16. "#,
  17. );
  18. vm.constructor(&[]);
  19. let random_key = account_new();
  20. vm.account_data.insert(
  21. random_key,
  22. AccountState {
  23. data: vec![],
  24. owner: None,
  25. lamports: 0,
  26. },
  27. );
  28. let other_key = account_new();
  29. vm.account_data.insert(
  30. other_key,
  31. AccountState {
  32. data: vec![],
  33. owner: None,
  34. lamports: 0,
  35. },
  36. );
  37. let metas = vec![
  38. AccountMeta {
  39. pubkey: Pubkey(vm.stack[0].data),
  40. is_writable: true,
  41. is_signer: false,
  42. },
  43. AccountMeta {
  44. pubkey: Pubkey(random_key),
  45. is_signer: true,
  46. is_writable: false,
  47. },
  48. AccountMeta {
  49. pubkey: Pubkey(random_key),
  50. is_signer: true,
  51. is_writable: false,
  52. },
  53. ];
  54. vm.function_metas(
  55. "check_deserialization",
  56. &metas,
  57. &[BorshToken::Address(vm.stack[0].program)],
  58. );
  59. }
  60. #[test]
  61. fn more_than_10_accounts() {
  62. let mut vm = build_solidity(
  63. r#"
  64. contract Testing {
  65. function check_deserialization(address my_address) public view {
  66. // This assertion ensure the padding is correctly added when
  67. // deserializing accounts
  68. assert(my_address == tx.program_id);
  69. }
  70. }
  71. "#,
  72. );
  73. vm.constructor(&[]);
  74. let mut metas: Vec<AccountMeta> = Vec::new();
  75. metas.push(AccountMeta {
  76. pubkey: Pubkey(vm.stack[0].data),
  77. is_writable: true,
  78. is_signer: false,
  79. });
  80. for i in 0..11 {
  81. let account = account_new();
  82. metas.push(AccountMeta {
  83. pubkey: Pubkey(account),
  84. is_writable: i % 2 == 0,
  85. is_signer: i % 2 == 1,
  86. });
  87. vm.account_data.insert(
  88. account,
  89. AccountState {
  90. data: vec![],
  91. owner: None,
  92. lamports: 0,
  93. },
  94. );
  95. }
  96. metas.push(metas[3].clone());
  97. let account = account_new();
  98. metas.push(AccountMeta {
  99. pubkey: Pubkey(account),
  100. is_signer: false,
  101. is_writable: false,
  102. });
  103. vm.account_data.insert(
  104. account,
  105. AccountState {
  106. data: vec![],
  107. owner: None,
  108. lamports: 0,
  109. },
  110. );
  111. vm.function_metas(
  112. "check_deserialization",
  113. &metas,
  114. &[BorshToken::Address(vm.stack[0].program)],
  115. );
  116. }