storage.rs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // SPDX-License-Identifier: Apache-2.0
  2. use crate::build_solidity;
  3. use parity_scale_codec::{Decode, Encode};
  4. #[test]
  5. fn storage_load_on_return() {
  6. #[derive(Debug, PartialEq, Eq, Encode, Decode)]
  7. struct SStruct {
  8. f1: i32,
  9. }
  10. let mut runtime = build_solidity(
  11. r##"
  12. contract foo {
  13. struct S { int32 f1; }
  14. S[] arr;
  15. function g() private returns (S storage, S storage) {
  16. return (arr[0], arr[1]);
  17. }
  18. function f() public returns (S, S) {
  19. S[] storage ptrArr = arr;
  20. ptrArr.push(S({f1: 1}));
  21. ptrArr.push(S({f1: 2}));
  22. return g();
  23. }
  24. }
  25. "##,
  26. );
  27. runtime.function("f", Vec::new());
  28. assert_eq!(
  29. runtime.output(),
  30. [SStruct { f1: 1 }, SStruct { f1: 2 }].encode(),
  31. );
  32. }
  33. #[test]
  34. fn storage_initializer_addr_type() {
  35. // The contracts storage initializer writes to the scratch buffer in the storage.
  36. let mut runtime = build_solidity(r#"contract C { address public owner = msg.sender; }"#);
  37. // But this must not overwrite the input length; deploy should not revert.
  38. runtime.constructor(0, Vec::new());
  39. assert!(runtime.output().is_empty());
  40. // Expect the storage initializer to work properly.
  41. runtime.function("owner", Vec::new());
  42. assert_eq!(runtime.output(), runtime.caller());
  43. }