import hashlib
import datetime
class TTBlockCoin:
def __init__(self, index, # 索引 timeStamp, # 时间戳 data, # 交易数据 lastHash):
self.index = index
self.timeStamp = timeStamp
self.data = data
self.lastHash = lastHash
self.selfHash = self.hash_TTBlockCoin()
def hash_TTBlockCoin(self):
sha = hashlib.md5()
datastr = str(self.index) + str(self.timeStamp) + str(self.data) + str(self.lastHash)
sha.update(datastr.encode("utf-8"))
return sha.hexdigest()
def create_first_TTBlock():
return TTBlockCoin(0, datetime.datetime.now(), "TT COIN", "tt001")
def create_money_TTBlock(last_block):
this_index = last_block.index + 1
this_timestamp = datetime.datetime.now()
this_data = "taotao coin" + str(this_index)
last_hash = last_block.selfHash
return TTBlockCoin(this_index,
this_timestamp,
this_data,
last_hash)
TTBlockCoins = [create_first_TTBlock()]
nums = 100
head_block = TTBlockCoins[0]
print("创世区块TT币来啦")
print(head_block.index, head_block.data, head_block.timeStamp,
head_block.lastHash, head_block.selfHash)
for i in range(nums):
ttBlock_another = create_money_TTBlock(head_block)
TTBlockCoins.append(ttBlock_another)
head_block = ttBlock_another
print(ttBlock_another.index, ttBlock_another.data,
ttBlock_another.timeStamp, ttBlock_another.lastHash,
ttBlock_another.selfHash)