一个简单的区块链包。
tinyblock的Python项目详细描述
块
dict({'previous_hash'(str):'...','timestamp'(int):'...','data'(any):'...','nonce'(int):'...','next_hash'(str):'...'})
用法
定义一个简单的区块链:
fromtinyblockimporttinyblock#The initial variable should be a list of blocks. If not set it, the default chain would be an empty list.my_blockchain=tinyblock()
向链中添加块:
#The parameter is the data of this blockmy_blockchain.add('This is a block.')
使用语句查找块:
#Find the blocks with features below. The return elements will content the index in origin chain list.#Completely match: previous_hash, nonce, next_hash.#Partly match: data. (Currently support str, int, float, list, dict, bool and tuple)#Range match: timestamp.(Could be an int, list or tuple)my_blockchain.find(previous_hash='',timestamp='',data='',nonce='',next_hash='')
弹出链的最后一个区块:
my_blockchain.pop()
检查区块链是否正确:
my_blockchain.chainCheck(print_option=True)
修复区块链(带开始和停止索引):
my_blockchain.chainFix(start=0,stop=4)
获取阻止列表:
my_blockchain.getChain()
自定义挖掘新块的规则:
#The default rule is hash start with '0000'.#To change the rule, you can override the mineRule function.defnewRule(hash):ifhash[0:5]=='0'*5:returnTruereturnFalsemy_blockchain.mineRule=newRule