50行python程式碼實現的小區塊鏈
import hashlib as hasher
import datetime as date
# Define what a Snakecoin block is
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()
# Generate genesis block
def create_genesis_block():
# Manually construct a block with
# index zero and arbitrary previous hash
return Block(0, date.datetime.now(), "Genesis Block", "0")
# Generate all later blocks in the blockchain
def next_block(last_block):
this_index = last_block.index + 1
this_timestamp = date.datetime.now()
this_data = "Hey! I`m block " + str(this_index)
this_hash = last_block.hash
return Block(this_index, this_timestamp, this_data, this_hash)
# Create the blockchain and add the genesis block
blockchain = [create_genesis_block()]
previous_block = blockchain[0]
# How many blocks should we add to the chain
# after the genesis block
num_of_blocks_to_add = 20
# Add blocks to the chain
for i in range(0, num_of_blocks_to_add):
block_to_add = next_block(previous_block)
blockchain.append(block_to_add)
previous_block = block_to_add
# Tell everyone about it!
print "Block #{} has been added to the blockchain!".format(block_to_add.index)
print "Hash: {}
".format(block_to_add.hash)
相關文章
- [譯] 用 Java 程式碼實現區塊鏈Java區塊鏈
- 一個簡單的區塊鏈程式碼實現區塊鏈
- 用Java程式碼實現區塊鏈技術Java區塊鏈
- 300行Kotlin程式碼實現的區塊鏈Kotlin區塊鏈
- 51行程式碼實現簡單的PHP區塊鏈行程PHP區塊鏈
- JavaScript實現區塊鏈JavaScript區塊鏈
- 雲+區塊鏈 實現區塊鏈技術的普惠應用區塊鏈
- 區塊鏈-NFT 的實現原理區塊鏈
- 【區塊鏈技術實現】區塊鏈
- 區塊鏈的幾個小故事.小白學區塊鏈01區塊鏈
- 一文讀懂區塊鏈以及一個區塊鏈的實現區塊鏈
- 一個簡單的區塊鏈貨幣,python實現區塊鏈Python
- (二)區塊鏈的共識演算法:PoS 及其 例子 程式碼 實現區塊鏈演算法
- 200 行程式碼實現一個簡單的區塊鏈應用行程區塊鏈
- 比特幣和區塊鏈(2):比特幣中區塊鏈的實現比特幣區塊鏈
- .Net Core實現區塊鏈初探區塊鏈
- 使用Javascript實現小型區塊鏈JavaScript區塊鏈
- 在 iOS 中實現區塊鏈iOS區塊鏈
- 300行ABAP程式碼實現一個最簡單的區塊鏈原型區塊鏈原型
- 200行golang 實現的區塊鏈Golang區塊鏈
- 區塊鏈的原理與golang實現例子區塊鏈Golang
- 用 Python 構建一個極小的區塊鏈Python區塊鏈
- NodeJS實現簡易區塊鏈NodeJS區塊鏈
- Python實現一條基於POS演算法的區塊鏈Python演算法區塊鏈
- 用不到 50 行的 Python 程式碼構建最小的區塊鏈Python區塊鏈
- V神:區塊鏈上投票流程的實現區塊鏈
- 區塊鏈DAPP的小知識區塊鏈APP
- 區塊鏈教程、區塊鏈指南、區塊鏈中文手冊、區塊鏈原理區塊鏈
- 5行python程式碼講清楚如何在區塊鏈挖礦Python區塊鏈
- 使用MVC模式實現區塊鏈開發MVC模式區塊鏈
- 用java實現一個簡單的區塊鏈Java區塊鏈
- 區塊鏈,中心去,何曾著眼看君王?用Go語言實現區塊鏈技術,透過Golang秒懂區塊鏈區塊鏈Golang
- 老碼農眼中的區塊鏈區塊鏈
- 新加坡的政府區塊鏈實驗可以實現監管理解區塊鏈
- LKL小蘋果:優質區塊鏈專案的價值體現蘋果區塊鏈
- 如何在NEO區塊鏈上實現資訊加密區塊鏈加密
- 區塊鏈安全:實現公鏈雙花攻擊的多種方法區塊鏈
- blockstack/stacks-blockchain:Stacks 2.0 區塊鏈的Rust實現Blockchain區塊鏈Rust