Java源码示例:org.bitcoinj.core.BloomFilter

示例1
@Test
public void bloom1() {
    DeterministicKey key2 = chain.getKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);
    DeterministicKey key1 = chain.getKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);

    int numEntries =
            (((chain.getLookaheadSize() + chain.getLookaheadThreshold()) * 2)   // * 2 because of internal/external
                    + chain.numLeafKeysIssued()
                    + 4  // one root key + one account key + two chain keys (internal/external)
            ) * 2;  // because the filter contains keys and key hashes.
    assertEquals(numEntries, chain.numBloomFilterEntries());
    BloomFilter filter = chain.getFilter(numEntries, 0.001, 1);
    assertTrue(filter.contains(key1.getPubKey()));
    assertTrue(filter.contains(key1.getPubKeyHash()));
    assertTrue(filter.contains(key2.getPubKey()));
    assertTrue(filter.contains(key2.getPubKeyHash()));

    // The lookahead zone is tested in bloom2 and via KeyChainGroupTest.bloom
}
 
示例2
@Test
public void bloom() throws Exception {
    ECKey key1 = group.freshKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);
    ECKey key2 = new ECKey();
    BloomFilter filter = group.getBloomFilter(group.getBloomFilterElementCount(), 0.001, (long) (Math.random() * Long.MAX_VALUE));
    assertTrue(filter.contains(key1.getPubKeyHash()));
    assertTrue(filter.contains(key1.getPubKey()));
    assertFalse(filter.contains(key2.getPubKey()));
    // Check that the filter contains the lookahead buffer and threshold zone.
    for (int i = 0; i < LOOKAHEAD_SIZE + group.getLookaheadThreshold(); i++) {
        ECKey k = group.freshKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);
        assertTrue(filter.contains(k.getPubKeyHash()));
    }
    // We ran ahead of the lookahead buffer.
    assertFalse(filter.contains(group.freshKey(KeyChain.KeyPurpose.RECEIVE_FUNDS).getPubKey()));
    group.importKeys(key2);
    filter = group.getBloomFilter(group.getBloomFilterElementCount(), 0.001, (long) (Math.random() * Long.MAX_VALUE));
    assertTrue(filter.contains(key1.getPubKeyHash()));
    assertTrue(filter.contains(key1.getPubKey()));
    assertTrue(filter.contains(key2.getPubKey()));
}
 
示例3
@Test
public void bloomFilterForMarriedChains() throws Exception {
    group = createMarriedKeyChainGroup();
    int bufferSize = group.getLookaheadSize() + group.getLookaheadThreshold();
    int expected = bufferSize * 2 /* chains */ * 2 /* elements */;
    assertEquals(expected, group.getBloomFilterElementCount());
    Address address1 = group.freshAddress(KeyChain.KeyPurpose.RECEIVE_FUNDS);
    assertEquals(expected, group.getBloomFilterElementCount());
    BloomFilter filter = group.getBloomFilter(expected + 2, 0.001, (long) (Math.random() * Long.MAX_VALUE));
    assertTrue(filter.contains(address1.getHash()));

    Address address2 = group.freshAddress(KeyChain.KeyPurpose.CHANGE);
    assertTrue(filter.contains(address2.getHash()));

    // Check that the filter contains the lookahead buffer.
    for (int i = 0; i < bufferSize - 1 /* issued address */; i++) {
        Address address = group.freshAddress(KeyChain.KeyPurpose.RECEIVE_FUNDS);
        assertTrue("key " + i, filter.contains(address.getHash()));
    }
    // We ran ahead of the lookahead buffer.
    assertFalse(filter.contains(group.freshAddress(KeyChain.KeyPurpose.RECEIVE_FUNDS).getHash()));
}
 
示例4
BloomFilter getBloomFilter(final int size, final double falsePositiveRate, final long nTweak) {
    final Set<Sha256Hash> keys = mUnspentOutpoints.keySet();
    Log.d(TAG, "getBloomFilter returning " + keys.size() + " items");
    final BloomFilter filter = new BloomFilter(size, falsePositiveRate, nTweak);
    for (final Sha256Hash hash : keys)
        filter.insert(hash.getReversedBytes());

    if (keys.isEmpty()) {
        // Add a fake entry to avoid downloading blocks when filter is empty,
        // as empty bloom filters are ignored by bitcoinj.
        // FIXME: This results in a constant filter that peers can use to identify
        //        us as a Green client. That is undesirable.
        filter.insert(new byte[] {(byte) 0xde, (byte) 0xad, (byte) 0xbe, (byte) 0xef});
    }
    return filter;
}
 
示例5
public BloomFilter getBloomFilter(final int size, final double falsePositiveRate, final long nTweak) {
    final Set<Sha256Hash> keys = mUnspentOutpoints.keySet();
    Log.d(TAG, "getBloomFilter returning " + keys.size() + " items");
    final BloomFilter filter = new BloomFilter(size, falsePositiveRate, nTweak);
    for (final Sha256Hash hash : keys)
        filter.insert(hash.getReversedBytes());

    if (keys.isEmpty()) {
        // Add a fake entry to avoid downloading blocks when filter is empty,
        // as empty bloom filters are ignored by bitcoinj.
        // FIXME: This results in a constant filter that peers can use to identify
        //        us as a GreenBits client. That is undesirable.
        filter.insert(new byte[]{(byte) 0xde, (byte) 0xad, (byte) 0xbe, (byte) 0xef});
    }
    return filter;
}
 
示例6
public BloomFilter getBloomFilter(int size, double falsePositiveRate, long nTweak) {
    BloomFilter filter = new BloomFilter(size, falsePositiveRate, nTweak);
    if (basic.numKeys() > 0)
        filter.merge(basic.getFilter(size, falsePositiveRate, nTweak));

    for (DeterministicKeyChain chain : chains) {
        filter.merge(chain.getFilter(size, falsePositiveRate, nTweak));
    }
    return filter;
}
 
示例7
@Override
public BloomFilter getFilter(int size, double falsePositiveRate, long tweak) {
    lock.lock();
    try {
        checkArgument(size >= numBloomFilterEntries());
        maybeLookAhead();
        return basicKeyChain.getFilter(size, falsePositiveRate, tweak);
    } finally {
        lock.unlock();
    }

}
 
示例8
@Override
public BloomFilter getFilter(int size, double falsePositiveRate, long tweak) {
    lock.lock();
    try {
        BloomFilter filter = new BloomFilter(size, falsePositiveRate, tweak);
        for (ECKey key : hashToKeys.values())
            filter.insert(key);
        return filter;
    } finally {
        lock.unlock();
    }
}
 
示例9
@Test
public void bloom2() throws Exception {
    // Verify that if when we watch a key, the filter contains at least 100 keys.
    DeterministicKey[] keys = new DeterministicKey[100];
    for (int i = 0; i < keys.length; i++)
        keys[i] = chain.getKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);
    chain = DeterministicKeyChain.watch(chain.getWatchingKey().dropPrivateBytes().dropParent());
    int e = chain.numBloomFilterEntries();
    BloomFilter filter = chain.getFilter(e, 0.001, 1);
    for (DeterministicKey key : keys)
        assertTrue("key " + key, filter.contains(key.getPubKeyHash()));
}
 
示例10
@Test
public void bloom() throws Exception {
    ECKey key1 = new ECKey();
    ECKey key2 = new ECKey();
    chain.importKeys(key1, key2);
    assertEquals(2, chain.numKeys());
    assertEquals(4, chain.numBloomFilterEntries());
    BloomFilter filter = chain.getFilter(4, 0.001, 100);
    assertTrue(filter.contains(key1.getPubKey()));
    assertTrue(filter.contains(key1.getPubKeyHash()));
    assertTrue(filter.contains(key2.getPubKey()));
    assertTrue(filter.contains(key2.getPubKeyHash()));
    ECKey key3 = new ECKey();
    assertFalse(filter.contains(key3.getPubKey()));
}
 
示例11
@Override
public BloomFilter getFilter(int size, double falsePositiveRate, long tweak) {
    lock.lock();
    try {
        checkArgument(size >= numBloomFilterEntries());
        maybeLookAhead();
        return basicKeyChain.getFilter(size, falsePositiveRate, tweak);
    } finally {
        lock.unlock();
    }

}
 
示例12
@Override
public BloomFilter getFilter(int size, double falsePositiveRate, long tweak) {
    lock.lock();
    try {
        BloomFilter filter = new BloomFilter(size, falsePositiveRate, tweak);
        for (ECKey key : hashToKeys.values())
            filter.insert(key);
        return filter;
    } finally {
        lock.unlock();
    }
}
 
示例13
@Test
public void bloom() throws Exception {
    ECKey key1 = new ECKey();
    ECKey key2 = new ECKey();
    chain.importKeys(key1, key2);
    assertEquals(2, chain.numKeys());
    assertEquals(4, chain.numBloomFilterEntries());
    BloomFilter filter = chain.getFilter(4, 0.001, 100);
    assertTrue(filter.contains(key1.getPubKey()));
    assertTrue(filter.contains(key1.getPubKeyHash()));
    assertTrue(filter.contains(key2.getPubKey()));
    assertTrue(filter.contains(key2.getPubKeyHash()));
    ECKey key3 = new ECKey();
    assertFalse(filter.contains(key3.getPubKey()));
}
 
示例14
@Override
public BloomFilter getFilter(int size, double falsePositiveRate, long tweak) {
    lock.lock();
    try {
        checkArgument(size >= numBloomFilterEntries());
        maybeLookAhead();
        return basicKeyChain.getFilter(size, falsePositiveRate, tweak);
    } finally {
        lock.unlock();
    }

}
 
示例15
@Override
public BloomFilter getFilter(int size, double falsePositiveRate, long tweak) {
    lock.lock();
    try {
        BloomFilter filter = new BloomFilter(size, falsePositiveRate, tweak);
        for (ECKey key : hashToKeys.values())
            filter.insert(key);
        return filter;
    } finally {
        lock.unlock();
    }
}
 
示例16
@Test
public void bloom() throws Exception {
    ECKey key1 = new ECKey();
    ECKey key2 = new ECKey();
    chain.importKeys(key1, key2);
    assertEquals(2, chain.numKeys());
    assertEquals(4, chain.numBloomFilterEntries());
    BloomFilter filter = chain.getFilter(4, 0.001, 100);
    assertTrue(filter.contains(key1.getPubKey()));
    assertTrue(filter.contains(key1.getPubKeyHash()));
    assertTrue(filter.contains(key2.getPubKey()));
    assertTrue(filter.contains(key2.getPubKeyHash()));
    ECKey key3 = new ECKey();
    assertFalse(filter.contains(key3.getPubKey()));
}
 
示例17
public BloomFilter getLastFilter() {
    return lastFilter;
}
 
示例18
@Override
public BloomFilter getBloomFilter(final int size, final double falsePositiveRate, final long nTweak) {
    return mSPV.getBloomFilter(size, falsePositiveRate, nTweak);
}
 
示例19
public BloomFilter getLastFilter() {
    return lastFilter;
}
 
示例20
@Override
public BloomFilter getBloomFilter(final int size, final double falsePositiveRate, final long nTweak) {
    return mSPV.getBloomFilter(size, falsePositiveRate, nTweak);
}
 
示例21
public BloomFilter getLastFilter() {
    return lastFilter;
}
 
示例22
/**
 * <p>Gets a bloom filter that contains all of the public keys from this chain, and which will provide the given
 * false-positive rate if it has size elements. Keep in mind that you will get 2 elements in the bloom filter for
 * each key in the key chain, for the public key and the hash of the public key (address form). For this reason
 * size should be <i>at least</i> 2x the result of {@link #numKeys()}.</p>
 * <p>
 * <p>This is used to generate a {@link BloomFilter} which can be {@link BloomFilter#merge(BloomFilter)}d with
 * another. It could also be used if you have a specific target for the filter's size.</p>
 * <p>
 * <p>See the docs for {@link BloomFilter#BloomFilter(int, double, long)} for a brief
 * explanation of anonymity when using bloom filters, and for the meaning of these parameters.</p>
 */
BloomFilter getFilter(int size, double falsePositiveRate, long tweak);
 
示例23
/**
 * <p>Gets a bloom filter that contains all of the public keys from this chain, and which will provide the given
 * false-positive rate if it has size elements. Keep in mind that you will get 2 elements in the bloom filter for
 * each key in the key chain, for the public key and the hash of the public key (address form). For this reason
 * size should be <i>at least</i> 2x the result of {@link #numKeys()}.</p>
 *
 * <p>This is used to generate a {@link BloomFilter} which can be {@link BloomFilter#merge(BloomFilter)}d with
 * another. It could also be used if you have a specific target for the filter's size.</p>
 *
 * <p>See the docs for {@link org.bitcoinj.core.BloomFilter#BloomFilter(int, double, long)} for a brief
 * explanation of anonymity when using bloom filters, and for the meaning of these parameters.</p>
 */
BloomFilter getFilter(int size, double falsePositiveRate, long tweak);
 
示例24
/**
 * <p>Gets a bloom filter that contains all of the public keys from this chain, and which will provide the given
 * false-positive rate if it has size elements. Keep in mind that you will get 2 elements in the bloom filter for
 * each key in the key chain, for the public key and the hash of the public key (address form). For this reason
 * size should be <i>at least</i> 2x the result of {@link #numKeys()}.</p>
 *
 * <p>This is used to generate a {@link BloomFilter} which can be {@link BloomFilter#merge(BloomFilter)}d with
 * another. It could also be used if you have a specific target for the filter's size.</p>
 *
 * <p>See the docs for {@link org.bitcoinj.core.BloomFilter#BloomFilter(int, double, long)} for a brief
 * explanation of anonymity when using bloom filters, and for the meaning of these parameters.</p>
 */
BloomFilter getFilter(int size, double falsePositiveRate, long tweak);