<?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:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:googleplay="http://www.google.com/schemas/play-podcasts/1.0"><channel><title><![CDATA[Aspiring web3 dev]]></title><description><![CDATA[Documenting my path to become a web3 developer.]]></description><link>https://journal0xrusowsky.substack.com</link><image><url>https://substackcdn.com/image/fetch/$s_!ZLUc!,w_256,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fjournal0xrusowsky.substack.com%2Fimg%2Fsubstack.png</url><title>Aspiring web3 dev</title><link>https://journal0xrusowsky.substack.com</link></image><generator>Substack</generator><lastBuildDate>Mon, 03 Aug 2026 16:41:45 GMT</lastBuildDate><atom:link href="https://journal0xrusowsky.substack.com/feed" rel="self" type="application/rss+xml"/><copyright><![CDATA[0xRusowsky]]></copyright><language><![CDATA[en]]></language><webMaster><![CDATA[journal0xrusowsky@substack.com]]></webMaster><itunes:owner><itunes:email><![CDATA[journal0xrusowsky@substack.com]]></itunes:email><itunes:name><![CDATA[0xRusowsky]]></itunes:name></itunes:owner><itunes:author><![CDATA[0xRusowsky]]></itunes:author><googleplay:owner><![CDATA[journal0xrusowsky@substack.com]]></googleplay:owner><googleplay:email><![CDATA[journal0xrusowsky@substack.com]]></googleplay:email><googleplay:author><![CDATA[0xRusowsky]]></googleplay:author><itunes:block><![CDATA[Yes]]></itunes:block><item><title><![CDATA[Blockchain Oracles]]></title><description><![CDATA[Artemis week-7]]></description><link>https://journal0xrusowsky.substack.com/p/blockchain-oracles</link><guid isPermaLink="false">https://journal0xrusowsky.substack.com/p/blockchain-oracles</guid><dc:creator><![CDATA[0xRusowsky]]></dc:creator><pubDate>Fri, 26 Aug 2022 08:31:39 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/h_600,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F027c8a2a-c2aa-4035-b3fd-7cee8181aff0_2950x1554.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>We are almost there, with only 2 more weeks ahead of us. This will most likely be the last week with a dedicated journal post. Next time I will either do a recap/review of the course or go over my course project (maybe I'm inspired and I can even ship 2 articles in 1 week).</p><p>Same trend as last week. We continued working on our course projects and we also had "crypto talks", this time we learned about front-end development tailored to the web3 needs, and some entrepreneurship talks.</p><p>As per the regular lectures, this week we learned how blockchain oracles work and some of the alternatives most broadly used in the industry.</p><h1>The need for oracles</h1><p>Blockchains are closed distributed systems. As we have previously seen, this means that each network node has to deterministically find the same output for a given input. Some of the most valuable blockchain properties that arise from these designs are its decentralization and mitigation of network downtime, the trustable consensus validation of transactions, and the prevention of&nbsp;double-spending.</p><p>Nevertheless, all these features have some tradeoffs. Since smart contracts are unable to perform calls to external APIs, the network cannot natively access external data. On top of that, it is impossible to generate on-chain cryptographically secure random numbers within smart contracts.</p><p>As you can imagine, oracles are the answer to these problems. Blockchain oracles are third-party services that bring real-world, off-chain information into the network by verifying and relaying the data. Since they bridge the on-chain and the off-chain world, they are known as blockchain middleware. Oracles solve this problem by posting the data on the blockchain. So any Ethereum node replaying the transaction will use the same immutable data that's posted for all to see. To do this, an oracle is typically made up of a smart contract and some off-chain components that can query APIs, then periodically send transactions to update the smart contract's data.</p><p>But fetching off-chain data is just the first part of the problem. Ideally, these oracles should be resistant to manipulation, provide accurate data, and be continuously updated with the latest information. As you can imagine, these properties cannot be ensured by a single (or centralized) oracle provider, since it could end up being hacked, outdated, or disconnected.</p><h1>Anatomy of a decentralized oracle</h1><p>High-level, a decentralized oracle is a network of independent nodes that provide on-chain data. Where the data of each individual node is aggregated and deterministically validated by the network.</p><p>To ensure data accuracy and try to prevent data manipulation, oracles networks should have a decentralized approach in each of its layers:</p><ul><li><p><strong>Data source:</strong> An oracle is only as secure as its data sources. To ensure information accuracy, data is fetched from multiple, and independent, data sources. The key to having accurate price data (one of the most common applications for oracles) is to have full market coverage to prevent data manipulation or inefficiencies due to low volumes.</p></li><li><p><strong>Node operator:</strong> As explained before, any decentralized oracle network should have several independent nodes fetching data. If node operators source price data from multiple independent data sources, they can take statistically representative measures such as the median to mitigate outliers and API downtime.</p></li><li><p><strong>Oracle network:</strong> An oracle network defines how the collection of nodes works together to create a single on-chain reference data point. The most common form of aggregation is taking the median of the reported values once a minimum number of nodes have responded to the request.</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!fpTR!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ffaf45165-b6a3-405f-b6e4-036b6b072274_1133x417.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!fpTR!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ffaf45165-b6a3-405f-b6e4-036b6b072274_1133x417.png 424w, https://substackcdn.com/image/fetch/$s_!fpTR!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ffaf45165-b6a3-405f-b6e4-036b6b072274_1133x417.png 848w, https://substackcdn.com/image/fetch/$s_!fpTR!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ffaf45165-b6a3-405f-b6e4-036b6b072274_1133x417.png 1272w, https://substackcdn.com/image/fetch/$s_!fpTR!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ffaf45165-b6a3-405f-b6e4-036b6b072274_1133x417.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!fpTR!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ffaf45165-b6a3-405f-b6e4-036b6b072274_1133x417.png" width="1200" height="441.65931156222416" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/faf45165-b6a3-405f-b6e4-036b6b072274_1133x417.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:417,&quot;width&quot;:1133,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:198025,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!fpTR!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ffaf45165-b6a3-405f-b6e4-036b6b072274_1133x417.png 424w, https://substackcdn.com/image/fetch/$s_!fpTR!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ffaf45165-b6a3-405f-b6e4-036b6b072274_1133x417.png 848w, https://substackcdn.com/image/fetch/$s_!fpTR!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ffaf45165-b6a3-405f-b6e4-036b6b072274_1133x417.png 1272w, https://substackcdn.com/image/fetch/$s_!fpTR!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ffaf45165-b6a3-405f-b6e4-036b6b072274_1133x417.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Chainlink decentralized price feed network</figcaption></figure></div><p></p><h3>Architecture</h3><p>For the network to fetch data, the interested parties (clients) will have to trigger a data request to the oracle network. The chain of action usually works as follows:</p><p>1.  A client calls the oracle smart contract to create a new data request.</p><p>2.  The oracle contract emits a log with a new event request.</p><p>3.  All the off-chain services that are subscribed to these logs (usually using something like the JSON-RPC&nbsp;<code>eth_subscribe</code>&nbsp;command) receive the event.</p><p>4.  The off-chain services proceed to do some tasks as defined by the log.</p><p>5.  The off-chain services respond with the data requested in a secondary transaction to the smart contract.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!ta9h!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb6d52efe-1870-4117-8267-62f72b656205_1400x1093.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ta9h!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb6d52efe-1870-4117-8267-62f72b656205_1400x1093.png 424w, https://substackcdn.com/image/fetch/$s_!ta9h!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb6d52efe-1870-4117-8267-62f72b656205_1400x1093.png 848w, https://substackcdn.com/image/fetch/$s_!ta9h!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb6d52efe-1870-4117-8267-62f72b656205_1400x1093.png 1272w, https://substackcdn.com/image/fetch/$s_!ta9h!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb6d52efe-1870-4117-8267-62f72b656205_1400x1093.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!ta9h!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb6d52efe-1870-4117-8267-62f72b656205_1400x1093.png" width="1400" height="1093" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/b6d52efe-1870-4117-8267-62f72b656205_1400x1093.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1093,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!ta9h!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb6d52efe-1870-4117-8267-62f72b656205_1400x1093.png 424w, https://substackcdn.com/image/fetch/$s_!ta9h!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb6d52efe-1870-4117-8267-62f72b656205_1400x1093.png 848w, https://substackcdn.com/image/fetch/$s_!ta9h!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb6d52efe-1870-4117-8267-62f72b656205_1400x1093.png 1272w, https://substackcdn.com/image/fetch/$s_!ta9h!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb6d52efe-1870-4117-8267-62f72b656205_1400x1093.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Architecture of a decentralized oracle.</figcaption></figure></div><p>From an oracle-user point of view, the implementation of oracles into their contracts is extremely easy and convenient. In fact, the most challenging part is the risk assessment behind the oracle that the user decides to choose. The following code snipped showcases how to create a request to the Chainlink price feed smart contract.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!ctfo!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe2e4ede4-6df1-4c3c-8061-8b82a83be34f_3258x1974.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ctfo!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe2e4ede4-6df1-4c3c-8061-8b82a83be34f_3258x1974.png 424w, https://substackcdn.com/image/fetch/$s_!ctfo!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe2e4ede4-6df1-4c3c-8061-8b82a83be34f_3258x1974.png 848w, https://substackcdn.com/image/fetch/$s_!ctfo!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe2e4ede4-6df1-4c3c-8061-8b82a83be34f_3258x1974.png 1272w, https://substackcdn.com/image/fetch/$s_!ctfo!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe2e4ede4-6df1-4c3c-8061-8b82a83be34f_3258x1974.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!ctfo!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe2e4ede4-6df1-4c3c-8061-8b82a83be34f_3258x1974.png" width="1200" height="726.9230769230769" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/e2e4ede4-6df1-4c3c-8061-8b82a83be34f_3258x1974.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:882,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:604573,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!ctfo!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe2e4ede4-6df1-4c3c-8061-8b82a83be34f_3258x1974.png 424w, https://substackcdn.com/image/fetch/$s_!ctfo!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe2e4ede4-6df1-4c3c-8061-8b82a83be34f_3258x1974.png 848w, https://substackcdn.com/image/fetch/$s_!ctfo!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe2e4ede4-6df1-4c3c-8061-8b82a83be34f_3258x1974.png 1272w, https://substackcdn.com/image/fetch/$s_!ctfo!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe2e4ede4-6df1-4c3c-8061-8b82a83be34f_3258x1974.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">How to fetch data from Chainlink&#8217;s price feeds.</figcaption></figure></div><h1>TWAP Oracles</h1><p>Despite the previous definition that we have seen for blockchain oracles, one can understand a price oracle as any tool used to fetch price information about a given asset. With this definition in mind, we can see another subset of oracles that fully leaves on-chain. We will call them on-chain oracles.</p><p>Time-Weighted Average Price (TWAP) Oracles were developed by Uniswap, and still are the most adopted form of on-chain oracles.</p><p>As we have previously before, an oracle needs to ensure the quality of the provided data and be manipulation resistant. In order to get the latest spot price of an asset, one could just use the liquidity of an AMM pool as a price oracle by simply dividing the number of tokens currently residing within each side of the pool to get an exchange rate. Despite this approach would give us the best data freshness, it can easily be manipulated with the usage of tools such as <a href="https://learn.bybit.com/defi/what-is-a-flash-loan-attack">flash loans</a>.</p><p>So instead of just using the latest price, one can sacrifice some accuracy to increase manipulation resistance by using several data points instead of just one.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!FFC8!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F027c8a2a-c2aa-4035-b3fd-7cee8181aff0_2950x1554.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!FFC8!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F027c8a2a-c2aa-4035-b3fd-7cee8181aff0_2950x1554.png 424w, https://substackcdn.com/image/fetch/$s_!FFC8!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F027c8a2a-c2aa-4035-b3fd-7cee8181aff0_2950x1554.png 848w, https://substackcdn.com/image/fetch/$s_!FFC8!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F027c8a2a-c2aa-4035-b3fd-7cee8181aff0_2950x1554.png 1272w, https://substackcdn.com/image/fetch/$s_!FFC8!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F027c8a2a-c2aa-4035-b3fd-7cee8181aff0_2950x1554.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!FFC8!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F027c8a2a-c2aa-4035-b3fd-7cee8181aff0_2950x1554.png" width="1200" height="632.1428571428571" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/027c8a2a-c2aa-4035-b3fd-7cee8181aff0_2950x1554.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:767,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!FFC8!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F027c8a2a-c2aa-4035-b3fd-7cee8181aff0_2950x1554.png 424w, https://substackcdn.com/image/fetch/$s_!FFC8!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F027c8a2a-c2aa-4035-b3fd-7cee8181aff0_2950x1554.png 848w, https://substackcdn.com/image/fetch/$s_!FFC8!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F027c8a2a-c2aa-4035-b3fd-7cee8181aff0_2950x1554.png 1272w, https://substackcdn.com/image/fetch/$s_!FFC8!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F027c8a2a-c2aa-4035-b3fd-7cee8181aff0_2950x1554.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Uniswap TWAP calculation</figcaption></figure></div><h3>Limitations of the TWAP</h3><p>Despite TWAP oracles being useful and convenient, they have some limitations when compared to oracles that can fetch off-chain data such as Chainlink.</p><ul><li><p><strong>Freshness/Accuracy: </strong>By design, a TWAP is a lagging indicator that provides an average price taken over a period of time. While this approach is fine in periods of short volatility, it can become a significant issue during times of considering volatility because the TWAP desynchronizes from the real price. As previously explained, this manipulation-resistant approach is inversely correlated with data freshness, meaning safety needs to be prioritized over accuracy.</p></li><li><p><strong>Market Coverage:</strong> Another critical issue of TWAP oracles is that they are restricted to a single DEX. Note that this lack of market coverage makes it easier (it is cheaper since the liquidity of a DEX is lower than the overall market liquidity) for malicious actors to tamper with the price of a pool.</p></li><li><p><strong>Feed Diversity:</strong> Finally, and related to the previous point, since TWAP oracles are limited to the pools of a DEX, they can only provide coverage for on-chain assets (that's why we called them on-chain oracles).</p></li></ul><p>So it can be concluded that TWAP oracles are better suited for low volatility assets with high liquidity.</p><h1>Resources</h1><p>If you are interested in oracles and want to do a deep dive into how they work, I recommend going over the following resources:</p><ul><li><p><a href="https://medium.com/@pedrodc/implementing-a-blockchain-oracle-on-ethereum-cedc7e26b49e">Implementing a blockchain oracle by Pedro Costa</a></p></li><li><p><a href="https://samczsun.com/so-you-want-to-use-a-price-oracle">Demonstration of the dangers of poorly selected oracles by samczsun</a></p></li><li><p><a href="https://smartcontentpublication.medium.com/twap-oracles-vs-chainlink-price-feeds-a-comparative-analysis-8155a3483cbd">TWAP Oracles vs Chainlink Price feeds by SmartContent</a></p></li></ul><p></p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://journal0xrusowsky.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading Aspiring web3 dev! Subscribe for free.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><p></p>]]></content:encoded></item><item><title><![CDATA[Digital Signatures: ecrecover]]></title><description><![CDATA[Artemis Week-6]]></description><link>https://journal0xrusowsky.substack.com/p/digital-signatures-ecrecover</link><guid isPermaLink="false">https://journal0xrusowsky.substack.com/p/digital-signatures-ecrecover</guid><dc:creator><![CDATA[0xRusowsky]]></dc:creator><pubDate>Sun, 21 Aug 2022 23:18:01 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!zgCB!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F756edb89-2847-4dc0-9b5b-f69a0f986aac_2472x813.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>As we have already gathered the vast majority of theoretical knowledge that we needed to get started with our web3 journey, the ratio of new things per week started to go down. Don't get me wrong, we haven't stopped learning, we are just learning more complex stuff which requires more time to digest, and we also started working on a small project of our choice to practice as much as possible (I will probably do an extra post at the end of the course).</p><p>On top of that, we also had several crypto talks with amazing guests where we learned about Zero-Knowledge Proofs, Foundry, or the Graph protocol. <em>Yes, I know what you are thinking... I have a lot of interesting topics in my backlog and I should definitely produce more content!</em></p><p>So, now that you know what we have been up to, let's focus on digital signature verification and <code>ecrecover</code>.</p><h1>First things first. Digital Signatures</h1><p>Before figuring out what the hell <code>ecrecover</code> is, we need to master the basics. You have probably signed messages with your wallet, but have you ever stopped think about how does it actually work?</p><p>Digital signatures work similar to <a href="https://artemis-slides-preview.vercel.app/modules/m1/03_cryptographic_building_blocks.html#/4/0/2">asymmetric cryptography encryption</a> but in the opposite way.</p><p>In digital signatures, some data is signed by hashing the message with the sender&#8217;s private key (and obviously a hashing algorithm too). This produces a hash digest, that can only be recreated by using one of the keys in the key pair used by the sender. The recipient will then use the sender's public key to hash the message and assert that it matches the received hash digest. If that happens, then the receiver can be sure that the sender was the one who signed the message, and that the message wasn't altered.</p><h3>Elliptic Curve Digital Signature Algorithm</h3><p>If you ever hear about Elliptic Curve Digital Signature Algorithm (ECDSA), you should know that it is one of the more complex and efficient public key encryption algorithms. Keys are generated via elliptic curve cryptography, which are smaller than the average keys generated by digital signing algorithms. Yet they are able to offer the same level of security as any other digital signature algorithms.</p><h3>ecrecover</h3><p>Smart contracts on Ethereum have access to the built-in ECDSA signature verification algorithm via <code>ecrecover</code>. This system method verifies the integrity of any arbitrary signed data and recovers the address corresponding to the signer by using <code>v, r, s </code>and the signature hash.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!zgCB!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F756edb89-2847-4dc0-9b5b-f69a0f986aac_2472x813.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!zgCB!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F756edb89-2847-4dc0-9b5b-f69a0f986aac_2472x813.png 424w, https://substackcdn.com/image/fetch/$s_!zgCB!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F756edb89-2847-4dc0-9b5b-f69a0f986aac_2472x813.png 848w, https://substackcdn.com/image/fetch/$s_!zgCB!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F756edb89-2847-4dc0-9b5b-f69a0f986aac_2472x813.png 1272w, https://substackcdn.com/image/fetch/$s_!zgCB!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F756edb89-2847-4dc0-9b5b-f69a0f986aac_2472x813.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!zgCB!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F756edb89-2847-4dc0-9b5b-f69a0f986aac_2472x813.png" width="964" height="317.1401098901099" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/756edb89-2847-4dc0-9b5b-f69a0f986aac_2472x813.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:479,&quot;width&quot;:1456,&quot;resizeWidth&quot;:964,&quot;bytes&quot;:253691,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!zgCB!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F756edb89-2847-4dc0-9b5b-f69a0f986aac_2472x813.png 424w, https://substackcdn.com/image/fetch/$s_!zgCB!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F756edb89-2847-4dc0-9b5b-f69a0f986aac_2472x813.png 848w, https://substackcdn.com/image/fetch/$s_!zgCB!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F756edb89-2847-4dc0-9b5b-f69a0f986aac_2472x813.png 1272w, https://substackcdn.com/image/fetch/$s_!zgCB!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F756edb89-2847-4dc0-9b5b-f69a0f986aac_2472x813.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Example implementation of ecrecover.</figcaption></figure></div><p>Signatures are interesting because users can approve stuff without having to submit a transaction. This feature is useful because it can improve the UX and reduce user gas costs by using meta-transactions or ERC20-permit (more on this later).</p><p>Nevertheless, as usual, better UX and convenience generally comes at the expense of security. Because of that, it is important to follow the community-developed standards and best practices when dealing with digital signatures.</p><h1>Digital Signature Standards</h1><p>There are three main standards that can be used to sign data with an Ethereum key:</p><ul><li><p><strong>Eth_sign:</strong> The simplest, most powerful, and most dangerous way to sign a message. This method is used to sign arbitrary data, so it can lead to users signing data that enables a future transaction without further confirmation. This feature is extremely dangerous because users may sign those types of "uncovered" transactions without knowing it. Because of that, the usage of <code>eth_sign</code> is discouraged.</p></li><li><p><strong>Personal_sign:</strong> <a href="https://eips.ethereum.org/EIPS/eip-191">EIP-191</a> introduced a format to sign data and ensure that it wasn't a valid transaction. To implement this method in geth, they prefixed any signed data with&nbsp;<code>"\x19Ethereum Signed Message:\n" + len(message)</code>. Thus, making it impossible to use these signatures as valid transactions.</p></li></ul><p><code>"0x19 [1 byte version] [versidon specific data] [data to sign]".</code></p><ul><li><p><strong>signTypedData_v4:</strong> To help standardize signature usage in Ethereum,&nbsp;<a href="https://eips.ethereum.org/EIPS/eip-712">EIP-712</a>&nbsp;was introduced and is currently widely used and considered a standard that helps developers avoid most common security issues when dealing with signatures.</p></li></ul><h3>How to implement EIP-712</h3><p>The main goal of this EIP was to ensure that users understood what they were signing. Providing explicit context of the contract address and network. Finally, this EIP also aimed to ensure that each signature could only be used by the intended contract.</p><p>These requirements are achieved by combining a <em>Domain Separator hash</em> and a <em>Typed Structed Data hash</em>, and using them inside <code>ecrecover</code>. </p><ul><li><p><strong>Domain Separator Hash:</strong> The domain separator is a hash that is computed from the domain data and is used to prevent reusability of one signature in another dApp.</p></li><li><p><strong>Typed Structured Data:</strong> The data that needs to be signed is defined using structs and Solidity primary types.</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!fNmV!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0204054-8f30-4805-be7e-abdaf24ee9a9_2889x2031.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!fNmV!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0204054-8f30-4805-be7e-abdaf24ee9a9_2889x2031.png 424w, https://substackcdn.com/image/fetch/$s_!fNmV!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0204054-8f30-4805-be7e-abdaf24ee9a9_2889x2031.png 848w, https://substackcdn.com/image/fetch/$s_!fNmV!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0204054-8f30-4805-be7e-abdaf24ee9a9_2889x2031.png 1272w, https://substackcdn.com/image/fetch/$s_!fNmV!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0204054-8f30-4805-be7e-abdaf24ee9a9_2889x2031.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!fNmV!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0204054-8f30-4805-be7e-abdaf24ee9a9_2889x2031.png" width="1200" height="843.9560439560439" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/a0204054-8f30-4805-be7e-abdaf24ee9a9_2889x2031.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:1024,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:665145,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!fNmV!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0204054-8f30-4805-be7e-abdaf24ee9a9_2889x2031.png 424w, https://substackcdn.com/image/fetch/$s_!fNmV!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0204054-8f30-4805-be7e-abdaf24ee9a9_2889x2031.png 848w, https://substackcdn.com/image/fetch/$s_!fNmV!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0204054-8f30-4805-be7e-abdaf24ee9a9_2889x2031.png 1272w, https://substackcdn.com/image/fetch/$s_!fNmV!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0204054-8f30-4805-be7e-abdaf24ee9a9_2889x2031.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Example implementation of the EIP-712 signature standard.</figcaption></figure></div><h3>Meta-transactions</h3><p>A meta-transaction is a method for separating who pays for the gas of a transaction from who benefits from the transaction's execution.</p><p>A user signs the meta-transaction and then sends it to an operator (note that since there is no interaction with the blockchain, no gas is required to sign). The operator takes this signed meta-transaction and submits it to the blockchain, paying for the fees of the outer, regular transaction himself.</p><h3>ERC20-Permit</h3><p>The ERC20 standard requires two different transactions to transfer ERC20 tokens from an externally owned account (EOA) by using a third-party smartcontract:</p><ul><li><p>First, the&nbsp;EOA needs to perform an <code>ERC20.approve()</code> transaction to whitelist the third-party smartcontract.</p></li><li><p> Then, the EOA needs to call the function from the third-party smartcontract that triggers the <code>ERC20.transferFrom()</code> to transfer the tokens.</p></li></ul><p>With the implementation of <a href="https://eips.ethereum.org/EIPS/eip-2612">EIP-2612</a>, also known as ERC20-Permit, it is possible to simplify the process by using signature verification. With the use of ERC20-Permit, users can simply sign the allowance amount with their wallet, and the operator can take care of the rest.</p><h3>Vulnerabilities and Best Practices</h3><p><strong>Signature Reply Attack:</strong> Unless some precautions are taken, the same signature can be used multiple times to execute a function. To prevent these types of exploits, the use of nonces is extremely encouraged (note that in the EIP-712 implementation above I have already used a nonce to prevent this vulnerability).</p><p><strong>Signature Malleability:</strong> Due to the symmetric structure of elliptic curves, for every set of<code> v, r, s </code>there is another different set of<code> v, r, s </code>that also has the same relationship. This results in two valid signatures and allows malicious actors to compute a valid signature without the target user's private key. The easiest way to solve this vulnerability is by using <a href="https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/cryptography/ECDSA.sol#L156-L167">ECDS OpenZeppelin library</a>.</p><h1>Resources</h1><p>If you are not familiar with basic encryption, you should definitely check <a href="https://artemis-slides-preview.vercel.app/modules/m1/03_cryptographic_building_blocks.html">Artemis' public slides</a>. Shoutout to the team for progressively sharing the content of the course and making it accessible to anyone interested!</p><p>If you want to see implementations of <a href="https://soliditydeveloper.com/meta-transactions">meta-transactions</a> and <a href="https://soliditydeveloper.com/erc20-permit">erc20-permit</a>, you can look into these articles by soliditydeveloper.com.</p><p>Finally, if you want to get a really in-depth understanding of digital signatures and, especially, their potential vulnerabilities, I highly encourage you to check <a href="https://medium.com/immunefi/intro-to-cryptography-and-signatures-in-ethereum-2025b6a4a33d">this great article by Immunefi</a>, one of the leading bug bounty platforms.</p><p></p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://journal0xrusowsky.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading Aspiring web3 dev! Subscribe for free.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><p></p>]]></content:encoded></item><item><title><![CDATA[Delegatecall & Contract Upgradability]]></title><description><![CDATA[Artemis Week-5]]></description><link>https://journal0xrusowsky.substack.com/p/delegatecall-and-contract-upgradability</link><guid isPermaLink="false">https://journal0xrusowsky.substack.com/p/delegatecall-and-contract-upgradability</guid><dc:creator><![CDATA[0xRusowsky]]></dc:creator><pubDate>Sun, 07 Aug 2022 08:48:04 GMT</pubDate><enclosure url="https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/506e6725-2c53-4d3b-9085-bf8709f9d14f_1326x766.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h1>The journey continues. Artemis - Week 5</h1><p>After 4 weeks of classes, we are already past half of the course. Looking back we can see that we have already absorbed plenty of new knowledge and that the basics are already consolidated.</p><p>We started the week by doing a deep-dive on how memory and storage work, answering questions such as <em>How do the slots get filled? How does the pointer work? etc</em>. If you want to get more knowledge regarding these topics, make sure to check out the recommended resources at the end of this post.</p><p>But that's not all, we also focused <code>delegatecall</code>, a powerful, but also dangerous, type of external call that can be leveraged to achieve contract upgradability. Before we look into it, let me share with you an easy-to-implement gas optimization tip that you should definitely take into account when developing smartcontracts.</p><h1>Tips of the week</h1><p><em>Sponsored by&nbsp;<a href="https://twitter.com/GonRMagalhaes">Gon&#231;alo</a>.</em></p><h3>Slot Packing</h3><p>Slot Packing is an optimization technique that allows us to save gas by using the SSTORE opcode fewer times, thanks to consolidating multiple variables in the same storage slot (32 bytes).</p><p>Since the solidity compiler will automatically combine several variables in the same storage slot, you should try to initialize them in such a way that you use the minimum amount of slots.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Q2Pb!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F20d0802d-c194-4ce6-ae8e-4eeb5b92bcfa_1909x526.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Q2Pb!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F20d0802d-c194-4ce6-ae8e-4eeb5b92bcfa_1909x526.png 424w, https://substackcdn.com/image/fetch/$s_!Q2Pb!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F20d0802d-c194-4ce6-ae8e-4eeb5b92bcfa_1909x526.png 848w, https://substackcdn.com/image/fetch/$s_!Q2Pb!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F20d0802d-c194-4ce6-ae8e-4eeb5b92bcfa_1909x526.png 1272w, https://substackcdn.com/image/fetch/$s_!Q2Pb!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F20d0802d-c194-4ce6-ae8e-4eeb5b92bcfa_1909x526.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Q2Pb!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F20d0802d-c194-4ce6-ae8e-4eeb5b92bcfa_1909x526.png" width="1200" height="330.4945054945055" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/20d0802d-c194-4ce6-ae8e-4eeb5b92bcfa_1909x526.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:401,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:137481,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Q2Pb!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F20d0802d-c194-4ce6-ae8e-4eeb5b92bcfa_1909x526.png 424w, https://substackcdn.com/image/fetch/$s_!Q2Pb!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F20d0802d-c194-4ce6-ae8e-4eeb5b92bcfa_1909x526.png 848w, https://substackcdn.com/image/fetch/$s_!Q2Pb!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F20d0802d-c194-4ce6-ae8e-4eeb5b92bcfa_1909x526.png 1272w, https://substackcdn.com/image/fetch/$s_!Q2Pb!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F20d0802d-c194-4ce6-ae8e-4eeb5b92bcfa_1909x526.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Slot packing example.</figcaption></figure></div><h3>msg.sender  vs  tx.origin</h3><p>Both <code>tx.origin</code> and <code>msg.sender</code> are global variables that refer to the caller of a contract. Nevertheless, there are some key distinctions that need to be made.</p><p>Weather <code>tx.origin</code> refers to the original externally owned account that started the transaction, <code>msg.sender</code> indicates the immediate account (ether an EOA or another contract account) that invoked that interacted with the contract.</p><p>In order to prevent security issues, <code>tx.origin</code> should not be used for authorization. Instead, you should use <code>msg.sender</code>.</p><h1>Delegatecall</h1><p>A <code>delegatecall</code> is a type of external call that will execute some code of another smartcontract in the context of the caller. Therefore, despite some external code will run, the upgraded state variables will be the ones from the caller.</p><p>Because of the described properties, when working with contracts that use <code>delegatecall</code>, rather than looking at the variable names, we should focus on the slot positions that the external contract will modify in the caller's state.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!JHRv!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F06b55334-ee0d-43b6-bb4f-a871e4f83d3e_1584x571.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!JHRv!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F06b55334-ee0d-43b6-bb4f-a871e4f83d3e_1584x571.png 424w, https://substackcdn.com/image/fetch/$s_!JHRv!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F06b55334-ee0d-43b6-bb4f-a871e4f83d3e_1584x571.png 848w, https://substackcdn.com/image/fetch/$s_!JHRv!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F06b55334-ee0d-43b6-bb4f-a871e4f83d3e_1584x571.png 1272w, https://substackcdn.com/image/fetch/$s_!JHRv!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F06b55334-ee0d-43b6-bb4f-a871e4f83d3e_1584x571.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!JHRv!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F06b55334-ee0d-43b6-bb4f-a871e4f83d3e_1584x571.png" width="1200" height="432.6923076923077" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/06b55334-ee0d-43b6-bb4f-a871e4f83d3e_1584x571.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:525,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:112328,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!JHRv!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F06b55334-ee0d-43b6-bb4f-a871e4f83d3e_1584x571.png 424w, https://substackcdn.com/image/fetch/$s_!JHRv!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F06b55334-ee0d-43b6-bb4f-a871e4f83d3e_1584x571.png 848w, https://substackcdn.com/image/fetch/$s_!JHRv!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F06b55334-ee0d-43b6-bb4f-a871e4f83d3e_1584x571.png 1272w, https://substackcdn.com/image/fetch/$s_!JHRv!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F06b55334-ee0d-43b6-bb4f-a871e4f83d3e_1584x571.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Diagram showcasing how delegatecall and storage work.</figcaption></figure></div><p>To help you better understand how <code>delegatecall</code> works, and to also showcase some of its dangers, let's look into Ethernaut's <a href="https://ethernaut.openzeppelin.com/level/0x97E982a15FbB1C28F6B8ee971BEc15C78b3d263F">Level 16 challenge</a>.</p><p><em><strong>Note:</strong> Ethernaut is a set of security-focused challenges developed by OpenZeppelin, that help aspiring web3 developers understand all the intricacies of the EVM.</em></p><h3>Ethernaut Level 16</h3><p>In this challenge, our goal is to take over the ownership of the Persistence contract. To do so, we will exploit some vulnerabilities due to a poor understanding of the mechanics behind <code>delegatecall</code> and storage updates.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Pxxq!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fc7550c14-d98b-442a-98b6-df2245223a28_1396x1067.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Pxxq!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fc7550c14-d98b-442a-98b6-df2245223a28_1396x1067.png 424w, https://substackcdn.com/image/fetch/$s_!Pxxq!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fc7550c14-d98b-442a-98b6-df2245223a28_1396x1067.png 848w, https://substackcdn.com/image/fetch/$s_!Pxxq!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fc7550c14-d98b-442a-98b6-df2245223a28_1396x1067.png 1272w, https://substackcdn.com/image/fetch/$s_!Pxxq!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fc7550c14-d98b-442a-98b6-df2245223a28_1396x1067.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Pxxq!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fc7550c14-d98b-442a-98b6-df2245223a28_1396x1067.png" width="1200" height="917.1919770773638" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/c7550c14-d98b-442a-98b6-df2245223a28_1396x1067.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:1067,&quot;width&quot;:1396,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:284180,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Pxxq!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fc7550c14-d98b-442a-98b6-df2245223a28_1396x1067.png 424w, https://substackcdn.com/image/fetch/$s_!Pxxq!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fc7550c14-d98b-442a-98b6-df2245223a28_1396x1067.png 848w, https://substackcdn.com/image/fetch/$s_!Pxxq!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fc7550c14-d98b-442a-98b6-df2245223a28_1396x1067.png 1272w, https://substackcdn.com/image/fetch/$s_!Pxxq!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fc7550c14-d98b-442a-98b6-df2245223a28_1396x1067.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Code of the contract that we need to exploit on Ethernaut&#8217;s level 16.</figcaption></figure></div><p>As you can see, when using <code>delegatecall</code> to execute the function <code>setTime()</code> from <code>LibraryContract</code>, we will be actually modifying <code>slot0</code> of the <code>Preservation</code> contract (instead of the variable <code>storedTime</code> which points to <code>slot4</code>).</p><p>By properly encoding the attacker contract address into the <code>delegatecall</code> inputs, we can change <code>slot0</code> so that <code>timeZone1Library</code> points to the attacker contract. Once we have managed to do so, we can execute any code we want to take ownership of the <code>Preservation</code> contract.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!fI9f!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fda2676f5-844c-4c54-bf17-5af8aa2883fd_1302x615.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!fI9f!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fda2676f5-844c-4c54-bf17-5af8aa2883fd_1302x615.png 424w, https://substackcdn.com/image/fetch/$s_!fI9f!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fda2676f5-844c-4c54-bf17-5af8aa2883fd_1302x615.png 848w, https://substackcdn.com/image/fetch/$s_!fI9f!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fda2676f5-844c-4c54-bf17-5af8aa2883fd_1302x615.png 1272w, https://substackcdn.com/image/fetch/$s_!fI9f!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fda2676f5-844c-4c54-bf17-5af8aa2883fd_1302x615.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!fI9f!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fda2676f5-844c-4c54-bf17-5af8aa2883fd_1302x615.png" width="1200" height="566.8202764976959" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/da2676f5-844c-4c54-bf17-5af8aa2883fd_1302x615.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:615,&quot;width&quot;:1302,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:200237,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!fI9f!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fda2676f5-844c-4c54-bf17-5af8aa2883fd_1302x615.png 424w, https://substackcdn.com/image/fetch/$s_!fI9f!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fda2676f5-844c-4c54-bf17-5af8aa2883fd_1302x615.png 848w, https://substackcdn.com/image/fetch/$s_!fI9f!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fda2676f5-844c-4c54-bf17-5af8aa2883fd_1302x615.png 1272w, https://substackcdn.com/image/fetch/$s_!fI9f!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fda2676f5-844c-4c54-bf17-5af8aa2883fd_1302x615.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Foundry test where we can mimic the required steps to exploit the contract. This is just a snippet, the full code is linked at the end of the post.</figcaption></figure></div><p>Now that we know what the attack will look like, we just need to implement the attacker contract.</p><p>Let me remark that to take ownership of the target contract, we need to properly initialize the slot pointers of the <code>Attacker</code> contract (unlike <code>LibraryContract</code> did). Then, it is just a matter of modifying the value of <code>owner</code> (<code>slot3</code>).</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!WurU!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F243a33fe-1711-4004-8c4c-95c7647df6da_1896x783.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!WurU!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F243a33fe-1711-4004-8c4c-95c7647df6da_1896x783.png 424w, https://substackcdn.com/image/fetch/$s_!WurU!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F243a33fe-1711-4004-8c4c-95c7647df6da_1896x783.png 848w, https://substackcdn.com/image/fetch/$s_!WurU!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F243a33fe-1711-4004-8c4c-95c7647df6da_1896x783.png 1272w, https://substackcdn.com/image/fetch/$s_!WurU!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F243a33fe-1711-4004-8c4c-95c7647df6da_1896x783.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!WurU!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F243a33fe-1711-4004-8c4c-95c7647df6da_1896x783.png" width="1200" height="495.3296703296703" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/243a33fe-1711-4004-8c4c-95c7647df6da_1896x783.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:601,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:257302,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!WurU!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F243a33fe-1711-4004-8c4c-95c7647df6da_1896x783.png 424w, https://substackcdn.com/image/fetch/$s_!WurU!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F243a33fe-1711-4004-8c4c-95c7647df6da_1896x783.png 848w, https://substackcdn.com/image/fetch/$s_!WurU!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F243a33fe-1711-4004-8c4c-95c7647df6da_1896x783.png 1272w, https://substackcdn.com/image/fetch/$s_!WurU!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F243a33fe-1711-4004-8c4c-95c7647df6da_1896x783.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">The attacker contract sets the value of  <code>owner</code>  as the decoded input  <code>_owner.</code></figcaption></figure></div><p>You can find the full implementation of this challenge in <a href="https://github.com/0xRusowsky/ethernaut-with-foundry">my Github repo</a>.</p><p><em><strong>Note:</strong> At the time of writing, I am still going over the Ethernaut challenges, so you may not find all the solutions there.</em></p><p>If I had to emphasize something about this post it would be the importance of a mindful slot storage initialization in your contracts, especially if you are dealing with <code>delegatecall</code>.</p><h1>Recommended Resources</h1><p>I highly encourage you to go over the <a href="https://ethernaut.openzeppelin.com/">Ethernaut challenges</a>. You will be able to test your understanding of some basic solidity concepts while forcing yourself to code.</p><p>If you want to do a deep dive and better understand how storage works, I recommend checking <a href="https://betterprogramming.pub/all-about-solidity-data-locations-part-i-storage-e50604bfc1ad">this article by BetterProgramming</a>. The article is really long, but you don&#8217;t need to read it all at once or completely.</p><p>Although in this post we didn&#8217;t go over contract upgradability, we introduced <code>delegatecall</code>, which is the opcode that enables contract upgradability. If you want to get a better understanding of how do upgradability implementations look like, I encourage you to look into <a href="https://eips.ethereum.org/EIPS/eip-1822">the Universal Upgradable Proxy Standard</a> (also known as just UUPS).</p><p></p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://journal0xrusowsky.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading Aspiring web3 dev! </p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><p></p>]]></content:encoded></item><item><title><![CDATA[Security 101. Reentrancy Attacks]]></title><description><![CDATA[Artemis Week-4]]></description><link>https://journal0xrusowsky.substack.com/p/security-101-reentrancy-attacks</link><guid isPermaLink="false">https://journal0xrusowsky.substack.com/p/security-101-reentrancy-attacks</guid><dc:creator><![CDATA[0xRusowsky]]></dc:creator><pubDate>Sun, 31 Jul 2022 19:36:21 GMT</pubDate><enclosure url="https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/ed02583f-9ae2-4e42-83aa-879f13c8241c_1400x731.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h1>Code and more code. Artemis - Week 4</h1><p>After getting started with solidity and learning the basics, the next step is to get as familiar as possible with the language. The best way to do so is clear: code as much as possible.</p><p>This week we did both active coding tasks, where we had to solve several challenges, and hands-on sessions where we would see the teachers code.</p><p>By following this approach, we covered ERC20 and ERC721 tokens (implementing the code ourselves, but also using the OpenZeppelin and Solmate libraries), we created a wETH token contract, a simple staking contract, the bare bones of an AMM, and even a basic voting system based on NFTs.</p><p>Since there is a lot of ground to cover, a simple post wouldn&#8217;t be enough to go through these implementations. Instead, I recommend checking the <a href="https://github.com/OpenZeppelin/openzeppelin-contracts/tree/master/contracts/token/ERC20">ERC20</a> and <a href="https://github.com/OpenZeppelin/openzeppelin-contracts/tree/master/contracts/token/ERC721">ERC721</a> contracts from OpenZeppelin, since it is an easy way to familiarize yourself with these token standards.</p><p><em><strong>Note:</strong> <a href="https://github.com/transmissions11/solmate/tree/main/src/tokens">Solmate</a> is also great, but since they follow a minimalist design to optimize gas costs, it is less intuitive for someone new to solidity.</em></p><p>Because of that, this week we won&#8217;t be covering any exercises. Instead, we will do a deep dive on a typical security bug: reentrancy attacks.</p><p>Last week when going through the <a href="https://journal0xrusowsky.substack.com/i/65247420/send-vs-transfer-vs-call">differences between send, transfer, and call functions</a>, we saw that it is best to use <code>call()</code> in combination with Reentrancy Guard or the checks-effects-interactions pattern to protect your functions.</p><p>But, what is a reentrancy attack? What are these protection methods? Let&#8217;s dive into it.</p><h1>The Reentrancy Attack</h1><p>As a smart contract programmer, one of the first things that you should learn is that <strong>external calls to untrusted contracts can introduce several unexpected risks</strong> and errors. External calls may execute malicious code in your contracts (and also in any other contract that depends on yours), taking over the control flow, and modifying the data that your functions were expecting. As such, <strong>every external call should be treated as a potential security risk</strong>.</p><p>A classical exploit pattern that leverages external calls to drain a smart contract is the reentrancy attack. As described in the diagram below, this type of attack leverages a customized<code> fallback()</code> function to repeatedly call<code> withdraw()</code> before the user balance is updated. By doing so, the exploiter is able to effectively transfer all the deposited funds into the vulnerable contract to its own.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!9EDY!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fc889fbc8-ca9c-47cd-91fb-07fcd1ab987c_1518x801.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!9EDY!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fc889fbc8-ca9c-47cd-91fb-07fcd1ab987c_1518x801.png 424w, https://substackcdn.com/image/fetch/$s_!9EDY!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fc889fbc8-ca9c-47cd-91fb-07fcd1ab987c_1518x801.png 848w, https://substackcdn.com/image/fetch/$s_!9EDY!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fc889fbc8-ca9c-47cd-91fb-07fcd1ab987c_1518x801.png 1272w, https://substackcdn.com/image/fetch/$s_!9EDY!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fc889fbc8-ca9c-47cd-91fb-07fcd1ab987c_1518x801.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!9EDY!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fc889fbc8-ca9c-47cd-91fb-07fcd1ab987c_1518x801.png" width="1200" height="632.967032967033" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/c889fbc8-ca9c-47cd-91fb-07fcd1ab987c_1518x801.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:768,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:83571,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!9EDY!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fc889fbc8-ca9c-47cd-91fb-07fcd1ab987c_1518x801.png 424w, https://substackcdn.com/image/fetch/$s_!9EDY!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fc889fbc8-ca9c-47cd-91fb-07fcd1ab987c_1518x801.png 848w, https://substackcdn.com/image/fetch/$s_!9EDY!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fc889fbc8-ca9c-47cd-91fb-07fcd1ab987c_1518x801.png 1272w, https://substackcdn.com/image/fetch/$s_!9EDY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fc889fbc8-ca9c-47cd-91fb-07fcd1ab987c_1518x801.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Explanation of how reentrancy attacks work.</figcaption></figure></div><blockquote><p><em><strong>Note:</strong>  If you perform an external static call, you will not have to worry about reentrancy attacks, since won&#8217;t be able to perform any status changes.</em></p></blockquote><h3>Check-Effects-Interaction Pattern</h3><p>A best-practice programming pattern that prevents unexpected executions of a contract. This pattern ensures that the contract author controls all the state variables by following some design principles. The key idea behind the check-effects interaction pattern is that <strong>a contract should always check and modify the state variables before performing any external calls.</strong></p><p>Let&#8217;s break down the design principles into 3 different steps.</p><ul><li><p><strong>Checks: </strong>Validate whether the inputs are acceptable or not.  Invalid inputs can return <code>false</code> and be manually handled, or fail hard and ensure that the transaction reverts. Note that if the transaction reverts, the world state won&#8217;t suffer any changes.</p></li><li><p><strong>Effects: </strong>Update the contract to a new valid state, assuming that the subsequent interactions will succeed. This step protects the contract from re-entrance and race conditions.</p></li><li><p><strong>Interactions: </strong>Lastly, the contract should invoke any external functions or perform the low-level function <code>call()</code>. On a general basis, you should generally limit interactions to "untrusted" contracts. You <em>must </em>always check the result of those interactions.</p><p>If the called contract "hard fails" with <code>require()</code> or <code>revert()</code> then the whole transaction will revert. But, if the external call <code>returns(bool success)</code> then you need to check for the <code>false</code> condition and manually handle it.</p></li></ul><p>To better understand the described principles, let&#8217;s look into some examples.</p><p><strong>Example 1: Single-function vulnerability</strong></p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!9Ayr!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa884e71f-18e6-4fb4-9c23-f9f59a92fd10_1725x420.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!9Ayr!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa884e71f-18e6-4fb4-9c23-f9f59a92fd10_1725x420.png 424w, https://substackcdn.com/image/fetch/$s_!9Ayr!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa884e71f-18e6-4fb4-9c23-f9f59a92fd10_1725x420.png 848w, https://substackcdn.com/image/fetch/$s_!9Ayr!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa884e71f-18e6-4fb4-9c23-f9f59a92fd10_1725x420.png 1272w, https://substackcdn.com/image/fetch/$s_!9Ayr!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa884e71f-18e6-4fb4-9c23-f9f59a92fd10_1725x420.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!9Ayr!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa884e71f-18e6-4fb4-9c23-f9f59a92fd10_1725x420.png" width="1200" height="292.5824175824176" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/a884e71f-18e6-4fb4-9c23-f9f59a92fd10_1725x420.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:355,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:76729,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!9Ayr!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa884e71f-18e6-4fb4-9c23-f9f59a92fd10_1725x420.png 424w, https://substackcdn.com/image/fetch/$s_!9Ayr!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa884e71f-18e6-4fb4-9c23-f9f59a92fd10_1725x420.png 848w, https://substackcdn.com/image/fetch/$s_!9Ayr!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa884e71f-18e6-4fb4-9c23-f9f59a92fd10_1725x420.png 1272w, https://substackcdn.com/image/fetch/$s_!9Ayr!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa884e71f-18e6-4fb4-9c23-f9f59a92fd10_1725x420.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a><figcaption class="image-caption">Vulnerable contract. The code doesn&#8217;t stick to the check-effects-interaction pattern and it can be re-entered.</figcaption></figure></div><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!9snm!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff6d96bdd-7c88-4efb-a312-684540d76de6_1717x411.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!9snm!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff6d96bdd-7c88-4efb-a312-684540d76de6_1717x411.png 424w, https://substackcdn.com/image/fetch/$s_!9snm!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff6d96bdd-7c88-4efb-a312-684540d76de6_1717x411.png 848w, https://substackcdn.com/image/fetch/$s_!9snm!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff6d96bdd-7c88-4efb-a312-684540d76de6_1717x411.png 1272w, https://substackcdn.com/image/fetch/$s_!9snm!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff6d96bdd-7c88-4efb-a312-684540d76de6_1717x411.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!9snm!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff6d96bdd-7c88-4efb-a312-684540d76de6_1717x411.png" width="1200" height="287.6373626373626" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/f6d96bdd-7c88-4efb-a312-684540d76de6_1717x411.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:349,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:74693,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!9snm!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff6d96bdd-7c88-4efb-a312-684540d76de6_1717x411.png 424w, https://substackcdn.com/image/fetch/$s_!9snm!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff6d96bdd-7c88-4efb-a312-684540d76de6_1717x411.png 848w, https://substackcdn.com/image/fetch/$s_!9snm!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff6d96bdd-7c88-4efb-a312-684540d76de6_1717x411.png 1272w, https://substackcdn.com/image/fetch/$s_!9snm!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff6d96bdd-7c88-4efb-a312-684540d76de6_1717x411.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a><figcaption class="image-caption">Secure contract. The code updates the state variables before performing the external calls, ensuring that re-entrances are not possible.</figcaption></figure></div><p><strong>Example 2: Multi-function vulnerability</strong></p><p>Despite you may not be performing an external call directly in one of your functions, you may be doing so in an indirect manner by calling an internal function which, in its turn, performs the external call. Whenever you are facing this scenario, you should treat that internal function (the one which performs the external call) as an untrusted function. Therefore, you must stick to the check-effects-interaction pattern in both functions.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!CXMs!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9507d672-b6ff-480a-b7a4-9f6fd42d9394_1697x707.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!CXMs!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9507d672-b6ff-480a-b7a4-9f6fd42d9394_1697x707.png 424w, https://substackcdn.com/image/fetch/$s_!CXMs!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9507d672-b6ff-480a-b7a4-9f6fd42d9394_1697x707.png 848w, https://substackcdn.com/image/fetch/$s_!CXMs!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9507d672-b6ff-480a-b7a4-9f6fd42d9394_1697x707.png 1272w, https://substackcdn.com/image/fetch/$s_!CXMs!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9507d672-b6ff-480a-b7a4-9f6fd42d9394_1697x707.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!CXMs!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9507d672-b6ff-480a-b7a4-9f6fd42d9394_1697x707.png" width="1200" height="500.27472527472526" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/9507d672-b6ff-480a-b7a4-9f6fd42d9394_1697x707.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:607,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:155869,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!CXMs!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9507d672-b6ff-480a-b7a4-9f6fd42d9394_1697x707.png 424w, https://substackcdn.com/image/fetch/$s_!CXMs!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9507d672-b6ff-480a-b7a4-9f6fd42d9394_1697x707.png 848w, https://substackcdn.com/image/fetch/$s_!CXMs!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9507d672-b6ff-480a-b7a4-9f6fd42d9394_1697x707.png 1272w, https://substackcdn.com/image/fetch/$s_!CXMs!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9507d672-b6ff-480a-b7a4-9f6fd42d9394_1697x707.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Vulnerable contract. Despite withdrawReward() being secure, getFirstWithdrawlBonus() doesn&#8217;t stick to the check-effects-interaction pattern and it can be re-entered.</figcaption></figure></div><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!T-0_!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb04a8810-a0d2-4cb5-9666-aab35960dcab_1702x717.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!T-0_!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb04a8810-a0d2-4cb5-9666-aab35960dcab_1702x717.png 424w, https://substackcdn.com/image/fetch/$s_!T-0_!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb04a8810-a0d2-4cb5-9666-aab35960dcab_1702x717.png 848w, https://substackcdn.com/image/fetch/$s_!T-0_!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb04a8810-a0d2-4cb5-9666-aab35960dcab_1702x717.png 1272w, https://substackcdn.com/image/fetch/$s_!T-0_!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb04a8810-a0d2-4cb5-9666-aab35960dcab_1702x717.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!T-0_!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb04a8810-a0d2-4cb5-9666-aab35960dcab_1702x717.png" width="1200" height="505.2197802197802" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/b04a8810-a0d2-4cb5-9666-aab35960dcab_1702x717.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:613,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:156194,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!T-0_!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb04a8810-a0d2-4cb5-9666-aab35960dcab_1702x717.png 424w, https://substackcdn.com/image/fetch/$s_!T-0_!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb04a8810-a0d2-4cb5-9666-aab35960dcab_1702x717.png 848w, https://substackcdn.com/image/fetch/$s_!T-0_!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb04a8810-a0d2-4cb5-9666-aab35960dcab_1702x717.png 1272w, https://substackcdn.com/image/fetch/$s_!T-0_!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb04a8810-a0d2-4cb5-9666-aab35960dcab_1702x717.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Secure contract. The code updates all the relevant state variables before performing the external calls, ensuring that re-entrances are not possible.</figcaption></figure></div><h3>Reentrancy Guard</h3><p>Another common practice is to use the Mutex pattern, also known as Reentrancy Guard. This method leverages the usage of modifiers (in combination with some checks) to lock the contract and ensure that a given function cannot be reentered until finishes its execution and the contract is unlocked.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!LajB!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F1a7c77fb-2d82-4f00-949b-27f0d5911823_1539x617.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!LajB!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F1a7c77fb-2d82-4f00-949b-27f0d5911823_1539x617.png 424w, https://substackcdn.com/image/fetch/$s_!LajB!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F1a7c77fb-2d82-4f00-949b-27f0d5911823_1539x617.png 848w, https://substackcdn.com/image/fetch/$s_!LajB!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F1a7c77fb-2d82-4f00-949b-27f0d5911823_1539x617.png 1272w, https://substackcdn.com/image/fetch/$s_!LajB!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F1a7c77fb-2d82-4f00-949b-27f0d5911823_1539x617.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!LajB!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F1a7c77fb-2d82-4f00-949b-27f0d5911823_1539x617.png" width="1200" height="481.31868131868134" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/1a7c77fb-2d82-4f00-949b-27f0d5911823_1539x617.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:584,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:90041,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!LajB!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F1a7c77fb-2d82-4f00-949b-27f0d5911823_1539x617.png 424w, https://substackcdn.com/image/fetch/$s_!LajB!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F1a7c77fb-2d82-4f00-949b-27f0d5911823_1539x617.png 848w, https://substackcdn.com/image/fetch/$s_!LajB!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F1a7c77fb-2d82-4f00-949b-27f0d5911823_1539x617.png 1272w, https://substackcdn.com/image/fetch/$s_!LajB!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F1a7c77fb-2d82-4f00-949b-27f0d5911823_1539x617.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Safe contract. Despite the user balance being updated after the call, attackers won&#8217;t be able to re-enter the contract because of the modifier.</figcaption></figure></div><h1>Recommended Resources</h1><p>In case you want to use OpenZeppelin&#8217;s implementation of the Reentrancy Guard in your contracts, you can check their <a href="https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/security/ReentrancyGuard.sol">GitHub repo</a>.</p><p>You may have noticed the small warnings that appear vscode scripts whenever there is an external call. If you want to have them too  &#8213;despite being simple it can help you avoid mistakes&#8213;, install the <a href="https://consensys.net/diligence/blog/2019/10/solidity-visual-auditor-extension-for-vs-code/">Solidity Visual Developer extension</a> that Consensys developed. It has plenty of interesting features.</p><p></p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://journal0xrusowsky.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading Aspiring web3 dev! Subscribe for free here.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><p></p>]]></content:encoded></item><item><title><![CDATA[First interactions with Solidity]]></title><description><![CDATA[Artemis Week-3]]></description><link>https://journal0xrusowsky.substack.com/p/first-interactions-with-solidity</link><guid isPermaLink="false">https://journal0xrusowsky.substack.com/p/first-interactions-with-solidity</guid><dc:creator><![CDATA[0xRusowsky]]></dc:creator><pubDate>Sat, 23 Jul 2022 15:43:30 GMT</pubDate><enclosure url="https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/f662a508-add6-4b9d-8cfe-d283c5d50e3f_1203x796.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h1>Solidity Basics. Artemis Week-3</h1><p>After covering a lot of theoretical (but yet important) ground in the previous 2 weeks, we finally got into solidity. This week has been packed with basic concepts and plenty of exercises to get familiar with the language. We went through the different object types, functions, events, modifiers, inheritance, etc.</p><p>There are plenty of excellent online resources which are publically available. On top of that, they are in all sorts of formats, so you can check both written and video formats.</p><p>Because of that, instead of explaining all these basic concepts, I will try to explain a couple of interesting things worth sharing (i.e. security and gas optimization tips), and also go through my thought process when attempting to solve some exercises.</p><h1>Tips of the week</h1><p><em>Sponsored by <a href="https://twitter.com/GonRMagalhaes">Gon&#231;alo</a>.</em></p><h3><strong>send vs transfer vs call</strong></h3><p>Despite the 3 methods can be used to transfer ether, they have key distinctions that are worth knowing. <code>send()</code> and <code>transfer()</code> used to be recommended, because they would limit gas expenditure to 2300 gwei. With this amount of available gas, people were able to do minor fallback operations like emitting events or even updating storage, but it wasn't enough to reenter a contract. Nevertheless, this was never a good method to avoid reentrancy attacks, as gas costs can vary with protocol updates (i.e. the Istanbul fork increased the gas cost for the <code>SLOD</code> opcode).</p><p>Because of that, it is recommended to use the low-level function <code>call()</code>, which defaults to forward all the available gas. Then, you can use a Reentrancy Guard or the checks-effects-interactions pattern to protect your functions.</p><p><em><strong>Note:</strong> For a more detailed explanation, read <a href="https://consensys.net/diligence/blog/2019/09/stop-using-soliditys-transfer-now/">this article.</a></em></p><h3><strong>indexed event parameters vs non-indexed (regular)</strong></h3><p>Events with indexed parameters are easier to retrieve because they are specially indexed in the world state (in the <em>logsBloom</em>). Because of that, they are slightly more expensive than events with only regular parameters.</p><p>This explanation can be confirmed with this simple Foundry example:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!-8bL!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F580a2b6c-e1a4-4954-b8bc-12bd161ad759_616x148.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!-8bL!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F580a2b6c-e1a4-4954-b8bc-12bd161ad759_616x148.png 424w, https://substackcdn.com/image/fetch/$s_!-8bL!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F580a2b6c-e1a4-4954-b8bc-12bd161ad759_616x148.png 848w, https://substackcdn.com/image/fetch/$s_!-8bL!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F580a2b6c-e1a4-4954-b8bc-12bd161ad759_616x148.png 1272w, https://substackcdn.com/image/fetch/$s_!-8bL!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F580a2b6c-e1a4-4954-b8bc-12bd161ad759_616x148.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!-8bL!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F580a2b6c-e1a4-4954-b8bc-12bd161ad759_616x148.png" width="616" height="148" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/580a2b6c-e1a4-4954-b8bc-12bd161ad759_616x148.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:148,&quot;width&quot;:616,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!-8bL!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F580a2b6c-e1a4-4954-b8bc-12bd161ad759_616x148.png 424w, https://substackcdn.com/image/fetch/$s_!-8bL!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F580a2b6c-e1a4-4954-b8bc-12bd161ad759_616x148.png 848w, https://substackcdn.com/image/fetch/$s_!-8bL!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F580a2b6c-e1a4-4954-b8bc-12bd161ad759_616x148.png 1272w, https://substackcdn.com/image/fetch/$s_!-8bL!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F580a2b6c-e1a4-4954-b8bc-12bd161ad759_616x148.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a><figcaption class="image-caption">Gas comparison between indexed and non-indexed events.</figcaption></figure></div><p><em><strong>Note:</strong> Logs are composed of Topics (up to 4) and Data. One topic is reserved for the event signature, so you can have a maximum of 3 indexed parameters.</em></p><p><em>In the <a href="https://ethereum.github.io/yellowpaper/paper.pdf">Ethereum Yellow Paper</a>, it can be asserted that each topic costs 375gwei, and each byte in data costs 8gwei * numberBytes.</em></p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!o3bS!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff15d8b72-d48b-4597-bc78-fa956ac70652_1219x125.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!o3bS!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff15d8b72-d48b-4597-bc78-fa956ac70652_1219x125.png 424w, https://substackcdn.com/image/fetch/$s_!o3bS!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff15d8b72-d48b-4597-bc78-fa956ac70652_1219x125.png 848w, https://substackcdn.com/image/fetch/$s_!o3bS!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff15d8b72-d48b-4597-bc78-fa956ac70652_1219x125.png 1272w, https://substackcdn.com/image/fetch/$s_!o3bS!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff15d8b72-d48b-4597-bc78-fa956ac70652_1219x125.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!o3bS!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff15d8b72-d48b-4597-bc78-fa956ac70652_1219x125.png" width="1219" height="125" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/f15d8b72-d48b-4597-bc78-fa956ac70652_1219x125.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:125,&quot;width&quot;:1219,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:55821,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!o3bS!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff15d8b72-d48b-4597-bc78-fa956ac70652_1219x125.png 424w, https://substackcdn.com/image/fetch/$s_!o3bS!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff15d8b72-d48b-4597-bc78-fa956ac70652_1219x125.png 848w, https://substackcdn.com/image/fetch/$s_!o3bS!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff15d8b72-d48b-4597-bc78-fa956ac70652_1219x125.png 1272w, https://substackcdn.com/image/fetch/$s_!o3bS!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff15d8b72-d48b-4597-bc78-fa956ac70652_1219x125.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a><figcaption class="image-caption">Event gas costs as defined in the Ethereum Yellow Paper.</figcaption></figure></div><h1>Task. Creating a basic Crowdfunding Platform</h1><p><em>Implement the backbone for a simple Crowdfunding platform with the following requirements:</em></p><ul><li><p><em>Anyone can create a campaign to acquire funding for their project.</em></p></li><li><p><em>Each campaign must, at least, have:</em></p><p><em>- A name.</em></p><p><em>- An owner.</em></p><p><em>- A campaign type (start-up or charity).</em></p><p><em>- A funding goal.</em></p><p><em>- A time limit to reach it, which can't be higher than 60 days.</em></p></li><li><p><em>All of the above properties must be defined at campaign creation, and can't be updated.</em></p></li><li><p><em>Until the time limit of a given campaign is reached, anyone can fund it by sending Ether. After the time limit is reached, it can't receive any more funds.</em></p></li><li><p><em>The owner of a campaign can only withdraw the raised Eth after the time limit is reached.</em></p></li><li><p><em>After the owner withdraws the funds, campaigns should be marked either as Fully-Funded (if raised &gt;= funding goal) or Partially-Funded (if raised &lt; funding goal).</em></p></li><li><p><em>An owner must be able to cancel a campaign. If this happens that campaign can't receive any more Ether.</em></p></li><li><p><em>Implement all the relevant events.</em></p></li></ul><h3>Solution Design</h3><p>When having an idea, the first thing you need to figure out is the architectural design that your solution should have. In this case, the most reasonable thing to do, in my eyes, was to have an independent contract for each campaign, so owners could directly interact with it and have full ownership. Because of that, I decided to create a factory contract <em>CampaignFactory</em> and a campaign contract called <em>Campaign.</em></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!IpdT!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fafb33e27-1fae-4d6f-b485-981f44f94660_1787x670.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!IpdT!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fafb33e27-1fae-4d6f-b485-981f44f94660_1787x670.png 424w, https://substackcdn.com/image/fetch/$s_!IpdT!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fafb33e27-1fae-4d6f-b485-981f44f94660_1787x670.png 848w, https://substackcdn.com/image/fetch/$s_!IpdT!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fafb33e27-1fae-4d6f-b485-981f44f94660_1787x670.png 1272w, https://substackcdn.com/image/fetch/$s_!IpdT!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fafb33e27-1fae-4d6f-b485-981f44f94660_1787x670.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!IpdT!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fafb33e27-1fae-4d6f-b485-981f44f94660_1787x670.png" width="1200" height="450" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/afb33e27-1fae-4d6f-b485-981f44f94660_1787x670.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:546,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:190418,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!IpdT!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fafb33e27-1fae-4d6f-b485-981f44f94660_1787x670.png 424w, https://substackcdn.com/image/fetch/$s_!IpdT!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fafb33e27-1fae-4d6f-b485-981f44f94660_1787x670.png 848w, https://substackcdn.com/image/fetch/$s_!IpdT!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fafb33e27-1fae-4d6f-b485-981f44f94660_1787x670.png 1272w, https://substackcdn.com/image/fetch/$s_!IpdT!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fafb33e27-1fae-4d6f-b485-981f44f94660_1787x670.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Diagram showcasing how factories work.</figcaption></figure></div><p>Because of the explained architecture, I started by creating the <em>Campaign </em>contract, making sure that it was compliant with all the given requirements.</p><blockquote><p><em><strong>Note:</strong> you can find the full script in this <strong><a href="https://gist.github.com/0xRusowsky/5b583aeff7e387fa00652a9b079c34ad">GitHub gist</a>.</strong></em></p></blockquote><p>First of all, we need to define the variables and any <em>enum</em> or <em>structs</em> that we may need. </p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!eDZE!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd4aaea54-f19b-4964-9345-ae447c9aec92_1386x1404.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!eDZE!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd4aaea54-f19b-4964-9345-ae447c9aec92_1386x1404.png 424w, https://substackcdn.com/image/fetch/$s_!eDZE!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd4aaea54-f19b-4964-9345-ae447c9aec92_1386x1404.png 848w, https://substackcdn.com/image/fetch/$s_!eDZE!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd4aaea54-f19b-4964-9345-ae447c9aec92_1386x1404.png 1272w, https://substackcdn.com/image/fetch/$s_!eDZE!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd4aaea54-f19b-4964-9345-ae447c9aec92_1386x1404.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!eDZE!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd4aaea54-f19b-4964-9345-ae447c9aec92_1386x1404.png" width="507" height="513.5844155844156" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/d4aaea54-f19b-4964-9345-ae447c9aec92_1386x1404.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;normal&quot;,&quot;height&quot;:1404,&quot;width&quot;:1386,&quot;resizeWidth&quot;:507,&quot;bytes&quot;:343334,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!eDZE!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd4aaea54-f19b-4964-9345-ae447c9aec92_1386x1404.png 424w, https://substackcdn.com/image/fetch/$s_!eDZE!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd4aaea54-f19b-4964-9345-ae447c9aec92_1386x1404.png 848w, https://substackcdn.com/image/fetch/$s_!eDZE!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd4aaea54-f19b-4964-9345-ae447c9aec92_1386x1404.png 1272w, https://substackcdn.com/image/fetch/$s_!eDZE!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd4aaea54-f19b-4964-9345-ae447c9aec92_1386x1404.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Note that  <code>owner</code>  must be a  <code>payable address</code><em>, </em>because it will have the ability to withdraw funds.</figcaption></figure></div><p>Once we have set up all the variables, we can create the contract constructor. In this case, the constructor will use a  <code>require() </code>to ensure that the deadline is less than 60 days.</p><p>Since in solidity timestamps are expressed in <a href="https://www.unixtimestamp.com/">unix</a> (a second-based system that starts on January 1st, 1970), the input variable <code>_deadline</code> used in the constructor will have a relative scope (see that the require clause states <code>_deadline &lt;= 3600*24*60</code> ). Nevertheless, the global variable <code>deadline</code> will have an absolute reference, so that we can compare it with any given timestamp.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!9kzt!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F1d9ee6e6-e2a6-40a5-8843-f4e91ab9a82e_1848x1176.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!9kzt!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F1d9ee6e6-e2a6-40a5-8843-f4e91ab9a82e_1848x1176.png 424w, https://substackcdn.com/image/fetch/$s_!9kzt!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F1d9ee6e6-e2a6-40a5-8843-f4e91ab9a82e_1848x1176.png 848w, https://substackcdn.com/image/fetch/$s_!9kzt!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F1d9ee6e6-e2a6-40a5-8843-f4e91ab9a82e_1848x1176.png 1272w, https://substackcdn.com/image/fetch/$s_!9kzt!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F1d9ee6e6-e2a6-40a5-8843-f4e91ab9a82e_1848x1176.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!9kzt!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F1d9ee6e6-e2a6-40a5-8843-f4e91ab9a82e_1848x1176.png" width="625" height="397.9223901098901" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/1d9ee6e6-e2a6-40a5-8843-f4e91ab9a82e_1848x1176.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:927,&quot;width&quot;:1456,&quot;resizeWidth&quot;:625,&quot;bytes&quot;:320900,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!9kzt!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F1d9ee6e6-e2a6-40a5-8843-f4e91ab9a82e_1848x1176.png 424w, https://substackcdn.com/image/fetch/$s_!9kzt!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F1d9ee6e6-e2a6-40a5-8843-f4e91ab9a82e_1848x1176.png 848w, https://substackcdn.com/image/fetch/$s_!9kzt!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F1d9ee6e6-e2a6-40a5-8843-f4e91ab9a82e_1848x1176.png 1272w, https://substackcdn.com/image/fetch/$s_!9kzt!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F1d9ee6e6-e2a6-40a5-8843-f4e91ab9a82e_1848x1176.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Since<code> owner </code>is a<code> payable address</code><em>, </em>we must alter the  <code>_owner </code>type to be compliant.</figcaption></figure></div><p>After coding the contract constructed, I then created the relevant events and some modifiers that will help with the required validations that we will have to implement in the campaign functions.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Fgbx!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F20de9376-a9b2-4512-9d8b-c31ff55f3c4a_2451x1746.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Fgbx!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F20de9376-a9b2-4512-9d8b-c31ff55f3c4a_2451x1746.png 424w, https://substackcdn.com/image/fetch/$s_!Fgbx!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F20de9376-a9b2-4512-9d8b-c31ff55f3c4a_2451x1746.png 848w, https://substackcdn.com/image/fetch/$s_!Fgbx!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F20de9376-a9b2-4512-9d8b-c31ff55f3c4a_2451x1746.png 1272w, https://substackcdn.com/image/fetch/$s_!Fgbx!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F20de9376-a9b2-4512-9d8b-c31ff55f3c4a_2451x1746.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Fgbx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F20de9376-a9b2-4512-9d8b-c31ff55f3c4a_2451x1746.png" width="1456" height="1037" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/20de9376-a9b2-4512-9d8b-c31ff55f3c4a_2451x1746.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1037,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:588308,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Fgbx!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F20de9376-a9b2-4512-9d8b-c31ff55f3c4a_2451x1746.png 424w, https://substackcdn.com/image/fetch/$s_!Fgbx!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F20de9376-a9b2-4512-9d8b-c31ff55f3c4a_2451x1746.png 848w, https://substackcdn.com/image/fetch/$s_!Fgbx!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F20de9376-a9b2-4512-9d8b-c31ff55f3c4a_2451x1746.png 1272w, https://substackcdn.com/image/fetch/$s_!Fgbx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F20de9376-a9b2-4512-9d8b-c31ff55f3c4a_2451x1746.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Implementation of events and modifiers.</figcaption></figure></div><p>Finally, we need to implement the contract functions. In this case, we only need 4 functions: <code>fallback(), receive(), withdrawFunds(), cancelCampaign()</code>.</p><p>For both  <code>fallback() </code>and<code> receive() </code>to be able to receive ether from other contracts and EOAs, we need to make them <code>payable</code>. On top of that, we will also add the <code>liveCampaign</code> modifier to ensure that no more donations are received after a campaign is closed.</p><p>Another topic worth mentioning is how to deal with withdrawals. As explained before in the <em>tips of the week </em>section, the low-level function <code>call()</code> is used. </p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!uwo9!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Faa4df5df-5182-43b9-8ec2-b97a9f76120e_2010x2202.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!uwo9!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Faa4df5df-5182-43b9-8ec2-b97a9f76120e_2010x2202.png 424w, https://substackcdn.com/image/fetch/$s_!uwo9!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Faa4df5df-5182-43b9-8ec2-b97a9f76120e_2010x2202.png 848w, https://substackcdn.com/image/fetch/$s_!uwo9!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Faa4df5df-5182-43b9-8ec2-b97a9f76120e_2010x2202.png 1272w, https://substackcdn.com/image/fetch/$s_!uwo9!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Faa4df5df-5182-43b9-8ec2-b97a9f76120e_2010x2202.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!uwo9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Faa4df5df-5182-43b9-8ec2-b97a9f76120e_2010x2202.png" width="661" height="724.1037087912088" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/aa4df5df-5182-43b9-8ec2-b97a9f76120e_2010x2202.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1595,&quot;width&quot;:1456,&quot;resizeWidth&quot;:661,&quot;bytes&quot;:618742,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!uwo9!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Faa4df5df-5182-43b9-8ec2-b97a9f76120e_2010x2202.png 424w, https://substackcdn.com/image/fetch/$s_!uwo9!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Faa4df5df-5182-43b9-8ec2-b97a9f76120e_2010x2202.png 848w, https://substackcdn.com/image/fetch/$s_!uwo9!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Faa4df5df-5182-43b9-8ec2-b97a9f76120e_2010x2202.png 1272w, https://substackcdn.com/image/fetch/$s_!uwo9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Faa4df5df-5182-43b9-8ec2-b97a9f76120e_2010x2202.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Implementation of the campaign contract functions.</figcaption></figure></div><p>With this, the campaign contract is finished. Now, we only need to create the crowdfunding factory contract.</p><p>In this case, the factory contract will only have a single function that will deploy a new instance of the campaign contract whenever it is called. On top of that, we will also add a couple of mappings to better deal with the deployed campaigns. These mappings will be tracking the deployed campaign contracts by owner (using an array), and the amount of deployed campaigns by each of them (to navigate the array).</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!-KcF!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F12dde58a-014f-440e-9b25-fe3c4aa0bd4c_2334x2316.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!-KcF!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F12dde58a-014f-440e-9b25-fe3c4aa0bd4c_2334x2316.png 424w, https://substackcdn.com/image/fetch/$s_!-KcF!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F12dde58a-014f-440e-9b25-fe3c4aa0bd4c_2334x2316.png 848w, https://substackcdn.com/image/fetch/$s_!-KcF!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F12dde58a-014f-440e-9b25-fe3c4aa0bd4c_2334x2316.png 1272w, https://substackcdn.com/image/fetch/$s_!-KcF!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F12dde58a-014f-440e-9b25-fe3c4aa0bd4c_2334x2316.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!-KcF!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F12dde58a-014f-440e-9b25-fe3c4aa0bd4c_2334x2316.png" width="1456" height="1445" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/12dde58a-014f-440e-9b25-fe3c4aa0bd4c_2334x2316.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1445,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:745833,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!-KcF!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F12dde58a-014f-440e-9b25-fe3c4aa0bd4c_2334x2316.png 424w, https://substackcdn.com/image/fetch/$s_!-KcF!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F12dde58a-014f-440e-9b25-fe3c4aa0bd4c_2334x2316.png 848w, https://substackcdn.com/image/fetch/$s_!-KcF!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F12dde58a-014f-440e-9b25-fe3c4aa0bd4c_2334x2316.png 1272w, https://substackcdn.com/image/fetch/$s_!-KcF!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F12dde58a-014f-440e-9b25-fe3c4aa0bd4c_2334x2316.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Implementation of the crowdfund factory contract.</figcaption></figure></div><p></p><h1>Recommended Resources</h1><p>If you want to get started with Solidity from the ground up, you should definitely check <strong><a href="https://www.youtube.com/playlist?list=PLO5VPQH6OWdVQwpQfw9rZ67O6Pjfo6q-p">this youtube playlist</a></strong> by @<a href="https://twitter.com/ProgrammerSmart">SmartContractProgrammer</a>.</p><p>If you prefer written content, you will also find plenty of resources on <a href="https://solidity-by-example.org/">this webpage</a>.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://journal0xrusowsky.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading Aspiring web3 dev! Subscribe for free.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[Data Storage Layer & EVM]]></title><description><![CDATA[Artemis Week-2]]></description><link>https://journal0xrusowsky.substack.com/p/data-storage-layer-and-evm</link><guid isPermaLink="false">https://journal0xrusowsky.substack.com/p/data-storage-layer-and-evm</guid><dc:creator><![CDATA[0xRusowsky]]></dc:creator><pubDate>Tue, 19 Jul 2022 19:40:07 GMT</pubDate><enclosure url="https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/90c95066-966c-432f-ad93-71d6ef07fbd0_998x733.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h1>Laying the foundations. Artemis Week-2</h1><p>Second week of lectures at Artemis, where despite we kept covering the fundamentals behind the technology, we also started to get into more practical stuff.</p><p>This week we covered different topics around the Ethereum ecosystem: proof-of-stake, nodes, clients, and the EVM. Again, all of them are interesting topics and with plenty of side-quests that could be explored. Nevertheless, I will focus on the Data Storage Layer and the Ethereum Virtual Machine.</p><p>Personally, I plan to further familiarize myself with the opcodes in the upcoming weeks, since they can help provide a better understanding of the overall system and potentially help with gas optimization and even security.</p><h1>Data Storage Layer</h1><p>To fully comprehend Ethereum it is key to first learn how its data storage layer works. To do so, the following topics should be understood.</p><h3>Merkle Patricia Trie</h3><p>A <em>trie</em> is an easy-to-implement and small-memory data structure that is extremely fast at finding common prefixes. As Ethereum uses a Merkle Tree to efficiently store hashes in blocks, the development of a new data structure called &#8220;Modified Merkel Patricia Trie&#8221; became the core of data storage. Because of that, the world state trie, the Account State, the Receipt trie, and the Transaction trie are implementations of a Merkle Patricia Trie.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!ETI6!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb1c60dcb-6668-43fc-9181-9a8309717650_800x565.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ETI6!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb1c60dcb-6668-43fc-9181-9a8309717650_800x565.png 424w, https://substackcdn.com/image/fetch/$s_!ETI6!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb1c60dcb-6668-43fc-9181-9a8309717650_800x565.png 848w, https://substackcdn.com/image/fetch/$s_!ETI6!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb1c60dcb-6668-43fc-9181-9a8309717650_800x565.png 1272w, https://substackcdn.com/image/fetch/$s_!ETI6!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb1c60dcb-6668-43fc-9181-9a8309717650_800x565.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!ETI6!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb1c60dcb-6668-43fc-9181-9a8309717650_800x565.png" width="800" height="565" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/b1c60dcb-6668-43fc-9181-9a8309717650_800x565.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:565,&quot;width&quot;:800,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!ETI6!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb1c60dcb-6668-43fc-9181-9a8309717650_800x565.png 424w, https://substackcdn.com/image/fetch/$s_!ETI6!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb1c60dcb-6668-43fc-9181-9a8309717650_800x565.png 848w, https://substackcdn.com/image/fetch/$s_!ETI6!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb1c60dcb-6668-43fc-9181-9a8309717650_800x565.png 1272w, https://substackcdn.com/image/fetch/$s_!ETI6!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb1c60dcb-6668-43fc-9181-9a8309717650_800x565.png 1456w" sizes="100vw" loading="lazy" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><a href="https://ethereum.stackexchange.com/questions/268/ethereum-block-architecture">Representation of a Merkle Patricia Trie system.</a></figcaption></figure></div><h3>World State</h3><p>The world state trie keeps a mapping between addresses <em>(EOAs and contract accounts)</em> and their account states. It can be seen as a global state that is constantly updated by transaction executions.</p><p>It is important to note that despite the world state trie can be used to retrieve all the accounts&#8217; state information from a root hash <em>(storage root)</em>, the actual information is stored in the Account State trie.</p><p>Finally, it is also important to take into account that all this information is not stored on the blockchain. Instead, a single root hash <em>(state root)</em> representing the latest state, is stored in each block. The state root is enough for <em>light nodes</em> and <em>full nodes</em> to request world state data to <em>full nodes</em> and <em>archival nodes </em>respectively.</p><p><em><strong>Note:</strong> If you are not familiar with it yet, you can check the characteristics of the different node types <a href="https://ethereum.org/en/developers/docs/nodes-and-clients/#:~:text=A%20%22node%22%20is%20a%20computer,to%20run%20a%20mining%20node).">here</a>.</em></p><h3>Transactions</h3><p>Transactions are cryptographically signed instructions initiated by accounts. Transactions are the reason behind changes in the world state, and can result in message calls or the creation of new contracts.</p><p>Similar to the world state trie, each block has its own separate transaction trie. This trie stores both the transaction-related information and the transaction order decided by the miner who assembled the block.</p><h3>Blocks</h3><p>A block is composed of two different parts: the header and the body. </p><ul><li><p><strong>Block header:</strong> Actual &#8220;block-chain&#8221; part of Ethereum. Contains the hash of its predecessor, ensuring an untampered chain. </p></li><li><p><strong>Block body:</strong> Contains a list of transactions that have been included in the block and a list of uncle/ommer block headers.</p></li></ul><p>All the exposed information can be summarized in the following diagram:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!mv0N!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F588b6bd1-4e71-4fef-a855-c00976582841_2698x1453.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!mv0N!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F588b6bd1-4e71-4fef-a855-c00976582841_2698x1453.png 424w, https://substackcdn.com/image/fetch/$s_!mv0N!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F588b6bd1-4e71-4fef-a855-c00976582841_2698x1453.png 848w, https://substackcdn.com/image/fetch/$s_!mv0N!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F588b6bd1-4e71-4fef-a855-c00976582841_2698x1453.png 1272w, https://substackcdn.com/image/fetch/$s_!mv0N!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F588b6bd1-4e71-4fef-a855-c00976582841_2698x1453.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!mv0N!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F588b6bd1-4e71-4fef-a855-c00976582841_2698x1453.png" width="954" height="513.6923076923077" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/588b6bd1-4e71-4fef-a855-c00976582841_2698x1453.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:784,&quot;width&quot;:1456,&quot;resizeWidth&quot;:954,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;Main Diagram&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="Main Diagram" title="Main Diagram" srcset="https://substackcdn.com/image/fetch/$s_!mv0N!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F588b6bd1-4e71-4fef-a855-c00976582841_2698x1453.png 424w, https://substackcdn.com/image/fetch/$s_!mv0N!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F588b6bd1-4e71-4fef-a855-c00976582841_2698x1453.png 848w, https://substackcdn.com/image/fetch/$s_!mv0N!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F588b6bd1-4e71-4fef-a855-c00976582841_2698x1453.png 1272w, https://substackcdn.com/image/fetch/$s_!mv0N!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F588b6bd1-4e71-4fef-a855-c00976582841_2698x1453.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><a href="https://www.lucassaldanha.com/ethereum-yellow-paper-walkthrough-2/">Relationship between blocks and the data storage layer in the Ethereum network.</a></figcaption></figure></div><h1>EVM Basics</h1><p>The EVM is a virtual, stack-based computation engine that powers the Ethereum Blockchain by enabling it to run and deploy smart contracts.</p><p>One can think of EVM as the execution environment for the Ethereum blockchain. As such, the EVM consists of opcodes (operation codes), which can be conceptualized as a set of instructions with associated gas costs.</p><p>Since the EVM is deterministic, Ethereum can be described as a network with a state transition function<em> </em>where<em>,</em> given a state and a set of transactions, the network will transition towards a new valid state.</p><h3><strong>High-level Language &#8594; Byte Code &#8594; Opcode</strong></h3><p>Smart contracts are compiled into bytecode, which is then executed by the EVM as a set of opcodes that perform specific tasks. In combination with enough resources, these opcodes allow the EVM to compute almost anything, grating it Turing-completeness.</p><h3>EVM Architecture</h3><p>The EVM execution is controlled by a 1024-item-deep stack, where each item is a 256-bit word.  During execution, the EVM maintains a volatile memory that works as a word-addressed byte array, and which does not persist between transactions. Contracts, however, can use storage tries (as addressable key-value pairs), associated with the network accounts to access persistent data.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!RPb5!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd0395215-5fc0-4c95-9d3e-6a3c615521f6_1893x818.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!RPb5!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd0395215-5fc0-4c95-9d3e-6a3c615521f6_1893x818.png 424w, https://substackcdn.com/image/fetch/$s_!RPb5!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd0395215-5fc0-4c95-9d3e-6a3c615521f6_1893x818.png 848w, https://substackcdn.com/image/fetch/$s_!RPb5!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd0395215-5fc0-4c95-9d3e-6a3c615521f6_1893x818.png 1272w, https://substackcdn.com/image/fetch/$s_!RPb5!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd0395215-5fc0-4c95-9d3e-6a3c615521f6_1893x818.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!RPb5!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd0395215-5fc0-4c95-9d3e-6a3c615521f6_1893x818.png" width="1200" height="518.4065934065934" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/d0395215-5fc0-4c95-9d3e-6a3c615521f6_1893x818.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:629,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:171241,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!RPb5!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd0395215-5fc0-4c95-9d3e-6a3c615521f6_1893x818.png 424w, https://substackcdn.com/image/fetch/$s_!RPb5!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd0395215-5fc0-4c95-9d3e-6a3c615521f6_1893x818.png 848w, https://substackcdn.com/image/fetch/$s_!RPb5!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd0395215-5fc0-4c95-9d3e-6a3c615521f6_1893x818.png 1272w, https://substackcdn.com/image/fetch/$s_!RPb5!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd0395215-5fc0-4c95-9d3e-6a3c615521f6_1893x818.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">EVM architecture and brief overview of its components.</figcaption></figure></div><h3>EVM State transition</h3><p>As explained before, to move into a new state, the EVM needs to have an initial state to transition from, and a set of transactions in charge of the state changes.</p><p>If these requirements are fulfilled, every new transaction will either perform a message call or deploy a new contract. Regardless of the transaction nature, the stack will be populated with the corresponding opcodes and data, to effectively compute some operations and transition into a new state.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!B3kB!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9d308f87-fb43-4217-8be2-367b420dfcf8_1635x698.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!B3kB!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9d308f87-fb43-4217-8be2-367b420dfcf8_1635x698.png 424w, https://substackcdn.com/image/fetch/$s_!B3kB!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9d308f87-fb43-4217-8be2-367b420dfcf8_1635x698.png 848w, https://substackcdn.com/image/fetch/$s_!B3kB!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9d308f87-fb43-4217-8be2-367b420dfcf8_1635x698.png 1272w, https://substackcdn.com/image/fetch/$s_!B3kB!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9d308f87-fb43-4217-8be2-367b420dfcf8_1635x698.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!B3kB!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9d308f87-fb43-4217-8be2-367b420dfcf8_1635x698.png" width="1200" height="512.6373626373627" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/9d308f87-fb43-4217-8be2-367b420dfcf8_1635x698.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:622,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:100018,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!B3kB!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9d308f87-fb43-4217-8be2-367b420dfcf8_1635x698.png 424w, https://substackcdn.com/image/fetch/$s_!B3kB!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9d308f87-fb43-4217-8be2-367b420dfcf8_1635x698.png 848w, https://substackcdn.com/image/fetch/$s_!B3kB!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9d308f87-fb43-4217-8be2-367b420dfcf8_1635x698.png 1272w, https://substackcdn.com/image/fetch/$s_!B3kB!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9d308f87-fb43-4217-8be2-367b420dfcf8_1635x698.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">EVM execution model showcasing how the different components interact.</figcaption></figure></div><h1>Recommended Resources</h1><p>If you feel like a quant today, you should definitely check the <a href="https://ethereum.github.io/yellowpaper/paper.pdf">Ethereum Yellow Paper</a>.</p><p>If you want to have a more in-depth view of how the Ethereum Virtual Machine works, check <a href="https://medium.com/swlh/getting-deep-into-evm-how-ethereum-works-backstage-ab6ad9c0d0bf">this article</a>.</p><p>You can find all the different opcodes, their explanation, and their minimum gas consumption <a href="https://ethereum.org/en/developers/docs/evm/opcodes/">here</a>.</p><p>If you want to fully understand opcodes and their correlation with stack, memory, and storage, <strong>I highly encourage you to play around with this <a href="https://www.evm.codes/playground">awesome tool</a>.</strong></p><p>Finally, I would like to give credit to <a href="https://twitter.com/GonRMagalhaes">Gon&#231;alo</a> for explaining all these complex topics in such a comprehensive manner.</p><p></p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://journal0xrusowsky.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading Aspiring web3 dev! Subscribe for free here.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><p></p><p></p><p></p>]]></content:encoded></item><item><title><![CDATA[Distributed Systems & Consensus Mechanisms]]></title><description><![CDATA[Artemis Week-1]]></description><link>https://journal0xrusowsky.substack.com/p/distributed-systems-and-consensus</link><guid isPermaLink="false">https://journal0xrusowsky.substack.com/p/distributed-systems-and-consensus</guid><dc:creator><![CDATA[0xRusowsky]]></dc:creator><pubDate>Mon, 11 Jul 2022 08:33:58 GMT</pubDate><enclosure url="https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/99769b95-6ae3-4280-8d87-7294615ed89e_988x811.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p><em>After months of preparation, the wait is finally over and we started the proof-of-concept course at Artemis. For those of you who don't know, Artemis is an educational project focused on web3 development, and the team&#8217;s goal is to help craft the next wave of talented developers of the ecosystem.</em></p><p><em>This industry grows fast, and there is plenty of information to be aware of if you want to be a successful developer. Because of that, Artemis offers an 8-week course that covers the very basics of blockchain development (to set a solid foundation), and which will finally go into more advanced stuff (to give students the required tools to keep improving on their own).</em></p><p><em>The course will not only focus on development but will also have complimentary weekly talks regarding different interesting topics that will help students get a clearer picture of the industry. Despite these talks will be really insightful and given by reputable industry personalities, I won't cover them in this dev journal because I want it to be focused on technical stuff. Instead, I will most likely write about some of them in my <a href="https://0xrusowsky.substack.com/">main blog</a> (check it out if you haven't already).</em></p><h1>Let's begin. Artemis Week-1</h1><p>First things first. Introduction week, where we went through the history of money, the history of blockchains, we did some basic cryptography (asymmetric keys, hash functions, etc.) and also went through consensus mechanisms. Despite all of the topics being really interesting, today we will focus on distributed systems and consensus mechanisms.</p><h1>Distributed Systems</h1><p>A distributed system contains multiple nodes that are physically separated but connected using a network. All the nodes in the system communicate and coordinate with each other to reach an agreement on a variety of topics (single data values, new transactions, the state of the network, etc.).</p><p>On top of that, it is necessary to not only coordinate but also maintain system reliability even in the presence of faulty processes. Distributed systems are especially useful because of their ability to not have a single point of failure.</p><h1>The Byzantine Generals&#8217; Problem</h1><p>Distributed systems must be able to handle downtime/failure of their components, and must also be resistant to malicious operators who attempt to alter the state of the network.</p><p>In 1982, Leslie Lamport, Robert Shostak, and Pease Marshall created an abstraction of this problem and presented it in a paper called <em><a href="https://sci-hub.hkvisa.net/10.1145/357172.357176">The Byzantine Generals Problem</a></em>. One of the hardest problems in distributed computing systems, and which was thought to be unsolvable for many years.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!72G0!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F807f8027-7863-48e8-a747-d128594ca14d_1718x709.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!72G0!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F807f8027-7863-48e8-a747-d128594ca14d_1718x709.png 424w, https://substackcdn.com/image/fetch/$s_!72G0!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F807f8027-7863-48e8-a747-d128594ca14d_1718x709.png 848w, https://substackcdn.com/image/fetch/$s_!72G0!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F807f8027-7863-48e8-a747-d128594ca14d_1718x709.png 1272w, https://substackcdn.com/image/fetch/$s_!72G0!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F807f8027-7863-48e8-a747-d128594ca14d_1718x709.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!72G0!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F807f8027-7863-48e8-a747-d128594ca14d_1718x709.png" width="1200" height="495.3296703296703" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/807f8027-7863-48e8-a747-d128594ca14d_1718x709.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:601,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:341447,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!72G0!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F807f8027-7863-48e8-a747-d128594ca14d_1718x709.png 424w, https://substackcdn.com/image/fetch/$s_!72G0!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F807f8027-7863-48e8-a747-d128594ca14d_1718x709.png 848w, https://substackcdn.com/image/fetch/$s_!72G0!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F807f8027-7863-48e8-a747-d128594ca14d_1718x709.png 1272w, https://substackcdn.com/image/fetch/$s_!72G0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F807f8027-7863-48e8-a747-d128594ca14d_1718x709.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Diagram explaining the Byzantine Generals Problem and its challenges.</figcaption></figure></div><p>The generals face several challenges such as:</p><ul><li><p>Finding out whether the content of a message is true (sent by an honest general) or not (sent by a traitor).</p></li><li><p>Being certain that the information in a message hasn&#8217;t been tempered.</p></li></ul><p>The only way for the generals to succeed is if they stick to an algorithm that gives them an action plan that helps them move from a deterministic problem to a probabilistic one.</p><h2>Byzantine Fault Tolerance</h2><p>A Byzantine Fault Tolerance (BFT) system is able to overcome the Byzantine Generals problem, and therefore successfully operate even if some nodes fail or act maliciously.</p><p>With the release of <a href="https://bitcoin.org/bitcoin.pdf">the Bitcoin whitepaper</a>, Satoshi Nakamoto was able to design a BFT system on a peer-to-peer network. This achievement was possible thanks to the combination of cryptography security, and the development of a consensus mechanism called proof-of-work (PoW).</p><h4>Cryptographic Security</h4><p>Satoshi used cryptographic hash functions and public-key encryption to prevent data tampering and to verify the identity of networks.</p><p>Cryptographic hash functions are algorithms that take an arbitrary amount of data as an input and produce a fixed-size output of enciphered text called a <em>hash</em>.</p><p>Hash functions are especially useful for security purposes due to their properties:</p><ul><li><p><strong>Non-reversible.</strong> Hash functions are unidirectional, they make it very hard to reconstruct the original input from the hash.</p></li><li><p><strong>Avalanche effect / Non-predictable</strong>. A change in just one bit of the original input results in a completely different hash.</p></li><li><p><strong>Determinism.</strong> A given input always generates the same hash value.</p></li><li><p><strong>Collision resistance.</strong> Different inputs shouldn&#8217;t produce the same hash.</p></li></ul><p>With the usage of cryptographic hash functions, transactions can be secured in a block that connects to other blocks by its hash value. All hashes can be traced back to an initial block that is the root of all hashes. Note that by ensuring that each block stores the hash of the prior block, we can be certain that previous data hasn&#8217;t been altered. This hash structure is known as a <em>Merkle Tree</em>, and it is used to verify hashes that originate from the genesis block.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!mfxw!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F645d537e-9500-4790-89cd-b242fb6cb497_2200x1400.svg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!mfxw!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F645d537e-9500-4790-89cd-b242fb6cb497_2200x1400.svg 424w, https://substackcdn.com/image/fetch/$s_!mfxw!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F645d537e-9500-4790-89cd-b242fb6cb497_2200x1400.svg 848w, https://substackcdn.com/image/fetch/$s_!mfxw!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F645d537e-9500-4790-89cd-b242fb6cb497_2200x1400.svg 1272w, https://substackcdn.com/image/fetch/$s_!mfxw!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F645d537e-9500-4790-89cd-b242fb6cb497_2200x1400.svg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!mfxw!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F645d537e-9500-4790-89cd-b242fb6cb497_2200x1400.svg" width="638" height="406.1991758241758" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/645d537e-9500-4790-89cd-b242fb6cb497_2200x1400.svg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:927,&quot;width&quot;:1456,&quot;resizeWidth&quot;:638,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&#193;rbol de Merkle - Wikipedia, la enciclopedia libre&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="&#193;rbol de Merkle - Wikipedia, la enciclopedia libre" title="&#193;rbol de Merkle - Wikipedia, la enciclopedia libre" srcset="https://substackcdn.com/image/fetch/$s_!mfxw!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F645d537e-9500-4790-89cd-b242fb6cb497_2200x1400.svg 424w, https://substackcdn.com/image/fetch/$s_!mfxw!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F645d537e-9500-4790-89cd-b242fb6cb497_2200x1400.svg 848w, https://substackcdn.com/image/fetch/$s_!mfxw!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F645d537e-9500-4790-89cd-b242fb6cb497_2200x1400.svg 1272w, https://substackcdn.com/image/fetch/$s_!mfxw!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F645d537e-9500-4790-89cd-b242fb6cb497_2200x1400.svg 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Representation of a Merkle Tree.</figcaption></figure></div><h4>Proof-of-Work</h4><p>PoW is a consensus mechanism that established a set of rules to validate and add new blocks into the network, and, therefore, solve the Byzantine generals&#8217; problem.</p><p>According to Satoshi, the generals must agree on a difficulty metric. Let&#8217;s say that this metric states that the final hashed value must start with three 0s. The generals will then create a message and hash it until the output sticks to the agreed difficulty condition.</p><p>Note that if someone (the messenger or other generals) tries to tamper with the message, the final hash will change drastically and it won&#8217;t be taken as valid because it won&#8217;t stick to the pre-agreed rules. The only option for traitors is to find another nonce that will result in a final hash that sticks to the difficulty condition. Since we already know that this is a resource-intensive process, the traitors won&#8217;t easily be able to find a new hash.</p><p>When the message is received, it can easily be validated by hashing the nonce and the message and checking it against the provided hash.</p><h1>Recommended Resources</h1><p>If you want to visually understand everything that is being discussed in this piece, would <strong>highly encourage</strong> <strong>you to check <a href="https://www.youtube.com/watch?v=c7yvOlwBPoQ&amp;list=PLJz1HruEnenCXH7KW7wBCEBnBLOVkiqIi&amp;index=9">this video</a> by <a href="https://twitter.com/austingriffith">Austin Griffith</a>.</strong></p><p>If you prefer a more academic description of the topic, I also encourage you to go through the resources of <a href="https://www.cl.cam.ac.uk/teaching/2122/ConcDisSys/dist-sys-notes.pdf">Distributed Systems course</a> at the University of Cambridge.</p><p></p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://journal0xrusowsky.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading Aspiring web3 dev! Subscribe for free here.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><p></p><p></p>]]></content:encoded></item><item><title><![CDATA[Coming soon]]></title><description><![CDATA[This is Aspiring web3 dev, a journal of my path attempting to become a web3 developer.]]></description><link>https://journal0xrusowsky.substack.com/p/coming-soon</link><guid isPermaLink="false">https://journal0xrusowsky.substack.com/p/coming-soon</guid><dc:creator><![CDATA[0xRusowsky]]></dc:creator><pubDate>Fri, 01 Jul 2022 14:46:47 GMT</pubDate><content:encoded><![CDATA[<p><strong>This is Aspiring web3 dev</strong>, a journal of my path attempting to become a web3 developer.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://journal0xrusowsky.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://journal0xrusowsky.substack.com/subscribe?"><span>Subscribe now</span></a></p>]]></content:encoded></item></channel></rss>