コード例 #1
0
    def test_add_transaction_update(self):
        cache = TransactionCache(self.store)

        tx = Transaction.from_hex(tx_hex_1)
        data = [
            tx.hash(),
            TxData(height=1295924,
                   position=4,
                   fee=None,
                   date_added=1,
                   date_updated=1), None, TxFlags.Unset, None
        ]
        with SynchronousWriter() as writer:
            cache.add([data], completion_callback=writer.get_callback())
            assert writer.succeeded()

        entry = cache.get_entry(tx.hash())
        assert entry is not None
        assert TxFlags.Unset == entry.flags & TxFlags.STATE_MASK

        with SynchronousWriter() as writer:
            cache.add_transaction(tx,
                                  TxFlags.StateCleared,
                                  completion_callback=writer.get_callback())
            assert writer.succeeded()

        tx_hash = tx.hash()
        entry = cache.get_entry(tx_hash)
        assert entry is not None
        assert cache.have_transaction_data_cached(tx_hash)
        assert TxFlags.StateCleared == entry.flags & TxFlags.StateCleared
コード例 #2
0
    def test_add_then_update(self):
        cache = TransactionCache(self.store)

        bytedata_1 = bytes.fromhex(tx_hex_1)
        tx_hash_1 = bitcoinx.double_sha256(bytedata_1)
        metadata_1 = TxData(position=11)
        with SynchronousWriter() as writer:
            cache.add(
                [(tx_hash_1, metadata_1, bytedata_1, TxFlags.StateDispatched)],
                completion_callback=writer.get_callback())
            assert writer.succeeded()

        assert cache.is_cached(tx_hash_1)
        entry = cache.get_entry(tx_hash_1)
        assert TxFlags.HasByteData | TxFlags.HasPosition | TxFlags.StateDispatched == entry.flags
        assert entry.bytedata is not None

        metadata_2 = TxData(fee=10, height=88)
        propagate_flags = TxFlags.HasFee | TxFlags.HasHeight
        with SynchronousWriter() as writer:
            cache.update([(tx_hash_1, metadata_2, None,
                           propagate_flags | TxFlags.HasPosition)],
                         completion_callback=writer.get_callback())
            assert writer.succeeded()

        entry = cache.get_entry(tx_hash_1)
        expected_flags = propagate_flags | TxFlags.StateDispatched | TxFlags.HasByteData
        assert expected_flags == entry.flags, \
            f"{TxFlags.to_repr(expected_flags)} !=  {TxFlags.to_repr(entry.flags)}"
        assert entry.bytedata is not None
コード例 #3
0
    def test_add_missing_transaction(self):
        cache = TransactionCache(self.store)

        tx_bytes_1 = bytes.fromhex(tx_hex_1)
        tx_hash_1 = bitcoinx.double_sha256(tx_bytes_1)

        with SynchronousWriter() as writer:
            cache.add_missing_transaction(
                tx_hash_1, 100, 94, completion_callback=writer.get_callback())
            assert writer.succeeded()

        assert cache.is_cached(tx_hash_1)
        entry = cache.get_entry(tx_hash_1)
        assert TxFlags.HasFee | TxFlags.HasHeight, entry.flags & TxFlags.METADATA_FIELD_MASK
        assert entry.bytedata is None

        tx_bytes_2 = bytes.fromhex(tx_hex_2)
        tx_hash_2 = bitcoinx.double_sha256(tx_bytes_2)

        with SynchronousWriter() as writer:
            cache.add_missing_transaction(
                tx_hash_2, 200, completion_callback=writer.get_callback())
            assert writer.succeeded()

        assert cache.is_cached(tx_hash_2)
        entry = cache.get_entry(tx_hash_2)
        assert TxFlags.HasHeight == entry.flags & TxFlags.METADATA_FIELD_MASK
        assert entry.bytedata is None
コード例 #4
0
    def test_get_entry(self):
        cache = TransactionCache(self.store)

        tx_1 = Transaction.from_hex(tx_hex_1)
        tx_hash_1 = tx_1.hash()
        data = TxData(position=11)
        with SynchronousWriter() as writer:
            cache.add([ (tx_hash_1, data, tx_1, TxFlags.StateSettled, None) ],
                completion_callback=writer.get_callback())
            assert writer.succeeded()

        entry = cache.get_entry(tx_hash_1, TxFlags.StateDispatched)
        assert entry is None

        entry = cache.get_entry(tx_hash_1, TxFlags.StateSettled)
        assert entry is not None
コード例 #5
0
    def test_get_transaction_after_metadata(self):
        # Getting an entry for a settled transaction should update from metadata-only to full.
        bytedata_set = bytes.fromhex(tx_hex_1)
        tx_hash = bitcoinx.double_sha256(bytedata_set)
        metadata_set = TxData(height=1, fee=2, position=None, date_added=1, date_updated=1)
        with SynchronousWriter() as writer:
            self.store.create([ (tx_hash, metadata_set, bytedata_set, TxFlags.StateSettled, None) ],
                completion_callback=writer.get_callback())
            assert writer.succeeded()

        cache = TransactionCache(self.store)
        metadata_get = cache.get_metadata(tx_hash)
        assert metadata_get is not None

        # Initial priming of cache will be only metadata.
        cached_entry_1 = cache._cache[tx_hash]
        assert not cache.have_transaction_data_cached(tx_hash)

        # Entry request will hit the database.
        entry = cache.get_entry(tx_hash)
        assert cache.have_transaction_data_cached(tx_hash)

        cached_entry_2 = cache._cache[tx_hash]
        assert entry.metadata == cached_entry_2.metadata
        assert entry.flags == cached_entry_2.flags
コード例 #6
0
    def test_get_entry_cached_already(self) -> None:
        metadata = TxData(position=11, date_added=1, date_updated=1)
        flags = TxFlags.HasPosition

        def _read(*args,
                  **kwargs) -> Tuple[bytes, Optional[bytes], TxFlags, TxData]:
            nonlocal metadata, flags
            return [(b"tx_hash", None, flags, metadata)]

        def _read_metadata(*args, **kwargs) -> Tuple[bytes, TxFlags, TxData]:
            nonlocal metadata, flags
            return [(b"tx_hash", flags, metadata)]

        mock_store = MockTransactionStore()
        mock_store.read = _read
        mock_store.read_metadata = _read_metadata

        cache = TransactionCache(mock_store, 0)

        # Verify that we do not hit the store for our cached entry.
        our_entry = TransactionCacheEntry(metadata, TxFlags.HasPosition)
        cache._cache[b"tx_hash"] = our_entry

        their_entry = cache.get_entry(b"tx_hash")
        assert our_entry.metadata == their_entry.metadata
        assert our_entry.flags == their_entry.flags
コード例 #7
0
    def test_get_entry(self):
        cache = TransactionCache(self.store)

        bytedata_1 = bytes.fromhex(tx_hex_1)
        tx_hash_1 = bitcoinx.double_sha256(bytedata_1)
        data = TxData(position=11)
        with SynchronousWriter() as writer:
            cache.add([(tx_hash_1, data, bytedata_1, TxFlags.StateSettled)],
                      completion_callback=writer.get_callback())
            assert writer.succeeded()

        entry = cache.get_entry(tx_hash_1, TxFlags.StateDispatched)
        assert entry is None

        entry = cache.get_entry(tx_hash_1, TxFlags.StateSettled)
        assert entry is not None
コード例 #8
0
    def test_add_then_update(self):
        cache = TransactionCache(self.store)

        bytedata_1 = bytes.fromhex(tx_hex_1)
        tx_hash_1 = bitcoinx.double_sha256(bytedata_1)
        metadata_1 = TxData(position=11)
        with SynchronousWriter() as writer:
            cache.add([(tx_hash_1, metadata_1, bytedata_1,
                        TxFlags.StateDispatched, None)],
                      completion_callback=writer.get_callback())
            assert writer.succeeded()

        assert cache.is_cached(tx_hash_1)
        entry = cache.get_entry(tx_hash_1)
        assert TxFlags.HasByteData | TxFlags.HasPosition | TxFlags.StateDispatched == entry.flags
        assert cache.have_transaction_data_cached(tx_hash_1)

        # NOTE: We are not updating bytedata, and it should remain the same. The flags we pass
        # into update are treated specially to achieve this.
        metadata_2 = TxData(fee=10, height=88)
        propagate_flags = TxFlags.HasFee | TxFlags.HasHeight
        with SynchronousWriter() as writer:
            cache.update([(tx_hash_1, metadata_2, None,
                           propagate_flags | TxFlags.HasPosition)],
                         completion_callback=writer.get_callback())
            assert writer.succeeded()

        # Check the cache to see that the flags are correct and that bytedata is cached.
        entry = cache.get_entry(tx_hash_1)
        expected_flags = propagate_flags | TxFlags.StateDispatched | TxFlags.HasByteData
        assert expected_flags == entry.flags, \
            f"{TxFlags.to_repr(expected_flags)} !=  {TxFlags.to_repr(entry.flags)}"
        assert cache.have_transaction_data_cached(tx_hash_1)

        # Check the store to see that the flags are correct and the bytedata is retained.
        rows = self.store.read(tx_hashes=[tx_hash_1])
        assert 1 == len(rows)
        get_tx_hash, bytedata_get, flags_get, metadata_get = rows[0]
        assert bytedata_1 == bytedata_get
        assert flags_get & TxFlags.HasByteData != 0
コード例 #9
0
    def test_add_transaction(self):
        cache = TransactionCache(self.store)

        tx = Transaction.from_hex(tx_hex_1)
        tx_hash = tx.hash()
        with SynchronousWriter() as writer:
            cache.add_transaction(tx_hash, tx, completion_callback=writer.get_callback())
            assert writer.succeeded()

        assert cache.is_cached(tx_hash)
        entry = cache.get_entry(tx_hash)
        assert TxFlags.HasByteData == entry.flags & TxFlags.HasByteData
        assert cache.have_transaction_data_cached(tx_hash)
コード例 #10
0
    def test_update_or_add(self):
        cache = TransactionCache(self.store)

        # Add.
        bytedata_1 = bytes.fromhex(tx_hex_1)
        tx_hash_1 = bitcoinx.double_sha256(bytedata_1)
        metadata_1 = TxData()
        with SynchronousWriter() as writer:
            cache.update_or_add(
                [(tx_hash_1, metadata_1, bytedata_1, TxFlags.StateSettled)],
                completion_callback=writer.get_callback())
            assert writer.succeeded()

        assert cache.is_cached(tx_hash_1)
        entry = cache.get_entry(tx_hash_1)
        assert TxFlags.HasByteData | TxFlags.StateSettled == entry.flags
        assert entry.bytedata is not None

        # Update.
        metadata_2 = TxData(position=22)
        with SynchronousWriter() as writer:
            updated_ids = cache.update_or_add(
                [(tx_hash_1, metadata_2, None,
                  TxFlags.HasPosition | TxFlags.StateDispatched)],
                completion_callback=writer.get_callback())
            assert writer.succeeded()

        entry = cache.get_entry(tx_hash_1)
        _tx_hash, store_flags, _metadata = self.store.read_metadata(
            tx_hashes=[tx_hash_1])[0]
        # State flags if present get set in an update otherwise they remain the same.
        expected_flags = TxFlags.HasPosition | TxFlags.HasByteData | TxFlags.StateDispatched
        assert expected_flags == store_flags, \
            f"{TxFlags.to_repr(expected_flags)} !=  {TxFlags.to_repr(store_flags)}"
        assert expected_flags == entry.flags, \
            f"{TxFlags.to_repr(expected_flags)} !=  {TxFlags.to_repr(entry.flags)}"
        assert bytedata_1 == entry.bytedata
        assert metadata_2.position == entry.metadata.position
        assert updated_ids == set([tx_hash_1])
コード例 #11
0
    def test_get_entry_cached_on_demand(self) -> None:
        metadata = TxData(position=11, date_added=1, date_updated=1)
        flags = TxFlags.HasPosition
        def _read(*args, **kwargs) -> Tuple[bytes, Optional[bytes], TxFlags, TxData]:
            nonlocal metadata, flags
            return [ (b"tx_hash", None, flags, metadata) ]
        def _read_metadata(*args, **kwargs) -> Tuple[bytes, TxFlags, TxData]:
            nonlocal metadata, flags
            return [ (b"tx_hash", flags, metadata) ]

        mock_store = MockTransactionStore()
        mock_store.read = _read
        mock_store.read_metadata = _read_metadata

        cache = TransactionCache(mock_store, 0)
        their_entry = cache.get_entry(b"tx_hash")
        assert their_entry.metadata == metadata
        assert their_entry.flags == flags