當前位置:首頁 » 區塊鏈知識 » HEYchain區塊鏈

HEYchain區塊鏈

發布時間: 2021-12-06 03:20:35

1. python能搞區塊鏈

當然可以
1 import hashlib as hasher
2 import datetime as date
3
4 # Define what a Snakecoin block is
5 class Block:
6 def __init__(self, index, timestamp, data, previous_hash):
7 self.index = index
8 self.timestamp = timestamp
9 self.data = data
10 self.previous_hash = previous_hash
11 self.hash = self.hash_block()
12
13 def hash_block(self):
14 sha = hasher.sha256()
15 sha.update(str(self.index) + str(self.timestamp) + str(self.data) + str(self.previous_hash))
16 return sha.hexdigest()
17
18 # Generate genesis block
19 def create_genesis_block():
20 # Manually construct a block with
21 # index zero and arbitrary previous hash
22 return Block(0, date.datetime.now(), "Genesis Block", "0")
23
24 # Generate all later blocks in the blockchain
25 def next_block(last_block):
26 this_index = last_block.index + 1
27 this_timestamp = date.datetime.now()
28 this_data = "Hey! I'm block " + str(this_index)
29 this_hash = last_block.hash
30 return Block(this_index, this_timestamp, this_data, this_hash)
31
32 # Create the blockchain and add the genesis block
33 blockchain = [create_genesis_block()]
34 previous_block = blockchain[0]
35
36 # How many blocks should we add to the chain
37 # after the genesis block
38 num_of_blocks_to_add = 20
39
40 # Add blocks to the chain
41 for i in range(0, num_of_blocks_to_add):
42 block_to_add = next_block(previous_block)
43 blockchain.append(block_to_add)
44 previous_block = block_to_add
45 # Tell everyone about it!
46 print "Block #{} has been added to the blockchain!".format(block_to_add.index)
47 print "Hash: {}\n".format(block_to_add.hash)

熱點內容
幣圈如何去找埋伏幣 發布:2025-08-25 15:34:41 瀏覽:37
卡住的比特幣 發布:2025-08-25 15:26:07 瀏覽:984
幣圈資金盤簡介 發布:2025-08-25 15:16:42 瀏覽:262
區塊鏈政策匯總 發布:2025-08-25 15:16:42 瀏覽:188
幣圈cmc是啥意思 發布:2025-08-25 15:16:39 瀏覽:14
比特幣被騙警察怎麼追回 發布:2025-08-25 15:02:43 瀏覽:12
幣圈高位震盪將迎來洗盤 發布:2025-08-25 15:00:47 瀏覽:159
甘肅幣圈大佬 發布:2025-08-25 14:54:18 瀏覽:273
幣圈專家解說 發布:2025-08-25 14:53:06 瀏覽:63
門羅幣能否取代比特幣 發布:2025-08-25 14:34:43 瀏覽:477