Beispiel #1
0
 def gettxbyhash(self, request=None, response=None):
     if request is not None:
         UDPHandler.sendmessage(json.dumps({"hash": request["hash"]}),
                                request["ip_addr"],
                                request["receiver_port"])
     elif response is not None:
         mempool = Mempool()
         mempool.add_transaction(
             Transaction.from_json(json.loads(response)["tx"]))
Beispiel #2
0
 def addtransaction(self, data):
     tx = Transaction.from_json({"block": "Mempool", **data})
     mempool = Mempool()
     mempool.add_transaction(tx)
     UDPHandler.broadcastmessage(
         json.dumps({
             "command": "sendtransaction",
             "tx": tx.to_json()
         }))
     return json.dumps({"status": "ok"})
Beispiel #3
0
    def test_get_transaction(self):
        tx = Transaction()
        input = TransactionInput()
        input.address = ""
        tx.add_input(input)
        tx.generate_hash()

        mp = Mempool()
        mp.flush_mempool()
        mp.add_transaction(tx)
        self.assertEqual(mp.get_transaction().to_json(), tx.to_json())
        mp.close()
Beispiel #4
0
def file_send(n, filehash, fileaddr, file_name):
    stx = StorageTx()
    mem = Mempool()
    # udp = UDPHandler()
    print(fileaddr)
    stx.add_input(filehash, fileaddr)
    part_filename = hashlib.sha256(file_name.encode('utf-8')).hexdigest()
    recv_hosts = ['15.207.11.83', '34.123.137.113']
    for i in range(0, n):
        host = recv_hosts[i]
        port = 5001

        filename = settings.TEMP_STORAGE_PATH + part_filename + str(i)
        filesize = math.ceil(os.path.getsize(filename))
        filetype = "non-temp"

        send = socket.socket()
        print(f"[+] Connecting to {host}:{port}")
        send.connect((host, port))
        print("[+] Connected.")

        info = {
            "filename": filename,
            "filesize": filesize,
            "filetype": filetype,
            "filehash": filehash,
            "fileaddr": fileaddr,
        }
        # send.send(f"{filename}{SEPARATOR}{filesize}{SEPARATOR}{filetype}{SEPARATOR}{filehash}{SEPARATOR}{fileaddr}".encode())
        send.sendall(pickle.dumps(info))
        filehash = ""
        with open(filename, "rb") as f:
            filehash = get_hash(filename, 15)
            print(filehash)
            while True:
                bytes_read = f.read(BUFFER_SIZE)
                if not bytes_read:
                    break
                send.sendall(bytes_read)
        stx.add_output(filehash, host, filename)
        send.close()
        os.remove(filename)

    stx.gen_tx_hash()
    mem.add_transaction(stx)
Beispiel #5
0
    def create_block(self):
        block = Block()
        block.create_coinbase_transaction()
        mempool = Mempool()
        blkc = BlockChain()

        mempool_size = mempool.get_len()
        if mempool_size == 0:
            return
        while mempool_size > 0:
            tx = mempool.get_transaction()
            block.add_transaction(tx)
            mempool_size -= 1
        
        block.calcalute_block_size()
        block.calculate_merkle_root()
        block.compute_hash()
        blkc.add_block(block)
Beispiel #6
0
 def sendtransaction(self, request=None, response=None):
     if request is not None:
         tx = Transaction.from_json(request['body'])
         UDPHandler.broadcastmessage(json.dumps(tx.to_json()))
     if response is not None:
         mm = Mempool()
         tx = Transaction.from_json(response["tx"])
         mm.add_transaction(tx)
         mm.close()
Beispiel #7
0
 def gettxbymindex(self, data):
     mm = Mempool()
     return mm.get_tx_by_mindex(data["body"].index)
Beispiel #8
0
 def getmempoollength(self, data):
     mm = Mempool()
     return mm.get_len()
Beispiel #9
0
    def test_remove_transaction(self):
        tx = Transaction()
        input = TransactionInput()
        input.address = ""
        tx.add_input(input)
        tx.generate_hash()

        mp = Mempool()
        mp.flush_mempool()
        mp.add_transaction(tx)
        self.assertEqual(mp.get_size(), 1)
        mp.remove_transaction(tx.hash)
        self.assertEqual(mp.get_size(), 0)
        mp.close()