org.bitcoinj.core.BloomFilter Java Examples

The following examples show how to use org.bitcoinj.core.BloomFilter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SPV.java    From GreenBits with GNU General Public License v3.0 6 votes vote down vote up
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;
}
 
Example #2
Source File: DeterministicKeyChainTest.java    From bcm-android with GNU General Public License v3.0 6 votes vote down vote up
@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
}
 
Example #3
Source File: KeyChainGroupTest.java    From bcm-android with GNU General Public License v3.0 6 votes vote down vote up
@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()));
}
 
Example #4
Source File: KeyChainGroupTest.java    From bcm-android with GNU General Public License v3.0 6 votes vote down vote up
@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()));
}
 
Example #5
Source File: SPV.java    From green_android with GNU General Public License v3.0 6 votes vote down vote up
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;
}
 
Example #6
Source File: KeyChainGroup.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
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;
}
 
Example #7
Source File: BasicKeyChainTest.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
@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()));
}
 
Example #8
Source File: BasicKeyChain.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
@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();
    }
}
 
Example #9
Source File: DeterministicKeyChain.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
@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();
    }

}
 
Example #10
Source File: BasicKeyChainTest.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
@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()));
}
 
Example #11
Source File: BasicKeyChain.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
@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();
    }
}
 
Example #12
Source File: DeterministicKeyChain.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
@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();
    }

}
 
Example #13
Source File: BasicKeyChainTest.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
@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()));
}
 
Example #14
Source File: DeterministicKeyChainTest.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
@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()));
}
 
Example #15
Source File: BasicKeyChain.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
@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();
    }
}
 
Example #16
Source File: DeterministicKeyChain.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
@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();
    }

}
 
Example #17
Source File: FilterMerger.java    From green_android with GNU General Public License v3.0 4 votes vote down vote up
public BloomFilter getLastFilter() {
    return lastFilter;
}
 
Example #18
Source File: PeerFilterProvider.java    From green_android with GNU General Public License v3.0 4 votes vote down vote up
@Override
public BloomFilter getBloomFilter(final int size, final double falsePositiveRate, final long nTweak) {
    return mSPV.getBloomFilter(size, falsePositiveRate, nTweak);
}
 
Example #19
Source File: PeerFilterProvider.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
@Override
public BloomFilter getBloomFilter(final int size, final double falsePositiveRate, final long nTweak) {
    return mSPV.getBloomFilter(size, falsePositiveRate, nTweak);
}
 
Example #20
Source File: FilterMerger.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
public BloomFilter getLastFilter() {
    return lastFilter;
}
 
Example #21
Source File: FilterMerger.java    From bcm-android with GNU General Public License v3.0 4 votes vote down vote up
public BloomFilter getLastFilter() {
    return lastFilter;
}
 
Example #22
Source File: KeyChain.java    From green_android with GNU General Public License v3.0 2 votes vote down vote up
/**
 * <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);
 
Example #23
Source File: KeyChain.java    From bcm-android with GNU General Public License v3.0 2 votes vote down vote up
/**
 * <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);
 
Example #24
Source File: KeyChain.java    From GreenBits with GNU General Public License v3.0 2 votes vote down vote up
/**
 * <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);