<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Aaron Cheng</title>
    <description>The latest articles on DEV Community by Aaron Cheng (@gitguudd).</description>
    <link>https://dev.to/gitguudd</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F815269%2F5246ff92-b2be-4695-bf46-1f7dcdcfa7ba.png</url>
      <title>DEV Community: Aaron Cheng</title>
      <link>https://dev.to/gitguudd</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/gitguudd"/>
    <language>en</language>
    <item>
      <title>The strangest thing in Ruby</title>
      <dc:creator>Aaron Cheng</dc:creator>
      <pubDate>Wed, 16 Mar 2022 05:03:07 +0000</pubDate>
      <link>https://dev.to/gitguudd/the-strangest-thing-in-ruby-5eim</link>
      <guid>https://dev.to/gitguudd/the-strangest-thing-in-ruby-5eim</guid>
      <description>&lt;p&gt;I recently ran in the the following error via Rubocop in Ruby 3.1:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Omit the hash value&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;I was puzzled and Googling didn't really help me find anything - until I dug into Rubocop itself (the error is given &lt;a href="https://www.rubydoc.info/gems/rubocop/RuboCop/Cop/HashShorthandSyntax"&gt;here&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;It turns out that this warning is saying that we are now allowed to omit hash values &lt;em&gt;if&lt;/em&gt; the variable name is the same as the hash key. &lt;/p&gt;

&lt;p&gt;See the following example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Assume Point has two parameters, X and Y

# Before Ruby 3.1
x = 3
y = 10
Point.new(x: x, y: y)

# After Ruby 3.1
x = 3
y = 10
Point.new(x:, y:)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice how we can now just use the key names and leave the variables empty!&lt;/p&gt;

&lt;p&gt;It's called Ruby's hash omission syntax, and it's new, as of Ruby 3.1!&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
      <category>webdev</category>
    </item>
    <item>
      <title>A Toy Blockchain in 100 Lines or Less</title>
      <dc:creator>Aaron Cheng</dc:creator>
      <pubDate>Mon, 14 Feb 2022 18:40:43 +0000</pubDate>
      <link>https://dev.to/gitguudd/a-toy-blockchain-in-100-lines-or-less-50ko</link>
      <guid>https://dev.to/gitguudd/a-toy-blockchain-in-100-lines-or-less-50ko</guid>
      <description>&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;As the popularity and mainstream awareness of NFTs and blockchains proliferate, I was pretty confused about how they worked - I kept hearing about "mining" and "blocks", and so I figured the best way to learn about the technology was to create a small model of it to see how the internals functioned (or at least a small subset of it).&lt;/p&gt;

&lt;p&gt;I also took the opportunity to use Rust to build it, as I've had my eye on it for a while, but haven't yet found the opportunity to use it for anything substantial.&lt;/p&gt;

&lt;h3&gt;
  
  
  First Principles
&lt;/h3&gt;

&lt;p&gt;As with any new technology, let's start with the basics. For the blockchain, I know that we must be able to have some sort of data structure that contains a chain of blocks. Since we'll be adding to the chain, we know that our chain will need to be some sort of variable-length container.&lt;/p&gt;

&lt;p&gt;For our purposes, we'll choose a &lt;code&gt;Vec&lt;/code&gt; that holds &lt;code&gt;Block&lt;/code&gt;s in Rust and declare it like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;struct BlockChain {
    chain: Vec&amp;lt;Block&amp;gt;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that we have a chain that holds &lt;code&gt;Block&lt;/code&gt;s we'll need to define the &lt;code&gt;Block&lt;/code&gt; itself:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;struct Block {
    index: usize,
    timestamp: i64,
    data: Vec&amp;lt;BlockData&amp;gt;,
    hash: String,
    previous_hash: String,
    proof: i64
} 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For most blockchains (and this one included), the block contains pretty standard fields:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;index: the index of the block on the chain&lt;/li&gt;
&lt;li&gt;timestamp: when this block was mined&lt;/li&gt;
&lt;li&gt;data: the data of the block itself - this is usually the list of transactions that this block is holding (normally this is all previous transactions + one additional transaction)&lt;/li&gt;
&lt;li&gt;hash: the SHA256 hash of this block&lt;/li&gt;
&lt;li&gt;previous_hash: the SHA256 hash of the block that came before&lt;/li&gt;
&lt;li&gt;proof: the number required for the Proof of Work algorithm (more on this later)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We'll also define the &lt;code&gt;BlockData&lt;/code&gt; as the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;struct BlockData {
    sender: String,
    recipient: String,
    amount: f64
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the end, our chain will eventually be a bunch of blocks holding data about who is sending the amount of our coin where. Other blockchains (such as Ethereum or Solana) will have their blockchain data strucutured differently to allow for things such as smart contracts or NFTs - you can view the Ethereum documentation to see what their data is structured like &lt;a href="https://ethereum.org/en/developers/docs/transactions/"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Second Steps
&lt;/h3&gt;

&lt;p&gt;So we've defined our chain and the basic data structures we'll be using - the next steps will be to create the chain itself.&lt;/p&gt;

&lt;p&gt;In Rust, we'll be using &lt;code&gt;impl&lt;/code&gt; to add methods to the &lt;code&gt;BlockChain&lt;/code&gt; struct:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;struct BlockChain {
    chain: Vec&amp;lt;Block&amp;gt;
}

impl BlockChain {
    // methods go here
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first method to add is &lt;code&gt;genesis_block&lt;/code&gt; -- all this really does is create the very first block of the chain. Keep in mind that this is Rust - so we'll just be implementing functionality for the &lt;code&gt;BlockChain&lt;/code&gt; type (and the various other types we have defined).&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;genesis_block&lt;/code&gt; method is as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fn genesis_block(&amp;amp;mut self) {
    let now = Utc::now();
    let mut new_block = Block {
        index: 0, 
        timestamp: now.timestamp(),
        data: [].to_vec(),
        hash: "".to_string(),
        previous_hash: "".to_string(),
        proof: 0
    };

    new_block.hash = new_block.hash();
    self.chain.push(new_block);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One thing to note is we are marking the &lt;code&gt;self&lt;/code&gt; variable to be mutable - this is because we are modifying the chain itself (i.e. adding a block to the chain). You'll also notice that almost everything in this block is empty - there is no data, no hash, the proof is set to 0 - this is because blocks are meant to be built from the previous block in the chain - if there is no other block, we must have some data somewhere in order for this block to be valid. &lt;/p&gt;

&lt;p&gt;Note: We're using the &lt;code&gt;chrono&lt;/code&gt; crate here (specifically &lt;code&gt;Utc&lt;/code&gt; from &lt;code&gt;chrono&lt;/code&gt;) so be sure to add the &lt;code&gt;chrono&lt;/code&gt; package to your &lt;code&gt;Cargo.toml&lt;/code&gt; file to have access to it.&lt;/p&gt;

&lt;p&gt;One last thing to note is that we are calling a &lt;code&gt;hash&lt;/code&gt; method on the &lt;code&gt;new_block&lt;/code&gt; object - but we haven't defined one yet! So let's do that now:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;impl Block {
    fn hash(self) -&amp;gt; String {
        let hashable_data = format!("{:?}{}{}{}{}{}", self.data, self.hash, self.index, self.previous_hash, self.proof, self.timestamp);
        return digest(hashable_data);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;hash&lt;/code&gt; method looks simple, but we're doing a lot here:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;the &lt;code&gt;digest&lt;/code&gt; method is using the &lt;code&gt;sha256&lt;/code&gt; crate, so that's installed via our &lt;code&gt;cargo.toml&lt;/code&gt; file, and declared by &lt;code&gt;use sha256::digest&lt;/code&gt; at the top of the file.&lt;/li&gt;
&lt;li&gt;We'll need to also add the &lt;code&gt;serde&lt;/code&gt; package that allows us to serialize and deserialize data so we can save the &lt;code&gt;BlockData&lt;/code&gt; as a String format (using the &lt;code&gt;format!&lt;/code&gt; macro). Be sure to install &lt;code&gt;serde&lt;/code&gt; and also add the following traits to the &lt;code&gt;BlockData&lt;/code&gt; struct: &lt;code&gt;#[derive(Serialize, Deserialize, Debug, Clone)]&lt;/code&gt;. We'll want to add those traits to both the &lt;code&gt;Block&lt;/code&gt; and &lt;code&gt;Blockchain&lt;/code&gt; structs as well to allow us to print the contents to stdout.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In the end, your &lt;code&gt;BlockData&lt;/code&gt; and &lt;code&gt;Block&lt;/code&gt; structs should look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#[derive(Serialize, Deserialize, Debug, Clone)]
struct BlockData {
    sender: String,
    recipient: String,
    amount: i64
}

#[derive(Serialize, Deserialize, Debug, Clone)]
struct Block {
    index: usize,
    timestamp: i64,
    data: Vec&amp;lt;BlockData&amp;gt;,
    hash: String,
    previous_hash: String,
    proof: i64
}

#[derive(Serialize, Deserialize, Debug, Clone)]
struct BlockChain {
    chain: Vec&amp;lt;Block&amp;gt;
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In our &lt;code&gt;main&lt;/code&gt; function, if we do the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let mut chain = BlockChain {
        chain: [].to_vec()
    };

chain.genesis_block();

println!("{:#?}", chain);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output should be similar to the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;BlockChain {
    chain: [
        Block {
            index: 0,
            timestamp: 1644628637,
            data: [],
            hash: "e7bcf22ad4e5d6c4387626c6ab70db8d52ba5977e85ac73e6ddcc51517a1a2ba",
            previous_hash: "",
            proof: 0,
        },
    ],
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that's great! We have our toy block chain that contains &lt;code&gt;Blocks&lt;/code&gt; and we have the ability to create an chain with the genesis block on it. Our next step is to give us the ability to add a block and then perform work!&lt;/p&gt;

&lt;h3&gt;
  
  
  Why Work?
&lt;/h3&gt;

&lt;p&gt;This is one of the defining characteristics of the blockchain - doing some sort of heavy computational work just to be able to add to the chain. It's part of the reason why verifying / adding blocks to the current Bitcoin chain is so slow (or any Blockchain network).&lt;/p&gt;

&lt;p&gt;The question remains however - why do we do this? Why not just allow for the addition of blocks via just &lt;code&gt;chain.push&lt;/code&gt; like we do for the genesis block - why perform this work?&lt;/p&gt;

&lt;p&gt;The answer lies in the original Bitcoin protocol paper. In it, the author (Satoshi Nakamoto) proposes Proof of Work as a way of solving the double spend problem (i.e. multiple people can't push to the chain and say "Aaron got 1000 bucks") - and one way to ensure that is to have a central authority checking every transaction. &lt;/p&gt;

&lt;p&gt;As blockchain is meant to be distributed, we need a way for everybody using the chain to come to an agreement about which blocks are valid, and which ones are not. This is where Proof of Work comes in.&lt;/p&gt;

&lt;p&gt;As transactions are broadcast to the blockchain network (Bitcoin or otherwise), miners collect them and validate them based on the &lt;em&gt;previous&lt;/em&gt; block. For our toy blockchain, that means doing something like the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;impl BlockChain {
    ...
    fn add_block(&amp;amp;mut self, sender: String, recipient: String, amount: i64) {
            // get the previous block
            let previous_block = self.chain.last().unwrap();

            // create new block data based on the new transaction
            let mut chain_data = previous_block.data.to_vec();
            let new_data = BlockData {
                sender: sender.to_string(),
                recipient: recipient.to_string(),
                amount: amount
            };
            chain_data.push(new_data);

            let now = Utc::now();
            // create a new block with the new transaction
            let mut new_block = Block {
                index: previous_block.index + 1, 
                timestamp: now.timestamp(),
                data: chain_data,
                hash: "".to_string(),
                previous_hash: previous_block.hash.to_string(),
                proof: 0
            };

            // do the proof of work 
            new_block.proof = new_block.mine(previous_block.proof);
            new_block.hash = new_block.hash();
            self.chain.push(new_block);
        }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can see in the above &lt;code&gt;add_block&lt;/code&gt; method we get the new transaction data, add it to the previous block's data and then perform the work in the &lt;code&gt;mine&lt;/code&gt; method. The &lt;code&gt;mine&lt;/code&gt; method looks like the following (it's based on the HashCash algorithm mentioned in the Bitcoin whitpaper):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;impl Block {
    // other methods
    fn mine(&amp;amp;self, previous_proof: i64) -&amp;gt; i64 {
        let mut hashed_string = "".to_string();
        let mut proof = 0;
        while !hashed_string.starts_with("00000") {
            let work_string = format!("f{}{}", previous_proof, proof);
            hashed_string = digest(work_string);
            proof += 1;
        }
        return proof;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By using the &lt;em&gt;previous&lt;/em&gt; block's proof, we've established a dependency on the previous block - assuming it is valid - there is no way for the current validator (or miner) to alter the blockchain without going through the whole chain and rehashing the proof for &lt;em&gt;every&lt;/em&gt; block on the chain. &lt;/p&gt;

&lt;p&gt;Even with small chains, this can prove to be prohibitively expensvie - although with this approach, just maintaining and adding new blocks can get more and more expensive over time - you can see this with Bitcoin, where the current energy expenditure of the Bitcoin network is greater than than &lt;a href="https://digiconomist.net/bitcoin-energy-consumption/"&gt;Thailand&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The most important part of the above function is the &lt;code&gt;starts_with("00000")&lt;/code&gt; part - that determines the "difficulty" of adding a block to the chain - i.e. how long it will take (on average) to add a block to the chain. This particular chain, on my particular computer will take roughly 8 seconds to finish the work for and add a block to the chain. Bitcoin takes roughly 10 minutes to add a block. To increase the difficulty of the mine, just add more &lt;code&gt;0&lt;/code&gt;s - to reduce, remove &lt;code&gt;0&lt;/code&gt;s.&lt;/p&gt;

&lt;h3&gt;
  
  
  What does this look like?
&lt;/h3&gt;

&lt;p&gt;We now have a working instance of a &lt;code&gt;Blockchain&lt;/code&gt;, consisting of multiple &lt;code&gt;Blocks&lt;/code&gt; that can be mined and added to the &lt;code&gt;BlockChain&lt;/code&gt; via our Proof of Work algorithm. &lt;/p&gt;

&lt;p&gt;For our toy blockchain this looks like the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fn main() {
    let mut chain = BlockChain {
        chain: [].to_vec()
    };

    chain.genesis_block();
    chain.add_block("mr.magoo".to_string(), "mr.magee".to_string(), 1000);

    println!("{:#?}", chain);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With the following output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;BlockChain {
    chain: [
        Block {
            index: 0,
            timestamp: 1644709286,
            data: [],
            hash: "223f430ce60c464d57fb13cae30c843b82260287dc329fc6be59cfc183fc48fb",
            previous_hash: "",
            proof: 0,
        },
        Block {
            index: 1,
            timestamp: 1644709286,
            data: [
                BlockData {
                    sender: "mr.magoo",
                    recipient: "mr.magee",
                    amount: 1000,
                },
            ],
            hash: "f315332a38dfcce0ce696f5371cf8a4de33db76d59f094322d0ddf68a7601ab6",
            previous_hash: "223f430ce60c464d57fb13cae30c843b82260287dc329fc6be59cfc183fc48fb",
            proof: 375214,
        },
    ],
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  What is missing
&lt;/h3&gt;

&lt;p&gt;We have a very basic blockchain implementation here, but there's a bunch of things missing which I'll list out in no particular order:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Peer-to-peer network&lt;/strong&gt;: our blockchain currently is only aware of itself, and will not mine blocks automatically - in fact, new blocks can only be added by hardcoding them into the &lt;code&gt;main&lt;/code&gt; function. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Chain verification&lt;/strong&gt;: Our blockchain process is currently not validating the chain - this is usually accomplished by only accepting the chain that is longest - right now we don't do that&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Even with those two big things missing (the whole decentralized concept really), to see a basic blockchain in action is super interesting - and the concepts between this blockchain and others isn't much different. &lt;/p&gt;

&lt;p&gt;You can view our completed source code &lt;a href="https://github.com/gitguudd/toychain/blob/main/src/main.rs"&gt;here&lt;/a&gt;&lt;/p&gt;

</description>
      <category>rust</category>
      <category>blockchain</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
