Aaron Cruz

How Mining Works

Aaron explains crypto mining Proof of Work using Ruby examples: the problems it solves and how it solves them. He answers the question "Why are we using so much electricity mining crypto?".
-------------------------------------
#pivorak is a Lviv Ruby Meetup

Join our Ruby meetups in Lviv:
Our official website - https://pivorak.com
Facebook -- https://www.facebook.com/pivorak
Twitter -- https://twitter.com/pivorakmeetup
Instagram -- https://www.instagram.com/pivorakmeetup

No crap. No crab. #pivorak.

Pivorak Conf 1.0

00:00:08.189 Hi, this is my second time in Ukraine. I was here a couple of years ago at RubyC in Kyiv. It was really nice, and I had a great time. I'm glad to be back!
00:00:19.980 Today, I'm going to give a talk about how crypto mining works. I think that I missed the boat on this topic; it would have been a better time to discuss cool blockchain stuff back in 2017.
00:00:30.510 However, it's still something I think about a lot, and it took me quite a while to understand the core of what's actually happening, especially in mining. Over the last year and a half, I've been managing a small crypto mining farm in Austria.
00:01:06.899 It's a bit weird because people often have mixed feelings about it. The setup is rather ghetto; it's inside a container filled with GPUs. You can see them up close, though the images might be a bit blurry.
00:01:24.959 When you enter the container in the dark, it looks beautiful with the gaming lights, but it's really loud and it dries out your eyes. So, it's not exactly a fun place to be.
00:01:38.220 Here's my mining rig in my office. You can see one video card and a fan. The only video cards I can get are the super busted ones, so I have taken other fans and sandwich them in. The rig isn't very efficient, but electricity is free in my office, which is a nice benefit.
00:02:24.340 This talk isn't going to be a traditional lecture. I've been trying to give versions of this talk for the last few months and have hated it every time, so I thought I would change things up and make it more of a demo experiment.
00:02:36.990 I want to talk a bit about electronic currency. We need to ensure that we're in the right space for this; that's better than simply where we were before. We want to create a simple, centralized electronic currency.
00:03:01.660 Our system will have one server that processes requests, with the ability to send money back and forth. It's a very simple structure, and we can call it an electronic currency for now. I built a simple Sinatra app to manage accounts.
00:03:20.320 I get 10 million units of the currency at the start because I'm the boss. Down here, we have one endpoint for sending money via a POST request. You just need a 'from' string, a 'to' string, and an amount. The system subtracts money from one account and adds to another.
00:04:06.100 It's running! I think it should print out the accounts after a certain amount of time, but right now, we want to send a request.
00:04:13.300 We have a client that just sends a POST request. Let's send money from my account to someone else. So, I'll send 10,000, not my entire amount, just enough to keep things interesting.
00:04:53.800 And look, we sent money! Now we both have currency in this system. We can continue sending money to others, and you can see the amounts being subtracted and added to our accounts.
00:05:34.990 To make this a bit more interactive, I brought along some Austrian wafer cookies called 'Manner.' They're actually vegan, which is nice. If someone can tell me one flaw in our electronic currency, they can have a cookie.
00:06:12.490 One issue I encountered is that I can send more money than I actually have. For example, if I try to send 100 million when I have a negative balance, the system flags that as a problem. This shows how our currency can have vulnerabilities.
00:08:41.660 Another problem is that anyone can send money to any account without authentication, which is definitely a concern. We need to address this and will get to that in a moment.
00:09:07.890 Moving on to step two, we will incorporate cryptography into our system. Instead of traditional passwords, we will use public and private keys to sign transactions. This will enhance security significantly.
00:09:54.440 Each wallet will have a public key and a private key. The public key acts as an address. Instead of sharing the entire public key, we can encode it, simplifying things.
00:10:39.520 When creating transactions, you'll use your wallet's private key to sign them, which generates a signature. This process allows you to verify that a specific wallet address initiated the transaction.
00:11:36.360 Let's quickly look at how our transaction is structured. Each transaction includes a 'from' address, an 'amount,' and a 'signature.' I'm using a library called Virtus to facilitate this.
00:12:24.400 Now we should go to our server to execute and verify transactions. We start with an empty account, and when we set up the server, we create a wallet. I print out the public and private keys for reference.
00:13:19.490 Next, we create our first transaction using the origin wallet, ensuring it's valid from the start. We provide the signature and the other necessary transaction details.
00:14:18.740 We then send this transaction to the server and verify that everything is functioning correctly. Our initial verification process results in a balance being credited to the public key associated with our transaction.
00:15:50.600 Now, we should create another wallet and transfer money from one account to another. The first account sends 10,000 units to the second.
00:17:03.780 To see how the distributed system works, when monitoring transactions, we should notice the state of accounts changing accordingly.
00:18:07.640 As we move forward, we must keep in mind that our server could theoretically still go down, potentially leading to database issues. If we lose our database, it could result in severe data loss.
00:19:26.990 Another concern is that someone with control over the server could create fake transactions. We need to maintain a secure environment to prevent these risks.
00:19:58.330 Let's also consider privacy issues. While our transactions may be authenticated, they are not encrypted. This means people can see the transaction flow between accounts.
00:20:49.020 Moving on, let's look at a more decentralized approach to currency management. Instead of money, we're going to build a system to record tweets using a gossip protocol.
00:21:58.640 In this system, when a node starts up, it receives a set of tweets and continues to share this information with other nodes in the network. Even if some nodes go offline, the tweets will still be accessible.
00:23:23.880 Let me explain how the gossiping works; the nodes will send their tweet data to each other and compare it, ensuring they always have the latest information.
00:25:00.210 Next, we have our tweeter. It streams tweets and adds them to a global data array in our network. However, don't do this kind of coding in production; it's sloppy.
00:25:54.430 When our server starts, the accounts are initially empty until we start processing transactions. It’s interesting to see how each node references each previous block it builds upon.
00:27:24.300 Let's run through a quick demo of how the tweets and transactions interact in this distributed system.
00:28:31.180 Within this system, blocks will reference the hash of the previous block they are built upon, forming a chain, also known as a blockchain.
00:29:09.430 To preserve transaction order, we'll implement a formal method for validating transactions. The blockchain effectively protects against double spending by ensuring that accounts can't have negative balances during transactions.
00:30:06.500 Through proof of work, we build blocks that contain transaction data. Each block includes a hash of the previous block and ensures that a certain amount of work is done to process the transaction.
00:31:01.650 We can use variables to define how many leading zeros we require to validate the block. This will illustrate how we can adjust mining difficulty based on network conditions.
00:32:34.490 As we test the system, let's set our target to observe variations in how long it takes to calculate a valid hash. This is where we will see proof of work in action.
00:34:19.400 After these steps, we aim to solidify our understanding of the blockchain's mechanics and how every transaction is validated based on computed hashes.
00:35:43.520 Now, we run our process to validate transactions and observe the entire system working cohesively, as nodes verify blocks and maintain the integrity of our coin.
00:37:41.700 The outcome shows a successful transaction that confirms whether previous transactions are adhering to our rules governing account balances.
00:38:53.740 Finally, let's review the overall structure once again, wishing to highlight how blocks are built sequentially and consistently reference their predecessors.
00:40:40.650 This allows us to eliminate risks associated with multiple account ownership and secures our cryptocurrency system against potential exploitation.
00:41:47.580 We have now come to our closing thoughts, discussing the importance of maintaining a secure network, the significance of minimizing double spending, and the use of blockchain technology to safeguard our digital transactions.
00:47:20.880 Thank you for your attention, feel free to ask any questions regarding the demonstrations or the core ideas I presented today.