@Test public void serializeWatching() throws Exception { group = new KeyChainGroup(params, watchingAccountKey); group.setLookaheadSize(LOOKAHEAD_SIZE); group.freshKey(KeyChain.KeyPurpose.RECEIVE_FUNDS); group.freshKey(KeyChain.KeyPurpose.CHANGE); group.getBloomFilterElementCount(); // Force lookahead. List<Protos.Key> protoKeys1 = group.serializeToProtobuf(); assertEquals( 3 + (group.getLookaheadSize() + group.getLookaheadThreshold() + 1) * 2, protoKeys1.size()); group = KeyChainGroup.fromProtobufUnencrypted(params, protoKeys1); assertEquals( 3 + (group.getLookaheadSize() + group.getLookaheadThreshold() + 1) * 2, group.serializeToProtobuf().size()); }
@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())); }
@Test public void freshAddress() throws Exception { group = createMarriedKeyChainGroup(); Address a1 = group.freshAddress(KeyChain.KeyPurpose.RECEIVE_FUNDS); Address a2 = group.freshAddress(KeyChain.KeyPurpose.RECEIVE_FUNDS); assertTrue(a1.isP2SHAddress()); assertNotEquals(a1, a2); group.getBloomFilterElementCount(); assertEquals( ((group.getLookaheadSize() + group.getLookaheadThreshold()) * 2) // * 2 because of internal/external + (2 - group.getLookaheadThreshold()) // keys issued + 4 /* master, account, int, ext */, group.numKeys()); Address a3 = group.currentAddress(KeyChain.KeyPurpose.RECEIVE_FUNDS); assertEquals(a2, a3); }
@Test public void findRedeemScriptFromPubHash() throws Exception { group = createMarriedKeyChainGroup(); Address address = group.freshAddress(KeyChain.KeyPurpose.RECEIVE_FUNDS); assertTrue(group.findRedeemDataFromScriptHash(address.getHash160()) != null); group.getBloomFilterElementCount(); KeyChainGroup group2 = createMarriedKeyChainGroup(); group2.freshAddress(KeyChain.KeyPurpose.RECEIVE_FUNDS); group2.getBloomFilterElementCount(); // Force lookahead. // test address from lookahead zone and lookahead threshold zone for (int i = 0; i < group.getLookaheadSize() + group.getLookaheadThreshold(); i++) { address = group.freshAddress(KeyChain.KeyPurpose.RECEIVE_FUNDS); assertTrue(group2.findRedeemDataFromScriptHash(address.getHash160()) != null); } assertFalse( group2.findRedeemDataFromScriptHash( group.freshAddress(KeyChain.KeyPurpose.RECEIVE_FUNDS).getHash160()) != null); }
@Test public void freshCurrentKeys() throws Exception { int numKeys = ((group.getLookaheadSize() + group.getLookaheadThreshold()) * 2) // * 2 because of internal/external + 1 // keys issued + 3 /* account key + int/ext parent keys */; assertEquals(numKeys, group.numKeys()); assertEquals(2 * numKeys, group.getBloomFilterElementCount()); ECKey r1 = group.currentKey(KeyChain.KeyPurpose.RECEIVE_FUNDS); assertEquals(numKeys, group.numKeys()); assertEquals(2 * numKeys, group.getBloomFilterElementCount()); ECKey i1 = new ECKey(); group.importKeys(i1); numKeys++; assertEquals(numKeys, group.numKeys()); assertEquals(2 * numKeys, group.getBloomFilterElementCount()); ECKey r2 = group.currentKey(KeyChain.KeyPurpose.RECEIVE_FUNDS); assertEquals(r1, r2); ECKey c1 = group.currentKey(KeyChain.KeyPurpose.CHANGE); assertNotEquals(r1, c1); ECKey r3 = group.freshKey(KeyChain.KeyPurpose.RECEIVE_FUNDS); assertNotEquals(r1, r3); ECKey c2 = group.freshKey(KeyChain.KeyPurpose.CHANGE); assertNotEquals(r3, c2); // Current key has not moved and will not under marked as used. ECKey r4 = group.currentKey(KeyChain.KeyPurpose.RECEIVE_FUNDS); assertEquals(r2, r4); ECKey c3 = group.currentKey(KeyChain.KeyPurpose.CHANGE); assertEquals(c1, c3); // Mark as used. Current key is now different. group.markPubKeyAsUsed(r4.getPubKey()); ECKey r5 = group.currentKey(KeyChain.KeyPurpose.RECEIVE_FUNDS); assertNotEquals(r4, r5); }
@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.getHash160())); Address address2 = group.freshAddress(KeyChain.KeyPurpose.CHANGE); assertTrue(filter.contains(address2.getHash160())); // 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.getHash160())); } // We ran ahead of the lookahead buffer. assertFalse( filter.contains(group.freshAddress(KeyChain.KeyPurpose.RECEIVE_FUNDS).getHash160())); }