variables.rs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // SPDX-License-Identifier: Apache-2.0
  2. use crate::build_solidity;
  3. use parity_scale_codec::Encode;
  4. #[test]
  5. fn global_constants() {
  6. // test that error is allowed as a variable name/contract name
  7. let mut runtime = build_solidity(
  8. r##"
  9. int32 constant error = 102 + 104;
  10. contract a {
  11. function test() public payable {
  12. assert(error == 206);
  13. }
  14. }"##,
  15. );
  16. runtime.constructor(0, Vec::new());
  17. runtime.function("test", Vec::new());
  18. let mut runtime = build_solidity(
  19. r#"
  20. string constant foo = "FOO";
  21. contract error {
  22. function test() public payable {
  23. assert(foo == "FOO");
  24. }
  25. }"#,
  26. );
  27. runtime.constructor(0, Vec::new());
  28. runtime.function("test", Vec::new());
  29. let mut runtime = build_solidity(
  30. r#"
  31. string constant foo = "FOO";
  32. contract a {
  33. function test(uint64 error) public payable {
  34. assert(error == 0);
  35. assert(foo == "FOO");
  36. }
  37. }"#,
  38. );
  39. runtime.constructor(0, Vec::new());
  40. runtime.function("test", 0u64.encode());
  41. }
  42. #[test]
  43. fn ensure_unread_storage_vars_write() {
  44. let mut runtime = build_solidity(
  45. r#"import "polkadot";
  46. contract C {
  47. uint8 foo;
  48. function c(uint8[32] code) public payable {
  49. foo = 123;
  50. require(set_code_hash(code) == 0);
  51. }
  52. }
  53. contract A { uint8 public foo; }"#,
  54. );
  55. runtime.function("c", runtime.contracts()[1].code.hash.encode());
  56. assert_eq!(runtime.storage().values().next().unwrap(), &123u8.encode());
  57. runtime.raw_function(runtime.blobs()[1].messages["foo"].to_vec());
  58. assert_eq!(runtime.output(), 123u8.encode());
  59. }
  60. #[test]
  61. fn ext_fn_call_is_reading_variable() {
  62. let mut runtime = build_solidity(
  63. r##"contract C {
  64. function ext_func_call(uint32 arg) public payable returns (uint32) {
  65. A a = new A();
  66. function(uint32) external returns (uint32) func = a.a;
  67. return func(arg);
  68. }
  69. }
  70. contract A {
  71. function a(uint32 arg) public pure returns (uint32) {
  72. return arg;
  73. }
  74. }"##,
  75. );
  76. runtime.set_transferred_value(1000);
  77. runtime.function("ext_func_call", 0xdeadbeefu32.encode());
  78. assert_eq!(runtime.output(), 0xdeadbeefu32.encode())
  79. }