日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Learn Blockchains by Building One

發布時間:2025/3/15 编程问答 14 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Learn Blockchains by Building One 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

You’re here because, like me, you’re psyched about the rise of Cryptocurrencies. And you want to know how Blockchains work—the fundamental technology behind them.

But understanding Blockchains isn’t easy—or at least wasn’t for me. I trudged through dense videos, followed porous tutorials, and dealt with the amplified frustration of too few examples.

I like learning by doing. It forces me to deal with the subject matter at a code level, which gets it sticking. If you do the same, at the end of this guide you’ll have a functioning Blockchain with a solid grasp of how they work.

Before you get?started…

Remember that a blockchain is an?immutable, sequential?chain of records called Blocks. They can contain transactions, files or any data you like, really. But the important thing is that they’re?chained?together using?hashes.

If you aren’t sure what a hash is,?here’s an explanation.

Who is this guide aimed at??You should be comfy reading and writing some basic Python, as well as have some understanding of how HTTP requests work, since we’ll be talking to our Blockchain over HTTP.

What do I need??Make sure that?Python 3.6+ (along with?pip) is installed. You’ll also need to install Flask and the wonderful Requests library:

pip install Flask==0.12.2 requests==2.18.4

Oh, you’ll also need an HTTP Client, like?Postman?or cURL. But anything will do.

Where’s the final code??The source code is?available here.


Step 1: Building a Blockchain

Open up your favourite text editor or IDE, personally I ???PyCharm. Create a new file, called?blockchain.py. We’ll only use a single file, but if you get lost, you can always refer to the?source code.

Representing a Blockchain

We’ll create a?Blockchain?class whose constructor creates an initial empty list (to store our blockchain), and another to store transactions. Here’s the blueprint for our class:

Blueprint of our Blockchain Class

Our?Blockchain?class is responsible for managing the chain. It will store transactions and have some helper methods for adding new blocks to the chain. Let’s start fleshing out some methods.

What does a Block look?like?

Each Block has an?index, a?timestamp?(in Unix time), a?list of transactions, a?proof?(more on that later), and the?hash of the previous Block.

Here’s an example of what a single Block looks like:

Example of a Block in our Blockchain

At this point, the idea of a?chain?should be apparent—each new block contains within itself, the hash of the previous Block.?This is crucial because it’s what gives blockchains immutability:?If an attacker corrupted an earlier Block in the chain then?all?subsequent blocks will contain incorrect hashes.

Does this make sense? If it doesn’t, take some time to let it sink in—it’s the core idea behind blockchains.

Adding Transactions to a?Block

We’ll need a way of adding transactions to a Block. Our?new_transaction()method is responsible for this, and it’s pretty straight-forward:

After?new_transaction()?adds a transaction to the list, it returns the?index?of the block which the transaction will be added to—the next one to be mined.?This will be useful later on, to the user submitting the transaction.

Creating new?Blocks

When our?Blockchain?is instantiated we’ll need to seed it with a?genesis?block—a block with no predecessors. We’ll also need to add a?“proof”?to our genesis block which is the result of mining (or proof of work). We’ll talk more about mining later.

In addition to creating the?genesis?block in our constructor, we’ll also flesh out the methods for?new_block(),?new_transaction()?and?hash():

The above should be straight-forward—I’ve added some comments and?docstrings?to help keep it clear. We’re almost done with representing our blockchain. But at this point, you must be wondering how new blocks are created, forged or mined.

Understanding Proof of?Work

A Proof of Work algorithm (PoW) is how new Blocks are created or?mined?on the blockchain.?The goal of PoW is to discover a number which solves a problem. The number must be?difficult to find?but easy to verify—computationally speaking—by anyone on the network. This is the core idea behind Proof of Work.

We’ll look at a very simple example to help this sink in.

Let’s decide that the?hash?of some integer?x?multiplied by another?y?must end in?0. So,?hash(x * y) = ac23dc...0. And for this simplified example, let’s fix?x = 5. Implementing this in Python:

from hashlib import sha256x = 5 y = 0 # We don't know what y should be yet...while sha256(f'{x*y}'.encode()).hexdigest()[-1] != "0":y += 1print(f'The solution is y = {y}')

The solution here is?y = 21. Since, the produced hash ends in?0:

hash(5 * 21) = 1253e9373e...5e3600155e860

In Bitcoin, the Proof of Work algorithm is called?Hashcash. And it’s not too different from our basic example above. It’s the algorithm that miners race to solve in order to create a new block. In general, the difficulty is determined by the number of characters searched for in a string. The miners are then rewarded for their solution by receiving a coin—in a transaction.

The network is able to?easily?verify their solution.

Implementing basic Proof of?Work

Let’s implement a similar algorithm for our blockchain. Our rule will be similar to the example above:

Find a number?p?that when hashed with the previous block’s solution a hash with 4 leading?0s is produced.

To adjust the difficulty of the algorithm, we could modify the number of leading zeroes. But 4 is sufficient. You’ll find out that the addition of a single leading zero makes a mammoth difference to the time required to find a solution.

Our class is almost complete and we’re ready to begin interacting with it using HTTP requests.


Step 2: Our Blockchain as an?API

We’re going to use the Python Flask Framework. It’s a micro-framework and it makes it easy to map endpoints to Python functions. This allows us talk to our blockchain over the web using HTTP requests.

We’ll create three methods:

  • /transactions/new?to create a new transaction to a block
  • /mine?to tell our server to mine a new block.
  • /chain?to return the full Blockchain.

Setting up?Flask

Our “server” will form a single node in our blockchain network. Let’s create some boilerplate code:

A brief explanation of what we’ve added above:

  • Line 15:?Instantiates our Node. Read more about Flask?here.
  • Line 18:?Create a random name for our node.
  • Line 21:?Instantiate our?Blockchain?class.
  • Line 24–26:?Create the?/mine?endpoint, which is a?GET?request.
  • Line 28–30:?Create the?/transactions/new?endpoint, which is a?POSTrequest, since we’ll be sending data to it.
  • Line 32–38:?Create the?/chain?endpoint, which returns the full Blockchain.
  • Line 40–41:?Runs the server on port 5000.

The Transactions Endpoint

This is what the request for a transaction will look like. It’s what the user sends to the server:

{"sender": "my address","recipient": "someone else's address","amount": 5 }

Since we already have our class method for adding transactions to a block, the rest is easy. Let’s write the function for adding transactions:

A method for creating Transactions

The Mining?Endpoint

Our mining endpoint is where the magic happens, and it’s easy. It has to do three things:

  • Calculate the Proof of Work
  • Reward the miner (us) by adding a transaction granting us 1 coin
  • Forge the new Block by adding it to the chain
  • Note that the recipient of the mined block is the address of our node. And most of what we’ve done here is just interact with the methods on our Blockchain class. At this point, we’re done, and can start interacting with our blockchain.

    Step 3: Interacting with our Blockchain

    You can use plain old cURL or Postman to interact with our API over a network.

    Fire up the server:

    $ python blockchain.py* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

    Let’s try mining a block by making a?GET?request to?http://localhost:5000/mine:

    Using Postman to make a GET?request

    Let’s create a new transaction by making a?POST?request tohttp://localhost:5000/transactions/new?with a body containing our transaction structure:

    Using Postman to make a POST?request

    If you aren’t using Postman, then you can make the equivalent request using cURL:

    $ curl -X POST -H "Content-Type: application/json" -d '{"sender": "d4ee26eee15148ee92c6cd394edd974e","recipient": "someone-other-address","amount": 5 }' "http://localhost:5000/transactions/new"

    I restarted my server, and mined two blocks, to give 3 in total. Let’s inspect the full chain by requesting?http://localhost:5000/chain:

    {"chain": [{"index": 1,"previous_hash": 1,"proof": 100,"timestamp": 1506280650.770839,"transactions": []},{"index": 2,"previous_hash": "c099bc...bfb7","proof": 35293,"timestamp": 1506280664.717925,"transactions": [{"amount": 1,"recipient": "8bbcb347e0634905b0cac7955bae152b","sender": "0"}]},{"index": 3,"previous_hash": "eff91a...10f2","proof": 35089,"timestamp": 1506280666.1086972,"transactions": [{"amount": 1,"recipient": "8bbcb347e0634905b0cac7955bae152b","sender": "0"}]}],"length": 3 }

    Step 4: Consensus

    This is very cool. We’ve got a basic Blockchain that accepts transactions and allows us to mine new Blocks. But the whole point of Blockchains is that they should be?decentralized. And if they’re decentralized, how on earth do we ensure that they all reflect the same chain? This is called the problem of?Consensus, and we’ll have to implement a Consensus Algorithm if we want more than one node in our network.

    Registering new?Nodes

    Before we can implement a Consensus Algorithm, we need a way to let a node know about neighbouring nodes on the network. Each node on our network should keep a registry of other nodes on the network. Thus, we’ll need some more endpoints:

  • /nodes/register?to accept a list of new nodes in the form of URLs.
  • /nodes/resolve?to implement our Consensus Algorithm, which resolves any conflicts—to ensure a node has the correct chain.
  • We’ll need to modify our Blockchain’s constructor and provide a method for registering nodes:

    A method for adding neighbouring nodes to our Network

    Note that we’ve used a?set()?to hold the list of nodes. This is a cheap way of ensuring that the addition of new nodes is idempotent—meaning that no matter how many times we add a specific node, it appears exactly once.

    Implementing the Consensus Algorithm

    As mentioned, a conflict is when one node has a different chain to another node. To resolve this, we’ll make the rule that?the longest valid chain is authoritative.?In other words, the longest chain on the network is the?de-facto?one. Using this algorithm, we reach?Consensus?amongst the nodes in our network.

    The first method?valid_chain()?is responsible for checking if a chain is valid by looping through each block and verifying both the hash and the proof.

    resolve_conflicts()?is a method which loops through all our neighbouring nodes,?downloads?their chains and verifies them using the above method.?If a valid chain is found, whose length is greater than ours, we replace ours.

    Let’s register the two endpoints to our API, one for adding neighbouring nodes and the another for resolving conflicts:

    At this point you can grab a different machine if you like, and spin up different nodes on your network. Or spin up processes using different ports on the same machine. I spun up another node on my machine, on a different port, and registered it with my current node. Thus, I have two nodes:?http://localhost:5000?and?http://localhost:5001.

    Registering a new?Node

    I then mined some new Blocks on node 2, to ensure the chain was longer. Afterward, I called?GET /nodes/resolve?on node 1, where the chain was replaced by the Consensus Algorithm:

    Consensus Algorithm at?Work

    And that’s a wrap... Go get some friends together to help test out your Blockchain.


    I hope that this has inspired you to create something new. I’m ecstatic about Cryptocurrencies because I believe that Blockchains will rapidly change the way we think about economies, governments and record-keeping.

    Update:?I’m planning on following up with a Part 2, where we’ll extend our Blockchain to have a Transaction Validation Mechanism as well as discuss some ways in which you can productionize your Blockchain.

    If you enjoyed this guide, or have any suggestions or questions, let me know in the comments. And if you’ve spotted any errors, feel free to contribute to the code?here!


    https://hackernoon.com/learn-blockchains-by-building-one-117428612f46

    總結

    以上是生活随笔為你收集整理的Learn Blockchains by Building One的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

    主站蜘蛛池模板: 欧美精品乱码视频一二专区 | 欧美日韩一区二区三区不卡视频 | 天天做夜夜爽 | 国产一二三在线观看 | 国产乱子伦精品无码码专区 | 蜜桃av在线 | 黄色麻豆视频 | 高跟鞋调教—视频|vk | 精品99久久久 | 亚欧视频在线观看 | 成熟丰满熟妇高潮xxxxx视频 | 日韩三级大片 | 亚洲在线视频网站 | 91片黄在线观看 | 欧美老女人性生活 | 不卡视频国产 | 国产精品99久 | 久久久www| 久操欧美 | 精品视频国产 | 二十四小时在线更新观看 | 香蕉视频网站在线观看 | 国产有码 | 欧美一级电影在线 | 亚洲成在人| 国产第一页屁屁影院 | 蜜乳av懂色av粉嫩av | 中国极品少妇xxxxx | 人妻一区二区三区在线 | 久久久久国色av免费观看性色 | 五月婷婷一区二区三区 | www日韩欧美 | 欧美中文一区 | 91麻豆精品一区二区三区 | 久久久久国产精品区片区无码 | 亚洲免费精品 | 国产一区二区三区久久 | 97视频国产| 在线观看国产精品视频 | 日韩福利影院 | 国产成人无码精品久久 | 久久久国产精 | 久久久国际精品 | 爱啪啪影视 | www.亚洲色图.com | 四虎影视av | 欧美做受高潮中文字幕 | 四虎精品在线观看 | 密桃成熟时在线观看 | 亚洲色图欧美在线 | 欧美精品久久久久久 | 日韩三级观看 | 天天操精品 | 人妻巨大乳一二三区 | 中文字幕免费一区二区 | 中文字幕亚洲日本 | 2018自拍偷拍 | 春草| 日韩av一| 在线观看日本视频 | 杂技xxx裸体xxxx欧美 | 日韩1024| 国产精品久久久久久久久久久新郎 | 亚洲成人国产精品 | 久久福利影视 | 成人深夜视频 | 手机在线永久免费观看av片 | 日韩精品中字 | 少妇搡bbbb搡bbb搡澳门 | 国产免费无遮挡吸奶头视频 | 五月婷婷中文字幕 | 亚洲成人第一页 | 亚洲色图一区二区 | 中国美女一级黄色片 | 国产免费av一区二区三区 | 久久精品99国产精 | 夜夜骚视频 | 免费看污黄网站在线观看 | 精品无码国产污污污免费网站 | 日韩精品影视 | 69xxx国产| 欧美天天性 | 中文字幕一区二区三区四区不卡 | 精品国产一区二区三区在线 | 欧美中文一区 | 黄色工厂在线观看 | 桃色视频在线 | 亚洲福利一区二区 | 国产又爽又猛又粗的视频a片 | www.一区二区三区四区 | 免费久久网站 | 亚洲免费一二三区 | 免费看黄色三级三级 | xxxxⅹxxxhd日本8hd| 精品一区三区 | www.中文字幕 | av成人在线免费观看 | 国产成人专区 | 中文字幕一区二区三区手机版 |