def receive_block(self, conn): self.changed = True print("CHECKING BLOCK") size = conn.recv(100).decode("utf-8") size = int(size) conn.sendall("OK".encode("utf-8")) addr = SocketOp.recv(size, conn) addr = jsonpickle.decode(addr) conn.sendall("OK".encode("utf-8")) size = conn.recv(100).decode("utf-8") size = int(size) conn.sendall("OK".encode("utf-8")) block = SocketOp.recv(size, conn) # print("BLOCK {}".format(block)) block = jsonpickle.decode(block) b = self.check_block(block) peer_opinion = False if b is True: peer_opinion = self.send_block(block, addr) self.changed = deepcopy(peer_opinion) print("Change Blockchain? {}".format(peer_opinion)) if peer_opinion is True: self.miner.save_block(block) else: self.get_parameter("b") conn.sendall(bytes([int(b)])) print("---BLOCKCHAIN LENGTH: {}".format(len(self.miner.blockchain))) print("---LEDGER LENGTH: {}".format(len(self.miner.ledger)))
def send_ledger(self, conn): data = deepcopy(self.miner.ledger) data = jsonpickle.encode(self.miner.ledger).encode("utf-8") length = len(data) print("WILL SEND {}".format(length)) SocketOp.send(str(length).encode("utf-8"), conn) response = conn.recv(2) SocketOp.send(data, conn) print("SENT LEDGER")
def send_blockchain(self, conn): data = deepcopy(self.miner.blockchain) data = jsonpickle.encode(data).encode("utf-8") length = len(data) print("WILL SEND {}".format(length)) SocketOp.send(str(length).encode("utf-8"), conn) response = conn.recv(2) SocketOp.send(data, conn) print("SENT BLOCKCHAIN")
def send_all(self, conn, addr): data = json.dumps(self.peers).encode("utf-8") try: length = len(data) conn.sendall( length.to_bytes((length.bit_length() + 7) // 8, byteorder="big")) response = conn.recv(2) SocketOp.send(data, conn) except: print("Connection error with {}".format(addr[0]))
def send_block(self, block, addr): addr.append(self.peer.address) print("BLOCK HAS BEEN TO {}".format(addr)) response = 1 for p in self.peer.peers: if p not in addr: print("PEER {}".format(p)) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: s.connect((p, self.peer.port)) s.sendall("B".encode("utf-8")) response = s.recv(2) data = jsonpickle.encode(addr).encode("utf-8") length = len(data) SocketOp.send(str(length).encode("utf-8"), s) response = s.recv(2) SocketOp.send(data, s) response = s.recv(2) data = jsonpickle.encode(block).encode("utf-8") length = len(data) SocketOp.send(str(length).encode("utf-8"), s) response = s.recv(2) SocketOp.send(data, s) response = s.recv(1) response = int.from_bytes(response, byteorder="big") s.close() except Exception as e: print("CONNECTION WITH {} FAILED, {}".format(p, e)) break return bool(response)
def start(self): print("TIMESTAMP SERVER STARTED") while True: try: self.ss.listen(50) conn, addr = self.ss.accept() print("CONNECTION FROM {}".format(addr[0])) data = self.rsa.sign(str(time())) data = jsonpickle.encode(data).encode("utf-8") length = len(data) SocketOp.send(str(length).encode("utf-8"), conn) response = conn.recv(2) SocketOp.send(data, conn) conn.close() except Exception as e: print("COULD NOT SEND TIMESTAMP") print(e)
def get_timestamp(self): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(self.timestamp_server) size = s.recv(100).decode("utf-8") size = int(size) s.sendall("OK".encode("utf-8")) ts = SocketOp.recv(size, s) ts = jsonpickle.decode(ts) ts = [str(ts[0]), str(ts[1])] return ts
def get_transactions(self): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(self.tx_address) size = s.recv(100).decode("utf-8") size = int(size) s.sendall("OK".encode("utf-8")) tx = SocketOp.recv(size, s) tx = jsonpickle.decode(tx) s.close() return tx
def tx_server(self): print("WEB SERVER STARTED") while True: try: self.get_peers() self.get_ledger() self.ss.listen(50) conn, addr = self.ss.accept() print("CONNECTION FROM {}".format(addr[0])) data = self.tx_queue[:self.max_tx] data = jsonpickle.encode(data).encode("utf-8") length = len(data) SocketOp.send(str(length).encode("utf-8"), conn) response = conn.recv(2) SocketOp.send(data, conn) conn.close() self.tx_queue = self.tx_queue[self.max_tx:] except Exception as e: print("COULD NOT SEND TRANSACTIONS") print(e)
def send_peers(self, conn, addr): if addr[0] not in self.peers: self.peers.append(addr[0]) no_peers = len(self.peers) notify = 0 if no_peers > self.max_peers: self.n += 1 self.max_peers *= 2 notify = 1 if no_peers == 1: data = "ONLY PEER" print("ONLY PEER") else: data = [] index = self.peers.index(addr[0]) l = len(self.peers) for i in range(self.n): peer_index = (index + pow(2, i)) % l while peer_index > no_peers: peer_index = (peer_index + 1) % l if addr[0] != self.peers[peer_index]: data.append(self.peers[peer_index]) data = json.dumps(data).encode("utf-8") try: length = len(data) conn.sendall( length.to_bytes((length.bit_length() + 7) // 8, byteorder="big")) response = conn.recv(2) SocketOp.send(data, conn) except: print("Connection error with {}".format(addr[0])) if notify == 1: self.notify()
def get_parameter(self, option): parameter = [] i = 0 while i < len(self.peer.peers): temp = [] try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((self.peer.peers[i], self.peer.port)) s.sendall(option.encode("utf-8")) length = s.recv(2).decode("utf-8") while length == "NO": print("{} DOES NOT HAVE AN ANSWER FOR {} YET".format( self.peer.peers[i], option)) sleep(6) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((self.peer.peers[i], self.peer.port)) s.sendall(option.encode("utf-8")) length = s.recv(2).decode("utf-8") length = s.recv(100).decode("utf-8") length = int(length) s.sendall("OK".encode("utf-8")) temp = SocketOp.recv(length, s) print("RECEIVED: {}".format(len(temp))) temp = jsonpickle.decode(temp) print("DECODED!") i += 1 s.close() except Exception as e: print("*Could not get {} from {}; {}".format( option, self.peer.peers[i], e)) i += 1 if len(temp) > len(parameter) and type(temp) is list: parameter = deepcopy(temp) if option == "b": if len(parameter) > len(self.miner.blockchain): self.miner.blockchain = deepcopy(parameter) self.miner.save_blockchain() print("---BLOCKCHAIN LENGTH: {}".format(len( self.miner.blockchain))) else: if len(parameter) > len(self.miner.ledger): self.miner.ledger = parameter self.miner.save_ledger() print("---LEDGER LENGTH: {}".format(len(self.miner.ledger)))
def get_ledger(self): self.lock.acquire() blockchain = [] i = 0 while i < len(self.peers): temp = [] try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((self.peers[i], 65434)) s.sendall("b".encode("utf-8")) length = s.recv(2).decode("utf-8") while length == "NO": print("{} DOES NOT HAVE AN ANSWER FOR {} YET".format( self.peers[i], "b")) sleep(6) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((self.peers[i], 65434)) s.sendall("b".encode("utf-8")) length = s.recv(2).decode("utf-8") length = s.recv(100).decode("utf-8") length = int(length) s.sendall("OK".encode("utf-8")) temp = SocketOp.recv(length, s) print("RECEIVED: {}".format(len(temp))) temp = jsonpickle.decode(temp) print("DECODED!") i += 1 s.close() except Exception as e: print("*Could not get ledger from {}; {}".format( self.peers[i], e)) i += 1 if len(temp) > len(blockchain) and type(temp) is list: blockchain = temp[:] ledger = [] for block in blockchain: ledger += block.transactions if len(ledger) > len(self.ledger): self.ledger = ledger print("---LEDGER LENGTH: {}".format(len(self.ledger))) self.lock.release()
def get_peers(self): peers = [] try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(self.boot_peer) self.address = s.getsockname()[0] s.send("P".encode("utf-8")) length = s.recv(100) length = int.from_bytes(length, byteorder="big") s.send("OK".encode("utf-8")) peers = SocketOp.recv(length, s) peers = json.loads(peers) print("PEERS: {}".format(peers)) s.close() except Exception as e: print("BOOT PEER CONNECTION ERROR") print(e) if peers == "ONLY PEER": peers = [] self.peers = peers
def inactive(self, addr): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(self.peer.boot_peer) s.sendall("I".encode("utf-8")) SocketOp.send(addr.encode("utf-8"), s) s.close()