Esempio n. 1
0
 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")
Esempio n. 2
0
 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")
Esempio n. 3
0
    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]))
Esempio n. 4
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)
Esempio n. 5
0
 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)
Esempio n. 6
0
 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)
Esempio n. 7
0
    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()
Esempio n. 8
0
 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()