0% found this document useful (0 votes)
7 views

Blockchain

The document describes a Python program that implements basic blockchain functionality including transactions, querying transactions, viewing transaction records, and searching blocks. The program allows a user to initiate a transaction by providing sender and receiver IDs, amount, and generates a hash. Transaction details are logged and the user can view records, query by hash, and search blocks of transactions.

Uploaded by

Yhon Pacavita
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Blockchain

The document describes a Python program that implements basic blockchain functionality including transactions, querying transactions, viewing transaction records, and searching blocks. The program allows a user to initiate a transaction by providing sender and receiver IDs, amount, and generates a hash. Transaction details are logged and the user can view records, query by hash, and search blocks of transactions.

Uploaded by

Yhon Pacavita
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

from hashlib import sha256

from os import system


import subprocess
from random import randint
from datetime import datetime
def pown():
for i in range(3):
found = False
count = randint(10, 100)
n = count
nonce = randint(1000, 1000000)
c = nonce
text = input("+(3)+ Escribe el ID de usuario que envia: -> ")
text1 = input("+(3)+ Escribe el ID del usuario que recibe: -> ")
monto = int(input("+(3)+ Escribe el monto a enviar: -> "))
strnulls = 4
nulls = makeStringNulls(strnulls)
try:
mode = 1
has = sha256((text + str(nonce)).encode()).hexdigest()
log = open("log.txt","a+")
log.write("Envia: "+text+ " ,Recibe: "+text1+" ,ID: "+str(n)+" ,Nonce:
"+str(c)+" ,Monto: "+str(monto)+" ,Hash: "+str(has)+" ,Fecha:
"+str(datetime.today().strftime('%Y-%m-%d %H:%M'))+"\n")
log.close()
except:
print("ERROR Tipo de Hash")
return
while not found:
if mode == 1:
hasht = sha256((text + str(nonce)).encode()).hexdigest()
if hasht.startswith(nulls):
found = True
print("Hash Anterior: -> " + str(has))
print("Hash Encontrado: -> " + str(hasht))
print("Envia: -> "+text+" Recibe: -> "+text1+" Monto: ->
"+str(monto)+" ID: -> "+str(count)+" Nonce: -> "+str(nonce)+" \n")
nonce += 1
count += 1
else:
print("ERROR! En el modelo")
found = True
def consulta():
system("./consulta.sh")
def regitros():
system("a=Numero-Registro: && echo $a && cat log.txt | wc -l")
system("cat log.txt")
def bloques():
system("./bloques.sh")
def makeStringNulls(nulls):
strnulls = ""
for i in range(nulls):
strnulls += "0"
return strnulls
def start():
starti = False
while not starti:
try:
startinput = int(input("(1)----> ¿Quieres realizar una transaccin? Si=
1, No= 2 -> "))
if startinput == 1:
pown()
elif startinput == 2:
startinput = int(input("(2)----> ¿Quieres realizar una consulta?
Si= 3, No 4 -> "))
if startinput == 3:
consulta()
elif startinput == 4:
startinput = int(input("(3)----> ¿Queres ver los registros? Si=
5, No= 6 -> "))
if startinput == 5:
regitros()
elif startinput == 6:
startinput = int(input("(4)-----> ¿Quieres consultar un
bloqe? Si= 7, No= 8 -> "))
if startinput == 7:
bloques()
elif startinput == 8:
print("Fin Registro y evaluacion BlockChain")
starti = True
else:
print("ERROR! de entrada")
except:
print("ERROR!")
start()
def proof(hasht, text):
if hasht == sha256(text.encode()).hexdigest():
print("Hash correcto" + hasht)
else:
print("Hash incorrecto " + hasht)
---------------------------------------------------------------------------
#!/bin/sh
echo "+(6)+ Escribe el hash que deseas buscar: ->"
read busc
grep $busc log.txt
-------------------------------------------------------------------------------
#!/bin/bash
a=1 b=2 c=3 d=4 e=5 f=6 g=7 h=8 i=9 j=10 k=11
echo "Escribe el bloque que deseas buscar: ->"
read bloq
if [[ $bloq == $a ]];then
awk 'NR==1, NR==3' log.txt
elif [[ $bloq == $b ]];then
awk 'NR==4, NR==6' log.txt
elif [[ $bloq == $c ]];then
awk 'NR==7, NR==9' log.txt
elif [[ $bloq == $d ]];then
awk 'NR==10, NR==12' log.txt
elif [[ $bloq == $e ]];then
awk 'NR==13, NR==15' log.txt
elif [[ $bloq == $f ]];then
awk 'NR==16, NR==18' log.txt
elif [[ $bloq == $g ]];then
awk 'NR==18, NR==20' log.txt
elif [[ $bloq == $h ]];then
awk 'NR==21, NR==23' log.txt
elif [[ $bloq == $i ]];then
awk 'NR==24, NR==26' log.txt
elif [[ $bloq == $j ]];then
awk 'NR==27, NR==29' log.txt
elif [[ $bloq == $k ]];then
awk 'NR==30, NR==32' log.txt
fi

You might also like