There are two parts to what I'm trying to do here - 1. Create a blockchain structure and 2. Enable P2P communication between 3 nodes. Part 1 can easily be done as there multiple sources online on how to code a blockchain structure. Here's what I've done for Part 1:
# Creating "getdata" request payload
def create_payload_getdata(tx_id):
count = 1
type = 1
hash = bytearray.fromhex(tx_id)
payload = struct.pack("<bb32s", count, type, hash)
return payload
# Print req/res data
def print_response(command, req_data, res_data):
print("")
print("Command: "+command)
print("Request:")
print(binascii.hexlify(req_data))
print("Response")
print(binascii.hexlify(res_data))
if __name__ == '__main__':
# magic value for the main network
magic_value = 0xd9b4bef9
tx_id = "fc57704eff327aecfadb2cf3774edc919ba69aba624b836461ce2be9c00a0c20"
peer_ip_address = '104.199.184.15'
peer_tcp_port = 8333
buffer_size = 1024
# Create Request Objects
ver_payload = create_payload_ver(peer_ip_address)
ver_msg = create_message(magic_value, 'version', ver_payload)
ver_ack_msg = create_message_verack()
get_data_payload = create_payload_getdata(tx_id)
get_data_msg = create_message(magic_value, "getdata", get_data_payload)
# Establish TCP connection
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((peer_ip_address, peer_tcp_port))
# Send message "version"
s.send(ver_msg)
res_data = s.recv(buffer_size)
print_response("version", ver_msg, res_data);
# Send msg "verack"
s.send(ver_ack_msg)
res_data = s.recv(buffer_size)
print_response("verack", ver_ack_msg, res_data)
# Send msg "getdata"
s.send(get_data_msg)
res_data = s.recv(buffer_size)
print_response("getdata", get_data_msg, res_data)
s.close()
However, for Part 2, I'm not sure how I can get all 3 nodes to synchronise their own blockchain when a change in the blockchain occurs (i.e adding a block to the block chain). All 3 nodes can serve as a client and server, but never both at the same time. For example, in the figure below, Node 1 is the Client while Node 2 is the Server. Node 3 will not be taking part in the transaction, but it will update its own blockchain once the transaction between Node 1 and Node 2 is complete.
A simple code snippet on how to synchronise the blockchain between 3 nodes, or even a link directing to a blockchain synchronisation implementing sockets in Python would be helpful.
