通过Python函数来详细解析区块链
我想对于那里的很多人来说,区块链就是这种现象,很难不让你头脑发热。我开始观看视频和阅读文章,但对我个人而言,直到我编写自己的简单区块链,我才真正理解它是什么以及它的潜在应用价值。
我对区块链的看法是它是一个公开的加密数据库。如果你是亚马逊并且你想使用该技术来跟踪库存水平,那么使用区块链是否有意义?可能没有,因为你的客户不想花费资源来验证你的区块链,因为他们只顾看着网站说Only 1 left!
。
我会让你考虑未来的应用。所以不用多说,让我们看看我们的7个函数!
def hash_function(k): """Hashes our transaction.""" if type(k) is not str: k = json.dumps(k, sort_keys=True) return hashlib.sha256(k).hexdigest()
区块链的核心是hash函数。如果没有加密,区块链将易于操作,并且交易将能够以欺诈方式写入。
def update_state(transaction, state): state = state.copy() for key in transaction: if key in state.keys(): state[key] += transaction[key] else: state[key] = transaction[key] return state
state
是来记录谁拥有代币交易情况。例如,我有10个代币,我给1到Medium,然后状态state
将是下面字典的值。
{‘transaction’: {‘Tom’: 9, ‘Medium’: 1}}
值得注意的是,透支不可能存在。如果现有只有10个代币,那么我就不能给某人11个代币。以下函数验证我们尝试进行的交易确实有效。此外,交易总体上必须平衡。我不能给5个代币并让收件人收到4个代币币,因为这样可以销毁和生成代币。
def valid_transaction(transaction, state): """A valid transaction must sum to 0.""" if sum(transaction.values()) is not 0: return False for key in transaction.keys(): if key in state.keys(): account_balance = state[key] else: account_balance = 0 if account_balance + transaction[key] < 0: return False return True
现在,我们可以制作我们的区块。读取前一个区块的信息,并将其用于将其链接到新区块。这也是区块链理念的核心。可以尝试以欺骗性的方式将看似有效的交易插入到区块链中,但是解密所有先前的块在计算上(几乎)是不可能的,这就保留了区块链的完整性。
def make_block(transactions, chain): """Make a block to go into the chain.""" parent_hash = chain[-1]['hash'] block_number = chain[-1]['contents']['block_number'] + 1 block_contents = { 'block_number': block_number, 'parent_hash': parent_hash, 'transaction_count': block_number + 1, 'transaction': transactions } return {'hash': hash_function(block_contents), 'contents': block_contents}
下面是一个小辅助函数来检查前一个块的哈希值:
def check_block_hash(block): expected_hash = hash_function(block['contents']) if block['hash'] is not expected_hash: raise return
一旦我们将所有东西组合在一起,就有时间来创建我们的区块。我们现在将更新区块链。
def check_block_validity(block, parent, state): parent_number = parent['contents']['block_number'] parent_hash = parent['hash'] block_number = block['contents']['block_number'] for transaction in block['contents']['transaction']: if valid_transaction(transaction, state): state = update_state(transaction, state) else: raise check_block_hash(block) # Check hash integrity if block_number is not parent_number + 1: raise if block['contents']['parent_hash'] is not parent_hash: raise return state
在我们完成之前,必须验证区块链:
def check_chain(chain): """Check the chain is valid.""" if type(chain) is str: try: chain = json.loads(chain) assert (type(chain) == list) except ValueError: # String passed in was not valid JSON return False elif type(chain) is not list: return False state = {} for transaction in chain[0]['contents']['transaction']: state = update_state(transaction, state) check_block_hash(chain[0]) parent = chain[0] for block in chain[1:]: state = check_block_validity(block, parent, state) parent = block return state
最后,需要一个交易功能,它将以上所有内容挂起:
def add_transaction_to_chain(transaction, state, chain): if valid_transaction(transaction, state): state = update_state(transaction, state) else: raise Exception('Invalid transaction.') my_block = make_block(state, chain) chain.append(my_block) for transaction in chain: check_chain(transaction) return state, chain
所以,现在我们有了7个函数。我们如何与它互动?好吧,首先我们需要用Genesis Block
启动我们的区块链。这是我们的新代币(或库存等)的开始。出于本文解释说明的目的,我会说我是Tom,将从拥有10个代币开始。
genesis_block = { 'hash': hash_function({ 'block_number': 0, 'parent_hash': None, 'transaction_count': 1, 'transaction': [{'Tom': 10}] }), 'contents': { 'block_number': 0, 'parent_hash': None, 'transaction_count': 1, 'transaction': [{'Tom': 10}] }, } block_chain = [genesis_block] chain_state = {'Tom': 10}
现在,看看当我将一些代币交给Medium时会发生什么:
chain_state, block_chain = add_transaction_to_chain(transaction={'Tom': -1, 'Medium': 1}, state=chain_state, chain=block_chain)
state
更新显示谁拥有多少代币:
{'Medium': 1, 'Tom': 9}
区块链看起来像这样:
[{'contents': {'block_number': 0, 'parent_hash': None, 'transaction': [{'Tom': 10}], 'transaction_count': 1}, 'hash': '064d0b480b3b92761f31831d30ae9f01954efaa62371b4b44f11465ec22abe93'}, {'contents': {'block_number': 1, 'parent_hash': '064d0b480b3b92761f31831d30ae9f01954efaa62371b4b44f11465ec22abe93', 'transaction': {'Medium': 1, 'Tom': 9}, 'transaction_count': 2}, 'hash': 'b4ae25f0cc0ee0b0caa66b9a3473e9a108652d53b1dc22a40962fef5c8c0f08c'}]
我们创建了第一个新交易并将其插入到堆栈顶部。现在,我希望我已经激起了你的好奇心,并且有兴趣将代码复制下来并使用它。
以上就是通过Python函数来详细解析区块链的详细内容,更多请关注其它相关文章!
上一篇: 实现强大的翻页跳转功能
下一篇: mysql排重查询_MySQL