{"id":2320,"date":"2019-02-12T14:38:46","date_gmt":"2019-02-12T01:38:46","guid":{"rendered":"http:\/\/www.talkcrypto.org\/blog\/?p=2320"},"modified":"2019-02-20T15:38:34","modified_gmt":"2019-02-20T02:38:34","slug":"remix-mstore-bug","status":"publish","type":"post","link":"https:\/\/www.talkcrypto.org\/blog\/2019\/02\/12\/remix-mstore-bug\/","title":{"rendered":"Remix MSTORE bug"},"content":{"rendered":"\n<p>I was looking into strings v bytes into a bit more detail and started comparing the gas consumption between them. The following code was used:<br><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">pragma solidity ^0.5.0;<br> contract StringVersusByte {<br>   string constant _string = \"hello\";<br>   bytes32 constant _bytes = \"hello\";<br> function  getAsString() pure public returns(string memory) {<br>     return _string;<br>   }<br> function  getAsBytes() pure public returns(bytes32) {<br>     return _bytes;<br>   }<br> }<br><\/pre>\n\n\n\n<p>After deploying, I executed the the getAsBytes() first and saw that the execution cost was 196 gas.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignleft is-resized\"><img decoding=\"async\" src=\"https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2019\/02\/Screen-Shot-2019-02-20-at-12.42.22-pm.png\" alt=\"\" class=\"wp-image-2326\" width=\"241\" height=\"63\" srcset=\"https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2019\/02\/Screen-Shot-2019-02-20-at-12.42.22-pm.png 436w, https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2019\/02\/Screen-Shot-2019-02-20-at-12.42.22-pm-300x78.png 300w\" sizes=\"(max-width: 241px) 100vw, 241px\" \/><\/figure><\/div>\n\n\n\n<p>I then looked to confirm this by stepping through the debugger and summing up the cost of all the op codes.<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignleft is-resized\"><img fetchpriority=\"high\" decoding=\"async\" src=\"https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2019\/02\/Screen-Shot-2019-02-20-at-12.49.45-pm.png\" alt=\"\" class=\"wp-image-2327\" width=\"266\" height=\"486\" srcset=\"https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2019\/02\/Screen-Shot-2019-02-20-at-12.49.45-pm.png 474w, https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2019\/02\/Screen-Shot-2019-02-20-at-12.49.45-pm-164x300.png 164w\" sizes=\"(max-width: 266px) 100vw, 266px\" \/><\/figure><\/div>\n\n\n\n<p>What I discovered was that it summed up to 172 and not 196 gas. I was out by 24 gas. <\/p>\n\n\n\n<p>I then looked at the debugger and looked at the remaining gas field. It should reduce by the amount of gas but in step 3 it doesn&#8217;t. It reduces by 12 instead of 3 as can be seen in the video.<\/p>\n\n\n\n<p>The culprit opcode is MSTORE and there are 2 MSTORE which makes up the missing 24 gas.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe title=\"Solidity gas consumption\" width=\"640\" height=\"360\" src=\"https:\/\/www.youtube.com\/embed\/7u6dW-Gagss?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>In the video, the remaining gas decreases by the expected amount until it reaches step 3. <\/p>\n\n\n\n<p>This issue is highlighted as a bug in remix via: https:\/\/github.com\/ethereum\/remix-ide\/issues\/966 with the reason being: <em>The cost of MSTORE is 3 gas, but the general cost if(sic) the step can be higher if the memory needs to be extended.<\/em><\/p>\n\n\n\n<p>I don&#8217;t know what this means and still figuring it out (https:\/\/github.com\/ethereum\/remix-ide\/issues\/966)<\/p>\n\n\n\n<p>I doubt that <a rel=\"noreferrer noopener\" aria-label=\"this (opens in a new tab)\" href=\"https:\/\/ethereum.stackexchange.com\/questions\/48576\/solidity-assembly-question-mstore\" target=\"_blank\">this<\/a> is the reason but it is interesting to understand that the first 32 bytes are reserved to hold the length of the array.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was looking into strings v bytes into a bit<\/p>\n","protected":false},"author":1,"featured_media":2321,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-2320","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ethereum"],"featured_image_urls":{"full":["https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2019\/02\/gas.jpg",300,187,false],"thumbnail":["https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2019\/02\/gas-150x150.jpg",150,150,true],"medium":["https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2019\/02\/gas-300x187.jpg",300,187,true],"medium_large":["https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2019\/02\/gas.jpg",300,187,false],"large":["https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2019\/02\/gas.jpg",300,187,false],"1536x1536":["https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2019\/02\/gas.jpg",300,187,false],"2048x2048":["https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2019\/02\/gas.jpg",300,187,false],"chromenews-featured":["https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2019\/02\/gas.jpg",300,187,false],"chromenews-large":["https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2019\/02\/gas.jpg",300,187,false],"chromenews-medium":["https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2019\/02\/gas.jpg",300,187,false]},"author_info":{"info":["seandotau"]},"category_info":"<a href=\"https:\/\/www.talkcrypto.org\/blog\/category\/ethereum\/\" rel=\"category tag\">Ethereum<\/a>","tag_info":"Ethereum","comment_count":"0","_links":{"self":[{"href":"https:\/\/www.talkcrypto.org\/blog\/wp-json\/wp\/v2\/posts\/2320","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.talkcrypto.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.talkcrypto.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.talkcrypto.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.talkcrypto.org\/blog\/wp-json\/wp\/v2\/comments?post=2320"}],"version-history":[{"count":10,"href":"https:\/\/www.talkcrypto.org\/blog\/wp-json\/wp\/v2\/posts\/2320\/revisions"}],"predecessor-version":[{"id":2341,"href":"https:\/\/www.talkcrypto.org\/blog\/wp-json\/wp\/v2\/posts\/2320\/revisions\/2341"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.talkcrypto.org\/blog\/wp-json\/wp\/v2\/media\/2321"}],"wp:attachment":[{"href":"https:\/\/www.talkcrypto.org\/blog\/wp-json\/wp\/v2\/media?parent=2320"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.talkcrypto.org\/blog\/wp-json\/wp\/v2\/categories?post=2320"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.talkcrypto.org\/blog\/wp-json\/wp\/v2\/tags?post=2320"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}