尽管一些人认为区块链是一个等待问题的解决方案,但毫无疑问,这种新技术是计算机的奇迹。可是,区块链究竟是什么呢?git
它是比特币或其余加密货币进行交易的数字帐本,帐本按时间顺序记录并对外公开。github
在更通常的术语中,它是一个公共数据库,新数据存储在一个名为块的容器中,并被添加到一个不可变链(后来的区块链)中添加了过去的数据。在比特币和其余加密货币的状况下,这些数据是一组交易记录。固然,数据能够是任何类型的。web
区块链技术已经催生了新的、彻底数字化的货币,如比特币和莱特币,这些货币并非由中央政府发行或管理的。所以为那些认为今天的银行系统是骗局或终将失败的人带来了新的自由。区块链所包含的以太坊技术对分布式计算进行了变革创新,它引入了一些有趣的概念,好比智能合约。算法
在本文中,我将用不到50行的Python2代码来作一个简单的区块链。我称它为SnakeCoin。数据库
首先将定义块将是什么样子。在区块链中,每一个块都存储一个时间戳和一个索引。在SnakeCoin中,须要把二者都存储起来。为了确保整个区块链的完整性,每一个块都有一个自动识别散列。与比特币同样,每一个块的散列将是块索引、时间戳、数据和前块哈希的加密哈希。数据能够是你想要的任何东西。服务器
import hashlib as hasher class Block: def __init__(self, index, timestamp, data, previous_hash): self.index = index self.timestamp = timestamp self.data = data self.previous_hash = previous_hash self.hash = self.hash_block() def hash_block(self): sha = hasher.sha256() sha.update(str(self.index) + str(self.timestamp) + str(self.data) + str(self.previous_hash)) return sha.hexdigest()
1
2
3
4
5
6
7
89
10
11
12114151617
|
import hashlib as hasher
class Block:
def __init__(self, index, timestamp, data, previous_hash):
self.index = index
self.timestamp = timestamp
self.data = data
self.previous_hash = previous_hash
self.hash = self.hash_block()
def hash_block(self):
sha = hasher.sha256()
sha.update(str(self.index) +
str(self.timestamp) +
str(self.data) +
str(self.previous_hash))
return sha.hexdigest()
|
这一步后有块结构,但如今是建立区块链,因此须要向实际的链中添加块。如前所述,每一个块都须要上一个块的信息。可是按照这个说法就有一个问题,区块链的第一个区块是如何到达那里的呢?不得不说,第一个块,或者说是起源块,它是一个特殊的块。在不少状况下,它是手动添加的,或者有独特的逻辑容许添加。app
下面将建立一个函数简单地返回一个起源块以便产生第一个区块。这个块是索引0,它具备任意的数据值和“前一个哈希”参数中的任意值。分布式
import datetime as date def create_genesis_block(): # Manually construct a block with # index zero and arbitrary previous hash return Block(0, date.datetime.now(), "Genesis Block", "0")
1
256
|
import datetime as date
def create_genesis_block():
# Manually construct a block with
# index zero and arbitrary previous hash
return Block(0, date.datetime.now(), "Genesis Block", "0")
|
如今已经建立好了起源块,接下来须要一个函数,以便在区块链中生成后续的块。这个函数将把链中的前一个块做为参数,建立要生成的块的数据,并使用适当的数据返回新块。当新的块哈希信息来自前面的块时,区块链的完整性会随着每一个新块而增长。若是不这样作,外部组织就更容易“改变过去”,用全新的方式取代已有的链条。这一系列的散列能够做为加密的证据,有助于确保一旦将块添加到区块链,它就不能被替换或删除。函数
1
2
3
4
5
6
|
1 def next_block(last_block): 2 this_index = last_block.index + 1 3 this_timestamp = date.datetime.now() 4 this_data = "Hey! I'm block " + str(this_index) 5 this_hash = last_block.hash 6 return Block(this_index, this_timestamp, this_data, this_hash)
|
大部分的工做已经完成,如今能够建立区块链了。在此次的示例中,区块链自己是一个简单的Python列表。列表的第一个元素是起源块。固然,还须要添加后续的块,由于SnakeCoin是最小的区块链,这里只添加20个新的块。能够用for循环来生成新块。区块链
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
1 # Create the blockchain and add the genesis block 2 blockchain = [create_genesis_block()] 3 previous_block = blockchain[0] 4 5 # How many blocks should we add to the chain 6 # after the genesis block 7 num_of_blocks_to_add = 20 8 9 # Add blocks to the chain 10 for i in range(0, num_of_blocks_to_add): 11 block_to_add = next_block(previous_block) 12 blockchain.append(block_to_add) 13 previous_block = block_to_add 14 # Tell everyone about it! 15 print "Block #{} has been added to the blockchain!".format(block_to_add.index) 16 print "Hash: {}\n".format(block_to_add.hash)
|
下面来测试一下目前产生的区块链。
看到了吧,这就是区块链。若是但愿在控制台中查看更多信息,能够编辑完整的源文件并打印每一个块的时间戳或数据。
这就是SnakeCoin要提供的全部东西。为了使SnakeCoin规模达到今天生产区块链的规模,必须添加更多的功能,好比服务器层,以跟踪多台机器上的链变化,以及在给定的时间段内限制添加的块数量的工做算法。
原文出处: Gerald Nash 译文出处:黑色巧克力