def test_store_get_transaction_by_hash():
    service = RedisService()
    t = Transaction("test_hash", '', "test to", "test from", 50.123, 0)
    success = service.store_object(t)
    assert success

    new_t = service.get_object_by_hash("test_hash", Transaction)

    assert t.to_string() == new_t.to_string()
    def get_contract_by_hash(contract_hash):
        """
        Get a contract by it's hash.

        Arguments:
        contract_hash   -- string of contract's hash to retrieve

        Returns:
        contract    -- contract object
        """
        rs = RedisService()
        return rs.get_object_by_hash(contract_hash, Contract)
def test_remove_from_mempool():
    ts = TransactionService()
    rs = RedisService()

    t = Transaction('mem_test_hash4', '', 'mem_william', 'mem_naween', 14605,
                    1)
    rs.store_object(t)

    ts.remove_from_mem_pool(t)

    new_t = rs.get_object_by_hash('mem_test_hash4', Transaction)

    print(t.to_string())
    print(new_t.to_string())
    assert new_t.is_mempool == 0
    def get_contracts_by_filter(contract_filters, include_mempool):
        """
        Get contracts by a filter.

        Arguments:
        contract_filters    -- list of ContractFilter objects to filter database on
        include_mempool     -- True if results should include those in the mempool, false otherwise

        Returns:
        list    -- list of contracts based on filters passed in
        """
        rs = RedisService()
        r = rs._connect()

        contract_hashes = set()
        temp_hashes = set()
        mempool_list = list()
        mempool_set = set()

        if not include_mempool:
            mempool_list = r.smembers('contract:is_mempool:0')
            for mem_hash in mempool_list:
                mempool_set.add(
                    mem_hash[9:]
                )  # remove 'contract:' from the beginning of the key

        if not contract_filters:
            if include_mempool:
                in_mempool_list = r.smembers('contract:is_mempool:1')
                in_mempool_set = set()
                for mem_hash in in_mempool_list:
                    in_mempool_set.add(
                        mem_hash[9:]
                    )  # remove 'contract:' from the beginning of the key
                contract_hashes = in_mempool_set.union(mempool_set)
            else:
                contract_hashes = mempool_set.copy()
        else:
            for contract_filter in contract_filters:
                print("key: " + contract_filter.key + " | range: " +
                      str(contract_filter.minimum) + "->" +
                      str(contract_filter.maximum))
                hashes = r.zrangebyscore(contract_filter.key,
                                         contract_filter.minimum,
                                         contract_filter.maximum)
                temp_hashes = set(hashes)
                # if first filter
                if len(contract_hashes) == 0:
                    contract_hashes = temp_hashes.copy()
                    # if mempool contracts are to be ignored
                    if not include_mempool:
                        contract_hashes = contract_hashes.intersection(
                            mempool_set)
                    temp_hashes.clear()
                else:
                    # after first filter
                    contract_hashes = contract_hashes.intersection(temp_hashes)

        contracts = list()
        for contract_hash in contract_hashes:
            print(contract_hash)
            contract = rs.get_object_by_hash(contract_hash, Contract, r)
            contracts.append(contract)
        contracts.sort(key=lambda c: (-c.created_timestamp))
        return contracts[:50]