mercoledì 31 gennaio 2018

Esempio mini blockchain

Uno stupido esempio di blockchain senza peer to peer e senza POW...solo per fare vedere come sono collegati i vari record e come si valida un blocco


------------------------------------------------------------------------
import sqlite3, hashlib, time

conn = sqlite3.connect("blockchain.txt")
c =conn.cursor()
c.execute("CREATE TABLE IF NOT EXISTS blocks (id integer PRIMARY KEY AUTOINCREMENT, tempo integer NOT NULL,data text NOT NULL, hash text NOT NULL, precedentehash text NOT NULL)")
c.execute("DELETE FROM blocks")

#INSERISCE IL PRIMO RECORD 
#il precedenteh puo' essere impostato come si desidera
indice = 0
precedentehash = "0"
millis = int(round(time.time() * 1000))
data = "Luca"
hash = hashlib.sha256(str(indice) + precedentehash + str(millis) + data)
hex_dig = hash.hexdigest()

query = "INSERT INTO blocks (tempo,data,hash,precedentehash) VALUES ("+str(millis)+",'"+data+"','" + hex_dig+"','" + precedentehash + "')"

print query

c.execute(query)
conn.commit()

# SECONDO RECORD

c.execute("SELECT * FROM blocks ORDER BY id DESC LIMIT 1")
row =  c.fetchone()
precedentehash = row[3]
indice = row[0]+1
millis = int(round(time.time() * 1000))
data ="Innocenti"
hash = hashlib.sha256(str(indice) + precedentehash + str(millis) + data)
hex_dig = hash.hexdigest()
query = "INSERT INTO blocks (tempo,data,hash,precedentehash) VALUES ("+str(millis)+",'"+data+"','" + hex_dig+"','" + precedentehash + "')"
print query

c.execute(query)
conn.commit()


# TERZO RECORD

c.execute("SELECT * FROM blocks ORDER BY id DESC LIMIT 1")
row =  c.fetchone()
precedentehash = row[3]
indice = row[0]+1

millis = int(round(time.time() * 1000))
data ="Firenze"
hash = hashlib.sha256(str(indice) + precedentehash + str(millis) + data)
hex_dig = hash.hexdigest()
query = "INSERT INTO blocks (tempo,data,hash,precedentehash) VALUES ("+str(millis)+",'"+data+"','" + hex_dig+"','" + precedentehash + "')"
print query

c.execute(query)
conn.commit()

#un blocco e' valido se
# l'indice e' maggiore del blocco precedente
# il precedenteh del blocco in esame e' uguale all'hash del record precedente
# l'hash del blocco e' conforme
print "---------------------------------------------"
#controlla l'integrita' del blocco 3
c.execute("SELECT * FROM blocks WHERE id=3")
c3 = c.fetchone()
c.execute("SELECT * FROM blocks WHERE id=2")
c2 = c.fetchone()

if c2[0] ==(c3[0]-1):
    print "Indice OK"
if c2[3] == c3[4]:
    print "HASH precedente Corretto"
    
precedentehash = c3[4]
millis = c3[1]
data = c3[2]
indice = c3[0]
hash = hashlib.sha256(str(indice) + precedentehash + str(millis) + data)
hex_dig = hash.hexdigest()

if c3[3] == hex_dig:
    print "Blocco corretto"

   
print "---------------------------------------------"

# mostra tutto il DB
c.execute("SELECT * FROM blocks")
rows = c.fetchall()
for r in rows:
    print(r)

conn.close()

Nessun commento:

Posta un commento

Perche' investire su Unix

 Un libro trovato nel libero scambio alla Coop su cio' che poteva essere e non e' stato...interessante la storia su Unix del primo c...