Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.09 KB | None | 0 0
  1. # uint256 code example
  2. ```
  3. contract ChopsticksBytes {
  4. uint256 a = 0;
  5.  
  6. constructor() public{
  7. a = 1;
  8. }
  9.  
  10. }
  11. ```
  12. # uint256 gas for the above code
  13. ```
  14. {
  15. "Creation": {
  16. "codeDepositCost": "10600",
  17. "executionCost": "25086",
  18. "totalCost": "35686"
  19. }
  20. }
  21. ```
  22. # bytes3 code example
  23. ```
  24. contract ChopsticksBytes {
  25. bytes3 a = hex"000000";
  26.  
  27. constructor() public{
  28. a = hex"000000";
  29. }
  30.  
  31. }
  32. ```
  33.  
  34. # bytes3 gas for the above code
  35. ```
  36. {
  37. "Creation": {
  38. "codeDepositCost": "10600",
  39. "executionCost": "40610",
  40. "totalCost": "51210"
  41. }
  42. }
  43. ```
  44.  
  45. Interestingly the uint256 is cheaper in terms of gas.
  46.  
  47. **More interestingly** bytes32 is considerably cheaper than both bytes8 and uint256 //TODO explain this succinctly
  48. ```
  49. contract ChopsticksBytes {
  50. bytes32 a;
  51.  
  52. constructor() public{
  53. a = hex"000000";
  54. }
  55.  
  56. }
  57. ```
  58. ```
  59. {
  60. "Creation": {
  61. "codeDepositCost": "10600",
  62. "executionCost": "5089",
  63. "totalCost": "15689"
  64. }
  65. }
  66. ```
  67. The cool part is that bytes32 actually allows us to play with a really huge number
  68. ```
  69.  
  70. contract ChopsticksBytes {
  71. bytes32 a;
  72.  
  73. constructor() public{
  74. a = hex"0000000000000000000000000000000000000000000000000000000000000000";
  75. }
  76.  
  77. }
  78. ```
  79.  
  80. This is really interesting because even a simple boolen is twice as expensive as a bytes32
  81. ```
  82. contract ChopsticksBytes {
  83. bool a;
  84.  
  85. constructor() public{
  86. a = false;
  87. }
  88.  
  89. }
  90.  
  91. {
  92. "Creation": {
  93. "codeDepositCost": "10600",
  94. "executionCost": "20333",
  95. "totalCost": "30933"
  96. }
  97. }
  98. ```
  99. This boolean is almost the same gas as a bytes 1
  100.  
  101. ```
  102. contract ChopsticksBytes {
  103. bytes1 a;
  104.  
  105. constructor() public{
  106. a = hex"00";
  107. }
  108.  
  109. }
  110. ```
  111. ```
  112. {
  113. "Creation": {
  114. "codeDepositCost": "10600",
  115. "executionCost": "20338",
  116. "totalCost": "30938"
  117. }
  118. }
  119. ```
  120.  
  121. Explicit converstion of a single digit int is also just as expensive, so its settled
  122. ```
  123. contract ChopsticksBytes {
  124. bytes32 a;
  125.  
  126.  
  127. constructor() public{
  128. a = bytes32(5);
  129. }
  130. }
  131. ```
  132. ```
  133. {
  134. "Creation": {
  135. "codeDepositCost": "10600",
  136. "executionCost": "20097",
  137. "totalCost": "30697"
  138. }
  139. }
  140. ```
  141.  
  142. Rough outline of contract[s]
  143. ```
  144. pragma lity ^1.2.3;
  145.  
  146. contract Hand {
  147. bytes32 a;
  148. constructor(bytes32 _initialHand) public {
  149. a = _initialHand;
  150. }
  151.  
  152. function getHand() public view returns(bytes32) {
  153. return a;
  154. }
  155.  
  156. function setHand(bytes32 _updatedHand) public {
  157. a = _updatedHand;
  158. }
  159. }
  160.  
  161. contract Hands {
  162. event NewHand(address owner, string side, address newHand);
  163. constructor() public {
  164. Hand leftHand = new Hand(hex"000001");
  165. emit NewHand(msg.sender, "LeftHandSide", leftHand);
  166. Hand rightHand = new Hand(hex"100001");
  167. emit NewHand(msg.sender, "RightHandSide", rightHand);
  168. }
  169.  
  170. }
  171.  
  172. contract Games {
  173. event GamesContractDeployed(address addressOFGamesContract);
  174. event NewPlayerAccount(address player, address hands);
  175. mapping(address => address) private playerAccounts;
  176.  
  177. constructor() public {
  178. emit GamesContractDeployed(this);
  179. }
  180.  
  181. function joinAGame() public {
  182. Hands playersHands = new Hands();
  183. playerAccounts[msg.sender] = playersHands;
  184. emit NewPlayerAccount(msg.sender, playersHands);
  185. }
  186.  
  187.  
  188. }
  189. ```
  190. Instructions
  191. ```
  192. // INSTRUCTION
  193. // The giver is always on the left followed by which hand
  194. // 0 is playerA and 1 is playerB
  195. // 0 is leftHand and 1 is rightHand
  196. // 00000101 playerA's leftHand is giving to playerB's rightHand
  197. // 00000100 playerA's leftHand is giving to playerB's leftHand
  198. // 00010101 playerA's rightHand is giving to playerB's rightHand
  199. // 00010100 playerA's rightHand is giving to playerB's leftHand
  200.  
  201. // 01000001 playerB's leftHand is giving to playerA's rightHand
  202. // 01000000 playerB's leftHand is giving to playerA's leftHand
  203. // 01010001 playerB's rightHand is giving to playerA's rightHand
  204. // 01010000 playerB's rightHand is giving to playerA's leftHand
  205. //bytes32 instructions = hex"0000";
  206. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement