Hi I am a beginner in python and I am working on the blockchain I want to store any hash that creates by any transaction. I want to transfer that hash to a local database. Can anyone tell me how can I do that? Here is my code. can anyone give me hint how to do it i will be veryhelpful
class Block:
def __init__( self, previous_block_hash, transaction_list ):
self.previous_block_hash = previous_block_hash
self.transaction_list = transaction_list
self.block_data = f"{' - '.join(transaction_list)} - {previous_block_hash}"
self.block_hash = hashlib.sha256(self.block_data.encode()).hexdigest()
class Blockchain:
def __init__( self ):
self.chain = [ ]
self.generate_genesis_block()
def generate_genesis_block( self ):
self.chain.append(Block("0", [ 'Genesis Block' ]))
def create_block_from_transaction( self, transaction_list ):
previous_block_hash = self.last_block.block_hash
self.chain.append(Block(previous_block_hash, transaction_list))
def display_chain( self ):
for i in range(len(self.chain)):
print(f"Hash {i + 1}: {self.chain [ i ].block_hash}\n")
@property
def last_block( self ):
return self.chain [ -1 ]
**t1 = Time_sensitive_df
t2 = "abcdefghijklmnopqrstuvwxyz"
t3 = normal_df
myblockchain = Blockchain()
myblockchain.create_block_from_transaction(t1)
myblockchain.create_block_from_transaction(t2)
myblockchain.create_block_from_transaction(t3)
myblockchain.display_chain()**
What I have tried:
i try to do it with socket programming but get many errors.
but i am getting an error On client side . on line sock.sendall(bytes(data,"utf-8")) TypeError: encoding without a string argument Can any one please tell what ia m doing wrong.?
<pre>import socket
import pandas as pd
import pickle
import hashlib
df = pd.read_csv(r"C:\Users\DELL\OneDrive\Desktop\mythesisdataset.csv",
names=[ 'SBP', 'DBP', 'HEARTRATE', "Temperature" ])
normal_df = (df [ (df.SBP > 120) & (df.DBP > 90) & (df.HEARTRATE < 100) & (df [ 'Temperature' ] < 100) ])
class Block:
def __init__( self, previous_block_hash, transaction_list ):
self.previous_block_hash = previous_block_hash
self.transaction_list = transaction_list
self.block_data = f"{' - '.join(transaction_list)} - {previous_block_hash}"
self.block_hash = hashlib.sha256(self.block_data.encode()).hexdigest()
class Blockchain:
def __init__( self ):
self.chain = [ ]
self.generate_genesis_block()
def generate_genesis_block( self ):
self.chain.append(Block("0", [ 'Genesis Block' ]))
def create_block_from_transaction( self, transaction_list ):
previous_block_hash = self.last_block.block_hash
self.chain.append(Block(previous_block_hash, transaction_list))
def display_chain( self ):
for i in range(len(self.chain)):
print(f"Hash {i + 1}: {self.chain [ i ].block_hash}\n")
@property
def last_block( self ):
return self.chain [ -1 ]
t1 = "abcdergfhiklm"
t2 = "abcdefghijklmnopqrstuvwxyz"
t3 = normal_df
myblockchain = Blockchain()
myblockchain.create_block_from_transaction(t1)
myblockchain.create_block_from_transaction(t2)
myblockchain.create_block_from_transaction(t3)
myblockchain.display_chain()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostname()
port = 12345
s.bind((host, port))
print("host name:", host, " socket name:", socket)
s.listen()
while True:
c, addr = s.accept()
# print('Got connection from', addr, '...')
df2 = pickle.dumps({myblockchain.create_block_from_transaction(t3)})
c.send(df2)
c.close() # Close the connection
This is the client side
import socket
import pickle
import data as data
import sys
# Create a # socket (SOCK_STREAM means a TCP socket)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
# Connect to server and send data
sock.connect((socket.gethostname(), 12345))
sock.sendall(bytes(data,"utf-8"))
# Receive data from the server and shut down
data = (sock.recv(1024))
df3 = pickle.loads(data)
print("Alert send")
print("received data on fog node")
print(df3)
finally:
sock.close()