个人中心
登出
中文简体
返回
登录后咨询在线客服
回到顶部

$比特币 (BTC.CC)$ 导入 hashlib 导入日期时间 类区块: def __init__(自我、索引、pr...

$比特币(BTC.CC)$ 导入 hashlib
导入日期时间

类区块:
def __init__(自我、索引、previous_hash、时间戳、数据、nonce=0):
self.index = 索引
self.previous_hash = 以前的哈希
self.timestamp = 时间戳
self.data = 数据
self.nonce = 随机数
self.hash = self.calculate_hash ()

def calculate_hash(自我):
返回 hashlib.sha256 ((str (self.index) + self.previous_hash + str (self.timestamp) + self.data + str (self.nonce)) .encode ()) .hexdigest ()

区块链类:
def __init__(自我):
self.chain = [self.create_genesis_block ()]

def create_genesis_block(自我):
返回区块(0,“0”,datetime.datetime.now(),“创世区块”)

def add_block(self,new_block):
新区块.previous_hash = self.chain [-1] .hash
new_block.hash = 新区块.calculate_hash ()
self.chain.append (new_block)

def proof_of_work(自我、格挡、难度):
而 block.hash [: 难度]!= '0' * 难度:
block.nonce += 1
block.hash = 区块.calculate_hash ()
打印(“开采的区块:”,block.hash)

# 用法示例
难度 = 4 # 哈希输出开始时所需的零数
区块链 = 区块链 ()

# 挖矿创世区块
blockchain.proof_of_work(区块链链 [0],难度)
打印(“创世区块已开采”)

# 添加其他方块
num_blocks = 3
对于 i 范围在 (1, num_blocks + 1) 中:
new_block = 区块 (i, “”, datetime.datetime.now (), “区块” + str (i))
blockchain.proof_of_work(新区块,难度)
区块链.add_block(新区块)
打印(“区块”,i,“已添加到区块链”)
免责声明:社区由Moomoo Technologies Inc.提供,仅用于教育目的。 更多信息
1
原文
举报
浏览 1万
评论
登录发表评论
  • 104686276 : 我给你一个线索,不只是查看 BTC 蜡烛 Genesis,
    这还不够,你需要检查它与什么有关联,例如与一些公司的股票起源,然后你看到流程的起源和来源,然后你就会真正知道它为什么向上或向下,编码会解释你一切,游戏计划变得 “有点” 容易得多

8粉丝
4关注
109来访
关注