<?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[SHARDLAB: Protocol Newsletter]]></title><description><![CDATA[Protocol Newsletter is a blockchain technology newsletter for builders. It goes beyond explanations, diving into key concepts and exploring their implementation at the code level, offering a hands-on learning experience.]]></description><link>https://0xshardlab.substack.com/s/protocol-newsletter</link><image><url>https://substackcdn.com/image/fetch/$s_!4i0Q!,w_256,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F382414b3-95d4-4ddd-b436-567d21cad88e_256x256.png</url><title>SHARDLAB: Protocol Newsletter</title><link>https://0xshardlab.substack.com/s/protocol-newsletter</link></image><generator>Substack</generator><lastBuildDate>Wed, 22 Apr 2026 03:54:00 GMT</lastBuildDate><atom:link href="https://0xshardlab.substack.com/feed" rel="self" type="application/rss+xml"/><copyright><![CDATA[SHARDLAB PTE. LTD.]]></copyright><language><![CDATA[en]]></language><webMaster><![CDATA[0xshardlab@substack.com]]></webMaster><itunes:owner><itunes:email><![CDATA[0xshardlab@substack.com]]></itunes:email><itunes:name><![CDATA[SHARDLAB]]></itunes:name></itunes:owner><itunes:author><![CDATA[SHARDLAB]]></itunes:author><googleplay:owner><![CDATA[0xshardlab@substack.com]]></googleplay:owner><googleplay:email><![CDATA[0xshardlab@substack.com]]></googleplay:email><googleplay:author><![CDATA[SHARDLAB]]></googleplay:author><itunes:block><![CDATA[Yes]]></itunes:block><item><title><![CDATA[OPAQUE Protocol: Let's overcome the inconveniences of Web3 with semi-decentralization]]></title><description><![CDATA[What is the opaque protocol, and semi-decentralization? How can it be used in blockchain?]]></description><link>https://0xshardlab.substack.com/p/opaque-protocol-lets-overcome-the</link><guid isPermaLink="false">https://0xshardlab.substack.com/p/opaque-protocol-lets-overcome-the</guid><dc:creator><![CDATA[Ch-iron]]></dc:creator><pubDate>Thu, 28 Aug 2025 08:00:58 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/6259d0ca-4362-4414-b3d4-12da172b5a87_1456x1048.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2>&#8265;&#65039; Are you curious about what OPAQUE protocol and semi-decentralization are?</h2><p>The OPAQUE Protocol, defined in <strong>RFC-9807</strong>, is a cryptographic protocol for client-server architectures that allows secure user registration and sign in without ever revealing the client's plain password to the server. In this respect, the OPAQUE Protocol shares similarities with <a href="https://0xshardlab.substack.com/p/zk-snarks-understand-it-then-build">Zero-Knowledge Proofs</a>(ZKP). Furthermore, it enables data to be securely stored without being disclosed to the server by encrypting it with a key known only to the client which derived from the protocol.</p><p>I define "semi-decentralization" as a system where data resides on the server(centralized), but the sovereignty over that data rests with the user or community(decentralized). The OPAQUE Protocol embodies this semi-decentralized nature because, while data is stored centrally on the server, access and manipulation rights are exclusively granted to the user. </p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://0xshardlab.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! Subscribe for free to receive new posts and support my work.</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><h2>&#128170; Can semi-decentralization overcome the inconveniences of Web3?</h2><p>The current user experience (UX) in Web3 is, admittedly, not as seamless as in Web2. I believe that a semi-decentralized approach offers a crucial interim step to bridge this UX gap. </p><p>For instance, Solana has made all its block data accessible via <a href="https://github.com/solana-labs/solana-bigtable">Google Cloud</a>(centralized) for faster access. This data is centrally stored in the cloud, but because it's generated and maintained by the Solana community and accessible to all users(decentralized), I consider it semi-decentralized. Similarly, Hyperliquid employs a comparable method by uploading data to <a href="https://hyperliquid.gitbook.io/hyperliquid-docs/historical-data">AWS-S3</a>.</p><p>Another example is <a href="https://ethereum.org/en/layer-2/learn/">Ethereum Layer 2 solutions</a>. Here, a sequencer processes all transactions(centralized) and records them on the Ethereum mainnet. However, the fact that anyone can verify these transactions and access the data(decentralized) makes this approach semi-decentralized.</p><p>As the examples above show, <strong>a hybrid model of centralization and decentralization allows us to gain the benefits of throughput and easy access without significantly losing decentralization</strong>.</p><h2>&#129343; Deep Dive into Opaque Protocol Step-by-Step</h2><p>The OPAQUE Protocol primarily consists of two phases, employing the <a href="https://datatracker.ietf.org/doc/rfc9497/">OPRF (Oblivious Pseudorandom Function)</a> protocol to generate unpredictable randomized password:</p><ol><li><p><strong>Registration (Sign-up)</strong></p></li><li><p><strong>Online Authenticated Key Exchange (Sign-in)</strong></p></li></ol><p>Before these two phases begin, server setup is required. Let's examine the step-by-step process, from setup through each phase, noting that many details are omitted to make it more intuitive.</p><h3>0. Server Setup</h3><p>The server prepares a <strong>public/private key pair</strong> and an <strong>OPRF seed</strong> (necessary for using the OPRF protocol).</p><h3>1. Registration</h3><p>This phase involves storing the client's randomized password on the server.</p><h4>Step 1: Registration Request</h4><ul><li><p>The client generates a <strong>blind element</strong> for the OPRF protocol from their password.</p></li><li><p>This blind element is sent to the server along with a <strong>user identifier</strong> (e.g. username).</p></li></ul><h4>Step 2: Registration Response</h4><ul><li><p>The server uses the received blind element from the client and its own OPRF seed to generate an <strong>evaluation element</strong>.</p></li><li><p>The evaluation element and the server's public key are then sent back to the client.</p></li></ul><h4>Step 3: Record</h4><h5>Client</h5><ul><li><p>The client uses the password entered in Step 1, the blind element generated in Step 1, and the evaluation element received from the server to obtain the OPRF function's output, which is used as the <strong>randomized password</strong>. An <strong>export key </strong>is extracted from this randomized password. The randomized password, along with other supplementary information, is then bundled into an <strong>envelope</strong> and sent to the server.</p></li></ul><h5>Server</h5><ul><li><p>The server stores the received envelope using the user identifier as a key.</p></li></ul><pre><code><code> Client                                                       Server
---------------------------------------------------------------------
(user_identifier, blind) = CreateRegistrationRequest(password)
                        Registration Request
           ---------------------------------------------&gt;

(server_public_key, evaluation) = CreateRegistrationResponse(server_public_key, blind, oprf_seed)
                        Registration Response
           &lt;---------------------------------------------

(envelope, export_key) = FinalizeRegistrationRequest(password, blind, evaluation)
                               Record
           ---------------------------------------------&gt;</code></code></pre><h3>2. Online Authenticated Key Exchange</h3><p>This phase requires going through the OPRF process once more to retrieve the randomized password obtained during registration. It's a process where the server and client exchange and verify each other's keys, subsequently generating an identical session key for future communication. The exchanged <strong>ke(key exchange) messages</strong> that appear at each step facilitate key exchange and verification between the client and server.</p><h4>Step 1: AKE Message 1</h4><ul><li><p>Similar to the registration process, a <strong>blind element</strong> is generated from the password. </p></li><li><p>A <strong>ke1 message</strong> is created. </p></li><li><p>The blind element, user identifier, and ke1 message are sent to the server.</p></li></ul><h4>Step 2: AKE Message 2</h4><ul><li><p>The server uses the received blind element from the client and its own OPRF seed to generate an <strong>evaluation element</strong>. </p></li><li><p>The server then finds the <strong>envelope</strong> corresponding to the user identifier. </p></li><li><p>A <strong>ke2 message</strong> is created. </p></li><li><p>The server's public key, the evaluation element, the envelope, and the ke2 message are sent to the client.</p></li></ul><h4>Step 3: AKE Message 3</h4><h5>Client</h5><ul><li><p>The client uses the password entered in Step 1, the blind element generated in Step 1, and the evaluation element received from the server to obtain the OPRF function's output, which is used as the <strong>randomized password</strong>. </p></li><li><p>The client then uses this randomized password to open the envelope and extract the <strong>export key</strong>. </p></li><li><p>A <strong>ke3 message</strong> is generated, and during this process, the client's <strong>session key</strong> is created. </p></li><li><p>The <strong>ke3 message</strong> is sent to the server.</p></li></ul><h5>Server</h5><ul><li><p>The server generates its <strong>session key</strong> from the ke3 message received from the client.</p></li></ul><pre><code><code> Client                                               Server
--------------------------------------------------------------
(ke1, blind) = GenerateKE1(password)
                        AKE Message 1
           ----------------------------------------&gt;

(server_public_key, ke2, evaluation, envelope) = GenerateKE2(server_public_key, blind, oprf_seed, ke1, envelope)
                        AKE Message 2           
           &lt;----------------------------------------

(ke3, export_key, session_key) = GenerateKE3(password, blind, evaluation, envelope, ke2)
                       AKE Message 3
           ----------------------------------------&gt;</code></code></pre><p>Ultimately, if the <strong>export key</strong> from the registration process matches the <strong>export key</strong> from the login process, and the client's <strong>session key</strong> matches the server's <strong>session key</strong>, the OPAQUE Protocol procedure concludes.</p><p>Subsequently, the session key is used for encryption and decryption during communication between the client and server. The export key is a key known only to the client and is used to encrypt data before storing it on the server, ensuring the data remains confidential from the server.</p><h4>Example of session key and export key Usage</h4><ul><li><p>Client encrypts data with export key &#8594; Client encrypts with session key &#8594; Server decrypts with session key &#8594; Server stores data</p></li><li><p>Server encrypts with session key &#8594; Client decrypts with session key &#8594; Client decrypts with export key &#8594; Client reads data</p></li></ul><h2>&#128272; Security Analysis</h2><ul><li><p>By using an unpredictable randomized password derived from the password via the OPRF protocol, <strong>pre-computing attacks</strong> like rainbow attacks are prevented.</p></li><li><p>To prevent <strong>client enumeration attacks</strong>, the same oprf seed is used for all users, making it impossible to distinguish between unregistered and registered users. However, client enumeration attacks during the registration process cannot be entirely prevented, so applications must implement various restrictions during registration to mitigate this attack.</p></li><li><p>Even if the server is compromised and records are leaked, all data is encrypted, thus protecting the information. Furthermore, a <strong>Key Stretching Function (KSF)</strong> is applied to the OPRF output (randomized password), significantly increasing the cost of an attack.</p></li><li><p>The export key should only be used after verifying the authenticity of the server (i.e. after the login process is complete).</p></li><li><p>To prevent a fake server from impersonating the legitimate server by stealing records, it's crucial to verify that the server public key obtained during registration matches the one obtained during login. &#128073; ShardLab offers an <a href="https://github.com/shard-lab/opaque-ke-wasm">OPAQUE Protocol library</a> that incorporates this functionality using WebAssembly.</p></li></ul><h2>&#129321; OPAQUE Protocol in Blockchain</h2><p>Recently, the blockchain domain has seen a growing adoption of semi-decentralized approaches, beyond just Solana, Hyperliquid, and Layer 2 solutions. The OPAQUE protocol, another semi-decentralized method, isn't yet widely adopted in the blockchain domain. However, it's clear that, like other semi-decentralized approaches, the OPAQUE protocol could bring a significantly improved UX in the Web3 user authentication, offering both security and convenience without largely sacrificing decentralization.</p><p><strong>&#8220;If you are a Web3 developer, why not consider using the OPAQUE Protocol for better security and UX?&#8221;</strong></p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://0xshardlab.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! Subscribe for free to receive new posts and support my work.</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[What is EIP-7702? Turning EOAs into Smart Wallets Explained]]></title><description><![CDATA[Hands-on with EIP-7702: Build, delegate, and upgrade your EOA just like a smart contract&#8212;all by making the tests pass.]]></description><link>https://0xshardlab.substack.com/p/what-is-eip-7702-turning-eoas-into</link><guid isPermaLink="false">https://0xshardlab.substack.com/p/what-is-eip-7702-turning-eoas-into</guid><dc:creator><![CDATA[SHARDLAB]]></dc:creator><pubDate>Thu, 03 Jul 2025 04:00:56 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/413789d3-a3d9-4936-83aa-022f4fd469d3_1456x1048.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2>What's the Big Idea?</h2><p>Traditionally, Ethereum accounts are divided into two clear categories:</p><ul><li><p><strong>Externally Owned Accounts (EOAs)</strong>: These are typical wallets controlled by private keys&#8212;like your MetaMask account&#8212;that hold ETH and send transactions. They have balances and nonces but no logic or code execution capability.</p></li><li><p><strong>Smart Contract Accounts</strong>: These contain executable logic stored on-chain. They can execute complex instructions, manage state, and automate transactions.</p></li></ul><p>But what if you could merge the flexibility and simplicity of EOAs with the powerful logic of smart contracts?</p><p>That's exactly what EIP-7702 achieves. It lets an EOA delegate its execution to a smart contract while still maintaining its original identity and control.</p><p></p><h2>Delegation</h2><p>Here's how EIP-7702 delegation changes the game:</p><ul><li><p><strong>No Deployment Costs</strong>: You don't deploy a new contract; you simply delegate your existing wallet.</p></li><li><p><strong>Keep Your Identity</strong>: Your EOA address, private keys, balance, and nonce remain unchanged.</p></li><li><p><strong>Flexible Logic</strong>: You can update or revoke the delegated logic at any time, instantly.</p></li></ul><p></p><h3>The New Transaction Type: AUTHORIZATION_LIST (EIP-7702 Transaction)</h3><p>EIP-7702 introduces a <strong>brand new transaction type</strong> that lets your EOA safely and flexibly delegate code execution.</p><p></p><h4><strong>What is the new transaction?</strong></h4><ul><li><p><strong>Name:</strong> Authorization List Transaction</p></li><li><p><strong>Format:</strong> The transaction includes an <code>authorization_list</code> in its payload, containing one or more signed authorizations. Each authorization specifies which implementation contract to delegate to, and includes a nonce for replay protection.</p></li><li><p><strong>Key Fields:</strong></p><ul><li><p><code>sender</code> (the EOA address)</p></li><li><p><code>authorization_list</code> (array of signed authorizations)</p></li><li><p>Each authorization: <code>{chain_id, address, nonce, y_parity, r, s}</code></p></li><li><p><code>to</code>, <code>value</code>, <code>data</code> (optional, for a call in the same tx)</p></li></ul></li></ul><p></p><h4><strong>What does this transaction do?</strong></h4><ul><li><p>When submitted, the EOA&#8217;s <code>code</code> field is set to a special delegation bytecode:</p></li></ul><pre><code><code>0xef0100 || &lt;Implementation Address&gt;</code></code></pre><ul><li><p>The EVM recognizes this as an instruction to delegate all code execution to the target implementation using <code>DELEGATECALL</code>.</p></li></ul><p></p><h4><strong>How do you use it?</strong></h4><ol><li><p><strong>Sign a delegation authorization</strong> off-chain (e.g., using MetaMask or your own signing tool), specifying the implementation contract address, a unique nonce, and chain id.</p></li><li><p><strong>Submit</strong> the Authorization List Transaction to the network. The transaction updates the EOA&#8217;s code (or reverts if invalid/nonce mismatch).</p></li><li><p><strong>Immediately, your EOA</strong> begins behaving like a smart contract wallet, forwarding calls to the new implementation.</p></li><li><p><strong>You can update or revoke</strong> the delegation at any time by submitting a new signed authorization with a higher nonce, or with <code>impl_address = 0x0</code> to clear delegation and revert to vanilla EOA behavior.</p></li></ol><p></p><h4><strong>Example Call:</strong></h4><p>Suppose your EOA is <code>0xA...123</code>, and you want to delegate to a BatchExecutor contract at <code>0xB...456</code>:</p><ul><li><p>You sign: <code>{ address: 0xB...456, nonce: 7, y_parity, r, s }</code></p></li></ul><pre><code><code>{
  "sender": "0xA...123",
  "authorization_list": [
    {
      "impl_address": "0xB...456",
      "nonce": 7,
      "v": 27,
      "r": "0x...",
      "s": "0x..."
    }
  ]
}</code></code></pre><ul><li><p>After confirmation, all code execution for <code>0xA...123</code> is delegated to <code>0xB...456</code>, using your EOA&#8217;s storage and balance.</p></li></ul><p>Whenever your EOA receives a transaction, the EVM forwards the execution (via <code>DELEGATECALL</code>) to the specified implementation contract. All execution happens in your EOA&#8217;s context&#8212;your balance, your state, your control.</p><p></p><h2>Practical Example: BatchExecutor Contract</h2><p>Let's take a concrete example: the <code>BatchExecutor</code> contract, designed to showcase EIP-7702 delegation.</p><p>With <code>BatchExecutor</code>, your EOA can execute multiple transactions in a single atomic operation. Imagine you need to perform several token transfers or calls across different DeFi protocols simultaneously. Instead of manually submitting each transaction, you simply delegate your EOA to the <code>BatchExecutor</code> contract:</p><ul><li><p>You sign and authorize a delegation to <code>BatchExecutor</code>.</p></li><li><p>Instantly, your EOA can call <code>executeBatch</code>, running multiple operations at once.</p></li><li><p>Your EOA balance, nonce, and state remain intact, maintaining all the familiar EOA properties.</p></li></ul><p><strong>For concrete code examples and a hands-on workshop, check out the <a href="https://github.com/shard-lab/protocol-camp/tree/main/ethereum/eip7702">protocol-camp EIP-7702 repository</a>.</strong></p><p>You'll find the BatchExecutor implementation, upgrade scenarios, and all related test cases ready for experimentation.</p><p></p><h2>&#9881;&#65039; But What About Security and Access Control?</h2><p>Good question. With great power comes great responsibility&#8212;and EIP-7702 makes these patterns possible, but <strong>does not mandate a specific access control model</strong>. How you implement security is entirely up to your delegated contract logic.</p><p>In the BatchExecutor example (which is just one possible use case), we show an access control pattern like:</p><ul><li><p><strong>Owner Control</strong>: Only your EOA can trigger batch operations (if the logic is written that way).</p></li><li><p><strong>Guardians</strong>: You can designate trusted guardian addresses to act on your behalf&#8212;useful for shared wallets or recovery (again, if supported by the contract you delegate to).</p></li><li><p><strong>Revocable Delegation</strong>: At any moment, you can revoke or change the delegation to another logic contract or revert to a standard EOA behavior (this is a core property of EIP-7702 itself).</p></li></ul><p>EIP-7702 delegation also makes upgrades easy and flexible. Suppose you want to enhance your batch execution logic later. You simply delegate your EOA to a new implementation (e.g., from <code>BatchExecutor</code> to <code>BatchExecutorV2</code>). Your EOA&#8217;s state persists across the upgrade.</p><p><strong>However, developers must ensure storage compatibility between implementations. If done improperly, storage slot collisions might occur, causing unintended data corruption. This is why following storage best practices, like using namespace-based storage slots (as proposed in EIP-7201), is essential.</strong></p><blockquote><p>&#9888;&#65039; <strong>Remember:</strong> EIP-7702 just enables delegation. The actual security, permissioning, and flexibility depend entirely on the implementation contract (like BatchExecutor or your own design). Always review and test the logic you delegate to!</p></blockquote><p></p><h2>&#128736;&#65039; Getting Hands-On</h2><p>Ready to see EIP-7702 in action?</p><p><strong>The purpose of this learning module is to make all provided tests pass by modifying the smart contracts as needed.</strong> As you work through the failing tests and implement fixes, you&#8217;ll encounter and solve various issues that can arise with EIP-7702&#8212;including delegation logic, access control, storage collisions, upgrade safety, and more. This hands-on approach ensures you learn not only the mechanics of EIP-7702, but also its important design considerations.</p><p>You can find all the example code and test cases here: &#128073; <a href="https://github.com/shard-lab/protocol-camp/tree/main/ethereum/eip7702">protocol-camp EIP-7702 repository</a></p><p>Through these tests, you'll:</p><ul><li><p>Verify delegation setup and execution behavior.</p></li><li><p>Test batch operations, access control, and ether reception.</p></li><li><p>Ensure upgrade compatibility and safe storage layouts.</p></li></ul><h3>Try it Yourself:</h3><pre><code><code>forge install
forge test</code></code></pre><p>Dive into the provided tests, fix intentional issues, and learn by solving each challenge step by step!</p><p></p><h2>&#127919; Wrapping Up</h2><p>EIP-7702 is a major step forward for Ethereum wallet and account design. It unlocks the flexibility of smart contract logic for EOAs&#8212;without losing the simplicity and direct control developers are used to.</p><p>Through this hands-on module, you&#8217;ve not only learned how delegation works, but also tackled real-world problems around code delegation, upgrade safety, and access control. As you modify the smart contracts and pass all tests, you gain direct experience with the subtleties and possibilities of EIP-7702.</p><p>Whether you&#8217;re building advanced wallets, dApps, or experimenting with new forms of user control, EIP-7702 provides a flexible foundation.</p><p>Ready to build the next generation of smart wallets?</p><p></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://0xshardlab.substack.com/p/what-is-eip-7702-turning-eoas-into?utm_source=substack&utm_medium=email&utm_content=share&action=share&quot;,&quot;text&quot;:&quot;Share&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://0xshardlab.substack.com/p/what-is-eip-7702-turning-eoas-into?utm_source=substack&utm_medium=email&utm_content=share&action=share"><span>Share</span></a></p><p></p>]]></content:encoded></item><item><title><![CDATA[A practical guide to StateTrie — How Blockchain Keeps Track of All Accounts]]></title><description><![CDATA[Understand how Ethereum and other blockchains organize, update, and recovery their state, and how you can explore it all in real code.]]></description><link>https://0xshardlab.substack.com/p/a-practical-guide-to-statetrie-how</link><guid isPermaLink="false">https://0xshardlab.substack.com/p/a-practical-guide-to-statetrie-how</guid><dc:creator><![CDATA[SHARDLAB]]></dc:creator><pubDate>Mon, 09 Jun 2025 10:02:11 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/1f2b5077-a3b8-4869-a052-e4d868925916_1456x1048.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p><em>(In blockchain, &#8220;state&#8221; means the current data about all accounts, balances, and smart contracts.)</em></p><p>In blockchain systems, efficiently and securely storing all account and contract state is critical. The StateTrie is a data structure designed for this purpose. Used in Ethereum and many other blockchains, it organizes accounts, balances, and contract data into a tree structure.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!pQFY!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F11dcbd70-6374-44fa-a970-4262de06d9a3_1176x904.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!pQFY!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F11dcbd70-6374-44fa-a970-4262de06d9a3_1176x904.png 424w, https://substackcdn.com/image/fetch/$s_!pQFY!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F11dcbd70-6374-44fa-a970-4262de06d9a3_1176x904.png 848w, https://substackcdn.com/image/fetch/$s_!pQFY!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F11dcbd70-6374-44fa-a970-4262de06d9a3_1176x904.png 1272w, https://substackcdn.com/image/fetch/$s_!pQFY!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F11dcbd70-6374-44fa-a970-4262de06d9a3_1176x904.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!pQFY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F11dcbd70-6374-44fa-a970-4262de06d9a3_1176x904.png" width="529" height="406.6462585034014" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/11dcbd70-6374-44fa-a970-4262de06d9a3_1176x904.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:904,&quot;width&quot;:1176,&quot;resizeWidth&quot;:529,&quot;bytes&quot;:29886,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://0xshardlab.substack.com/i/165508159?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F11dcbd70-6374-44fa-a970-4262de06d9a3_1176x904.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!pQFY!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F11dcbd70-6374-44fa-a970-4262de06d9a3_1176x904.png 424w, https://substackcdn.com/image/fetch/$s_!pQFY!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F11dcbd70-6374-44fa-a970-4262de06d9a3_1176x904.png 848w, https://substackcdn.com/image/fetch/$s_!pQFY!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F11dcbd70-6374-44fa-a970-4262de06d9a3_1176x904.png 1272w, https://substackcdn.com/image/fetch/$s_!pQFY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F11dcbd70-6374-44fa-a970-4262de06d9a3_1176x904.png 1456w" sizes="100vw" 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 role="img" 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><title></title><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">Ethereum State Trie (https://shorturl.at/8PWfD)</figcaption></figure></div><h1>Want to See the Code First?</h1><p>Most articles about state are conceptual. Here, you can check out real TypeScript code &#8212; read, run, and modify it yourself. This isn't just theory: every core idea explained below is directly backed by a working implementation.</p><ul><li><p>&#128279; <strong><a href="https://github.com/shard-lab/protocol-camp/tree/main/ethereum/statetrie">Jump straight to the code and tests</a></strong></p></li><li><p>Or, keep reading to see how StateTrie works, step by step.</p></li></ul><p>What sets this guide apart:</p><ul><li><p>You see not just what a StateTrie is, but exactly <em>how</em> state updates and lookups are done &#8212; with actual data flows and visual step-throughs.</p></li><li><p>Each major concept is paired with both an intuitive explanation and a concrete code example (see <a href="https://github.com/shard-lab/protocol-camp/tree/main/ethereum/statetrie">GitHub</a>).</p></li></ul><h1>Why Do We Need a StateTrie?</h1><ul><li><p><strong>Integrity of State:</strong> With a single root hash, you can verify the entire system state hasn't been tampered with.</p></li><li><p><strong>Efficient Partial Updates:</strong> When an account changes, only the nodes along that account's path are updated. The rest of the tree remains unchanged and can be reused.</p></li><li><p><strong>History and Rollbacks:</strong> Every version of the tree (and its nodes) is kept in the database, making it easy to restore or compare any past state. This enables rollbacks and historical proofs.</p></li></ul><p>These properties enable reliable state management, fast verification, and support for time-travel (rollback) features in blockchain systems.</p><h1>How Does StateTrie Work?</h1><h2>State History and Snapshots</h2><p>Imagine the StateTrie as a tree whose root hash is like a unique fingerprint for the blockchain&#8217;s state at a certain time. Every update (like a transaction) makes a new root hash, just like how every new Git commit makes a new commit hash. Each root hash is a snapshot. You can always go back to any previous root hash and query that exact state&#8212;just like checking out a specific commit in Git.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!B44i!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc90b3a6c-ea36-495a-b4c6-b54aa8382add_1524x770.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!B44i!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc90b3a6c-ea36-495a-b4c6-b54aa8382add_1524x770.png 424w, https://substackcdn.com/image/fetch/$s_!B44i!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc90b3a6c-ea36-495a-b4c6-b54aa8382add_1524x770.png 848w, https://substackcdn.com/image/fetch/$s_!B44i!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc90b3a6c-ea36-495a-b4c6-b54aa8382add_1524x770.png 1272w, https://substackcdn.com/image/fetch/$s_!B44i!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc90b3a6c-ea36-495a-b4c6-b54aa8382add_1524x770.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!B44i!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc90b3a6c-ea36-495a-b4c6-b54aa8382add_1524x770.png" width="552" height="279.032967032967" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c90b3a6c-ea36-495a-b4c6-b54aa8382add_1524x770.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:736,&quot;width&quot;:1456,&quot;resizeWidth&quot;:552,&quot;bytes&quot;:151274,&quot;alt&quot;:&quot;&quot;,&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;:&quot;https://0xshardlab.substack.com/i/165508159?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc90b3a6c-ea36-495a-b4c6-b54aa8382add_1524x770.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!B44i!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc90b3a6c-ea36-495a-b4c6-b54aa8382add_1524x770.png 424w, https://substackcdn.com/image/fetch/$s_!B44i!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc90b3a6c-ea36-495a-b4c6-b54aa8382add_1524x770.png 848w, https://substackcdn.com/image/fetch/$s_!B44i!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc90b3a6c-ea36-495a-b4c6-b54aa8382add_1524x770.png 1272w, https://substackcdn.com/image/fetch/$s_!B44i!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc90b3a6c-ea36-495a-b4c6-b54aa8382add_1524x770.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 role="img" 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><title></title><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></figure></div><p></p><h2>A Step-by-Step Look at Set and Get Operations</h2><p>The StateTrie organizes blockchain account data using a tree structure, where each path through the tree is determined by the hash of the account&#8217;s address. Let&#8217;s break down exactly what happens when you store (set) or retrieve (get) account data.</p><h3>1. Storing Data (<code>set</code>): Inserting or Updating an Account</h3><p>The set operation is how you add or change the data for an account. Here&#8217;s how it works, step by step:</p><h4>a. Hashing and Path Generation</h4><ul><li><p>The account address (e.g. <code>0x0001</code>) is hashed (commonly using SHA-256).</p></li><li><p>The resulting hash is split into hexadecimal digits (nibbles), e.g.<br><code>4bf5122f3a4554...</code> &#8594; <code>[4, b, f, 5, 1, 2, 2, f, 3, ...]</code></p></li><li><p>Each nibble decides which branch to follow at each level of the tree.</p></li></ul><h4>b. Tree Traversal and Node Creation</h4><ul><li><p>Starting from the root, the algorithm follows the path specified by the nibbles.</p></li><li><p>If a branch does not exist for a given nibble, a new branch node is created.</p></li><li><p>When the path is exhausted (all nibbles followed), a new leaf node containing the account and value is created.</p></li></ul><h4>c. Updating Parent Branches and Root Hash</h4><ul><li><p>After inserting or updating a leaf, all parent branch nodes along the path are also updated, because each branch&#8217;s hash depends on its children.</p></li><li><p>Only the nodes along the changed path are new; unchanged branches are reused.</p></li><li><p>The new root hash represents the entire new state.</p></li></ul><h3>Example:</h3><p>Suppose you insert three accounts, each with a unique address.</p><p><strong>Step 1: Insert </strong><code>0x0001</code></p><pre><code><code>Root (Branch)
&#9492;&#9472;&#9472; [4] &#8594; Leaf (key: 0x0001, value: 100)</code></code></pre><p><strong>Step 2: Insert </strong><code>0x0002</code><strong> (different path)</strong></p><pre><code>Root (Branch) 
&#9500;&#9472;&#9472; [4] &#8594; Leaf (key: 0x0001, value: 100) 
&#9492;&#9472;&#9472; [d] &#8594; Leaf (key: 0x0002, value: 200)</code></pre><p><strong>Step 3: Insert </strong><code>0x0003</code><strong> (shared prefix, deeper branch)</strong></p><pre><code>Root (Branch) 
&#9500;&#9472;&#9472; [4] &#8594; Branch 
&#9474;    &#9492;&#9472;&#9472; [b] &#8594; Branch 
&#9474;         &#9492;&#9472;&#9472; [f] &#8594; ... 
&#9474;               &#9500;&#9472;&#9472; [4] &#8594; Leaf (key: 0x0001, value: 100) 
&#9474;               &#9492;&#9472;&#9472; [a] &#8594; Leaf (key: 0x0003, value: 300) 
&#9492;&#9472;&#9472; [d] &#8594; Leaf (key: 0x0002, value: 200)</code></pre><p>Only the path from the root to the changed/inserted leaf needs to be updated; all other paths and nodes are reused, making updates efficient and enabling state history.</p><p>Only the path from the root to the changed/inserted leaf needs to be updated; all other paths and nodes are reused, making updates efficient and enabling state history.</p><h3>What Happens in the Worst and Best Cases?</h3><p>In the <strong>worst case</strong>, every account&#8217;s key hash is completely different, so each inserted key requires the maximum possible depth&#8212;one new branch node for every nibble (hex digit) in the hash. This means the tree can be quite deep (e.g., up to 64 levels for a 256-bit hash).</p><p>However, in most real-world cases, <strong>many accounts share common prefixes</strong> in their hashed keys. This allows multiple accounts to share branch nodes along their path&#8212;<br><strong>only the final part of the path differs, and only that part of the tree is unique to each key. </strong>Unchanged &#8220;parent paths&#8221; are always reused.</p><p>Suppose the hashes for two addresses both start with the same nibbles:</p><pre><code>Account A: [4, b, f, ...]
Account B: [4, b, f, ...]</code></pre><p>Their paths will share the <code>[4]</code>, <code>[b]</code>, <code>[f]</code> branches. Only when their paths diverge (say at the 7th nibble) does the tree branch out to different leaves. This <strong>sharing of paths</strong> means the Trie is very space-efficient and minimizes the number of new nodes needed for each operation.</p><h4>In Ethereum&#8217;s Full MPT</h4><p>The real Merkle Patricia Trie used by Ethereum optimizes shared prefixes even further:</p><ul><li><p><strong>Extension nodes</strong> compress long sequences of branch nodes with a single child into one node, reducing tree depth and database lookups.</p></li><li><p>This enables even faster operations and lower storage use for keys with long shared prefixes.</p></li></ul><p></p><h3>2. Retrieving Data (<code>get</code>): Looking Up an Account</h3><p>The get operation finds the value for a given key. Here&#8217;s how it works:</p><h4>a. Hashing the Key</h4><ul><li><p>The search key is hashed in the same way as for insertion, producing a nibble path.</p></li></ul><h4>b. Traversing the Tree</h4><ul><li><p>Starting at the root, the algorithm follows each nibble as a branch index.</p></li><li><p>At each branch node, it checks if the branch exists for the current nibble:</p><ul><li><p>If yes, continue following the path.</p></li><li><p>If no, the key does not exist in the trie (return null).</p></li></ul></li></ul><h4>c. Reaching a Leaf</h4><ul><li><p>Once a leaf node is reached, the algorithm checks whether the key stored in the leaf matches the search key.</p><ul><li><p>If it matches, return the value.</p></li><li><p>If not, return null.</p></li></ul></li></ul><h3>Example:</h3><p>Looking for <code>0x0001</code></p><pre><code>Root
&#9492;&#9472;&#9472; [4] &#8594; Branch
      &#9492;&#9472;&#9472; [b] &#8594; Branch
            &#9492;&#9472;&#9472; [f] &#8594; ...
                  &#9492;&#9472;&#9472; [4] &#8594; Leaf (key: 0x0001, value: 100)</code></pre><ul><li><p>Each branch index is determined by a nibble of the hashed key.</p></li><li><p>If any part of the path is missing, or the final leaf key does not match, the lookup fails.</p></li></ul><h4>Edge Cases</h4><ul><li><p><strong>Missing Branch:</strong> The path for the key doesn&#8217;t exist in the trie (returns null).</p></li><li><p><strong>Mismatched Leaf:</strong> The leaf node is reached, but the key in the leaf is different (returns null).</p></li></ul><p></p><h2>How Is This Used in Blockchains?</h2><p>Every time the state trie is updated, the root hash changes. This new hash uniquely identifies the blockchain&#8217;s state at that exact moment. You can use any previous root hash to "travel back" and query or prove what the state was&#8212;just like how you can check out any previous commit in Git to see an old version of your codebase. The StateTrie is thus the engine for both real-time state access and historical, tamper-proof snapshots.</p><pre><code>   ...  Root Hash A   &#8594;   Root Hash B   &#8594;   Root Hash C   ...
         (block 100)      (block 101)       (block 102)
           &#9474;                 &#9474;                  &#9474;
         Trie A           Trie B             Trie C
           &#9474;                 &#9474;                  &#9474;
   [state after      [state after        [state after
    block 100]        block 101]          block 102]</code></pre><p>You can always query any past state by starting from its root hash.<br></p><h3>Finding an Account&#8217;s Value in a Specific State</h3><p>To find the balance or data for a specific account at a given point in time (a specific block number):</p><ol><li><p>Get the root hash of the state you&#8217;re interested in (for example, a certain block number).</p></li><li><p>Hash the account&#8217;s address to generate the path through the StateTrie.</p></li><li><p>Traverse the tree from the root hash, following each nibble (hex digit) of the hashed key at each branch.</p></li><li><p>When you reach a leaf node:</p><ul><li><p>If the leaf&#8217;s key matches the account you&#8217;re looking for, return the value (such as the account balance).</p></li><li><p>If not, the account does not exist in that state.</p></li></ul></li></ol><pre><code><code>At block #123, the state root is: 0xabcdef...
You want to check the balance of address 0x1234... in this state.

1. Use the state root to start the trie traversal.
2. Hash 0x1234... to get the path [4, b, f, ...].
3. Follow this path through the trie, moving from root to each branch node.
4. Reach the leaf node. Check: Is it 0x1234...? If yes, return value. If not, not found.</code></code></pre><h1>Final Summary</h1><p>The StateTrie is how blockchains like Ethereum keep track of all account and contract data &#8212; securely and efficiently. It turns the entire state into a hashed tree where each update only touches part of the structure, making writes fast and storage lean. Every change creates a new root hash, giving you an exact snapshot of the system at any block. With just that root, you can verify, query, or roll back to any past state. Whether you're updating balances or digging into history, the StateTrie keeps the state consistent, verifiable, and ready for whatever&#8217;s next.</p><div><hr></div><div class="captioned-button-wrap" data-attrs="{&quot;url&quot;:&quot;https://0xshardlab.substack.com/p/a-practical-guide-to-statetrie-how?utm_source=substack&utm_medium=email&utm_content=share&action=share&quot;,&quot;text&quot;:&quot;Share&quot;}" data-component-name="CaptionedButtonToDOM"><div class="preamble"><p class="cta-caption">Thank you for reading. If you enjoy this post, feel free to share!</p></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://0xshardlab.substack.com/p/a-practical-guide-to-statetrie-how?utm_source=substack&utm_medium=email&utm_content=share&action=share&quot;,&quot;text&quot;:&quot;Share&quot;}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://0xshardlab.substack.com/p/a-practical-guide-to-statetrie-how?utm_source=substack&utm_medium=email&utm_content=share&action=share"><span>Share</span></a></p></div><p></p>]]></content:encoded></item><item><title><![CDATA[Essential Knowledge for Developers Venturing into Blockchain]]></title><description><![CDATA[The Minimum You Need to Know to Build or Integrate with Blockchain &#8212; from Nodes and Wallets to Smart Contracts and Gas]]></description><link>https://0xshardlab.substack.com/p/essential-knowledge-for-developers</link><guid isPermaLink="false">https://0xshardlab.substack.com/p/essential-knowledge-for-developers</guid><pubDate>Thu, 29 May 2025 02:00:33 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/59f6efc2-58d6-40e0-935b-bb33cc60439f_1456x1048.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>When you first encounter blockchain, you&#8217;ll likely come across concepts like decentralization, distributed ledgers, consensus algorithms, tokenomics, smart contracts, transactions, and more. Indeed, having a solid grasp of these ideas&#8212;and understanding how they all fit together&#8212;is crucial if you want to fully comprehend blockchain technology.</p><p>However, in this piece, I&#8217;d like to focus more on the knowledge <em>developers</em> need to actually build or integrate with blockchain projects.</p><blockquote><p>&#8220;When you&#8217;re faced with the situation of having to develop on or connect to a blockchain, what are the minimum essentials you need to know?&#8221;</p></blockquote><p>The following sections aim to give you a broad overview.</p><div><hr></div><h2>1. Traditional Server/Client Architecture vs. Blockchain</h2><p>In typical web or mobile applications, the flow usually looks like this</p><ul><li><p>Client (Web/App) &#8594; Server (Backend) &#8594; Database (Data Storage)</p></li><li><p>A central server handles most of the logic, and data is stored and retrieved from a database.</p></li></ul><p><strong>Blockchain</strong> takes a very different approach:</p><ul><li><p>Multiple nodes in the blockchain network all share the same ledger (data).</p></li><li><p>There&#8217;s no central server; instead, these nodes record new transactions via a consensus mechanism.</p></li><li><p>Anyone can run a node or access the data, and even if one node goes down, the network as a whole continues to function.</p></li></ul><p>The key point here is that it&#8217;s the entire <em>network</em>, not a single server or database, that manages and verifies the data. As a result, changes (transactions) are handled very carefully, ensuring immutability and transparency.</p><div><hr></div><h2>2. So, Is Blockchain a Database?</h2><p>Blockchain isn&#8217;t really a direct replacement for traditional RDB or NoSQL databases. Rather, it&#8217;s a specialized data structure designed for transparency and immutability.</p><ul><li><p>When you add data (write) via a transaction, it must pass network validation and be shared by all nodes.</p></li><li><p>Once recorded, it&#8217;s very hard to change or delete.</p></li><li><p>Large amounts of data can be expensive (due to gas fees), and on-chain storage is not highly efficient.</p></li></ul><p>In practical implementations, crucial data&#8212;like ownership or transaction records&#8212;goes on-chain, while secondary data (images, logs, metadata, etc.) is stored off-chain (IPFS or a traditional database), then linked via hashes or references.</p><ul><li><p>Event-based history<strong>:</strong> When a contract&#8217;s state changes, it emits events, which can be indexed for easy retrieval.</p></li><li><p>Indexing solutions (e.g., The Graph): They gather events from the blockchain and store them in a more query-friendly form&#8212;like a database&#8212;for use by your dApp.</p></li></ul><p>The main takeaway is that <strong>blockchains are designed for trust and immutability, not for dumping and querying massive data</strong>. Often, you run a separate indexer to convert &#8220;events &#8594; DB,&#8221; boosting efficiency and convenience.</p><div><hr></div><h2>3. Blockchain Nodes: Run Your Own or Use a Service?</h2><p>We noted that a node plays the role of both server and database in a blockchain. But <strong>must you operate your own node</strong> to interact with a blockchain? Not necessarily.</p><p><strong>1) Running your own node</strong></p><ul><li><p>For major chains like Ethereum, running a full node requires hundreds of gigabytes of storage and a stable network connection.</p></li><li><p>Costs, maintenance, and DevOps expertise can be significant.</p></li><li><p>However, if you need large-scale on-chain data collection&#8212;like analyzing high-frequency transactions, running trading bots, or conducting big data research&#8212;operating your own node can be advantageous.</p></li></ul><p><strong>2) Using external node services</strong></p><ul><li><p>Services like Infura, Alchemy, or QuickNode provide RPC endpoints (URLs), giving you direct access to blockchain functions (submitting transactions, querying data, etc.) without managing infrastructure yourself.</p></li><li><p>Many dApp projects rely on these to get up and running quickly.</p></li></ul><p><strong>3) Local development nodes</strong></p><ul><li><p>Tools like Ganache and Hardhat Network let you spin up a &#8220;fake blockchain&#8221; locally.</p></li><li><p>Perfect for testing, learning, or personal side projects.</p></li></ul><p>In short, <strong>you don&#8217;t always need to run your own node</strong>. You can decide based on your project&#8217;s traffic, data needs, and operational resources.</p><div><hr></div><h2>4. User Authentication: &#8220;Log In&#8221; vs. &#8220;Connect Wallet&#8221;</h2><p>In traditional web or mobile apps, we typically use ID/PW, JWT, OAuth, etc. for user authentication.<br>In blockchain dApps, however, wallets (like MetaMask, Trust Wallet, etc.) handle the concept of &#8220;authentication.&#8221;</p><ul><li><p>A wallet stores the user&#8217;s private key, enabling them to sign transactions.</p></li><li><p>Authentication Flow:</p><ol><li><p>The dApp (client) requests a signature for a specific message (or transaction).</p></li><li><p>The user&#8217;s wallet signs it with their private key and returns the signature.</p></li><li><p>The dApp verifies that the public key (wallet address) and the signature match, confirming the user&#8217;s ownership of that address.</p></li></ol></li></ul><p>Security Implications</p><ul><li><p>There is no central server storing your password; instead, users themselves manage their private keys.</p></li><li><p>If a private key is lost or compromised, there&#8217;s almost no way to recover it (no &#8220;reset password&#8221; option).</p></li><li><p>Familiar features like MFA or password recovery aren&#8217;t typically available.</p></li></ul><p>To address these challenges, various solutions are emerging, such as:</p><ul><li><p>Account Abstraction (AA): where the wallet is a smart contract account, enabling features like social recovery or fee subsidization.</p></li><li><p>Seedless logins, smart contract wallets, and other innovations to improve security and user experience.</p></li></ul><div><hr></div><h2>5. Smart Contracts</h2><p>A blockchain isn&#8217;t just a ledger of data&#8212;it allows you to run code (program logic) directly on-chain. We call this code a smart contract.</p><h4>5.1 What Is a Smart Contract?</h4><ul><li><p><strong>Immutable Code</strong><br>Once deployed to the blockchain, the code is difficult (or sometimes impossible) to modify.<br>If a bug is discovered in the logic, patching can be extremely difficult. Thorough testing and security audits are critical.</p></li><li><p><strong>Decentralized Trust</strong><br>Unlike a scenario where you host code on a private server and claim &#8220;this is how it works,&#8221;<br>a blockchain-based contract is openly deployed on the network. Anyone can review the logic and see exactly how it&#8217;s executed.<br>(For example, by plugging the contract address into a block explorer to verify the source code and transaction history.)</p></li><li><p><strong>Deployment Process</strong><br>You write code in languages like Solidity or Vyper (for Ethereum), compile it, then send a deploy transaction.<br>The contract is assigned a unique address (like a wallet address starting with <code>0x</code>).<br>Once deployed, anyone can interact with it by sending transaction calls to that address. The network executes the logic and records the results on-chain.</p></li></ul><h4>5.2 Tokens, NFTs, and DeFi Are Also Smart Contracts</h4><p>Some of the most prominent uses of smart contracts include <strong>tokens</strong> (ERC-20), <strong>NFTs</strong> (ERC-721), and <strong>DeFi</strong> protocols.</p><ul><li><p><strong>ERC-20 (Tokens)</strong><br>The standard interface for creating interoperable tokens on Ethereum.<br>If you implement functions like <code>balanceOf</code>, <code>transfer</code>, and <code>approve</code>, wallets and exchanges automatically recognize your token and enable features like balance checks and transfers.</p></li><li><p><strong>ERC-721 (NFTs)</strong><br>A standard for non-fungible tokens.<br>Each token has a unique ID, allowing ownership of each item to be distinctly tracked&#8212;common for artwork, profile pictures, in-game items, memberships, etc.</p></li><li><p><strong>DeFi (Decentralized Finance)</strong><br>Smart contracts implementing financial services such as lending, staking, swaps, or liquidity pools.<br>Anyone can use them under the same conditions, without a central authority.</p></li></ul><h4>5.3 Example: Basic ERC-20 Code</h4><p>Below is a simple example illustrating how an ERC-20 contract might work. (In practice, the standard includes events and more functions, but this showcases the core logic.)</p><pre><code>pragma solidity ^0.8.0;

contract MyToken {
    mapping(address =&gt; uint256) private _balances;

    string public name = "MyToken";
    string public symbol = "MTK";
    uint8 public decimals = 18;
    uint256 public totalSupply;

    constructor(uint256 initialSupply) {
        _balances[msg.sender] = initialSupply;
        totalSupply = initialSupply;
    }

    function balanceOf(address account) external view returns (uint256) {
        return _balances[account];
    }

    function transfer(address to, uint256 amount) external returns (bool) {
        require(_balances[msg.sender] &gt;= amount, "Not enough balance");
        _balances[msg.sender] -= amount;
        _balances[to] += amount;
        return true;
    }
}</code></pre><p>You&#8217;ll see it tracks balances in a <code>mapping</code> and uses a <code>transfer</code> function to update them. <strong>ERC-721 (NFT)</strong> follows a similar model, except each token ID is unique and individually tracked.</p><p>In essence, <strong>&#8220;tokens&#8221; or &#8220;NFTs&#8221; are simply interfaces provided by smart contracts</strong>.</p><div><hr></div><h2>6. Transactions &amp; Gas Fees</h2><ul><li><p><strong>Transaction</strong>: Any operation that changes blockchain state (like sending funds or calling a contract).</p></li><li><p><strong>Gas Fee</strong>: The cost paid to the network (in ETH, MATIC, BNB, etc., depending on the chain) for processing each transaction.</p></li></ul><p>As a developer, consider:</p><ul><li><p><strong>UX</strong></p><ul><li><p>Every time a user sends a transaction, they must confirm it in their wallet and pay a gas fee.</p></li><li><p>Frequent transaction prompts may hurt usability. You usually want to design so that on-chain actions happen only when truly necessary.</p></li></ul></li><li><p><strong>Cost</strong></p><ul><li><p>Large transaction volumes can drive gas costs up significantly.</p></li><li><p>Each chain has different fees and throughput, so choosing the right chain can greatly affect the business model.</p></li></ul></li></ul><div><hr></div><h2>7. ABI (Application Binary Interface)</h2><p>Once you deploy a smart contract, how do you actually call its functions or access its variables?<br>Think of how REST APIs require you to know the endpoints, parameters, and methods.<br>Similarly, a contract needs a description of <strong>&#8220;which functions exist and what parameters/return types they use.&#8221;</strong><br>That&#8217;s exactly what the <strong>ABI (Application Binary Interface)</strong> is for.</p><p>Without the ABI, simply knowing the contract&#8217;s address tells you nothing about how to interact with its internal functions. Hence, to use a deployed contract, you must have its ABI.</p><p><strong>Example</strong><br>Suppose an ERC-20 token contract has <code>balanceOf</code> and <code>transfer</code> as part of its ABI:</p><pre><code>[
  {
    "type": "function",
    "name": "balanceOf",
    "inputs": [
      { "name": "account", "type": "address" }
    ],
    "outputs": [
      { "type": "uint256" }
    ]
  },
  {
    "type": "function",
    "name": "transfer",
    "inputs": [
      { "name": "to", "type": "address" },
      { "name": "amount", "type": "uint256" }
    ],
    "outputs": [
      { "type": "bool" }
    ]
  }
]
</code></pre><ul><li><p><strong>balanceOf</strong>: takes an <code>address</code> and returns a <code>uint256</code> balance</p></li><li><p><strong>transfer</strong>: takes a <code>to</code> address and an <code>amount</code> (<code>uint256</code>), returns a <code>bool</code> indicating success</p></li></ul><p>Using this ABI, you can call these contract functions. For instance, in <strong><a href="https://docs.ethers.org/v6/getting-started/">ethers.js</a></strong>:</p><pre><code>import { ethers } from "ethers";
import MyTokenABI from "./MyToken.json"; // Precompiled contract ABI

// Deployed contract address
const tokenAddress = "0x123456789ABCDEF...";

// Create contract instance with address + ABI
const tokenContract = new ethers.Contract(tokenAddress, MyTokenABI, signer);

// Example: check balance
const balance = await tokenContract.balanceOf("0xABCD1234...");
console.log(balance.toString());

// Example: transfer tokens
await tokenContract.transfer("0xEFGH5678...", 100);</code></pre><p><strong>ABI</strong> is effectively the &#8220;spec sheet&#8221; for function signatures: which parameters they require and what they return. If you&#8217;re curious how the actual <strong>function call</strong> process works, it involves:</p><ol><li><p>Calculating a 4-byte <a href="https://docs.soliditylang.org/en/latest/abi-spec.html#function-selector">function selector</a> (via <code>Keccak-256</code> of the function signature),</p></li><li><p>Encoding parameters into <code>call data</code>,</p></li><li><p>Sending it to the EVM, which uses the selector + encoded arguments to execute the correct function.</p></li></ol><p>For more details on the encoding rules, check the <a href="https://docs.soliditylang.org/en/latest/abi-spec.html">official documentation.</a></p><div><hr></div><h2>8. Overlooked Challenges: Security, Cost, and UX</h2><ul><li><p><strong>Security</strong></p><ul><li><p>Once deployed, a smart contract is hard to patch. A single bug can lead to massive loss of funds.</p></li><li><p>Projects dealing with large amounts of value usually invest in professional security audits.</p></li></ul></li><li><p><strong>Cost (Gas Fees)</strong></p><ul><li><p>A large number of transactions can become expensive and inconvenient for users.</p></li><li><p>Decide how to handle those costs (do users pay or does the platform subsidize?).</p></li></ul></li><li><p><strong>UX</strong></p><ul><li><p>Everything from installing a wallet to paying gas and waiting for confirmations differs from typical web apps.</p></li><li><p>Often, it&#8217;s best to limit on-chain transactions to only what&#8217;s absolutely necessary, lowering user friction.</p></li></ul></li></ul><div><hr></div><h2>9. With So Many Blockchains, Which Do You Choose?</h2><p>Ethereum, Polygon, BNB Chain, Avalanche, Solana&#8230; the list continues to grow. Each chain has its own strengths and weaknesses, so choose based on your project&#8217;s needs:</p><ul><li><p><strong>Gas Fees &amp; Throughput</strong></p><ul><li><p>Ethereum offers robust security and a vast ecosystem but has relatively high gas fees.</p></li><li><p>Polygon (a Layer 2 solution) provides cheaper, faster transactions, but might involve bridging assets.</p></li></ul></li><li><p><strong>Ecosystem (Wallets, DEX, NFT marketplaces, etc.)</strong></p><ul><li><p>A chain that users and developers already widely adopt can reduce friction (e.g., MetaMask support, established markets, etc.).</p></li></ul></li><li><p><strong>Security &amp; Decentralization</strong></p><ul><li><p>A chain with fewer nodes or weaker consensus might pose risks to trust and reliability.</p></li></ul></li></ul><p>Ultimately, factors like gas fees, processing speed, security, and ecosystem support should guide your decision. Your project&#8217;s size, goals, and user base will shape the best choice.</p><div><hr></div><h2>Final Thoughts</h2><p>There&#8217;s no need to dive deeply into consensus algorithms, tokenomics, sidechains, or Layer 2 solutions right away. A good starting point is to experiment on a testnet, deploy a basic smart contract, and send transactions from a wallet to get familiar with the flow. This hands-on approach will give you a taste of the UX and security challenges inherent in blockchain-based applications. Later, you can dive deeper into more advanced topics as your project demands.</p><p></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://0xshardlab.substack.com/p/essential-knowledge-for-developers?utm_source=substack&utm_medium=email&utm_content=share&action=share&quot;,&quot;text&quot;:&quot;Share&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://0xshardlab.substack.com/p/essential-knowledge-for-developers?utm_source=substack&utm_medium=email&utm_content=share&action=share"><span>Share</span></a></p><p></p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://0xshardlab.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! Subscribe for free to receive new posts and support my work.</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[How is Smart Contract development different from others?]]></title><description><![CDATA[What if you faced a kind of development work where you couldn&#8217;t make any changes once it was written?]]></description><link>https://0xshardlab.substack.com/p/how-is-smart-contract-development</link><guid isPermaLink="false">https://0xshardlab.substack.com/p/how-is-smart-contract-development</guid><dc:creator><![CDATA[SHARDLAB]]></dc:creator><pubDate>Thu, 08 May 2025 10:10:21 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/e24f1875-e8cf-42a2-b3f8-346c8ec6e358_1456x1048.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>If you&#8217;re a developer exploring blockchain for the first time, you&#8217;ll inevitably come across the concept of Smart Contracts. You&#8217;ll also encounter the word &#8220;Dapp,&#8221; leading you to realize that, indeed, you can run applications on the blockchain via Smart Contracts. You may even hear about various security incidents involving Dapps.</p><p>No matter what domain you&#8217;re currently in as a developer, you&#8217;ve probably asked yourself at least once:<br>&#8220;Alright, so what makes Smart Contract development different from the kind of development I&#8217;m used to (or from general software development)?&#8221;</p><p>This article is written for those who are wrestling with exactly that question. It is based on the author&#8217;s personal experience and opinions.</p><div><hr></div><h2>Blockchain and Smart Contracts</h2><p>Put very simply, when developing a typical software product (application), we divide it into a front end (web/app) and a back end (server), using a client-server architecture. The server handles business logic, stores critical data in a database (DB), and provides data to the client. The client, in turn, provides a user interface based on this data.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Se_Y!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa2c9ac95-f0c9-4c0b-af51-e77ec9b0c8d0_1760x349.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Se_Y!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa2c9ac95-f0c9-4c0b-af51-e77ec9b0c8d0_1760x349.png 424w, https://substackcdn.com/image/fetch/$s_!Se_Y!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa2c9ac95-f0c9-4c0b-af51-e77ec9b0c8d0_1760x349.png 848w, https://substackcdn.com/image/fetch/$s_!Se_Y!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa2c9ac95-f0c9-4c0b-af51-e77ec9b0c8d0_1760x349.png 1272w, https://substackcdn.com/image/fetch/$s_!Se_Y!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa2c9ac95-f0c9-4c0b-af51-e77ec9b0c8d0_1760x349.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Se_Y!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa2c9ac95-f0c9-4c0b-af51-e77ec9b0c8d0_1760x349.png" width="1456" height="289" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a2c9ac95-f0c9-4c0b-af51-e77ec9b0c8d0_1760x349.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:289,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:31756,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://0xshardlab.substack.com/i/162179659?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa2c9ac95-f0c9-4c0b-af51-e77ec9b0c8d0_1760x349.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Se_Y!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa2c9ac95-f0c9-4c0b-af51-e77ec9b0c8d0_1760x349.png 424w, https://substackcdn.com/image/fetch/$s_!Se_Y!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa2c9ac95-f0c9-4c0b-af51-e77ec9b0c8d0_1760x349.png 848w, https://substackcdn.com/image/fetch/$s_!Se_Y!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa2c9ac95-f0c9-4c0b-af51-e77ec9b0c8d0_1760x349.png 1272w, https://substackcdn.com/image/fetch/$s_!Se_Y!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa2c9ac95-f0c9-4c0b-af51-e77ec9b0c8d0_1760x349.png 1456w" sizes="100vw" fetchpriority="high"></picture><div></div></div></a></figure></div><p>In a Dapp (Decentralized Application), which runs on the blockchain, the logic is written in a Smart Contract and is executed directly on the blockchain.</p><p>So, how is this different from the traditional structure? Think of the blockchain as the database (DB), and the Smart Contract as the server&#8217;s business logic. Of course, to handle more complex queries or improve performance, you can add a separate indexing server. But fundamentally, the Smart Contract is in charge of the server&#8217;s core logic.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!k3uC!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe77d6f73-7c3f-47d4-a34b-316bbf0c403b_1751x663.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!k3uC!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe77d6f73-7c3f-47d4-a34b-316bbf0c403b_1751x663.png 424w, https://substackcdn.com/image/fetch/$s_!k3uC!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe77d6f73-7c3f-47d4-a34b-316bbf0c403b_1751x663.png 848w, https://substackcdn.com/image/fetch/$s_!k3uC!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe77d6f73-7c3f-47d4-a34b-316bbf0c403b_1751x663.png 1272w, https://substackcdn.com/image/fetch/$s_!k3uC!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe77d6f73-7c3f-47d4-a34b-316bbf0c403b_1751x663.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!k3uC!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe77d6f73-7c3f-47d4-a34b-316bbf0c403b_1751x663.png" width="1456" height="551" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e77d6f73-7c3f-47d4-a34b-316bbf0c403b_1751x663.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:551,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:62262,&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;:&quot;https://0xshardlab.substack.com/i/162179659?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe77d6f73-7c3f-47d4-a34b-316bbf0c403b_1751x663.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!k3uC!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe77d6f73-7c3f-47d4-a34b-316bbf0c403b_1751x663.png 424w, https://substackcdn.com/image/fetch/$s_!k3uC!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe77d6f73-7c3f-47d4-a34b-316bbf0c403b_1751x663.png 848w, https://substackcdn.com/image/fetch/$s_!k3uC!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe77d6f73-7c3f-47d4-a34b-316bbf0c403b_1751x663.png 1272w, https://substackcdn.com/image/fetch/$s_!k3uC!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe77d6f73-7c3f-47d4-a34b-316bbf0c403b_1751x663.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 role="img" 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><title></title><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></figure></div><p>Here&#8217;s a question you might ask: &#8220;Then, is Smart Contract development basically back-end (server) development?&#8221; The answer is, no&#8212;it&#8217;s not quite the same.</p><div><hr></div><h2>What Makes Smart Contract Development Different?</h2><h3>A Deep Understanding of the Blockchain</h3><ul><li><p>Front-end developers need a basic grasp of how browsers work, the DOM structure, and UI event flows.</p></li><li><p>Back-end developers need to be comfortable with networking, databases, and request-response structures.</p></li><li><p>Likewise, a Smart Contract developer must have a thorough understanding of the blockchain&#8217;s architecture and how it operates.</p></li></ul><p>There&#8217;s a lot to learn, but at the very least, you should be able to answer questions like:</p><ul><li><p><strong>How does a transaction get processed and included in a block? (What is a transaction&#8217;s life cycle?)</strong></p></li><li><p><strong>When a transaction is executed, how does the state change?</strong></p></li><li><p><strong>Why do transaction fees occur, and how much do they cost?</strong></p></li></ul><p>Just as a server environment requires CPU, memory, and disk resources to run logic or store data, running a Smart Contract on a blockchain also consumes computational and storage resources from blockchain nodes. Because of this, every transaction you submit incurs a fee (e.g., gas), which can vary greatly depending on how you design your contract. In other words, resource efficiency is directly tied to cost efficiency.</p><p>Most importantly, blockchains are fundamentally immutable. Once a contract is deployed, it typically can&#8217;t be modified. If you have flawed logic or an inefficient state design, you can&#8217;t just fix it with a simple patch.</p><p>Moreover, each chain has its own structure and unique characteristics that are crucial to understand.</p><p>For instance:</p><ul><li><p><strong>Ethereum (EVM-based)</strong> uses an Account model, with accounts categorized as EOA (Externally Owned Account) or CA (Contract Account). State data is stored in Contract Accounts, managed in a key-value storage format. Logic and state are defined together within a single contract.</p></li><li><p><strong>Solana</strong>, on the other hand, takes a very different approach. The logic resides in what&#8217;s called a Program Account (PA), and the state is stored in a separate account called a Program Derived Address (PDA). The PDA is &#8220;owned&#8221; by a specific Program Account (PA), and the state data is recorded in the PDA. In other words, logic and state are physically separated, and you, the developer, need to explicitly design how to connect them.</p></li></ul><p>These structural differences go far beyond simple syntax; they alter your entire way of thinking when designing a Smart Contract.</p><p>For example, the approach to storing and managing state data, understanding which resources you can access, calculating the associated costs, how complex a single transaction&#8217;s logic can be, or whether parallel processing is possible&#8212;all of these depend on the specific blockchain&#8217;s architecture. Hence, you need to keep each chain&#8217;s characteristics and constraints in mind from the very early stages of design.</p><p>Ultimately, merely learning the language syntax isn&#8217;t enough for proper Smart Contract development. You need a deep understanding of the blockchain as an execution environment. Otherwise, you risk writing contracts that don&#8217;t work as intended&#8212;or even worse, open the door to catastrophic asset losses.</p><p></p><h4>Blockchain in Smart Contract</h4><p>Let&#8217;s look at how all this impacts your code. We&#8217;ll use Solidity&#8212;arguably the most popular language on the EVM&#8212;to illustrate.</p><pre><code><code>// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract SimpleWallet {
    mapping(address =&gt; uint256) public balances;
    address[] public depositors;

    function deposit() external payable {
        bool exists = false;
        for (uint i = 0; i &lt; depositors.length; i++) {
            if (depositors[i] == msg.sender) {
                exists = true;
                break;
            }
        }
        if (!exists) {
            depositors.push(msg.sender);
        }
        balances[msg.sender] += msg.value;
    }

    function withdraw() external {
        uint256 amount = balances[msg.sender];
        (bool sent, ) = msg.sender.call{value: amount}("");
        require(sent, "Transfer failed");
        balances[msg.sender] = 0;
    }
}
</code></code></pre><p>Solidity looks a bit like JavaScript, so at first glance it may not seem too difficult to understand. The contract&#8217;s functionality is simple: you can deposit ETH, and the contract records the depositor&#8217;s address. A depositor can withdraw the exact amount they&#8217;ve deposited.</p><p>However, this contract has some issues:</p><ol><li><p><strong>There&#8217;s no receive function.</strong><br>To receive ETH in a contract, you need a <code>receive</code> function. Without it, depositing ETH will fail (although depositing <code>0 ETH</code> might succeed).</p></li><li><p><strong>No consideration for gas fees&#8212;list traversal.</strong><br>In Solidity, every operation costs gas. As the list of depositors grows, the for-loop runs longer, causing higher gas fees. Excessive gas usage can lead to failed transactions.</p></li><li><p><strong>Vulnerability to reentrancy attacks.</strong></p></li></ol><p><strong>Reentrancy Attacks</strong></p><p>A reentrancy (or &#8220;re-entry&#8221;) attack occurs when one contract calls an external contract (or address), and the called contract (the attacker) calls back into the original contract&#8217;s function before the first execution completes. It&#8217;s akin to recursion.</p><p>Let&#8217;s assume we added a receive function:</p><pre><code><code>// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract SimpleWallet {
&#9;...
&#9;receive() external payable {}
}
</code></code></pre><p>There&#8217;s a feature in Solidity:</p><ul><li><p>A contract can call another contract.</p></li><li><p>Logic is executed sequentially.</p></li><li><p>When ETH is transferred, <code>receive</code> is triggered.</p></li></ul><p>These features allow for a reentrancy attack. With the following exploit contract, an attacker could withdraw more ETH than they originally deposited</p><pre><code><code>contract ReentrancyExploit {
    SimpleWallet public simpleWallet;

    constructor(address _simpleWallet) {
        simpleWallet = SimpleWallet(_simpleWallet);
    }

    // entrancy
    function attack() external payable {
        simpleWallet.deposit{value: msg.value}();
        simpleWallet.withdraw();  
    }

    // reentrancy
    receive() external payable {
        if (address(simpleWallet).balance &gt;= msg.value) {
            simpleWallet.withdraw(); 
        }
    }
}
</code></code></pre><p>Here&#8217;s how an attack might unfold when <code>attack()</code> is called:</p><ol><li><p><code>simpleWallet.deposit{value: msg.value}()</code> is called.</p></li><li><p>Then <code>simpleWallet.withdraw()</code> is called, which executes a <code>call</code> function inside <code>simpleWallet</code> that sends ETH back to <code>ReentrancyExploit</code>.</p></li><li><p>During this call, <code>ReentrancyExploit</code>&#8217;s <code>receive()</code> function is triggered:</p></li></ol><pre><code>(bool sent, ) = msg.sender.call{value: amount}("");</code></pre><ol start="4"><li><p>The <code>receive()</code> function calls <code>simpleWallet.withdraw()</code> again.</p></li><li><p>Since <code>balances[msg.sender]</code> hasn&#8217;t yet been reset to zero, the attacker can continue withdrawing the same amount repeatedly.</p></li></ol><p>Even this simple logic shows how a deeper understanding of the blockchain is necessary to write Smart Contracts.</p><blockquote><p>For more information about reentrancy attacks, see <a href="https://blog.chain.link/reentrancy-attacks-and-the-dao-hack/">here</a>.</p></blockquote><div><hr></div><h2>Required Knowledge / Skills</h2><h4>In-depth Blockchain Knowledge</h4><p>Understanding the transaction life cycle and how transactions are processed is fundamental. You also need to know how each blockchain network&#8217;s consensus mechanism and virtual machine (VM) operate.</p><p>Moreover, learning about various types of DApps&#8212;DeFi, NFTs, and beyond&#8212;is extremely helpful when designing and implementing real Smart Contracts.</p><p>Although blockchain can be segmented into many areas&#8212;DeFi, infrastructure, security, crypto-economics&#8212;they&#8217;re all interconnected. You should aim for a big-picture understanding that goes beyond a single niche.</p><h3>Testing and Debugging</h3><p>Because Smart Contracts run in a blockchain environment, testing and debugging them isn&#8217;t as straightforward as in typical applications. The blockchain is <strong>immutable</strong>, so once a state changes, you can&#8217;t roll it back. Transactions are atomic, making state changes mid-execution tough to track, and every state-changing transaction costs money.</p><p>This means you can&#8217;t rely on some of the standard testing strategies you might use in a typical app&#8212;like rolling back a DB to a prior state or stepping through your code with breakpoints in production.</p><p>Still, testing and debugging are crucial in Smart Contract development because a single bug can lead directly to asset losses. And once a contract is deployed, it&#8217;s nearly impossible to roll back mistakes.</p><p>Fortunately, tools like Hardhat, Foundry, and Truffle have emerged to mitigate these challenges. They offer features like <code>console.log</code> debugging, fork testing, and VM-simulated environments. Even so, debugging in a blockchain VM is still difficult, and it ultimately falls on you to implement meticulous logic and thorough tests to ensure everything works as intended.</p><h4>Adhering to Standards and Tracking Updates</h4><p>Blockchain has multiple standards that must be understood. They&#8217;re tied not only to ecosystem compatibility but also gas optimization and security.</p><p>Take ERC20, the most widely used standard for fungible tokens, as an example. Its <code>transfer</code> function must adhere to this interface</p><pre><code>function transfer(address to, uint256 value) external returns (bool)</code></pre><p>If <code>transfer</code> succeeds, it should return <code>true</code>, otherwise <code>false</code>. What if there&#8217;s an ERC20 token that doesn&#8217;t actually return a bool?</p><pre><code>function withdraw(address token, address to, uint256 amount) external {
   bool isSuccess = IERC20(token).transfer(to, amount);
   require(isSuccess, 'failed to withdraw);
}</code></pre><p>In this snippet, <code>transfer</code> is expected to return a bool, but if it doesn&#8217;t, decoding fails and the transaction reverts. This scenario can actually happen and cause assets to be stuck (&#8220;frozen&#8221;).</p><p>New standards keep emerging, and existing ones can change via EIPs (Ethereum Improvement Proposals). You need to keep track of these changes and quickly adapt to stay safe and compatible. This ongoing awareness isn&#8217;t just academic&#8212;it&#8217;s vital to real-world development.</p><h3>Logical Thinking</h3><p>All of the above ultimately ties back to rigorous logic and problem-solving skills. Because of blockchain&#8217;s immutability and difficulty in patching mistakes, Smart Contracts demand more careful condition checks and error handling than typical applications.</p><p>For instance, in back-end development, a bug might be fixable by rolling back the DB or manually correcting data. In Smart Contracts, once a transaction has executed and changed state, there&#8217;s no going back.</p><p>You can design contracts to be upgradeable, but even that approach introduces new complexities and can pose additional security risks. It&#8217;s no magic bullet.</p><p>Therefore, a Smart Contract developer&#8217;s job isn&#8217;t just about producing functional code; it&#8217;s about anticipating edge cases and eliminating even minor flaws. This level of thoroughness and logical precision is the most crucial trait of a Smart Contract engineer, more so than any specific skill with tools or languages.</p><div><hr></div><h2>The Necessary Tech Stack</h2><h4>Programming Languages</h4><p>Smart Contracts run on a Virtual Machine, which requires bytecode the VM can understand. We write it in a high-level (human-readable) language that compiles down to that bytecode. On EVM-based platforms, Solidity is the most popular. On Solana (SVM), Rust is the go-to. You&#8217;ll need to learn whatever language the platform demands.</p><p>Generally, developers stick to Solidity and Rust because of their ecosystems and available tooling. However, these aren&#8217;t the only options. For the EVM, there&#8217;s also the Python-like Vyper or the low-level Yul language. On Solana, you can even use C/C++. Unless you have a specific need, you&#8217;ll likely be most productive with the widely adopted languages and tooling.</p><h3>Frameworks</h3><p>Smart Contract development involves compiling, testing, debugging, and deploying. Frameworks exist to streamline each step, boosting your productivity. You can do it all without a framework, but it&#8217;s much more cumbersome.</p><p>On the EVM side, you can use <a href="https://hardhat.org/">Hardhat</a> (JavaScript/TypeScript) or <a href="https://book.getfoundry.sh/forge/">Foundry</a> (Solidity native). On Solana, <a href="https://www.anchor-lang.com/docs">Anchor</a> is widely used. Each has its pros and cons, so choose what suits your needs.</p><h3>Various Tools and Third-Party Services</h3><p>It&#8217;s a good idea not to limit yourself. Experiment with tools like Tenderly, Etherscan, or node providers like Infura, Alchemy, or QuickNode. All can significantly enhance your development efficiency.</p><blockquote><p>As with any technology, simply knowing one language or framework won&#8217;t guarantee a great piece of software. All knowledge is interconnected and should be applied organically.</p></blockquote><div><hr></div><h2>The Mindset of a Smart Contract Developer</h2><p>In typical development, you assume some mistakes will happen&#8212;patches can be deployed, or a database can be updated. But these &#8220;safety nets&#8221; can also encourage sloppy work or create technical debt: leaving <code>TODO</code>s scattered around the code, skimping on tests, or ignoring error handling. While this might be acceptable in a traditional project as a trade-off, it&#8217;s not advisable in Smart Contract development.</p><p>A Smart Contract developer must always keep in mind that you &#8220;can&#8217;t easily fix things&#8221; later. You should strive for perfection from the outset. Mistakes directly translate to financial loss, sometimes in the tens or hundreds of millions of dollars. So you have to prioritize security and caution above all else.</p><p>Yes, you can design your contracts to be upgradeable, but this also affects the entire architecture and can expose new vulnerabilities. Upgradeability isn&#8217;t a silver bullet.</p><h2>Conclusion &amp; Learning Module</h2><p>Smart Contract development is about more than just writing code in a specific language. It requires a deep understanding of blockchain&#8217;s unique environment and demands meticulous design for code that can&#8217;t simply be edited later.</p><p>Every line of code, every choice in variable names or structures, can have huge implications for the assets of countless users. The degree of caution required is on another level compared to traditional software development.</p><p>On the flip side, once you overcome these challenges, the reward is immense: you&#8217;re creating an immutable<strong>, </strong>decentralized application that lives on the blockchain. That&#8217;s both fascinating and incredibly meaningful. I hope this article has offered some insight to those considering Smart Contract development.</p><p>We at [Shard Lab] provide a <strong><a href="https://github.com/shard-lab/protocol-camp">Learning Module</a></strong> designed to help developers deepen their understanding of blockchain. We aim to make it easy for you to gain experience in various blockchain environments&#8212;covering core, application, and tooling aspects across multiple chains. We hope it proves to be a valuable experience for you!</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://0xshardlab.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://0xshardlab.substack.com/subscribe?"><span>Subscribe now</span></a></p><p></p>]]></content:encoded></item><item><title><![CDATA[Randomness in Blockchain]]></title><description><![CDATA[How to ensure randomness in blockchain?]]></description><link>https://0xshardlab.substack.com/p/randomness-in-blockchain</link><guid isPermaLink="false">https://0xshardlab.substack.com/p/randomness-in-blockchain</guid><dc:creator><![CDATA[Toby Kim]]></dc:creator><pubDate>Fri, 25 Apr 2025 04:00:21 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/40922698-4533-42b1-8326-406ee0807032_1456x1048.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2>Did you know that blockchain doesn&#8217;t inherently support randomness?</h2><p>At its core, the blockchain system is designed with the philosophy that it must be more transparent and fair than any other system. All data must be verifiable, and anyone should be able to reproduce the same results under the same conditions.</p><p>However, this very property ironically becomes an obstacle when we try to generate &#8220;true randomness,&#8221; which must be unpredictable. Verifiability and unpredictability are, in essence, opposing concepts.</p><p>So in this article, we&#8217;ll be exploring a few key questions:</p><ul><li><p>Why is randomness such a challenging problem in blockchain?</p></li><li><p>How can we produce random numbers that are both unpredictable and tamper-proof?</p></li><li><p>What do the main solutions look like under the hood?</p></li><li><p>Which approach should we choose when implementing real-world applications?</p></li></ul><div><hr></div><h2>Defining Randomness</h2><p>A random number is a value whose outcome cannot be predicted and is statistically uniform in its distribution. In nature, random numbers (e.g., those derived from atmospheric noise or radioactive decay) rely on external physical phenomena, so we can call them &#8220;truly random.&#8221; However, a computer program fundamentally works in a deterministic manner, making it difficult&#8212;if not impossible&#8212;to produce &#8220;completely random&#8221; values purely from internal processes.</p><p>There are generally two approaches to generating random numbers on a computer:</p><ol><li><p><strong>True Random Number Generator (TRNG)</strong></p><ul><li><p>Relies on physical phenomena (like atmospheric noise or radioactive decay) measured by sensors to generate randomness.</p></li><li><p>It&#8217;s very hard to predict, but it often requires specialized hardware or sensors, can be expensive to implement, and may be slow.</p></li></ul></li><li><p><strong>Pseudo-Random Number Generator (PRNG)</strong></p><ul><li><p>Uses mathematical algorithms to produce sequences that appear random.</p></li><li><p>A given seed will always generate the same sequence of numbers if the algorithm is repeated.</p></li><li><p>This method is fast and convenient, and it&#8217;s used in most software-based random number generators. However, if the seed or internal state is leaked, it&#8217;s possible for an attacker to predict the future outputs.</p></li></ul></li></ol><p>In a decentralized system like a blockchain, transparency and verifiability are crucial. Yet we need a random number that&#8217;s unpredictable and still verifiable by every participant. Over the years, various solutions have been proposed, and we&#8217;ll walk through a few of the major approaches.</p><div><hr></div><h2>Why Randomness Is Tough on the Blockchain</h2><h3>Deterministic Environment</h3><p>Because all nodes in a blockchain network must maintain the same state, it&#8217;s not feasible to simply call a function like <code>random()</code> arbitrarily. If one node produces a different random value than another node, the network&#8217;s consensus would break.</p><h3>Transparency </h3><p>Any data stored on-chain is ultimately visible to everyone. Even if you try to keep your random seed hidden, at some point it may become public on the chain. As soon as it&#8217;s in a predictable state, attackers could exploit it.</p><h3>Block Producer (Miner/Validator) Manipulation</h3><p>Whether a blockchain is PoW (miner-based) or PoS (validator-based), the entity that creates the block can attempt to manipulate certain elements&#8212;like re-trying block hashes or rearranging transactions&#8212;to get a favorable outcome.</p><p>If you rely on block hashes or timestamps alone for randomness, a block producer might simply discard (&#8220;orphan&#8221;) a block if the resulting hash isn&#8217;t what they want. They can keep trying until they get a hash that suits them.</p><div><hr></div><h2>Attempts to Implement Randomness on the Blockchain</h2><h3>Hash-Based Randomness (Block Hash, Tx Hash, etc.)</h3><p>As mentioned, the blockchain is a deterministic and transparent environment, which makes it tough to achieve true randomness. While values like <code>blockhash</code>, <code>difficulty</code>, or <code>timestamp</code> may look &#8220;random,&#8221; they&#8217;re still produced deterministically by block producers and can be manipulated to some degree.</p><pre><code>pragma solidity ^0.8.0;

contract RandomContract {
    function getRandom() public view returns (uint256) {
        bytes32 blockHash = blockhash(block.number - 1);
        uint256 randomValue = uint256(
            keccak256(
                abi.encodePacked(blockHash, msg.sender, block.timestamp)
            )
        );
        return (randomValue % 100) + 1;
    }
}</code></pre><p>This doesn&#8217;t provide secure randomness. For high-stakes use cases&#8212;such as games, lotteries, or anything with real monetary value&#8212;this approach is strongly discouraged.</p><p></p><h3>Commit &amp; Reveal</h3><p>Commit &amp; Reveal is a technique where participants first submit (Commit) a hash of a secret value, and then later reveal (Reveal) the original value. By hashing the secret initially, no one can know the actual input until it&#8217;s revealed. This can mitigate third-party manipulation (including by block producers) and help ensure fairness.</p><ul><li><p><strong>Commit</strong>: Each participant chooses a random seed and a secret salt, then submits the hash (e.g., via keccak256) to the blockchain. At this point, nobody knows the actual inputs&#8212;only the hash.</p></li><li><p><strong>Reveal</strong>: After the commit phase, each participant discloses their original data (seed, salt). Other participants verify that the hash matches the committed value.</p></li></ul><p>Below is a simple Solidity example:</p><pre><code>pragma solidity ^0.8.0;

contract SimpleLottery {
    mapping(address =&gt; bytes32) public commits;
    mapping(address =&gt; uint256) public revealedSeed;
    address[] public players;

    /**
     * @dev Commit phase: submit the hash.
     *  - hash = keccak256(abi.encodePacked(seed, salt))
     */
    function commitHash(bytes32 _commit) external {
        require(commits[msg.sender] == 0, "Already committed");
        commits[msg.sender] = _commit;
        players.push(msg.sender);
    }

    /**
     * @dev Reveal phase: disclose the actual seed and salt.
     */
    function revealSeed(uint256 _seed, string memory _salt) external {
        require(commits[msg.sender] != 0, "No commit found");

        bytes32 checkHash = keccak256(abi.encodePacked(_seed, _salt));
        require(checkHash == commits[msg.sender], "Invalid reveal");

        revealedSeed[msg.sender] = _seed;
    }

    /**
     * @dev A simple example of deriving a final random number
     *      by hashing all revealed seeds together.
     */
    function getFinalRandom() external view returns (uint256) {
        uint256 totalSeed;
        for (uint256 i = 0; i &lt; players.length; i++) {
            totalSeed += revealedSeed[players[i]];
        }
        
        return uint256(keccak256(abi.encodePacked(totalSeed)));
    }
}</code></pre><p>Here, the final random value depends on the combination of everyone&#8217;s revealed seeds. Because the original seeds are hashed beforehand, it&#8217;s hard to predict them in advance. However, if certain participants collude, they might influence the final value, and if someone doesn&#8217;t reveal, the process can stall. Practical implementations usually require additional safeguards like strict reveal deadlines or penalties.</p><p></p><h3>VRF (Verifiable Random Function)</h3><p>A VRF uses a secret key (sk) to generate an output (&#946;) from an input (&#945;) in such a way that &#946; looks random, while also producing a proof (&#960;) that &#946; indeed came from &#945;. Anyone can verify this proof using the corresponding public key (pk), but only the holder of the secret key can produce a valid &#946;&#8211;&#960; pair.</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;VRFsk&#8203;(&#945;)&#8594;(&#946;,&#960;)&quot;,&quot;id&quot;:&quot;NUADYMRRBB&quot;}" data-component-name="LatexBlockToDOM"></div><ul><li><p><strong>Input (&#945;)</strong></p></li><li><p><strong>Secret Key (sk)</strong></p></li><li><p><strong>Output (&#946;)</strong>: Pseudorandom-looking result</p></li><li><p><strong>Proof (&#960;)</strong>: Cryptographic proof that &#946; was derived from &#945;</p></li></ul><p>The verification step uses:</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;Verifypk&#8203;(&#945;,&#946;,&#960;)&#8594;{True / False}&quot;,&quot;id&quot;:&quot;LGQGCYAOIL&quot;}" data-component-name="LatexBlockToDOM"></div><ul><li><p><strong>Public Key (pk)</strong> corresponding to sk</p></li><li><p><strong>&#945;, &#946;, &#960;</strong></p></li><li><p>Returns True if valid, False if tampered</p></li></ul><p>VRFs stand out because they provide cryptographic assurance that the random output hasn&#8217;t been altered. Thanks to this property, VRFs are widely considered one of the most robust ways to generate randomness on-chain. Chainlink, for instance, offers a VRF service that DApps can use without needing deep cryptographic expertise, making it a popular choice in the blockchain ecosystem.</p><blockquote><p><a href="https://blog.aavegotchi.com/aavegotchi-expands-use-of-chainlink-vrf-to-help-calculate-alchemica-with-gotchiverse-land-parcels/">Aavegotchi</a>, <a href="https://medium.com/pooltogether/improving-pooltogether-with-chainlink-vrf-dcf1a3d6ea">PoolTogether</a>, and others are utilizing Chainlink VRFs to ensure the reliability of their random numbers, especially in the NFT/Gaming space. For more examples, check out the <a href="https://chain.link/case-studies">Chainlink Case Study</a>.</p></blockquote><div><hr></div><h2>VRF in Code</h2><p>We've prepared a <strong><a href="https://github.com/shard-lab/protocol-camp/tree/main/ethereum/vrf">learning module</a></strong> showcasing how to use Chainlink VRF for generating random numbers in a DApp. Through this module, we hope you'll gain a clear understanding of how randomness can be practically applied in the blockchain (DApp) environment.</p><p>In this learning module, we expect you to interact with a <a href="https://testnet.bscscan.com/address/0x27e5De33cB6d31894a875891D3D86aA5F5b8aB2a#code">contract</a> that interfaces with a VRF deployed on a real on-chain (Bsc Testnet) and gain a better understanding of the real case.</p><div><hr></div><h2>Conclusion and Takeaways</h2><ul><li><p>In a blockchain environment, the deterministic and transparent nature makes generating random numbers far more difficult than in traditional computing systems.</p></li><li><p>From simpler approaches (like hash-based methods or Commit &amp; Reveal) to cryptographically verifiable techniques (like VRF), each solution has its own strengths and weaknesses. It&#8217;s essential to choose the right one based on the dApp&#8217;s required level of security, acceptable delay, and implementation complexity.</p></li></ul><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://0xshardlab.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://0xshardlab.substack.com/subscribe?"><span>Subscribe now</span></a></p><p></p>]]></content:encoded></item><item><title><![CDATA[What It Really Means to Be a Blockchain Developer]]></title><description><![CDATA[What blockchain developers really do: insights on smart contracts, wallets, nodes, mainnet and the skills that shape Web3.]]></description><link>https://0xshardlab.substack.com/p/what-it-really-means-to-be-a-blockchain</link><guid isPermaLink="false">https://0xshardlab.substack.com/p/what-it-really-means-to-be-a-blockchain</guid><dc:creator><![CDATA[SHARDLAB]]></dc:creator><pubDate>Wed, 23 Apr 2025 09:21:54 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/fe1eef4b-3b79-40b8-87c2-9b9f1eefd186_1456x1048.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p></p><blockquote><p>&#129517; <strong>About This Series</strong><br>As a Tech Lead at ShardLab, I often get asked what it actually means to be a blockchain developer. This post kicks off a new series where I share what I&#8217;ve learned from working across different parts of the blockchain stack&#8212;mainnets, wallets, smart contracts, DApps, and beyond&#8212;along with insights into the roles, skills, and challenges that define blockchain development today.</p><p>Subscribe for in-depth interviews with talented builders, code-level examples, and lessons from the field!</p></blockquote><p></p><h2>&#128075; How I Got Here</h2><p>I didn&#8217;t fall in love with blockchain because of its philosophy at first. What drew me in was its architecture&#8212;the idea of building decentralized systems with new assumptions, new constraints, and new tradeoffs. As an engineer, it was a completely different kind of challenge, and that alone was exciting.</p><p>In 2017, I started building blockchain systems&#8212;long before I fully understood the broader implications of what I was working on. At the time, I was simply fascinated by the rawness of the ecosystem, the unfamiliar design patterns, and the lack of tooling or documentation. There were almost no tutorials or open-source references&#8212;and definitely no ChatGPT to guide you. So I learned the hard way: by building.</p><p>It wasn&#8217;t until a few years later, around 2020, that things really started to click. I remember looking at something like Uniswap V2 in 2020 and thinking: how is it possible that fewer than ten developers built something moving billions of dollars in value every day? That moment helped me realize blockchain isn&#8217;t just a technical innovation&#8212;it&#8217;s a lens into much bigger and harder questions about governance, economics, politics, incentives, ownership&#8212;even art.</p><p>What excites me most now is not just the technology, but the opportunity to contribute to systems that are technically demanding and socially meaningful. Blockchain is messy, idealistic, and sometimes contradictory&#8212;but that's exactly what makes it worth building.</p><p>Over the past 8+ years, I&#8217;ve worked on a wide range of systems:</p><ul><li><p>Developing a mainnet entirely from scratch (no forks, no SDKs)</p></li><li><p>Operating and maintaining node services</p></li><li><p>Designing and implementing a custom smart contract language</p></li><li><p>Building hot &amp; cold wallet infrastructure (B2B and B2C)</p></li><li><p>Creating and scaling decentralized applications (DApps)</p></li><li><p>(Briefly) auditing smart contracts</p><p></p></li></ul><blockquote><p>"Blockchain development" isn&#8217;t one job&#8212;it&#8217;s an ecosystem of entirely different roles.</p></blockquote><p><strong>Each area demands different skill sets, capabilities, development styles, and even entirely different ways of thinking.</strong></p><p>This post offers a personal overview of those areas&#8212;what makes them interesting, challenging, and worth exploring if you&#8217;re curious about a career in blockchain.</p><p></p><h2>&#128269; Overview of Blockchain Development Areas</h2><p>As the blockchain ecosystem matured, I found myself working across different layers of the stack&#8212;not because I set out to do so, but because each phase of the space&#8217;s growth brought new technical needs and open questions. And the more I moved between domains, the more I realized how distinct each one really was.</p><p>From designing low-level consensus protocols to building UX for everyday users, the skill sets, decision-making logic, and even development cultures differ significantly. Most blockchain developers specialize in one or two of these areas&#8212;but understanding the landscape as a whole helps you navigate where your skills might fit and where you might want to go.</p><p>Here&#8217;s how I personally think about the major domains of blockchain development:</p><div><hr></div><h3>&#127959;&#65039; 1. Mainnet Development</h3><p>Mainnet development is perhaps the most foundational and complex area in blockchain engineering. It&#8217;s very similar to building a distributed system from the ground up&#8212;because that&#8217;s essentially what it is. This work typically involves high-performance system programming using languages like Rust, Go, or C++.</p><p>At this level, you&#8217;re thinking about how to store and retrieve blockchain data efficiently, how nodes communicate and reach consensus quickly, and whether you should design your own virtual machine for executing smart contracts&#8212;or adopt an existing one. You&#8217;re also often involved in designing the token economy, defining governance mechanisms, and shaping the underlying philosophy and purpose of the chain itself.</p><p>&#128161; <strong>Key tradeoffs</strong>: decentralization vs. scalability vs. security (a.k.a. the blockchain trilemma). Offering flexibility&#8212;such as dynamic validator sets or upgradable modules&#8212;can make the protocol more adaptable, but also introduces audit complexity and surface area for bugs.</p><p>This domain is filled with low-level concerns and hard tradeoffs. From performance bottlenecks to protocol-level attacks, everything has to be considered. It goes far beyond writing code&#8212;it requires thinking about why the chain exists in the first place, what purpose it serves, and what values it needs to uphold.</p><p>Fortunately, many components of modern blockchains have become modular. You don&#8217;t have to reinvent every piece. Tools like Cosmos SDK, Substrate, or Polygon Edge offer starting points for launching a chain. But if you&#8217;re building from scratch&#8212;as I did&#8212;you quickly realize just how many invisible complexities are involved.</p><p></p><h3>&#129504; 2. Smart Contract Language &amp; VM Design</h3><p>This domain sits at the intersection of systems programming, language design, and blockchain execution semantics. It&#8217;s one of the most intellectually intense areas I've touched, and something relatively few developers get to experience firsthand.</p><p>I worked on an experimental language and virtual machine (VM) as an open-source side project. It wasn&#8217;t production-ready, but the experience taught me a lot about how smart contracts actually run at a low level.</p><p>When designing a smart contract language or VM, there are several key considerations that shape how it functions and how safely it can be used:</p><ul><li><p><strong>Determinism</strong>: The same input should always produce the same result. Since smart contracts run independently on every node, there can&#8217;t be any variation. No randomness. No calls to current time. No I/O from external systems.</p></li><li><p><strong>Termination</strong>: All smart contracts must eventually halt. Infinite loops or non-terminating calls are unacceptable.</p></li><li><p><strong>Sandboxing</strong>: The VM must strictly isolate execution&#8212;no touching the file system, memory, or network beyond what the protocol permits.</p></li><li><p><strong>Gas Metering</strong>: Every operation has a cost, paid by the user. Instruction design must consider not just expressiveness but execution safety and cost-efficiency.</p></li></ul><p>The more flexibility a language allows, the more surface area it introduces for misuse or vulnerabilities. That&#8217;s why many blockchains introduce purpose-built domain-specific languages (DSLs) like Move or Cadence, while others constrain general-purpose languages like Solidity.</p><blockquote><p>Building a secure, expressive, and efficient execution layer is as much about the philosophy of contract safety as it is about engineering elegance.</p></blockquote><p></p><h3>&#128752;&#65039; 3. Node Service Operation</h3><p>Node operation isn&#8217;t always the most glamorous job in blockchain&#8212;but it&#8217;s one of the most critical. Without well-run, well-synced nodes, everything else on a chain grinds to a halt.</p><p>In some cases, teams operate their own dedicated nodes (e.g. for RPC endpoints, validators, or infrastructure services). In others, specialized providers like QuickNode or Alchemy offer scalable node access behind unified APIs.</p><p>The core challenges often mirror Web2 infrastructure engineering&#8212;but with blockchain-specific constraints:</p><ul><li><p><strong>Consistency</strong>: Because blockchains are constantly growing, different nodes may be a few blocks apart. This can cause inconsistent read results if traffic isn&#8217;t routed carefully.</p></li><li><p><strong>High-load behavior</strong>: Some RPC calls (e.g. <code>eth_call</code>, balance queries) dominate traffic and need caching, rate-limiting, and sometimes precomputed responses.</p></li><li><p><strong>Archive data</strong>: Serving historical state at any block height (i.e. archive mode) consumes massive storage and is costly to maintain.</p></li></ul><p>As teams scale, operating dozens or even hundreds of nodes reliably becomes a full-time challenge. Failover strategies, logging and monitoring, traffic shaping, and endpoint optimization all come into play.</p><blockquote><p>The invisible work of node infrastructure is what keeps blockchain apps usable. It&#8217;s a thankless, backend-heavy task&#8212;but an essential one.</p></blockquote><p></p><h3>&#128272; 4. Wallet</h3><p>This is the area I&#8217;ve spent the most time in, and it&#8217;s far more nuanced than it may appear on the surface. Wallet development spans deeply technical and user-centric challenges that go well beyond sending and receiving crypto. While MetaMask is often used as a familiar reference point, even it is a complex product under the hood.</p><p>When designing a wallet&#8212;whether for consumers or institutions&#8212;you need to consider areas like:</p><ul><li><p><strong>Key Management</strong>: Will the wallet be custodial or non-custodial? This decision impacts whether users manage their own keys or if the service secures them via hardware modules, MPC, or other strategies.</p></li><li><p><strong>Signature Schemes &amp; Address Derivation</strong>: Each chain uses different cryptographic curves and derivation paths, which affect compatibility and integration.</p></li><li><p><strong>Asset Models</strong>: Some blockchains use UTXOs (like Bitcoin), while others use account-based balances (like Ethereum), requiring different syncing and transaction logic.</p></li><li><p><strong>Token Standards</strong>: Even within the same ecosystem, standards like ERC-20, ERC-721, and others may have small variations that impact how transactions and balances are handled.</p></li><li><p><strong>User Experience</strong>: UX decisions&#8212;such as how to handle recovery, transaction previewing, and gas estimation&#8212;can deeply affect usability and user trust.</p></li></ul><p>These aren&#8217;t just technical details&#8212;they shape everything from user onboarding to long-term scalability and security.</p><p>Beyond B2C products like MetaMask, there are also enterprise-grade wallet systems&#8212;hot, warm, and cold wallets designed for institutions. These systems include policy engines, approval flows, compliance tooling.</p><p>In multi-chain environments, another layer of complexity comes from indexing assets, handling network fees in native tokens, and abstracting the different behaviors across chains into a unified experience.</p><p>Lately, I&#8217;ve been especially excited about <strong>Account Abstraction (AA) wallets</strong>. These offer programmable account behavior, enabling features like gas sponsorship, social recovery, batched transactions, and session keys. They represent a huge leap forward in UX and security&#8212;but also require an entirely new architecture.</p><blockquote><p>What fascinated me most was abstracting all of this complexity into a UX that Web2 users could understand and trust&#8212;without hiding the responsibility and security guarantees that make blockchain powerful in the first place.</p></blockquote><p></p><h3>&#127760; 5. Decentralized Applications (DApps)</h3><p>When most people say they want to build something in blockchain, this is usually what they mean. DApps (Decentralized Applications) are products that combine smart contracts, frontend UIs, and indexing layers to deliver blockchain-based functionality to users.</p><p>At a high level, DApp development includes:</p><ul><li><p><strong>Smart Contracts</strong>: Core application logic lives on-chain. This can include DeFi logic, NFT minting, DAOs, and more.</p></li><li><p><strong>Frontend</strong>: Typically built in TypeScript and React, it interacts with wallet providers, reads blockchain data, and triggers transactions.</p></li><li><p><strong>Indexers</strong>: Since querying on-chain state directly can be slow or limited, DApps often use off-chain indexers like The Graph or custom services to power UIs.</p></li><li><p><strong>Backend / Off-chain Logic</strong>: Sometimes needed for things like authentication, metadata handling, or signature aggregation.</p></li></ul><p>Unlike traditional apps, <strong>blockchain transactions are irreversible and often carry fees</strong>. This introduces new engineering considerations that Web2 developers may not be accustomed to:</p><ul><li><p>You optimize not just for speed, but for <strong>gas efficiency</strong>&#8212;even small changes in contract structure can result in significant cost savings.</p></li><li><p>Once deployed, contracts are immutable unless carefully designed with upgradeability in mind&#8212;this raises the bar for writing reliable code before launch.</p></li><li><p>Error handling and transaction feedback become crucial, since failed transactions cost users money and can&#8217;t be undone.</p></li></ul><p>Even DApp architecture itself has changed the way we model products. For example, Uniswap V2 introduced an AMM model that works without an order book, without a matching engine, and without fiat. That wouldn&#8217;t have made sense in a centralized world&#8212;but makes perfect sense when trustless settlement is the default.</p><p>DApps must provide <strong>clear value over Web2</strong> in order to justify the added complexity and cost of using blockchain. While the UX and infrastructure still have a long way to go, progress is accelerating&#8212;and we&#8217;re already seeing some glimpses of what&#8217;s possible.</p><p></p><h3>&#128373;&#65039; 6. Smart Contract Auditing</h3><p>This is a domain I&#8217;ve only brushed against directly, but I&#8217;ve had the chance to watch experienced auditors work and learn from several notable hacks and reviews. It&#8217;s one of the most critical&#8212;and most unforgiving&#8212;areas in blockchain development.</p><p>At its core, auditing is about identifying weaknesses before attackers do. But it&#8217;s not just about reviewing single contracts. It often involves understanding entire systems of contracts, protocol invariants, and external interactions.</p><p>Good auditors need:</p><ul><li><p><strong>Deep EVM-level knowledge</strong>: From storage slots and delegate calls, to gas mechanics and precompiled contracts.</p></li><li><p><strong>An understanding of common attack vectors</strong>: Reentrancy, overflow/underflow, improper access control, faulty logic in upgradability patterns.</p></li><li><p><strong>Cross-contract reasoning</strong>: Some bugs only emerge when contracts interact in unexpected ways.</p></li><li><p><strong>Tool fluency</strong>: Tools like Slither, MythX, Foundry, or formal verification frameworks can help&#8212;but they don&#8217;t replace human judgment.</p></li></ul><p>What I learned is that smart contract auditing isn't about confirming that everything works&#8212;it's about assuming that someone will try to break it, and figuring out how. You have to adopt an adversarial mindset and actively look for creative ways contracts might be abused. Especially in DeFi, where huge sums of money are on the line, the motivation for attackers is enormous.</p><p>Here are just a few high-profile examples that shaped how the industry thinks about auditing:</p><ul><li><p>The DAO (2016) &#8212; Reentrancy attack that drained ~$60M in ETH and led to Ethereum&#8217;s hard fork.</p></li><li><p>Parity (2017) &#8212; A bug in a shared multisig contract led to 500K ETH being permanently frozen.</p></li><li><p>Wormhole (2022) &#8212; A Solana bridge was exploited for ~$300M after a signature verification bug.</p></li><li><p>Ronin (2022) &#8212; Validators were compromised via social engineering, leading to ~$600M in losses.</p></li></ul><div><hr></div><h2>&#128188; Industry Role Breakdown</h2><p>Aside from my own experience, I&#8217;ve observed how blockchain developer roles are structured across different teams&#8212;startups, protocol foundations, infrastructure providers, and exchanges. The boundaries between roles are often blurry, but there are some broadly recognized categories that help when navigating job descriptions or thinking about career paths.</p><h3>Smart Contract Engineer</h3><p>This role is all about writing and maintaining the logic that lives directly on-chain. These engineers work primarily in languages like Solidity, Move, or Rust, and often operate in high-stakes environments like DeFi, NFTs, or DAOs. A strong understanding of smart contract patterns, gas optimization, and security practices&#8212;such as guarding against reentrancy or ensuring safe upgradeability&#8212;is essential. They collaborate closely with auditors and product teams and often own the testing and deployment pipelines for contract delivery.</p><h3>Blockchain Protocol Engineer</h3><p>Protocol engineers focus on the base layer of the blockchain itself. They implement consensus mechanisms, build peer-to-peer networking layers, design transaction propagation rules, and work on the core blockchain clients. Their work involves deep system-level programming, often in Rust, Go, or C++. They typically contribute to Layer 1 or Layer 2 protocols, or ecosystem tools like validators, relayers, and light clients. These engineers think in terms of state transitions, fork choice rules, and performance tradeoffs at the protocol level.</p><h3>Web3 Frontend/Backend Engineer</h3><p>This role spans building interfaces and APIs that connect users to smart contracts. On the frontend side, these engineers integrate wallets like MetaMask or WalletConnect, handle transaction flows, and reflect on-chain state in real time. On the backend, they might aggregate indexed blockchain data, manage cache layers, or trigger off-chain processes like queuing or metadata management. Many wallet developers fall into this category as well, since building a wallet often involves combining frontend logic with cryptographic backends.</p><h3>Infrastructure / DevOps Engineer (Web3 Infra)</h3><p>Infra engineers handle the operational backbone of blockchain systems. They deploy and monitor RPC nodes, configure archive nodes, set up logs and metrics, and ensure services remain highly available. Their stack usually includes Kubernetes, Docker, Grafana, Prometheus, and cloud providers like AWS or GCP. They may also support developers by maintaining indexers, orchestrating testnets, and managing CI/CD pipelines for protocol or app-level deployments.</p><h3>Security Engineer / Auditor</h3><p>Security roles exist both in-house and at specialized firms. These engineers audit smart contracts and blockchain infrastructure, looking for vulnerabilities before attackers do. They read low-level EVM bytecode, fuzz edge cases, simulate exploits, and validate that core protocol invariants hold. Their toolkit may include Slither, Foundry, MythX, or formal verification frameworks. This role requires not only technical depth but also an adversarial mindset and strong understanding of game theory and financial mechanics&#8212;especially in the context of DeFi.</p><p>These roles often overlap in practice. Smart contract engineers sometimes audit each other&#8217;s work; backend engineers often collaborate with infra to tune indexers; and everyone benefits from knowing a little security. Still, these role archetypes provide a useful way to navigate the landscape.</p><p></p><h2>&#128640; Final Thoughts: The Road Ahead</h2><blockquote><p><em>&#8220;Which part of the blockchain development stack do you want to focus on?&#8221;</em></p></blockquote><p>It&#8217;s about building systems that transfer and store value, establish trust without intermediaries, and operate reliably in decentralized environments. Each area demands a different mindset and engineering approach.</p><p>It sounds ambitious&#8212;and it is. But that&#8217;s also what makes this space so intellectually and creatively exciting.</p><p>We&#8217;re still early in this journey. Despite all the headlines, blockchain hasn&#8217;t yet reached the level of mass adoption many of us envision. That&#8217;s not because the potential isn&#8217;t there&#8212;but because there are still so many challenges to solve.</p><p>The user experience is often clunky or intimidating. Developer tools are still maturing. Security issues continue to shake trust. And most importantly, many DApps still struggle to deliver clear value beyond what Web2 alternatives already offer. Until blockchain applications can provide a distinctly better or more meaningful experience&#8212;whether through ownership, composability, or censorship resistance&#8212;most users won&#8217;t have a reason to switch.</p><p>But that&#8217;s also what makes this moment interesting. The space is still being defined, and there&#8217;s real opportunity to shape how things evolve. What the ecosystem needs most isn&#8217;t just more users&#8212;it&#8217;s more people willing to build, explore, and contribute in their own way. Whether that&#8217;s prototyping new DApps, contributing to infrastructure, improving UX, or writing better docs&#8212;there&#8217;s no single path, and no permission needed.</p><p>If you're reading this and wondering whether there's a place for you: there is.</p><p>Whether you're new to Web3 or looking to deepen your expertise, I hope this post gave you a clearer picture of what it means to be a blockchain developer.</p><p>In the upcoming posts, I&#8217;ll explore each area more deeply&#8212;with practical examples, technical insights, and interviews with builders shaping the future.</p><p>&#10024; Stay tuned&#8212;and thank you for reading &#128588;</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://0xshardlab.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">If you enjoy the series, feel free to share it and don&#8217;t forget to subscribe to Protocol Newsletter for more!</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[A Step into MEV]]></title><description><![CDATA[Learning How to Maximize Value in Blockchain Transactions]]></description><link>https://0xshardlab.substack.com/p/a-step-into-mev</link><guid isPermaLink="false">https://0xshardlab.substack.com/p/a-step-into-mev</guid><dc:creator><![CDATA[Toby Kim]]></dc:creator><pubDate>Sat, 05 Apr 2025 15:23:13 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/7736b5f6-6e32-45cf-aed9-7f698521c84e_1456x1048.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2>MEV Learning Module</h2><p>Before transactions ever make it on-chain, they pass through a hidden battleground. Validators, traders, and bot operators compete fiercely to reorder transactions in their favor, profiting handsomely in the process.</p><p>We call this <strong>MEV (Maximal Extractable Value)</strong>&#8212;the pursuit of maximum profit during block production. While the average user may not notice it, within the industry, MEV is often described as an &#8220;invisible tax,&#8221; underscoring its growing impact on the blockchain economy.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://0xshardlab.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! Subscribe for free to receive new posts and support my work.</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>In this article, we&#8217;ll explore the fundamentals of MEV and the challenges it poses. We&#8217;ll also introduce a <strong><a href="https://github.com/shard-lab/protocol-camp/tree/main/advanced/mev">MEV Learning Module</a></strong>, which provides a sandboxed EVM environment for hands-on experience in implementing MEV strategies. Let&#8217;s dive in.</p><p></p><div><hr></div><h2>How MEV Works</h2><p>To understand MEV, we first need to look at <strong>how transactions are processed on a blockchain</strong>. We&#8217;ll use Ethereum (EVM) as our example, noting that not all blockchains follow the same exact structure.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Rrh3!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F392e5513-7753-47e8-9e51-04324aaab189_3276x962.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Rrh3!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F392e5513-7753-47e8-9e51-04324aaab189_3276x962.png 424w, https://substackcdn.com/image/fetch/$s_!Rrh3!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F392e5513-7753-47e8-9e51-04324aaab189_3276x962.png 848w, https://substackcdn.com/image/fetch/$s_!Rrh3!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F392e5513-7753-47e8-9e51-04324aaab189_3276x962.png 1272w, https://substackcdn.com/image/fetch/$s_!Rrh3!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F392e5513-7753-47e8-9e51-04324aaab189_3276x962.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Rrh3!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F392e5513-7753-47e8-9e51-04324aaab189_3276x962.png" width="1456" height="428" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/392e5513-7753-47e8-9e51-04324aaab189_3276x962.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:428,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:139007,&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;:&quot;https://0xshardlab.substack.com/i/160062234?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F392e5513-7753-47e8-9e51-04324aaab189_3276x962.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Rrh3!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F392e5513-7753-47e8-9e51-04324aaab189_3276x962.png 424w, https://substackcdn.com/image/fetch/$s_!Rrh3!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F392e5513-7753-47e8-9e51-04324aaab189_3276x962.png 848w, https://substackcdn.com/image/fetch/$s_!Rrh3!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F392e5513-7753-47e8-9e51-04324aaab189_3276x962.png 1272w, https://substackcdn.com/image/fetch/$s_!Rrh3!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F392e5513-7753-47e8-9e51-04324aaab189_3276x962.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 role="img" 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><title></title><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></figure></div><ol><li><p><strong>User</strong><br>Most users generate and sign transactions&#8212;such as sending ETH or swapping tokens&#8212;through wallets like MetaMask.</p></li><li><p><strong>Mempool</strong><br>Once created, a transaction doesn&#8217;t go straight into a block. Instead, it&#8217;s placed in a public <strong>Mempool</strong> (a waiting area) shared by blockchain nodes:</p><ul><li><p>The Mempool holds transactions not yet included in a block.</p></li><li><p>Because the <strong>Mempool is</strong> <strong>public</strong>, anyone can see these pending transactions.</p></li></ul></li><li><p><strong>Miner/Validator</strong></p><ul><li><p>In Proof-of-Work, <strong>miners</strong>; in Proof-of-Stake, <strong>validators</strong> select transactions from the Mempool to build new blocks.</p></li><li><p>Generally, block producers pick transactions that pay <strong>higher gas fees</strong> first to maximize their own profit.</p></li></ul></li><li><p><strong>Blockchain</strong></p><ul><li><p>Once a transaction is included in a block, that transaction is considered confirmed.</p></li></ul></li></ol><p>Because the Mempool is public, it inadvertently becomes a source of MEV opportunities. Anyone who can see pending transactions has a window to reorder, front-run, or otherwise strategize to capture additional profit.</p><blockquote><p><strong><a href="https://www.paradigm.xyz/2020/08/ethereum-is-a-dark-forest">Ethereum is a Dark Forest</a></strong> offers a vivid real-world account of MEV. The author compares Ethereum to a dark forest, where front-running bots lurk like predators waiting to pounce on unsuspecting transactions. This analogy illustrates how transparency can paradoxically enable hidden conflicts. Being able to view pending transactions in real time creates an environment where opportunistic players can exploit that visibility.</p></blockquote><p></p><div><hr></div><h2>MEV Strategies</h2><p>Below are some of the most common strategies used to extract MEV.</p><h4>Front-Running</h4><p>Front-running involves <strong>detecting someone else&#8217;s transaction in the Mempool</strong> and <strong>submitting your own transaction first</strong> so it executes ahead of theirs.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Qk8h!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F22220398-6557-4528-8d15-6157def371c7_1101x1222.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Qk8h!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F22220398-6557-4528-8d15-6157def371c7_1101x1222.png 424w, https://substackcdn.com/image/fetch/$s_!Qk8h!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F22220398-6557-4528-8d15-6157def371c7_1101x1222.png 848w, https://substackcdn.com/image/fetch/$s_!Qk8h!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F22220398-6557-4528-8d15-6157def371c7_1101x1222.png 1272w, https://substackcdn.com/image/fetch/$s_!Qk8h!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F22220398-6557-4528-8d15-6157def371c7_1101x1222.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Qk8h!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F22220398-6557-4528-8d15-6157def371c7_1101x1222.png" width="270" height="299.6730245231608" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/22220398-6557-4528-8d15-6157def371c7_1101x1222.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1222,&quot;width&quot;:1101,&quot;resizeWidth&quot;:270,&quot;bytes&quot;:77709,&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;:&quot;https://0xshardlab.substack.com/i/160062234?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F22220398-6557-4528-8d15-6157def371c7_1101x1222.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Qk8h!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F22220398-6557-4528-8d15-6157def371c7_1101x1222.png 424w, https://substackcdn.com/image/fetch/$s_!Qk8h!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F22220398-6557-4528-8d15-6157def371c7_1101x1222.png 848w, https://substackcdn.com/image/fetch/$s_!Qk8h!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F22220398-6557-4528-8d15-6157def371c7_1101x1222.png 1272w, https://substackcdn.com/image/fetch/$s_!Qk8h!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F22220398-6557-4528-8d15-6157def371c7_1101x1222.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 role="img" 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><title></title><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></figure></div><p>In the previously mentioned <strong><a href="https://www.paradigm.xyz/2020/08/ethereum-is-a-dark-forest">Ethereum is a Dark Forest</a></strong>, we can observe a real-world example of Front Running. To simplify it, let&#8217;s assume there&#8217;s a contract where a password is entered to access a vault.</p><pre><code>contract Vault {
    bytes32 private hashedPassword;

    constructor(bytes32 hashedPassword) {
        hashedPassword = hashedPassword;
    }

    function withdraw(string memory password) public {
        if (keccak256(abi.encodePacked(password)) == hashedPassword) {
            payable(msg.sender).transfer(address(this).balance);
        }
    }
}</code></pre><p>If someone who knows the password sends a transaction to withdraw funds, as soon as that transaction appears in the Mempool, an <strong>MEV bot could detect it</strong>, copy the password, and submit its own transaction first&#8212;stealing the funds before the original user&#8217;s transaction even executes.</p><p></p><h4>Back-Running</h4><p>Back-running is the opposite of front-running: you <strong>intentionally place your transaction after</strong> another transaction.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!ysxg!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F46f3be10-185f-4546-8c34-658515b66e47_1101x1222.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ysxg!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F46f3be10-185f-4546-8c34-658515b66e47_1101x1222.png 424w, https://substackcdn.com/image/fetch/$s_!ysxg!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F46f3be10-185f-4546-8c34-658515b66e47_1101x1222.png 848w, https://substackcdn.com/image/fetch/$s_!ysxg!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F46f3be10-185f-4546-8c34-658515b66e47_1101x1222.png 1272w, https://substackcdn.com/image/fetch/$s_!ysxg!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F46f3be10-185f-4546-8c34-658515b66e47_1101x1222.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!ysxg!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F46f3be10-185f-4546-8c34-658515b66e47_1101x1222.png" width="271" height="300.7829246139873" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/46f3be10-185f-4546-8c34-658515b66e47_1101x1222.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1222,&quot;width&quot;:1101,&quot;resizeWidth&quot;:271,&quot;bytes&quot;:78138,&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;:&quot;https://0xshardlab.substack.com/i/160062234?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F46f3be10-185f-4546-8c34-658515b66e47_1101x1222.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!ysxg!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F46f3be10-185f-4546-8c34-658515b66e47_1101x1222.png 424w, https://substackcdn.com/image/fetch/$s_!ysxg!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F46f3be10-185f-4546-8c34-658515b66e47_1101x1222.png 848w, https://substackcdn.com/image/fetch/$s_!ysxg!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F46f3be10-185f-4546-8c34-658515b66e47_1101x1222.png 1272w, https://substackcdn.com/image/fetch/$s_!ysxg!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F46f3be10-185f-4546-8c34-658515b66e47_1101x1222.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 role="img" 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><title></title><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></figure></div><p>In a Lending Protocol, there is the concept of liquidation. When the value of a borrower&#8217;s collateral falls below the value of the loaned asset, a third party can repay the collateral on behalf of the borrower and acquire the loaned asset at a discount to prevent losses for the Lending Protocol. The example below shows how in AAVE2, USDT (collateral) is repaid on behalf of the borrower, and ETH is acquired at a lower price.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!hmgV!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6a396014-3858-468e-9c79-fc2620067939_2734x1400.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!hmgV!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6a396014-3858-468e-9c79-fc2620067939_2734x1400.png 424w, https://substackcdn.com/image/fetch/$s_!hmgV!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6a396014-3858-468e-9c79-fc2620067939_2734x1400.png 848w, https://substackcdn.com/image/fetch/$s_!hmgV!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6a396014-3858-468e-9c79-fc2620067939_2734x1400.png 1272w, https://substackcdn.com/image/fetch/$s_!hmgV!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6a396014-3858-468e-9c79-fc2620067939_2734x1400.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!hmgV!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6a396014-3858-468e-9c79-fc2620067939_2734x1400.png" width="1456" height="746" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/6a396014-3858-468e-9c79-fc2620067939_2734x1400.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:746,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:607487,&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;:&quot;https://0xshardlab.substack.com/i/160062234?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6a396014-3858-468e-9c79-fc2620067939_2734x1400.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!hmgV!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6a396014-3858-468e-9c79-fc2620067939_2734x1400.png 424w, https://substackcdn.com/image/fetch/$s_!hmgV!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6a396014-3858-468e-9c79-fc2620067939_2734x1400.png 848w, https://substackcdn.com/image/fetch/$s_!hmgV!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6a396014-3858-468e-9c79-fc2620067939_2734x1400.png 1272w, https://substackcdn.com/image/fetch/$s_!hmgV!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6a396014-3858-468e-9c79-fc2620067939_2734x1400.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 role="img" 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><title></title><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></figure></div><p>Liquidation occurs when the value of the assets changes. Therefore, <a href="https://etherscan.io/tx/0xc82ca7594ac4a80469854eea0bee959b70575abb40d6cd5b108a31525e429e99">Liquidation Transactions</a> follow the transactions that update the Lending Protocol&#8217;s prices and trigger liquidation immediately after the update.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!6QRP!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff1af735e-3871-4b59-8eb8-d6bd8bad0be9_3307x238.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!6QRP!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff1af735e-3871-4b59-8eb8-d6bd8bad0be9_3307x238.png 424w, https://substackcdn.com/image/fetch/$s_!6QRP!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff1af735e-3871-4b59-8eb8-d6bd8bad0be9_3307x238.png 848w, https://substackcdn.com/image/fetch/$s_!6QRP!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff1af735e-3871-4b59-8eb8-d6bd8bad0be9_3307x238.png 1272w, https://substackcdn.com/image/fetch/$s_!6QRP!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff1af735e-3871-4b59-8eb8-d6bd8bad0be9_3307x238.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!6QRP!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff1af735e-3871-4b59-8eb8-d6bd8bad0be9_3307x238.png" width="1456" height="105" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f1af735e-3871-4b59-8eb8-d6bd8bad0be9_3307x238.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:105,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:148769,&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;:&quot;https://0xshardlab.substack.com/i/160062234?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff1af735e-3871-4b59-8eb8-d6bd8bad0be9_3307x238.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!6QRP!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff1af735e-3871-4b59-8eb8-d6bd8bad0be9_3307x238.png 424w, https://substackcdn.com/image/fetch/$s_!6QRP!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff1af735e-3871-4b59-8eb8-d6bd8bad0be9_3307x238.png 848w, https://substackcdn.com/image/fetch/$s_!6QRP!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff1af735e-3871-4b59-8eb8-d6bd8bad0be9_3307x238.png 1272w, https://substackcdn.com/image/fetch/$s_!6QRP!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff1af735e-3871-4b59-8eb8-d6bd8bad0be9_3307x238.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p></p><h4>Sandwich Attack</h4><p>A <strong>sandwich attack</strong> combines front-running and back-running. You insert your transactions both before and after someone else&#8217;s transaction, effectively &#8220;sandwiching&#8221; it.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!fvRz!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7d05a316-12ed-446d-aa64-6533bed84628_1101x1222.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!fvRz!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7d05a316-12ed-446d-aa64-6533bed84628_1101x1222.png 424w, https://substackcdn.com/image/fetch/$s_!fvRz!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7d05a316-12ed-446d-aa64-6533bed84628_1101x1222.png 848w, https://substackcdn.com/image/fetch/$s_!fvRz!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7d05a316-12ed-446d-aa64-6533bed84628_1101x1222.png 1272w, https://substackcdn.com/image/fetch/$s_!fvRz!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7d05a316-12ed-446d-aa64-6533bed84628_1101x1222.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!fvRz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7d05a316-12ed-446d-aa64-6533bed84628_1101x1222.png" width="271" height="300.7829246139873" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/7d05a316-12ed-446d-aa64-6533bed84628_1101x1222.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1222,&quot;width&quot;:1101,&quot;resizeWidth&quot;:271,&quot;bytes&quot;:80835,&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;:&quot;https://0xshardlab.substack.com/i/160062234?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7d05a316-12ed-446d-aa64-6533bed84628_1101x1222.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!fvRz!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7d05a316-12ed-446d-aa64-6533bed84628_1101x1222.png 424w, https://substackcdn.com/image/fetch/$s_!fvRz!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7d05a316-12ed-446d-aa64-6533bed84628_1101x1222.png 848w, https://substackcdn.com/image/fetch/$s_!fvRz!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7d05a316-12ed-446d-aa64-6533bed84628_1101x1222.png 1272w, https://substackcdn.com/image/fetch/$s_!fvRz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7d05a316-12ed-446d-aa64-6533bed84628_1101x1222.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 role="img" 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><title></title><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></figure></div><ol><li><p>Detect a large swap pending in the Mempool.</p></li><li><p>Place a buy transaction first (front-run) to push the token&#8217;s price up.</p></li><li><p>Let the target user&#8217;s swap execute at this higher price.</p></li><li><p>Immediately sell (back-run) to cash in on the price difference.</p><p></p></li></ol><p>As a result, the victim&#8217;s swap faces worse slippage, while the attacker profits from this manipulation. Below is a real example on Etherscan, where the attacker&#8217;s front-run and back-run transactions flank the target user&#8217;s swap.</p><p>Below is an actual example of a sandwich attack on Etherscan:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!qgQv!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0be70e94-8705-47ae-83dd-85646bb6f742_3326x346.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!qgQv!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0be70e94-8705-47ae-83dd-85646bb6f742_3326x346.png 424w, https://substackcdn.com/image/fetch/$s_!qgQv!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0be70e94-8705-47ae-83dd-85646bb6f742_3326x346.png 848w, https://substackcdn.com/image/fetch/$s_!qgQv!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0be70e94-8705-47ae-83dd-85646bb6f742_3326x346.png 1272w, https://substackcdn.com/image/fetch/$s_!qgQv!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0be70e94-8705-47ae-83dd-85646bb6f742_3326x346.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!qgQv!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0be70e94-8705-47ae-83dd-85646bb6f742_3326x346.png" width="1456" height="151" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/0be70e94-8705-47ae-83dd-85646bb6f742_3326x346.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:151,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:313824,&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;:&quot;https://0xshardlab.substack.com/i/160062234?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0be70e94-8705-47ae-83dd-85646bb6f742_3326x346.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!qgQv!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0be70e94-8705-47ae-83dd-85646bb6f742_3326x346.png 424w, https://substackcdn.com/image/fetch/$s_!qgQv!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0be70e94-8705-47ae-83dd-85646bb6f742_3326x346.png 848w, https://substackcdn.com/image/fetch/$s_!qgQv!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0be70e94-8705-47ae-83dd-85646bb6f742_3326x346.png 1272w, https://substackcdn.com/image/fetch/$s_!qgQv!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0be70e94-8705-47ae-83dd-85646bb6f742_3326x346.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!0nCf!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe2dbeed1-f7aa-4a79-9004-3a2cc5ba3694_5594x1022.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!0nCf!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe2dbeed1-f7aa-4a79-9004-3a2cc5ba3694_5594x1022.png 424w, https://substackcdn.com/image/fetch/$s_!0nCf!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe2dbeed1-f7aa-4a79-9004-3a2cc5ba3694_5594x1022.png 848w, https://substackcdn.com/image/fetch/$s_!0nCf!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe2dbeed1-f7aa-4a79-9004-3a2cc5ba3694_5594x1022.png 1272w, https://substackcdn.com/image/fetch/$s_!0nCf!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe2dbeed1-f7aa-4a79-9004-3a2cc5ba3694_5594x1022.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!0nCf!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe2dbeed1-f7aa-4a79-9004-3a2cc5ba3694_5594x1022.png" width="1456" height="266" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e2dbeed1-f7aa-4a79-9004-3a2cc5ba3694_5594x1022.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:266,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1229000,&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;:&quot;https://0xshardlab.substack.com/i/160062234?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe2dbeed1-f7aa-4a79-9004-3a2cc5ba3694_5594x1022.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!0nCf!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe2dbeed1-f7aa-4a79-9004-3a2cc5ba3694_5594x1022.png 424w, https://substackcdn.com/image/fetch/$s_!0nCf!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe2dbeed1-f7aa-4a79-9004-3a2cc5ba3694_5594x1022.png 848w, https://substackcdn.com/image/fetch/$s_!0nCf!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe2dbeed1-f7aa-4a79-9004-3a2cc5ba3694_5594x1022.png 1272w, https://substackcdn.com/image/fetch/$s_!0nCf!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe2dbeed1-f7aa-4a79-9004-3a2cc5ba3694_5594x1022.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>The attacker detects a pending swap in the Mempool, then places a <a href="https://etherscan.io/tx/0x0792c66f93152f17297ee6dac2cf78db62976fa8c46dfcc05de72aa8a2a7b02a">Front Run Tx</a> to buy and a <a href="https://etherscan.io/tx/0x20e06873014a3fc97a272847fef2f1652501f69f9ee2bc12f39c4052bf50a7bd">Back Run Tx</a> to sell, sandwiching the user&#8217;s <a href="https://etherscan.io/tx/0x4a0590af9c5c3635f5bd221accf83b34658dd8e62416c549f6384c93f971aa58">Target Tx</a> in between.</p><p></p><h4>And More&#8230;</h4><p>MEV isn&#8217;t limited to front-running and sandwich attacks. There are many other tactics&#8212;like arbitrage, NFT minting races, and just-in-time liquidity.</p><p></p><div><hr></div><h2>Problems &amp; Future Solutions</h2><p>MEV brings several challenges:</p><ol><li><p><strong>Network trust Erosion</strong></p><ul><li><p>If a single block producer consistently monopolizes MEV, it undermines the network&#8217;s decentralization and damages user trust in the system.</p></li></ul></li><li><p><strong>User Harm</strong></p><ul><li><p>Attacks like sandwiching directly cause real losses to ordinary users, making the blockchain ecosystem less appealing.</p></li></ul></li><li><p><strong>Reduced Usability</strong></p><ul><li><p>Intense MEV competition often leads to spikes in gas fees or network congestion, diminishing overall usability.</p><p></p></li></ul></li></ol><p>However, some forms of MEV&#8212;like arbitrage or liquidations&#8212;can improve market efficiency. Even so, negative impacts have spurred efforts to mitigate or manage MEV.</p><p>Here are some different ways to solve MEV problems:</p><ol><li><p><strong>Application-Level Solutions</strong></p><ul><li><p><strong>CoW Swap</strong>: Matches users who want to trade opposite sides of the same pair directly, helping to prevent sandwich attacks by eliminating the need for a public on-chain swap.</p></li></ul></li><li><p><strong>Middleware</strong></p><ul><li><p><strong>FlashBots</strong>: Bypasses the public Mempool via a <strong>private relay</strong>.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!xDbY!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1ef15771-34be-4823-871a-fddea2cf97a7_1400x640.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!xDbY!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1ef15771-34be-4823-871a-fddea2cf97a7_1400x640.png 424w, https://substackcdn.com/image/fetch/$s_!xDbY!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1ef15771-34be-4823-871a-fddea2cf97a7_1400x640.png 848w, https://substackcdn.com/image/fetch/$s_!xDbY!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1ef15771-34be-4823-871a-fddea2cf97a7_1400x640.png 1272w, https://substackcdn.com/image/fetch/$s_!xDbY!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1ef15771-34be-4823-871a-fddea2cf97a7_1400x640.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!xDbY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1ef15771-34be-4823-871a-fddea2cf97a7_1400x640.png" width="1400" height="640" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/1ef15771-34be-4823-871a-fddea2cf97a7_1400x640.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:640,&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_!xDbY!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1ef15771-34be-4823-871a-fddea2cf97a7_1400x640.png 424w, https://substackcdn.com/image/fetch/$s_!xDbY!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1ef15771-34be-4823-871a-fddea2cf97a7_1400x640.png 848w, https://substackcdn.com/image/fetch/$s_!xDbY!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1ef15771-34be-4823-871a-fddea2cf97a7_1400x640.png 1272w, https://substackcdn.com/image/fetch/$s_!xDbY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1ef15771-34be-4823-871a-fddea2cf97a7_1400x640.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 role="img" 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><title></title><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></figure></div><ul><li><p><strong>Searchers</strong> create bundles of transactions designed to extract MEV.</p></li><li><p>A <strong>Relay</strong> validates these bundles.</p></li><li><p><strong>Miners/Validators</strong> then choose the most profitable bundle to include.</p></li><li><p>Although it requires dedicated infrastructure, FlashBots is an example of tackling MEV at the middleware level.</p></li></ul></li></ul></li><li><p><strong>Protocol-Level Solutions</strong></p><ul><li><p><strong>Proposer-Builder Separation (PBS)</strong>: Splits block production into two roles&#8212;&#8220;<strong>Builders</strong>,&#8221; who extract MEV, and &#8220;<strong>Proposers</strong>,&#8221; who select which blocks to finalize. Separating responsibilities can help prevent a single entity from unilaterally reordering transactions and monopolizing MEV, thereby supporting network decentralization.</p></li></ul><p></p></li></ol><div><hr></div><h2>Conclusion</h2><p>MEV has evolved into a pivotal aspect of blockchain economics. The inherent transparency of blockchains allows creative strategies like front-running, back-running, and sandwich attacks to thrive. However, not all MEV is detrimental; certain forms, such as arbitrage and liquidations, can enhance overall market efficiency.</p><p>As MEV grows more complex, the industry is actively pursuing solutions at multiple layers&#8212;applications, middleware, and protocols. This <strong><a href="https://github.com/shard-lab/protocol-camp/tree/main/advanced/mev">MEV Learning Module</a></strong> offers hands-on opportunities to experiment with these strategies in a sandboxed environment. By understanding how MEV works in practice, we can better develop and support safer, more efficient blockchain ecosystems for everyone.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://0xshardlab.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! Subscribe for free to receive new posts and support my work.</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[zk-SNARKs: Understand It, Then Build It]]></title><description><![CDATA[A hands-on guide to zk-SNARKs &#8212; from equations to working code, using TypeScript and elliptic curves]]></description><link>https://0xshardlab.substack.com/p/zk-snarks-understand-it-then-build</link><guid isPermaLink="false">https://0xshardlab.substack.com/p/zk-snarks-understand-it-then-build</guid><dc:creator><![CDATA[SHARDLAB]]></dc:creator><pubDate>Tue, 01 Apr 2025 06:25:49 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/f2605a16-8f96-4c24-b7b5-09c23181a69a_1456x1048.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Have you ever wanted to prove you know something &#8212; without revealing what it is?</p><p>Imagine:</p><ul><li><p>"I know the solution to this puzzle, but I won&#8217;t tell you the answer."</p></li><li><p>"I&#8217;ve done the computation correctly, but I don&#8217;t want to show you the inputs."</p></li></ul><p>That&#8217;s the magic of Zero-Knowledge Proofs (ZKPs) &#8212; a way to convince someone you know something, without giving away a single clue about what that something is.</p><p>This post is your beginner-friendly guide to zk-SNARKs &#8212; a powerful type of ZKP &#8212; explained with metaphors, light formulas, and grounded examples.</p><div><hr></div><h2>&#128736;&#65039; What Makes This Guide Different?</h2><p>There are plenty of articles that <em>explain</em> zk-SNARKs &#8212; but very few that actually <strong>show you how to implement them step-by-step in code</strong>.</p><p>That&#8217;s what sets this guide apart.</p><p>Throughout this post, every concept is <strong>directly connected to real, working code</strong> that you can read, run, and modify. This isn't just theory &#8212; it's an implementation-first approach that is designed to help you <strong>truly understand</strong> zk-SNARKs by building them yourself.</p><p>Here&#8217;s what you&#8217;ll get that most other resources don&#8217;t:</p><ul><li><p><strong>R1CS not just as equations</strong>, but broken down into <strong>variable flows</strong> and <strong>witness vectors</strong> &#8212; so you can see how each part of the computation maps into constraints.</p></li><li><p><strong>Lagrange interpolation implemented in code</strong>, so you can build QAP polynomials from constraint matrices, not just read about it.</p></li><li><p>The <strong>full QAP transformation</strong>: from R1CS constraints to polynomial identity checks.</p></li><li><p>Pairing-based proof verification using <code>noble-bls12-381</code> &#8212; so you can see how the cryptographic pairing check works under the hood.</p></li></ul><p>This hands-on approach is powered by a complete <strong>Learning Module</strong> on GitHub that complements this post. You&#8217;ll be guided through building a zk-SNARK prover and verifier from scratch &#8212; no hidden black boxes.</p><blockquote><p>If you've ever thought, "I kind of get the idea&#8230; but I wish someone showed me how to actually code it," this is exactly the guide for you.</p></blockquote><p>&#128279; <a href="https://github.com/shard-lab/protocol-camp/tree/main/advanced/zksnark">Jump to the Learning Module</a></p><div><hr></div><h2>Why Should You Care?</h2><p>zk-SNARKs aren't just cryptographic novelties. They're already being used to solve real problems in privacy, scalability, and verification.</p><ul><li><p><strong>Privacy</strong>: Prove you've made a valid transaction without revealing the sender, receiver, or amount.</p></li><li><p><strong>Scalability</strong>: Summarize thousands of off-chain transactions into a single on-chain proof (as in zkRollups).</p></li><li><p><strong>Selective Disclosure</strong>: Prove you're over 18, or a verified user, without revealing anything else about your identity.</p></li><li><p><strong>Verifiable Voting</strong>: Enable private but auditable election systems.</p></li></ul><p>At a deeper level, zk-SNARKs let us move from trust-based systems to proof-based systems: from "trust me" to "prove it &#8212; without showing me how."</p><div><hr></div><h2>How zk-SNARKs Work - An Overview</h2><p>Before diving into code, let&#8217;s map the process from idea to proof &#8212; with enough detail to help you truly understand what each stage means and why it matters.</p><ol><li><p><strong>Model the computation as an equation</strong><br>Let&#8217;s say you want to prove you know a number <code>x</code> such that: <code>(x + 2)(x + 3)(x - 1) = 60</code> You want to prove this without revealing what <code>x</code> is. So the first step is to define this target computation.</p></li><li><p><strong>Convert it into constraints (R1CS)</strong><br>We break it into intermediate steps:</p><pre><code><code>a = x + 2 
b = x + 3 
c = a * b 
d = x - 1 
out = c * d</code></code></pre></li><li><p><strong>Create constraints for the multiplication steps:</strong></p><pre><code><code>c = (x + 2)(x + 3) // Constraint 1
out = c(x - 1)     // Constraint 2</code></code></pre><p>Each step becomes a constraint of the form <code>(A &#183; w)(B &#183; w) = (C &#183; w)</code>, where <code>w</code> holds all inputs and intermediate values. If all constraints hold, the computation is correct. We'll later convert them into polynomials using Lagrange interpolation.</p></li><li><p><strong>Run a trusted setup (CRS)</strong><br>A one-time ceremony generates encrypted references to powers of a secret number au au. These values (the CRS) are shared between the prover and verifier and enable secure computations "in the exponent" without ever revealing au au.</p></li><li><p><strong>Create a proof</strong><br>The prover uses their secret witness (the values of xx, aa, bb, etc.) and the CRS to generate a compact zk-SNARK proof. This proof confirms that the prover knows inputs satisfying the constraints, and thus, the original computation.</p></li><li><p><strong>Verify the proof</strong><br>Using the CRS and elliptic curve pairings, the verifier checks a single equation involving the committed polynomials. If it holds, the verifier is convinced the prover did the computation correctly &#8212; without learning any private data.</p></li></ol><p>Each of these stages is broken down in the following sections &#8212; each one comes with real TypeScript code and detailed explanations.</p><div><hr></div><h2>Step 1: Model the Computation as an Equation</h2><p>The first step in constructing a zk-SNARK is to define what you're trying to prove &#8212; without revealing the private input. For example:</p><pre><code><code>(x + 2)(x + 3)(x - 1) = 60</code></code></pre><p>You know some <code>x</code> that satisfies this equation, but you don&#8217;t want to reveal <code>x</code>. This step transforms a real-world claim into a formal computation &#8212; a statement that can be checked by others.</p><p>This is essential because zk-SNARKs can only prove knowledge of satisfying inputs for known computations. So you begin by explicitly expressing the logic you want to prove.</p><p>What&#8217;s introduced here:</p><ul><li><p><strong>Encrypted exponentiation</strong>: like proving 1 + 2 = 3 using <code>g^1 * g^2 = g^3</code></p></li><li><p>The <strong>target statement</strong>: a computation we want to prove knowledge of.</p></li><li><p>The concept of <strong>hiding the witness</strong>: <code>x</code> is known only to the prover.</p></li></ul><div><hr></div><h2>Step 2: Convert to R1CS (Rank-1 Constraint System)</h2><p>To move toward a verifiable proof, we break down our equation <code>(x + 2)(x + 3)(x - 1) = 60</code> into small, checkable steps. Why? Because zk-SNARKs need to express logic as a sequence of <strong>multiplications</strong>, which are non-linear and harder to fake than simple additions:</p><pre><code><code>a = x + 2
b = x + 3
c = a * b
d = x - 1
out = c * d</code></code></pre><p>We now define constraints only for the two multiplication steps:</p><ol><li><p><code>c = a * b</code></p></li><li><p><code>out = c * d</code></p></li></ol><p>Each of these becomes a separate R1CS constraint:</p><pre><code><code>(A &#183; w) * (B &#183; w) = (C &#183; w)</code></code></pre><p>Let&#8217;s define our witness vector:</p><pre><code><code>w = [1, x, a, b, c, d, out]</code></code></pre><p>(The <code>1</code> is for constant terms.)</p><p>Constraint matrices:</p><ul><li><p>Constraint 1: <code>c = (x + 2)(x + 3)</code></p><ul><li><p>A = [2, 1, 0, 0, 0, 0, 0] &#8594; selects <code>x + 2</code></p></li><li><p>B = [3, 1, 0, 0, 0, 0, 0] &#8594; selects <code>x + 3</code></p></li><li><p>C = [0, 0, 0, 0, 1, 0, 0] &#8594; outputs to <code>c</code></p></li></ul></li><li><p>Constraint 2: <code>out = c * (x - 1)</code></p><ul><li><p>A = [0, 0, 0, 0, 1, 0, 0] &#8594; selects <code>c</code></p></li><li><p>B = [-1, 1, 0, 0, 0, 0, 0] &#8594; selects <code>x - 1</code></p></li><li><p>C = [0, 0, 0, 0, 0, 0, 1] &#8594; outputs to <code>out</code></p></li></ul></li></ul><h3>Why Multiplication Constraints?</h3><p>Additions are linear and easy to merge &#8212; but <strong>multiplications are where logic becomes non-trivial</strong>. Every interesting computation can be reduced to additions and multiplications, and R1CS tracks each multiplication independently.</p><p>By checking that each multiplication constraint is satisfied, we know the entire logic has been executed correctly &#8212; this is what makes R1CS powerful:</p><p><strong>If all constraints are satisfied, then the full computation is correct.</strong></p><p>What&#8217;s introduced here:</p><ul><li><p>The <strong>witness vector</strong>: holds all inputs and intermediate values.</p></li><li><p>The <strong>R1CS format</strong>: breaks logic into clean multiplication constraints.</p></li><li><p><strong>Multiplication as the core of verification</strong>.</p></li></ul><div><hr></div><h2>Step 3: Transform R1CS to QAP (Quadratic Arithmetic Program)</h2><p>We now want to transform our R1CS constraints into a form that supports efficient proof generation and verification &#8212; this is where the <strong>Quadratic Arithmetic Program (QAP)</strong> comes in.</p><p>From Step 2, we had two multiplication constraints represented as R1CS matrices. We now assign each constraint a unique position on the x-axis:</p><ul><li><p>Constraint 1 &#8594; x = 1</p></li><li><p>Constraint 2 &#8594; x = 2</p></li></ul><p>Then, for each variable in the witness vector (<code>[1, x, a, b, c, d, out]</code>), we extract its role in the constraints to build polynomials:</p><p>Let&#8217;s take variable <code>x</code> as an example:</p><ul><li><p>In A matrix: it appears in both constraints &#8594; coefficients = [1, 0]</p></li><li><p>In B matrix: also in both &#8594; coefficients = [1, 1]</p></li><li><p>In C matrix: not used directly &#8594; coefficients = [0, 0]</p></li></ul><p>We now use <strong>Lagrange interpolation</strong> to construct a polynomial <code>u_x(x)</code> that satisfies:</p><pre><code><code>u_x(1) = 1   (from constraint 1)
u_x(2) = 0   (from constraint 2)</code></code></pre><p>This gives us:</p><pre><code><code>u_x(x) = (x - 2) / (1 - 2) = -(x - 2)</code></code></pre><p>Similarly, we do this for all variables and all matrices (A, B, C), building sets of polynomials:</p><ul><li><p><code>u&#7522;(x)</code>: left input</p></li><li><p><code>v&#7522;(x)</code>: right input</p></li><li><p><code>w&#7522;(x)</code>: output</p></li></ul><p>Using these, we compute the overall identity:</p><pre><code><code>P(x) = (&#931; w&#7522; * u&#7522;(x)) * (&#931; w&#7522; * v&#7522;(x)) - (&#931; w&#7522; * w&#7522;(x))</code></code></pre><p>This polynomial <code>P(x)</code> must evaluate to 0 at all constraint points (x = 1, 2).</p><p>To check that without revealing anything, we introduce:</p><h3>The Vanishing Polynomial <code>t(x)</code></h3><pre><code><code>t(x) = (x - 1)(x - 2)</code></code></pre><p>If <code>P(x)</code> is divisible by <code>t(x)</code>, then all constraints are satisfied. So we require:</p><pre><code><code>P(x) = h(x) * t(x)</code></code></pre><p>What&#8217;s introduced here:</p><ul><li><p><strong>Lagrange interpolation</strong>: turns discrete constraint coefficients into polynomials</p></li><li><p><strong>QAP polynomials</strong> (<code>u&#7522;</code>, <code>v&#7522;</code>, <code>w&#7522;</code>): one per witness variable</p></li><li><p><strong>P(x)</strong>: encodes all constraint logic into one equation</p></li><li><p><strong>Vanishing polynomial </strong><code>t(x)</code>: lets us check all constraints together</p></li></ul><p>This transformation sets us up to prove that the computation is valid &#8212; without revealing any of the inputs.</p><div><hr></div><h2>Step 4: Trusted Setup (CRS)</h2><p>Let&#8217;s say you want to prove that <code>1 + 2 = 3</code>, but without showing any of the actual numbers.</p><p>Instead of sharing <code>1</code>, <code>2</code>, or <code>3</code>, you raise a public group generator <code>g</code> to the power of those numbers:</p><pre><code><code>g&#185;, g&#178;, g&#179;</code></code></pre><p>Then the verifier can check:</p><pre><code><code>g&#185; &#183; g&#178; = g&#179;</code></code></pre><p>This works because exponentiation in cryptographic groups preserves multiplication:</p><pre><code><code>g&#185; &#183; g&#178; = g^(1+2) = g&#179;</code></code></pre><p>Even though the verifier never sees the actual values, they can still check the computation.</p><p>This is the intuition behind <strong>working in the exponent</strong> &#8212; we represent computations using group elements like <code>g^x</code>, and the verifier checks relations using elliptic curve pairings. a <strong>trusted setup</strong> to generate a <strong>Common Reference String (CRS)</strong> &#8212; a set of cryptographic values derived from a secret parameter au au, like:</p><pre><code><code>g&#185;, g&#7511;, g&#7511;&#178;, g&#7511;&#179;, &#8230;, g^(t(&#964;))</code></code></pre><p>These values are generated once and published publicly. Importantly, no one should ever learn the value of au au &#8212; otherwise, they could forge proofs.</p><p>The CRS enables us to work with polynomials in encrypted form. The prover can commit to their polynomial evaluations at au au using these powers, and the verifier can later check that those commitments satisfy the necessary identity.</p><p>What&#8217;s introduced here:</p><ul><li><p><strong>CRS (Common Reference String)</strong>: shared cryptographic data based on a secret au au</p></li><li><p><strong>Commitments to polynomials in exponent</strong>: instead of sending raw values, we send elliptic curve points</p></li><li><p><strong>Trusted setup ceremonies</strong>: often multi-party, to ensure no one knows the full au au</p></li></ul><p>This sets the foundation for zero-knowledge: polynomials are committed without revealing them.</p><div><hr></div><h2>Step 5: Create a Proof</h2><p>With the CRS and secret witness in hand, the prover builds the zk-SNARK proof. This proof contains cryptographic commitments to the polynomials we built earlier:</p><ul><li><p><code>A(&#964;)</code>: evaluation of the left polynomial side</p></li><li><p><code>B(&#964;)</code>: evaluation of the right side</p></li><li><p><code>C(&#964;)</code>: output polynomial</p></li><li><p><code>H(&#964;)</code>: the quotient polynomial where <code>P(x) = H(x) * t(x)</code></p></li></ul><p>Each is committed using elliptic curve points, e.g.:</p><pre><code><code>A = g^{A(&#964;)} &#8712; G1
B = g^{B(&#964;)} &#8712; G2
C = g^{C(&#964;)} &#8712; G1
H = g^{H(&#964;)} &#8712; G1</code></code></pre><p>Together, these commitments encode the proof that:</p><ul><li><p>You know a valid witness <code>w</code></p></li><li><p>It satisfies all constraints in R1CS &#8594; QAP</p></li><li><p>Without revealing any part of <code>w</code></p></li></ul><p>What&#8217;s introduced here:</p><ul><li><p><strong>Cryptographic commitments</strong> to polynomial evaluations</p></li><li><p><strong>Quotient polynomial H(x)</strong>: shows <code>P(x)</code> divides by <code>t(x)</code></p></li><li><p><strong>Structured proof format</strong>: allows small, efficient zk-SNARKs</p></li></ul><p>The proof is compact and non-interactive &#8212; no need to ask the prover any questions afterward.</p><div><hr></div><h2>Step 6: Verify the Proof</h2><p>The verifier now uses the CRS and pairing-based cryptography to check a single equation:</p><pre><code><code>e(A, B) = e(C, g) &#183; e(H, g^{t(&#964;)})</code></code></pre><p>This pairing check validates the main QAP identity in encrypted form:</p><pre><code><code>(A(&#964;) * B(&#964;)) - C(&#964;) = H(&#964;) * t(&#964;)</code></code></pre><p>If this identity holds, it means:</p><ul><li><p>The prover followed the computation logic</p></li><li><p>The secret witness satisfied all constraints</p></li><li><p>The proof is valid &#8212; without revealing the witness</p></li></ul><p>What&#8217;s introduced here:</p><ul><li><p><strong>Pairing checks</strong>: special cryptographic operations verifying equations in the exponent</p></li><li><p><strong>Soundness via randomness</strong>: the prover doesn&#8217;t know au au, so they can&#8217;t forge false polynomials</p></li><li><p><strong>Efficient verification</strong>: checking the proof takes constant time, no matter how big the original computation was</p></li></ul><p>In one elegant equation, the verifier confirms the truth of the entire computation.</p><h2>Theory to Code</h2><p>Feeling overwhelmed by zk-SNARK papers and slides? We get it. That&#8217;s why this guide includes a companion Learning Module &#8212; a GitHub repo full of walkthroughs and testable code.</p><p>In the repo, you'll find:</p><ul><li><p>Modular files: cleanly separated logic for R1CS, QAP, CRS, and proofs</p></li><li><p>Tests that match theory: each test corresponds to a concept in this guide</p></li><li><p>Minimal but modern stack: written in TypeScript, built on <code>noble-bls12-381</code></p></li></ul><p>&#128279; <a href="https://github.com/shard-lab/protocol-camp/tree/main/advanced/zksnark">View the zk-SNARK Learning Module on GitHub</a></p><div><hr></div><h2>Ready to See It in Action?</h2><p>Now that you've seen how zk-SNARKs work &#8212; from constraint modeling to polynomial transformations to cryptographic verification &#8212; it&#8217;s time to put the theory into code.</p><p>Our <a href="https://github.com/shard-lab/protocol-camp/tree/main/advanced/zksnark">Learning Module</a> takes everything you&#8217;ve just read and turns it into an actual working implementation. You&#8217;ll build your own prover and verifier using TypeScript and <code>noble-bls12-381</code>.</p><p>Each step &#8212; from R1CS generation to QAP polynomials to trusted setup &#8212; is covered with clear, testable code. If you're ready to turn ideas into code, dive into the repo and start experimenting.</p><div><hr></div><h2>Appendix: What This Guide Leaves Out &#8212; and Why</h2><p>This guide walks you through a full working version of zk-SNARKs &#8212; from R1CS to QAP to pairing-based verification. But it intentionally <strong>leaves out a few advanced features</strong> found in fully optimized zk-SNARK schemes like Groth16. Here's a quick overview:</p><h3>1. No Public Inputs Handling</h3><p>In many real-world use cases, you need to prove something <strong>about a public value</strong> &#8212; like proving that a transaction involves a known address. Groth16 handles this by including public inputs in the proof structure.</p><p>To do this, the trusted setup must include terms like:</p><pre><code><code>[g^{&#945;&#183;u&#7522;(&#964;)}], [g^{&#946;&#183;v&#7522;(&#964;)}], [g^{&#947;}], etc.</code></code></pre><p>And the final proof includes a separate component for public input evaluation.</p><p><strong>Why we skipped it:</strong> We focused this guide on private inputs only, to simplify the core ideas. Handling public inputs securely introduces extra pairing terms and CRS setup rules that would distract from the fundamentals.</p><h3>2. No &#945;, &#946;, &#947; Powers</h3><p>Groth16 introduces additional secret powers (&#945;, &#946;, &#947;) into the CRS, which are used to tie the different parts of the proof together and prevent specific types of forgery.</p><p>These terms affect how the proof elements like &#960;_A, &#960;_B, &#960;_C are constructed and verified:</p><pre><code><code>e(&#960;_A, &#960;_B) = e(&#960;_C, g) &#183; e(g^{&#945;&#183;&#946;}, g^&#947;)</code></code></pre><p><strong>Why we skipped it:</strong> Our goal was to focus on the <strong>structure of the computation and constraint system</strong>, not optimizations or tighter zero-knowledge guarantees. Adding &#945;, &#946;, &#947; increases complexity in both setup and verification.</p><p>By leaving these out, we can help you build an end-to-end zk-SNARK from scratch, understand all the intermediate representations, and trace how constraints become provable identities.</p><p>Once you're comfortable with the basics, stepping up to Groth16 is just a matter of adding public input handling and adjusting the CRS and pairing logic.</p><div><hr></div><p>We hope this was useful and provided you with a clear understanding on zk-SNARKs.</p><p><strong>&#128279; If you have questions or want to discuss about the topic and/or Learning Module, join us on <a href="https://discord.gg/pzAWj2HE">Discord</a>!</strong></p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://0xshardlab.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">Enjoy what we wrote? Subscribe for more!</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[Welcome to Protocol Newsletter]]></title><description><![CDATA[Closing the gap between theory and real-world blockchain development.]]></description><link>https://0xshardlab.substack.com/p/welcome-to-protocol-newsletter</link><guid isPermaLink="false">https://0xshardlab.substack.com/p/welcome-to-protocol-newsletter</guid><dc:creator><![CDATA[SHARDLAB]]></dc:creator><pubDate>Mon, 17 Mar 2025 08:45:06 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/6af70fc0-5516-4a23-8916-7b6222b86af5_1456x1048.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Beginnings are hard. Learning blockchain? Even harder.</p><p>Have you ever felt like you <em>kind of</em> understand how blockchain works but struggle to actually build something with it? The gap between knowing the concepts and implementing them in code is real&#8212;and that's exactly why we started this newsletter.</p><h3>Why This, Why Now?</h3><p>Blockchain development can feel overwhelming. There&#8217;s a flood of information out there, but a lot of it stays at the surface level. We wanted to create a space where Web3 builders&#8212;whether you're just starting out or already deep in the trenches&#8212;can go beyond theory and into actual implementation.</p><h3>What to Expect</h3><p>Protocol Newsletter isn&#8217;t just another blog or newsletter about blockchain concepts. It&#8217;s a hands-on learning resource designed to help you truly <em>build</em> on Web3. Every post will break down complex blockchain technologies, provide real-world coding examples, and explore both foundational concepts and cutting-edge developments.</p><ul><li><p><strong>Step-by-step breakdowns</strong> of blockchain protocols</p></li><li><p><strong>Code implementations</strong> to help you understand how things work under the hood</p></li><li><p><strong>Deep dives into emerging trends</strong> so you stay ahead of the curve</p></li></ul><h3>Join the Community</h3><p>This isn&#8217;t just about content&#8212;it&#8217;s about building a space where Web3 developers can learn, experiment, and grow together. Whether you're here to refine your skills or take your first steps into blockchain development, I hope this newsletter becomes a valuable part of your journey.</p><p>Hit that <strong>Subscribe</strong> button and let&#8217;s dive into the world of Web3 &#128640;</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://0xshardlab.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://0xshardlab.substack.com/subscribe?"><span>Subscribe now</span></a></p>]]></content:encoded></item></channel></rss>