美文网首页程序员区块链研习社
来吧,创造一条区块链(下)

来吧,创造一条区块链(下)

作者: Andytl的世界 | 来源:发表于2018-07-26 07:05 被阅读27次

上一篇介绍了一条迷你区块链‘蛇链’的诞生,用了不到50行代码。为了把‘蛇链’变成正真可以使用的区块链,本篇给‘蛇链’添加了一些新的功能,发行‘蛇币’,真的能收发币了!下面是文章的翻译。

蛇币来了!

Note: This article assumes you’ve read part one.
警告:没看上一篇不许看这篇(ง ˙o˙)ว。

The tiniest blockchain was extremely simple, and it was relatively easy to make. But, with its simplicity came a few flaws.
迷你区块链非常简单,简单也带来一些问题。

First, SnakeCoin only ran on one single machine, so it was far from distributed, let alone decentralized.
第一个问题,目前‘蛇链’只能单机运行,离分布式还很远,更别说去中心化了。

Second, blocks could be added to the chain as fast as the host computer could create a Python object and add it to a list.
第二个问题,区块增长的速度和计算机速度一样快,其实就是计算机创建Python对象并添加到列表的速度。

In the case of a simple blockchain, that’s not a problem, but we’re now going to let SnakeCoin be an actual cryptocurrency so we’ll need control the amount of blocks (and coins) that can be created at a time.
在迷你区块链中,出块速度快不是什么问题,但如果想让‘蛇币’成为正真的加密货币,我们必须限制出块速度。

From now on, SnakeCoin’s data will be transactions, so each block’s data field will be a list of some transactions. We’ll define a transaction as follows. Each transaction will be a JSON object detailing the sender of the coin, the receiver of the coin, and the amount of SnakeCoin that is being transferred.
‘蛇链’的区块数据存放交易记录,也就成了‘蛇币’。每条交易记录是JSON格式,包含发币人、收币人和交易金额。

Note: Transactions are in JSON format for a reason I’ll detail shortly.
交易记录为啥用JSON格式后面会讲。

{
  "from": "71238uqirbfh894-random-public-key-a-alkjdflakjfewn204ij",
  "to": "93j4ivnqiopvh43-random-public-key-b-qjrgvnoeirbnferinfo",
  "amount": 3
}

Now that we know what our transactions will look like, we need a way to add them to one of the computers in our blockchain network, called a node.
知道交易记录长啥样了,接下来我们需要把交易记录添加到区块链网络中的一台电脑(或者叫节点)。

To do that, we’ll create a simple HTTP server so that any user can let our nodes know that a new transaction has occurred.
为了实现这个功能,我们创建一个简单的HTTP服务,任何用户可以让节点知道新交易记录来了。

A node will be able to accept a POST request with a transaction (like above) as the request body. This is why transactions are JSON formatted; we need them to be transmitted to our server in a request body.
节点将能够接收POST请求,请求中包含交易记录。这就是为啥我们用JSON格式表示交易记录。我们需要交易记录作为请求体发送到服务器。

pip install flask # Install our web server framework first
node = Flask(__name__)

# Store the transactions that
# this node has in a list
this_nodes_transactions = []

@node.route('/txion', methods=['POST'])
def transaction():
  if request.method == 'POST':
    # On each new POST request,
    # we extract the transaction data
    new_txion = request.get_json()
    # Then we add the transaction to our list
    this_nodes_transactions.append(new_txion)
    # Because the transaction was successfully
    # submitted, we log it to our console
    print "New transaction"
    print "FROM: {}".format(new_txion['from'])
    print "TO: {}".format(new_txion['to'])
    print "AMOUNT: {}\n".format(new_txion['amount'])
    # Then we let the client know it worked out
    return "Transaction submission successful\n"

node.run()

Awesome! Now we have a way to keep a record of users when they send SnakeCoins to each other. This is why people refer to blockchains as public, distributed ledgers: all transactions are stored for all to see and are stored on every node in the network.
666!我们已经有办法记录‘蛇币’交易了。可以看出,为什么人们把区块链当作公开的、分布式的账本,因为所有的交易记录都能被所有人看到而且存储在网络中每个节点上。

But, a question arises: where do people get SnakeCoins from? Nowhere, yet. There’s no such thing as a SnakeCoin yet, because not one coin has been created and issued yet.
问题又来了,从哪里得到‘蛇币’呢?目前还没发行出‘蛇币’。

To create new coins, people have to mine new blocks of SnakeCoin. When they successfully mine new blocks, a new SnakeCoin is created and rewarded to the person who mined the block. The coin then gets circulated once the miner sends the SnakeCoin to another person.
为了发行‘蛇币’,需要在‘蛇链’上挖出新的区块,俗称‘挖矿’。当新的区块被挖出,新的‘蛇币’也就产生了,并奖赏给挖出的人,俗称‘矿工’。矿工把‘蛇币’发送给别人,‘蛇币’就流通起来了。

We don’t want it to be too easy to mine new SnakeCoin blocks, because that will create too many SnakeCoins and they will have little value.
我们不能轻易产生区块了,这会导致‘蛇币’通货膨胀而贬值。

Conversely, we don’t want it to be too hard to mine new blocks, because there wouldn’t be enough coins for everyone to spend, and they would be too expensive for our liking.
当然,我们也不能使得挖矿太难,否则没有足够的货币流通了,通货紧缩。

To control the difficulty of mining new SnakeCoins, we’ll implement a Proof-of-Work (PoW) algorithm. A Proof-of-Work algorithm is essentially an algorithm that generates an item that is difficult to create but easy to verify.
为了控制‘蛇币’的挖矿难度,需要使用‘工作量证明’算法。‘工作量证明’算法其实就是一个很难得到计算结果而很容易验证计算结果的算法(比如数独游戏)。

The item is called the proof and, as it sounds, it is proof that a computer performed a certain amount of work.
算法名称叫‘证明’,也就是证明计算机做出了一定量的计算。

In SnakeCoin, we’ll create a somewhat simple Proof-of-Work algorithm. To create a new block, a miner’s computer will have to increment a number. When that number is divisible by 9 (the number of letters in “SnakeCoin”) and the proof number of the last block, a new SnakeCoin block will be mined and the miner will be given a brand new SnakeCoin.
在‘蛇币’中,我们采用一个简单的‘工作量证明’算法。矿工计算机只需要把一个数字(证明数)递增来挖矿。具体就是,这个增长的数字一旦能同时被9整除和上个区块的证明数同时整除,就挖到了一个‘蛇币’,矿工也就得到这个‘蛇币’作为奖励。

# ...blockchain
# ...Block class definition

miner_address = "q3nf394hjg-random-miner-address-34nf3i4nflkn3oi"

def proof_of_work(last_proof):
  # Create a variable that we will use to find
  # our next proof of work
  incrementor = last_proof + 1
  # Keep incrementing the incrementor until
  # it's equal to a number divisible by 9
  # and the proof of work of the previous
  # block in the chain
  while not (incrementor % 9 == 0 and incrementor % last_proof == 0):
    incrementor += 1
  # Once that number is found,
  # we can return it as a proof
  # of our work
  return incrementor

@node.route('/mine', methods = ['GET'])
def mine():
  # Get the last proof of work
  last_block = blockchain[len(blockchain) - 1]
  last_proof = last_block.data['proof-of-work']
  # Find the proof of work for
  # the current block being mined
  # Note: The program will hang here until a new
  #       proof of work is found
  proof = proof_of_work(last_proof)
  # Once we find a valid proof of work,
  # we know we can mine a block so 
  # we reward the miner by adding a transaction
  this_nodes_transactions.append(
    { "from": "network", "to": miner_address, "amount": 1 }
  )
  # Now we can gather the data needed
  # to create the new block
  new_block_data = {
    "proof-of-work": proof,
    "transactions": list(this_nodes_transactions)
  }
  new_block_index = last_block.index + 1
  new_block_timestamp = this_timestamp = date.datetime.now()
  last_block_hash = last_block.hash
  # Empty transaction list
  this_nodes_transactions[:] = []
  # Now create the
  # new block!
  mined_block = Block(
    new_block_index,
    new_block_timestamp,
    new_block_data,
    last_block_hash
  )
  blockchain.append(mined_block)
  # Let the client know we mined a block
  return json.dumps({
      "index": new_block_index,
      "timestamp": str(new_block_timestamp),
      "data": new_block_data,
      "hash": last_block_hash
  }) + "\n"

Now, we can control the number of blocks mined in a certain time period, and we can issue new coins for people in the network to send to each other.
好了,我们已经能够控制挖矿的速度,也能发行新货币用于流通。

But like we said, we’re only doing this on one computer. If blockchains are decentralized, how do we make sure that the same chain is on every node?
但是这还是一台电脑在计算。区块链应该是分布式的,怎么同步每个节点中的账本呢?

To do this, we make each node broadcast its version of the chain to the others and allow them to receive the chains of other nodes. After that, each node has to verify the other nodes’ chains so that the every node in the network can come to a consensus of what the resulting blockchain will look like. This is called a consensus algorithm.
为了实现同步,可以让每个节点广播自己的账本,并接收其他节点的账本。每个节点检查其他节点的账本,全网最后得到共识-哪条链是最终版。这个过程被称为‘共识算法’。

Our consensus algorithm will be rather simple: if a node’s chain is different from another’s (i.e. there is a conflict), then the longest chain in the network stays and all shorter chains will be deleted. If there is no conflict between the chains in our network, then we carry on.
我们的共识算法很简单,当一个节点的链(账本)和别人不同时(也就是产生冲突时),只保留最长的链,其他短链删除。如果全网没有节点冲突,就继续保持。

@node.route('/blocks', methods=['GET'])
def get_blocks():
  chain_to_send = blockchain
  # Convert our blocks into dictionaries
  # so we can send them as json objects later
  for block in chain_to_send:
    block_index = str(block.index)
    block_timestamp = str(block.timestamp)
    block_data = str(block.data)
    block_hash = block.hash
    block = {
      "index": block_index,
      "timestamp": block_timestamp,
      "data": block_data,
      "hash": block_hash
    }
  # Send our chain to whomever requested it
  chain_to_send = json.dumps(chain_to_send)
  return chain_to_send

def find_new_chains():
  # Get the blockchains of every
  # other node
  other_chains = []
  for node_url in peer_nodes:
    # Get their chains using a GET request
    block = requests.get(node_url + "/blocks").content
    # Convert the JSON object to a Python dictionary
    block = json.loads(block)
    # Add it to our list
    other_chains.append(block)
  return other_chains

def consensus():
  # Get the blocks from other nodes
  other_chains = find_new_chains()
  # If our chain isn't longest,
  # then we store the longest chain
  longest_chain = blockchain
  for chain in other_chains:
    if len(longest_chain) < len(chain):
      longest_chain = chain
  # If the longest chain wasn't ours,
  # then we set our chain to the longest
  blockchain = longest_chain

We’re just about done now. After running the full SnakeCoin server code, run the following commands in your terminal. Assuming you have cURL installed.
快完工了!运行‘蛇币’服务端代码后,需要在你的终端运行几行命令。假设你已经安装了cURL。

1.Create a transaction.
创造交易

curl "localhost:5000/txion" \
     -H "Content-Type: application/json" \
     -d '{"from": "akjflw", "to":"fjlakdj", "amount": 3}'
  1. Mine a new block.
    挖矿
curl localhost:5000/mine
  1. Check out the results. From the client window, we see this.
    查看结果,在客户端界面可以看到下图。


    来吧,创造一条区块链(下)

With a little bit of pretty printing we see that after mining we get some cool information on our new block.
从显示中我们得到一些有意思的挖矿信息。

{
  "index": 2,
  "data": {
    "transactions": [
      {
        "to": "fjlakdj",
        "amount": 3,
        "from": "akjflw"
      },
      {
        "to": "q3nf394hjg-random-miner-address-34nf3i4nflkn3oi",
        "amount": 1,
        "from": "network"
      }
    ],
    "proof-of-work": 36
  },
  "hash": "151edd3ef6af2e7eb8272245cb8ea91b4ecfc3e60af22d8518ef0bba8b4a6b18",
  "timestamp": "2017-07-23 11:23:10.140996"
}

And that’s it! We’ve made a fairly sized blockchain at this point. Now, SnakeCoin can be launched on multiple machines to create a network, and real SnakeCoins can be mined.
看呐!我们已经创造了一个相当规模的区块链。‘蛇币’可以发行在一个多设备构造的网络中,而且可以挖‘蛇币’了。

Please feel free to tinker with the SnakeCoin server code as much as you’d like, and ask as many questions as you need! In the next part, we’ll discuss creating a SnakeCoin wallet, so users can send, receive, and store their SnakeCoins.
关于‘蛇币’代码可以随便问我。下一部分,我们将讨论如何创建一个‘蛇币’钱包,大家可以用这个钱包收币、发币、存币。

相关文章

  • 来吧,创造一条区块链(下)

    上一篇介绍了一条迷你区块链‘蛇链’的诞生,用了不到50行代码。为了把‘蛇链’变成正真可以使用的区块链,本篇给‘蛇链...

  • 来吧,创造一条区块链(上)

    看到一篇外文文章《Let’s Build the Tiniest Blockchain》(搭梯子出去看哦),很适合...

  • 以太坊测试链--rinkeby获取以太币

    主网络与测试网络前面说到一条区块链由一个创世区块开始,也就是说,一个创世区块可以创造和代表一条区块链。如果我们给钱...

  • 区块链编程完全指南

    区块链是未来的方向。区块链创造未来一点都不牵强。区块链技术从最初的公共的,无需授权的技术,逐渐演变为具体用例下的实...

  • 主要是以太坊的一些东西

    区块链开发 0 忘了什么时候整理的了,可能有点乱,先放上来吧 以太坊和区块链的关系 区块链技术:从区块链历史上来说...

  • CyberVein是什么

    CyberVein是一条面对全球开放的公有链,CyberVein的创造是基于我们团队对区块链以及数据经济形态在多...

  • 5月6日线上分享会第一部分:Ulord介绍

    1、Ulord项目介绍 Ulord是一条点对点的价值传递公链,遵从自由开放、尊重创造的思想,通过搭建区块链底层...

  • 区块链初识

    区块链(blockchain)是眼下的大热门,新闻媒体大量报道,宣称它将创造未来。 区块链的本质 区块链是什么?一...

  • 向敏:期待区块链技术让用户行为变得值钱

    首先笔者并不信仰区块链,更不信任区块链技术本身能创造价值,如果无法高效与产业融合,区块链技术就是个屁。 但区块链作...

  • 2020年区块链落地元年

    首先,价值创造。区块链价值创造就是创造信任。区块链作为一种分布式账本技术,将传统的对中心化信用机构的信任转变为对区...

网友评论

    本文标题:来吧,创造一条区块链(下)

    本文链接:https://www.haomeiwen.com/subject/gsmumftx.html