Registrierung Kalender Mitgliederliste Teammitglieder Suche Häufig gestellte Fragen Zur Startseite

Informatiker Board » Suche » Suchergebnis » Hallo Gast [Anmelden|Registrieren]
Zeige Beiträge 1 bis 1 von 1 Treffern
Autor Beitrag
Thema: Blockchain Programmierung Python (3.6)
Nassimal

Antworten: 1
Hits: 4.045
Blockchain Programmierung Python (3.6) 17.03.2019 12:49 Forum: Praktische Informatik


Meine Frage:
Hallo, ich versuche eine Blockchain zu Programmieren. Der Code unten scheint nicht richtig zu funktionieren. Er fängt mit Block 1 an (obwohl mit dem Genesis Block begonnen werden muss) und nach 10 Blöcken fängt es von vorne an. Hat jemand ekne Lösung?

import datetime
import hashlib

class Block:
blockNo = 0
data = None
next = None
hash = None
nonce = 0
previous_hash = 0x0
timestamp = datetime.datetime.now()

def __init__(self, data):
self.data = data

def hash(self):
h = hashlib.sha256()
h.update(
str(self.nonce).encode('utf-8') +
str(self.data).encode('utf-8') +
str(self.previous_hash).encode('utf-8') +
str(self.timestamp).encode('utf-8') +
str(self.blockNo).encode('utf-8')
)
return h.hexdigest()

def __str__(self):
return "Block Hash: " + str(self.hash()) + "\nBlockNo: " + str(self.blockNo) + "\nBlock Data: " + str(self.data) + "\nHashes: " + str(self.nonce) + "\n--------------"

class Blockchain:

diff = 20
maxNonce = 2**32
target = 2 ** (256-diff)

block = Block("Genesis")
dummy = head = block

def add(self, block):

block.previous_hash = self.block.hash()
block.blockNo = self.block.blockNo + 1

self.block.next = block
self.block = self.block.next

def mine(self, block):
for n in range(self.maxNonce):
if int(block.hash(), 16) <= self.target:
self.add(block)
print(block)
break
else:
block.nonce += 1

blockchain = Blockchain()

for n in range(10):
blockchain.mine(Block("Block " + str(n+1)))

while blockchain.head != None:
print(blockchain.head)
blockchain.head = blockchain.head.next


Meine Ideen:
Leider habe ich keine Ideen. Vielleicht liegt es an der Schleife.
Zeige Beiträge 1 bis 1 von 1 Treffern