File size: 1,873 Bytes
f9870f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# -*- coding: utf-8 -*-
"""Class Blockchain

Automatically generated by Colab.

Original file is located at
    https://colab.research.google.com/drive/1EWMY4elMMGzoscgstOWF8wkyv0-3yShT
"""

import hashlib
import time

class Block:
  def __init__(self, index, previous_hash, timestamp, data, hash):
    self.index = index
    self.previous_hash = previous_hash
    self.timestamp = timestamp
    self.data = data
    self.hash = hash

  def calculate_hash(index, previous_hash, timestamp, data):
    value = str(index) + str(previous_hash) + str(timestamp) + str(data)
    return hashlib.sha256(value.encode('utf-8')).hexdigest()

def create_genesis_block():
  return Block(0, "0", int(time.time()), "Genesis Block", Block.calculate_hash(0, "0", int(time.time()), "Genesis Block"))

class Blockchain:
  def __init__(self):
    self.chain = [create_genesis_block()]

  def get_latest_block(self):
    return self.chain[-1]

  def add_block(self, new_block):
    new_block.previous_hash = self.get_latest_block().hash
    new_block.hash = Block.calculate_hash(new_block.index, new_block.previous_hash, new_block.timestamp, new_block.data)

def create_new_block(previous_block, data):
  index = previous_block.index + 1
  timestamp = int(time.time())
  return Block(index, previous_block.hash, timestamp, data, Block.calculate_hash(index, previous_block.hash, timestamp, data))

blockchain = Blockchain()

new_block = create_new_block(blockchain.get_latest_block(), "First new block after genesis")
blockchain.add_block(new_block)

new_block = create_new_block(blockchain.get_latest_block(), "Second new block after genesis")
blockchain.add_block(new_block)

for block in blockchain.chain:
  print(f"Index: {block.index}")
  print(f"Previous Hash: {block.previous_hash}")
  print(f"Timestamp: {block.timestamp}")
  print(f"Data: {block.data}")
  print(f"Hash: {block.hash}\n")