example.sol 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // example.sol
  2. contract example {
  3. // Process state
  4. enum State {
  5. Running,
  6. Sleeping,
  7. Waiting,
  8. Stopped,
  9. Zombie,
  10. StateCount
  11. }
  12. // Variables in contract storage
  13. State state;
  14. int32 pid;
  15. uint32 reaped = 3;
  16. // Constants
  17. State constant bad_state = State.Zombie;
  18. int32 constant first_pid = 1;
  19. // Our constructors
  20. constructor(int32 _pid) {
  21. // Set contract storage
  22. pid = _pid;
  23. }
  24. // Reading but not writing contract storage means function
  25. // can be declared view
  26. function is_zombie_reaper() public view returns (bool) {
  27. /* must be pid 1 and not zombie ourselves */
  28. return (pid == first_pid && state != State.Zombie);
  29. }
  30. // Returning a constant does not access storage at all, so
  31. // function can be declared pure
  32. function systemd_pid() public pure returns (uint32) {
  33. // Note that cast is required to change sign from
  34. // int32 to uint32
  35. return uint32(first_pid);
  36. }
  37. /// Convert celcius to fahrenheit
  38. function celcius2fahrenheit(int32 celcius) pure public returns (int32) {
  39. int32 fahrenheit = celcius * 9 / 5 + 32;
  40. return fahrenheit;
  41. }
  42. /// Convert fahrenheit to celcius
  43. function fahrenheit2celcius(int32 fahrenheit) pure public returns (int32) {
  44. return (fahrenheit - 32) * 5 / 9;
  45. }
  46. /// is this number a power-of-two
  47. function is_power_of_2(uint n) pure public returns (bool) {
  48. return n != 0 && (n & (n - 1)) == 0;
  49. }
  50. /// calculate the population count (number of set bits) using Brian Kerningham's way
  51. function population_count(uint n) pure public returns (uint count) {
  52. for (count = 0; n != 0; count++) {
  53. n &= (n - 1);
  54. }
  55. }
  56. /// calculate the power of base to exp
  57. function power(uint base, uint exp) pure public returns (uint) {
  58. return base ** exp;
  59. }
  60. /// returns true if the address is 0
  61. function is_address_zero(address a) pure public returns (bool) {
  62. return a == address(0);
  63. }
  64. /// reverse the bytes in an array of 8 (endian swap)
  65. function byte8reverse(bytes8 input) public pure returns (bytes8 out) {
  66. out = ((input << 56) & hex"ff00_0000_0000_0000") |
  67. ((input << 40) & hex"00ff_0000_0000_0000") |
  68. ((input << 24) & hex"0000_ff00_0000_0000") |
  69. ((input << 8) & hex"0000_00ff_0000_0000") |
  70. ((input >> 8) & hex"0000_0000_ff00_0000") |
  71. ((input >> 24) & hex"0000_0000_00ff_0000") |
  72. ((input >> 40) & hex"0000_0000_0000_ff00") |
  73. ((input >> 56) & hex"0000_0000_0000_00ff");
  74. }
  75. /// This mocks a pid state
  76. function get_pid_state(uint64 _pid) pure private returns (State) {
  77. uint64 n = 8;
  78. for (uint16 i = 1; i < 10; ++i) {
  79. if ((i % 3) == 0) {
  80. n *= _pid / uint64(i);
  81. } else {
  82. n /= 3;
  83. }
  84. }
  85. return State(n % uint64(State.StateCount));
  86. }
  87. /// Overloaded function with different return value!
  88. function get_pid_state() view private returns (uint32) {
  89. return reaped;
  90. }
  91. function reap_processes() public {
  92. uint32 n = 0;
  93. while (n < 100) {
  94. if (get_pid_state(n) == State.Zombie) {
  95. // reap!
  96. reaped += 1;
  97. }
  98. n++;
  99. }
  100. }
  101. function run_queue() public pure returns (uint16) {
  102. uint16 count = 0;
  103. // no initializer means its 0.
  104. uint32 n=0;
  105. do {
  106. if (get_pid_state(n) == State.Waiting) {
  107. count++;
  108. }
  109. }
  110. while (++n < 1000);
  111. return count;
  112. }
  113. // cards
  114. enum suit { club, diamonds, hearts, spades }
  115. enum value { two, three, four, five, six, seven, eight, nine, ten, jack, queen, king, ace }
  116. struct card {
  117. value v;
  118. suit s;
  119. }
  120. card card1 = card(value.two, suit.club);
  121. card card2 = card({s: suit.club, v: value.two});
  122. // This function does a lot of copying
  123. function set_card1(card memory c) public returns (card memory previous) {
  124. previous = card1;
  125. card1 = c;
  126. }
  127. /// return the ace of spades
  128. function ace_of_spaces() public pure returns (card memory) {
  129. return card({s: suit.spades, v: value.ace });
  130. }
  131. /// score card
  132. function score_card(card memory c) public pure returns (uint32 score) {
  133. if (c.s == suit.hearts) {
  134. if (c.v == value.ace) {
  135. score = 14;
  136. }
  137. if (c.v == value.king) {
  138. score = 13;
  139. }
  140. if (c.v == value.queen) {
  141. score = 12;
  142. }
  143. if (c.v == value.jack) {
  144. score = 11;
  145. }
  146. }
  147. // all others score 0
  148. }
  149. }