<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Francis Odisi on Medium]]></title>
        <description><![CDATA[Stories by Francis Odisi on Medium]]></description>
        <link>https://medium.com/@francis.odisi?source=rss-2edbaa53f25e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*uDNnsi78XwcO3r-aGtnX6A.jpeg</url>
            <title>Stories by Francis Odisi on Medium</title>
            <link>https://medium.com/@francis.odisi?source=rss-2edbaa53f25e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 00:04:31 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@francis.odisi/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[GoodGhosting Smart Contract Audits]]></title>
            <link>https://medium.com/halofi/goodghosting-smart-contract-audits-cc271fb9c268?source=rss-2edbaa53f25e------2</link>
            <guid isPermaLink="false">https://medium.com/p/cc271fb9c268</guid>
            <category><![CDATA[smart-contracts]]></category>
            <category><![CDATA[security]]></category>
            <category><![CDATA[goodghosting]]></category>
            <category><![CDATA[defi]]></category>
            <category><![CDATA[saving]]></category>
            <dc:creator><![CDATA[Francis Odisi]]></dc:creator>
            <pubDate>Mon, 23 Aug 2021 08:58:10 GMT</pubDate>
            <atom:updated>2021-08-23T14:38:57.172Z</atom:updated>
            <content:encoded><![CDATA[<p>We started <a href="https://goodghosting.com/">GoodGhosting’s</a> <a href="https://github.com/Good-Ghosting/goodghosting-smart-contracts">smart contracts</a> development aiming to include smart contract security into our development life cycle from the very start, instead of delaying this to the end of the process, when all contracts are “done” and ready for one big audit.</p><p><strong>The </strong><a href="https://goodghosting.com/"><strong>GoodGhosting</strong></a><strong> team understands that security is a key aspect of blockchain and DeFi applications. </strong>Thinking about potential bugs or attacks must not be restricted to the technical team only. It helps to have more people involved, with different perspectives and backgrounds. The more you actively think about security, the higher the chances of identifying potential exploits, eliminating or mitigating risks and catching bugs.</p><p>Although we put great effort into smart contract security ourselves, we also know we’re well off with getting help from security professionals. Shadowy super coders as they say, who can provide us with top notch advice to help us secure our smart contracts. And that’s exactly what we did.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/910/0*imiDtC8PWyFSHQ1m" /></figure><p>Today, we’re happy to announce that <strong>we completed the security audits of our smart contracts with both Quantstamp (on 12/Aug/2021) and Dedaub (on 06/Jul/2021). </strong>Both audit reports are publicly available as of today.</p><p>To dive into the technical details 👇</p><h3>🛡Security Audit Reports🛡</h3><h4>Scope</h4><p><a href="https://www.dedaub.com/">Dedaub</a> and <a href="https://quantstamp.com/">Quantstamp</a> audited our core smart contracts used in our savings pools on Polygon. The contracts are:</p><ul><li><a href="https://github.com/Good-Ghosting/goodghosting-smart-contracts/blob/master/contracts/GoodGhosting.sol">GoodGhosting.sol</a></li><li><a href="https://github.com/Good-Ghosting/goodghosting-smart-contracts/blob/master/contracts/GoodGhostingPolygon.sol">GoodGhostingPolygon.sol</a></li><li><a href="https://github.com/Good-Ghosting/goodghosting-smart-contracts/blob/master/contracts/GoodGhostingPolygonWhitelisted.sol">GoodGhostingPolygonWhitelisted.sol</a></li><li><a href="https://github.com/Good-Ghosting/goodghosting-smart-contracts/blob/master/contracts/MerkleDistributor.sol">MerkleDistributor.sol</a></li></ul><h3>1) Dedaub Audit Report</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_Xw32F5jq62w-4jO2GQxVw.png" /><figcaption><a href="https://www.dedaub.com/">Dedaub</a> logo</figcaption></figure><p><a href="https://www.dedaub.com/">Dedaub</a> audited our repository at commit <a href="https://github.com/Good-Ghosting/goodghosting-smart-contracts/tree/691ae203636d0f3ca4419fa81de26549bd754f24">691ae20</a> and <a href="https://github.com/Good-Ghosting/goodghosting-smart-contracts/tree/68764656a8e08c2be8fbe1e816307d4a0ac79b56">6876465</a> (review after issues were addressed).</p><p>The final <a href="https://docs.goodghosting.com/goodghosting/security">security audit report</a> was issued by <a href="https://www.dedaub.com/">Dedaub</a> on 06/Jul/2021 and the summary of findings is available below. Please refer to the <a href="https://docs.goodghosting.com/goodghosting/security">audit report</a> for <a href="https://www.dedaub.com/">Dedaub</a>’s definition of each severity level.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/432/1*rWYtHdaajm9vBYoTnnpnVQ.png" /><figcaption>Summary of findings reported by Dedaub, categorized by severity and resolution status</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/330/0*GkCspgY_pGXTkYyQ" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/491/0*Thnl3qz-_WBfk6t8" /></figure><h4>Reported Issues</h4><p>You’ll find below a summary of the issues reported by <a href="https://www.dedaub.com/">Dedaub</a>. Please refer to the <a href="https://docs.goodghosting.com/goodghosting/security">audit report</a> for more details about each reported issue.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/925/1*DZcpqc2FW9Uqad4LPXSTcg.png" /></figure><h3>2) Quantstamp Audit Report</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/583/1*jxMbJpARqz0eHZbtGM97Yg.jpeg" /><figcaption>Quantstamp <a href="https://certificate.quantstamp.com/full/good-ghosting-core-files-audit">audit seal</a></figcaption></figure><p>Quantstamp audited our repository at commit <a href="https://github.com/Good-Ghosting/goodghosting-smart-contracts/tree/f1729c45601e62e137bfea9d84024e0643081f0e">f1729c4</a> and <a href="https://github.com/Good-Ghosting/goodghosting-smart-contracts/tree/5e2eb637e7ccfe805809788c5ce62f088c759f82">5e2eb63</a> (review after issues were addressed).</p><p>The final <a href="https://docs.goodghosting.com/goodghosting/security">security audit report</a> was issued by <a href="https://quantstamp.com/">Quantstamp</a> on 12/Aug/2021 and the summary of findings is available below. Please refer to the <a href="https://docs.goodghosting.com/goodghosting/security">audit report</a> for <a href="https://quantstamp.com/">Quantstamp</a>’s definition of each severity level.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/565/1*a6WZYokKX1XeKHoaM1zp8g.png" /><figcaption>Summary of findings reported by Quantstamp, categorized by severity and resolution status</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/294/0*LKcnmCuzDOqmQQfL" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/487/0*ohpxcswCk-sg7sqq" /></figure><h4>Reported Issues</h4><p>You’ll find below a summary of the issues reported by <a href="https://quantstamp.com/">Quantstamp</a>. Please refer to the <a href="https://docs.goodghosting.com/goodghosting/security">audit report</a> for more details about each reported issue.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/914/1*3Nq0KzQ3HIriKvfztliiHw.png" /></figure><h3>Code Coverage</h3><p>This is the current code coverage for our unit tests:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*KUBFTLgkeIa-JCe6" /></figure><h3>Conclusion</h3><p>We’re really happy with the conclusion of the smart contract audits performed by <a href="https://www.dedaub.com/">Dedaub</a> and <a href="https://quantstamp.com/">Quantstamp</a>, and making the reports publicly available.</p><p>We were glad to read in the reports that our code is “mature and well-tested”, and that no critical vulnerabilities were discovered. Currently our smart contracts are being tested with five saving pools live on Polygon, as part of our <a href="https://docs.goodghosting.com/goodghosting/guarded-launch/launch">Guarded Launch approach</a>. During the audit process, we capped the number of participants and TVL (over $70,000 for the four pools combined) to lower risk. <strong>Now our audits are complete, we are excited to continue working to improve </strong><a href="https://goodghosting.com/"><strong>GoodGhosting</strong></a><strong> and onboard more users.</strong></p><p>🚨 Keep up to date with the latest pools and the developments on our <a href="https://discord.gg/qpWKgxsABJ">Discord channel</a>, on our <a href="https://twitter.com/goodghosting">Twitter</a> and/or<a href="http://eepurl.com/hvQ7j1"> mailing list</a> 🚨</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cc271fb9c268" width="1" height="1" alt=""><hr><p><a href="https://medium.com/halofi/goodghosting-smart-contract-audits-cc271fb9c268">GoodGhosting Smart Contract Audits</a> was originally published in <a href="https://medium.com/halofi">Halofi 😇 (prev. GoodGhosting)</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Intro to Challenges of Upgrading Smart Contracts in Ethereum with Solidity]]></title>
            <link>https://levelup.gitconnected.com/introduction-to-ethereum-smart-contract-upgradability-with-solidity-789cc497c56f?source=rss-2edbaa53f25e------2</link>
            <guid isPermaLink="false">https://medium.com/p/789cc497c56f</guid>
            <category><![CDATA[blockchain]]></category>
            <category><![CDATA[ethereum]]></category>
            <category><![CDATA[upgradability]]></category>
            <category><![CDATA[solidity]]></category>
            <category><![CDATA[smart-contract-blockchain]]></category>
            <dc:creator><![CDATA[Francis Odisi]]></dc:creator>
            <pubDate>Fri, 31 Jan 2020 02:30:09 GMT</pubDate>
            <atom:updated>2020-09-30T14:43:22.951Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*1mWjCXD5l7k3U-Wj" /><figcaption>Photo by <a href="https://unsplash.com/@ffstop?utm_source=medium&amp;utm_medium=referral">Fotis Fotopoulos</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>When developing software, we frequently need to release new versions to add new functionality or bug fixes. There’s no difference when it comes to smart contract development. Although, updating a smart contract to a new version is usually not as simple as updating other types of software of the same complexity.</p><p>Most Blockchains, especially public ones like Ethereum, implement the intrinsic concept of immutability, which in theory, does not allow anyone to change the blockchain’s “past”. The immutability is applied to all transactions in the blockchain, including transactions used to deploy smart contracts and the associated code. In other words, once the smart contract’s code is deployed to the blockchain, it will “live” forever “AS IS” — no one can change it. If a bug is found or a new functionality needs to be added, we cannot replace the code of a deployed contract.</p><p>If a smart contract is immutable, how are you able to upgrade it to newer versions? The answer lies in deploying a new smart contract to the blockchain.</p><p>But this approach raises a couple of challenges that need to be addressed. The most basic and common ones are:</p><ul><li>All users that use the smart contract need to reference the address of the new contract’s version</li><li>The first contract’s version should be disabled, enforcing every user to use the new version</li><li>Usually, you need to make sure the data (state) from the old version is migrated or somehow available to the new version. In the most simple scenario, this means you need to copy/migrate the state from the old version to the new contract’s version</li></ul><p>The sections below describe these challenges in more detail. To better illustrate it, we’ll use the two versions below of MySmartContract as a reference:</p><p>Version 1</p><pre>contract MySmartContract {<br>  uint32 public counter;</pre><pre>  constructor() public {<br>    counter = 0;<br>  }</pre><pre>  function incrementCounter() public {<br>    counter += 2; // This &quot;bug&quot; is intentional.<br>  }<br>}</pre><p>Version 2</p><pre>contract MySmartContract {<br>  uint32 public counter;</pre><pre>  constructor(uint32 _counter) public {<br>    counter = _counter;<br>  }</pre><pre>  function incrementCounter() public {<br>    counter++;<br>  }<br>}</pre><h4>Users to Reference the New Contract’s Address</h4><p>When deployed to the blockchain, every instance of the smart contract is assigned to a unique address. This address is used to reference the smart contract’s instance in order to invoke its methods and read/write data from/to the contract’s storage (state).</p><p>When you deploy an updated version of the contract to the blockchain, the new instance of the contract will be deployed at a new address. This new address is different from the first contract’s address. This means that all users, other smart contracts and/or dApps (decentralized applications) that interact with the smart contract will need to be updated so they use the address of the updated version. Spoiler: there are some options to avoid this issue, that you’ll see at the end of this section.</p><p>So, let’s consider the following scenario:</p><p>You created MySmartContract using the code of Version 1 above. It is deployed to the blockchain at address A1 (this is not a real Ethereum address - used only for illustration purposes). All users that want to interact with Version 1 need to use the address A1 to reference it.</p><p>Now, after a while, we noticed the bug in the method incrementCounter: it is incrementing the counter by 2, instead of incrementing it by 1. A fix is implemented, resulting in Version 2 of MySmartContract. This new contract&#39;s version is deployed to the blockchain at address D5. At this point, if a user wants to interact with Version 2, it needs to use the address D5, not A1. This is the reason why all users that are interacting with MySmartContract will need to update so they refer to the new address D5.</p><p>You probably agree that forcing users to update is not the best approach, considering that updating a smart contract’s version should be as transparent as possible to users using it.</p><p>There are different strategies that can be used to address this problem. Some design patterns like <a href="https://consensys.github.io/smart-contract-best-practices/software_engineering/#upgrading-broken-contracts">Registry</a>, different types of <a href="https://blog.openzeppelin.com/proxy-patterns/">Proxies</a> can be used to make it easier to upgrade and provide transparency to users. Another great option is to use the <a href="https://ens.domains/">Ethereum Name Service</a> and register a user-friend name that resolves to your contract’s address. With this option, users of the contract don’t need to know the contract’s address, only its user-friendly name. As a result, upgrading to a new address would be transparent to your contract’s users. The specific strategy adopted depends on the scenario in which the smart contract will be used.</p><h4>Disabling Old Versions of the Contract</h4><p>We learned in the section above that all users would need an update to use Version 2&#39;s address (D5) or our contract should implement some mechanism to make this process transparent to users. Despite that, if you&#39;re the owner of the contract, you probably want to enforce that all users use only the most up to date version D5. If a user inadvertently or not uses A1, you want to guarantee that Version 1 is deprecated and unavailable for usage.</p><p>In such scenarios, you could implement a technique to stop MySmartContract&#39;s Version 1. This technique is implemented by a Design Pattern named <a href="https://consensys.github.io/smart-contract-best-practices/software_engineering/#circuit-breakers-pause-contract-functionality">Circuit Breaker</a>. It&#39;s also commonly referred to as <em>Pausable Contracts</em> or <em>Emergency Stop</em>.</p><p>A Circuit Breaker, in general terms, stops a smart contract functionalities. Additionally, it can enable specific functionalities that will be available only when the contract is stopped. This pattern commonly implements some sort of <a href="https://solidity.readthedocs.io/en/latest/common-patterns.html#restricting-access">access restriction</a>, so only allowed actors (like an admin or owner) have the required permission to trigger the Circuit Breaker and stop the contract.</p><p>Some scenarios where this pattern can be used are:</p><ul><li>Stopping a contract’s functionalities when a bug is found</li><li>Stop some contract’s functionalities after a certain state is reached (frequently used together with a <a href="https://solidity.readthedocs.io/en/latest/common-patterns.html#state-machine">State Machine</a> pattern)</li><li>Stop the contract’s functionalities during upgrades processes, so external actors cannot change the contract’s state during the upgrade;</li><li>Stop a deprecated version of a contract after a new version is deployed</li></ul><p>Now let’s see how you could implement a Circuit Breaker to stop MySmartContract&#39;s incrementCounter function, so counter wouldn&#39;t change during the migration process. This modification would need to be in place in Version 1, when it was first deployed.</p><pre>// Version 1 implementing a Circuit Breaker with access restriction to owner<br>contract MySmartContract {<br>  uint32 public counter;<br>  bool private stopped = false;<br>  address private owner;</pre><pre>  /**<br>  @dev Checks if the contract is not stopped; reverts if it is.<br>  */<br>  modifier isNotStopped {<br>    require(!stopped, &#39;Contract is stopped.&#39;);<br>    _;<br>  }</pre><pre>  /**<br>  @dev Enforces the caller to be the contract&#39;s owner.<br>  */<br>  modifier isOwner {<br>    require(msg.sender == owner, &#39;Sender is not owner.&#39;);<br>    _;<br>  }</pre><pre>  constructor() public {<br>    counter = 0;<br>    // Sets the contract&#39;s owner as the address that deployed the contract.<br>    owner = msg.sender;<br>  }</pre><pre>  /**<br>  @notice Increments the contract&#39;s counter if contract is active.<br>  @dev It will revert if the contract is stopped. See modifier &quot;isNotStopped&quot;<br>   */<br>  function incrementCounter() isNotStopped public {<br>    counter += 2; // This is an intentional bug.<br>  }</pre><pre>  /**<br>  @dev Stops / Unstops the contract.<br>   */<br>  function toggleContractStopped() isOwner public {<br>      stopped = !stopped;<br>  }<br>}</pre><p>In the code above you can see that Version 1 of MySmartContract now implements a modifier isNotStopped. This modifier will revert the transaction if the contract is stopped. The function incrementCounter was changed to use the modifier isNotStopped, so it will only execute when the contract is NOT stopped.</p><p>With this implementation, right before the migration starts, the owner of the contract can invoke the function toggleContractStopped and stop the contract. Note that this function uses the modifier isOwner to restrict access to the contract&#39;s owner.</p><p>To learn more about Circuit Breakers, make sure you check Consensys’ post about <a href="https://consensys.github.io/smart-contract-best-practices/software_engineering/#circuit-breakers-pause-contract-functionality">Circuit Breakers</a> and OpenZeppelin’s reference implementation of <a href="https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/lifecycle/Pausable.sol">Pausable</a> contracts.</p><h4>Contract’s Data (State) Migration</h4><p>Most smart contracts need to keep some sort of state in its internal storage. The number of state variables required by each contract varies greatly depending on the use case. In our example, the original MySmartContract&#39;s Version 1 has a single state variable counter.</p><p>Now consider that Version 1 of MySmartContract has been in use for a while. By the time you find the bug in incrementCounter function, the value of counter is already at 100. This scenario would raise some questions:</p><ul><li>What will you do with the state of MySmartContract Version 2?</li><li>Can you reset the counter to 0 (zero) in Version 2 or should you migrate the state from Version 1 to make sure counter is initialized with 100 in Version 2?</li></ul><p>The answers to these questions will depend on the use case. In the example of this article, which is a really simple scenario and counter has no important usage, you wouldn&#39;t have any issues if counter is reset to 0. But, this is not the desired approach in most cases.</p><p>Let’s suppose you cannot reset the value to 0 and need to set counter to 100 in Version 2. In a simple contract as MySmartContract this wouldn&#39;t be difficult. You could change the constructor of Version 2 to receive the initial value of counter as a parameter. At deployment, you would pass the value 100 to the constructor, and this would solve your problem.</p><p>After implementing this approach, the constructor of MySmartContract Version 2 would look like this:</p><pre>constructor(uint32 _counter) public {<br>    counter = _counter;<br>  }</pre><p>If your use case is as simple as presented above (or similar), this is probably the way to go from a data migration perspective. The complexity of implementing other approaches wouldn’t be worth it. But, bear in mind that most production-ready smart contracts are not as simple as MySmartContract and frequently have a more complex state.</p><p>Now consider a contract that uses multiple <a href="https://solidity.readthedocs.io/en/latest/types.html#structs">structs</a>, <a href="https://solidity.readthedocs.io/en/latest/types.html#mapping-types">mappings</a>, and <a href="https://solidity.readthedocs.io/en/latest/types.html#arrays">arrays</a>. If you need to copy data between contract versions with such complex storage, you would probably face one or more the challenges below:</p><ul><li>A bunch of transactions to be processed on the blockchain, which may take a considerable amount of time, depending on the data set</li><li>Additional code to handle reading data from “Version 1&quot; and writing it to &quot;Version 2&quot; (unless done manually)</li><li>Spend real money to pay for gas. Remember that you need to pay gas to process transactions in the blockchain. According to the <a href="https://ethereum.github.io/yellowpaper/paper.pdf">Ethereum Yellow Paper — Appendix G. Fee Schedule</a>, the SSTORE operation, upcode used to write data to Ethereum, costs 20000 <a href="https://solidity.readthedocs.io/en/latest/introduction-to-smart-contracts.html#gas">gas units</a> <em>&quot;when the storage value is set to non-zero from zero&quot;</em> and 5000 <a href="https://solidity.readthedocs.io/en/latest/introduction-to-smart-contracts.html#gas">gas units</a> <em>&quot;when storage value’s zeroness remains unchanged&quot;</em>.</li><li>Freeze Version 1&#39;s state by using some mechanism (like a Circuit Breaker) to make sure no more data is appended to Version 1 during the migration.</li><li>Implement access restriction mechanisms to avoid external parties (not related to the migration) from invoking functions of Version 2 during the migration. This would be required to make sure Version 1&#39;s data could be copied/migrated to Version 2 without being compromised and/or corrupted in Version 2;</li></ul><p>In contracts with more complex state, the work required to perform an upgrade is quite significant and can incur considerable gas costs to copy data over the blockchain. Using <a href="https://solidity.readthedocs.io/en/latest/contracts.html#libraries">Libraries</a> and <a href="https://blog.openzeppelin.com/proxy-patterns/">Proxies</a> can help you develop smart contracts that are easier to upgrade. With this approach, the data would be kept in a contract that stores the state but bears no logic (<em>state contract</em>). The second contract or library implements the logic, but bears no state (<em>logic contract</em>). So when a bug is found in the logic, you only need to upgrade the <em>logic contract</em>, without worrying about migrating the state stored in the <em>state contract</em> (see <em>Note</em> below).</p><p><em>Note:</em> This approach generally uses <a href="https://solidity.readthedocs.io/en/latest/introduction-to-smart-contracts.html#delegatecall-callcode-and-libraries">Delegatecall</a>. The <em>state contract</em> invokes the functions in the <em>logic contract</em> using <em>delegatecall</em>. The <em>logic contract</em> then executes its logic in the context of <em>state contract</em>, which means that <em>“storage, current address and balance still refer to the calling contract, only the code is taken from the called address.”</em> (from Solidity docs referenced above).</p><h3>Making MySmartContract Easier to Upgrade</h3><p>Below you can see how Version 1 and Version 2 would look like if we implement the changes described here in this article. It&#39;s important to mention again that the strategies used for MySmartContract are acceptable considering its simplicity: state variables and logic.</p><p>First, let’s see Version 1 changes:</p><p>Version 1 — Without Upgradable Mechanisms</p><pre>contract MySmartContract {<br>  uint32 public counter;</pre><pre>  constructor() public {<br>    counter = 0;<br>  }</pre><pre>  function incrementCounter() public {<br>    counter += 2; // This &quot;bug&quot; is intentional.<br>  }<br>}</pre><p>In the code below, Version 1 implements a <a href="https://consensys.github.io/smart-contract-best-practices/software_engineering/#circuit-breakers-pause-contract-functionality">Circuit Breaker</a> with an <a href="https://solidity.readthedocs.io/en/latest/common-patterns.html#restricting-access">Access Restriction</a> mechanism that allows the owner to stop the contract once it is deprecated.</p><p>Version 1 — With Deprecation Mechanism</p><pre>contract MySmartContract {<br>  uint32 public counter;<br>  bool private stopped = false;<br>  address private owner;</pre><pre>  /**<br>  @dev Checks if the contract is not stopped; reverts if it is.<br>  */<br>  modifier isNotStopped {<br>    require(!stopped, &#39;Contract is stopped.&#39;);<br>    _;<br>  }</pre><pre>  /**<br>  @dev Enforces the caller to be the contract&#39;s owner.<br>  */<br>  modifier isOwner {<br>    require(msg.sender == owner, &#39;Sender is not owner.&#39;);<br>    _;<br>  }</pre><pre>  constructor() public {<br>    counter = 0;<br>    // Sets the contract&#39;s owner as the address that deployed the contract.<br>    owner = msg.sender;<br>  }</pre><pre>  /**<br>  @notice Increments the contract&#39;s counter if contract is active.<br>  @dev It will revert is the contract is stopped. See modifier &quot;isNotStopped&quot;<br>   */<br>  function incrementCounter() isNotStopped public {<br>    counter += 2; // This is an intentional bug.<br>  }</pre><pre>  /**<br>  @dev Stops / Unstops the contract.<br>   */<br>  function toggleContractStopped() isOwner public {<br>      stopped = !stopped;<br>  }<br>}</pre><p>Now let’s see how Version 2 would look like: Version 2 - Without Upgradable Mechanisms</p><pre>contract MySmartContract {<br>  uint32 public counter;</pre><pre>  constructor(uint32 _counter) public {<br>    counter = _counter;<br>  }</pre><pre>  function incrementCounter() public {<br>    counter++;<br>  }<br>}</pre><p>In the code below Version 2 implements the same <a href="https://consensys.github.io/smart-contract-best-practices/software_engineering/#circuit-breakers-pause-contract-functionality">Circuit Breaker</a> and <a href="https://solidity.readthedocs.io/en/latest/common-patterns.html#restricting-access">Access Restriction</a> mechanisms as Version 1. In addition, it implements a constructor that allows setting the initial value of counter during deployment. This mechanism can be used, which can be used during an upgrade to copy data from an old version.</p><p>Version 2 — With Simple Upgradable Mechanism</p><pre>contract MySmartContract {<br>  uint32 public counter;<br>  bool private stopped = false;<br>  address private owner;</pre><pre>  /**<br>  @dev Checks if the contract is not stopped; reverts if it is.<br>  */<br>  modifier isNotStopped {<br>    require(!stopped, &#39;Contract is stopped.&#39;);<br>    _;<br>  }</pre><pre>  /**<br>  @dev Enforces the caller to be the contract&#39;s owner.<br>  */<br>  modifier isOwner {<br>    require(msg.sender == owner, &#39;Sender is not owner.&#39;);<br>    _;<br>  }</pre><pre>  constructor(uint32 _counter) public {<br>    counter = _counter; // Allows setting counter&#39;s initial value on deployment.<br>    // Sets the contract&#39;s owner as the address that deployed the contract.<br>    owner = msg.sender;<br>  }</pre><pre>  /**<br>  @notice Increments the contract&#39;s counter if contract is active.<br>  @dev It will revert is the contract is stopped. See modifier &quot;isNotStopped&quot;<br>   */<br>  function incrementCounter() isNotStopped public {<br>    counter++; // Fixes bug introduced in version 1.<br>  }</pre><pre>  /**<br>  @dev Stops / Unstops the contract.<br>   */<br>  function toggleContractStopped() isOwner public {<br>      stopped = !stopped;<br>  }<br>}</pre><p>Although the changes above implement some mechanisms that help upgrading smart contracts, the first challenge described in the beginning of this article, <em>Users to Reference the New Contract’s Address</em>, is not solved with these simple techniques. More advanced patterns like <a href="https://blog.openzeppelin.com/proxy-patterns/">Proxies</a> and <a href="https://consensys.github.io/smart-contract-best-practices/software_engineering/#upgrading-broken-contracts">Registry</a>, or using the <a href="https://ens.domains/">ENS</a> to register a user-friendly name to your contract, would be required to avoid all users from upgrading to reference the new address of Version 2.</p><h3>Conclusion</h3><p>The principle of upgradable smart contracts is described in the Ethereum white paper’s <a href="https://github.com/ethereum/wiki/wiki/White-Paper#decentralized-autonomous-organizations">DAO section</a> that reads:</p><p>“*Although code is theoretically immutable, one can easily get around this and have de-facto mutability by having chunks of the code in separate contracts, and having the address of which contracts to call stored in the modifiable storage. *”</p><p>Although it is achievable, upgrading smart contracts can be quite challenging. The immutability of the blockchain adds more complexity to smart contract’s upgrades because it forces you to carefully analyze the scenario in which the smart contract will be used, understand the available mechanisms, and then decide which mechanisms are a good fit to your contract, so a potential and probable upgrade will be smooth.</p><p>Smart Contract upgradability is an active area of research. Related patterns, mechanisms and best practices are still under continuous discussion and development. Using <em>Libraries</em> and some Design Patterns like <a href="https://consensys.github.io/smart-contract-best-practices/software_engineering/#circuit-breakers-pause-contract-functionality">Circuit Breaker</a>, <a href="https://solidity.readthedocs.io/en/latest/common-patterns.html#restricting-access">Access Restriction</a>, <a href="https://blog.openzeppelin.com/proxy-patterns/">Proxies</a> and <a href="https://consensys.github.io/smart-contract-best-practices/software_engineering/#upgrading-broken-contracts">Registry</a> can help you to tackle some of the challenges. However, in more complex scenarios, these mechanisms alone may not be able to address all the issues, and you may need to consider more complex patterns like <a href="https://blog.colony.io/writing-upgradeable-contracts-in-solidity-6743f0eecc88/">Eternal Storage</a>, not mentioned in this article.</p><p>You can check the full source code, including related unit tests (not mentioned in this article for simplicity reasons) in this <a href="https://github.com/fodisi/solidity-design-patterns">github repository</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=789cc497c56f" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/introduction-to-ethereum-smart-contract-upgradability-with-solidity-789cc497c56f">Intro to Challenges of Upgrading Smart Contracts in Ethereum with Solidity</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>