/** * Provide this FilteredBlock with a transaction which is in its Merkle tree. * @return false if the tx is not relevant to this FilteredBlock */ public boolean provideTransaction(Transaction tx) throws VerificationException { Sha256Hash hash = tx.getHash(); if (getTransactionHashes().contains(hash)) { associatedTransactions.put(hash, tx); return true; } return false; }
/** * Provide this FilteredBlock with a transaction which is in its Merkle tree. * @return false if the tx is not relevant to this FilteredBlock */ public boolean provideTransaction(Transaction tx) throws VerificationException { Sha256Hash hash = tx.getHash(); if (getTransactionHashes().contains(hash)) { associatedTransactions.put(hash, tx); return true; } return false; }
/** * Provide this FilteredBlock with a transaction which is in its Merkle tree. * @return false if the tx is not relevant to this FilteredBlock */ public boolean provideTransaction(Transaction tx) throws VerificationException { Sha256Hash hash = tx.getHash(); if (getTransactionHashes().contains(hash)) { associatedTransactions.put(hash, tx); return true; } return false; }
/** * Provide this FilteredBlock with a transaction which is in its Merkle tree. * @return false if the tx is not relevant to this FilteredBlock */ public boolean provideTransaction(Transaction tx) throws VerificationException { Sha256Hash hash = tx.getHash(); if (getTransactionHashes().contains(hash)) { associatedTransactions.put(hash, tx); return true; } return false; }
class OrphanBlock { final Block block; final List<Sha256Hash> filteredTxHashes; final Map<Sha256Hash, Transaction> filteredTxn; OrphanBlock(Block block, @Nullable List<Sha256Hash> filteredTxHashes, @Nullable Map<Sha256Hash, Transaction> filteredTxn) { final boolean filtered = filteredTxHashes != null && filteredTxn != null; Preconditions.checkArgument((block.transactions == null && filtered) || (block.transactions != null && !filtered)); this.block = block; this.filteredTxHashes = filteredTxHashes; this.filteredTxn = filteredTxn; } } // Holds blocks that we have received but can't plug into the chain yet, eg because they were created whilst we
class OrphanBlock { final Block block; final List<Sha256Hash> filteredTxHashes; final Map<Sha256Hash, Transaction> filteredTxn; OrphanBlock(Block block, @Nullable List<Sha256Hash> filteredTxHashes, @Nullable Map<Sha256Hash, Transaction> filteredTxn) { final boolean filtered = filteredTxHashes != null && filteredTxn != null; Preconditions.checkArgument((block.transactions == null && filtered) || (block.transactions != null && !filtered)); this.block = block; this.filteredTxHashes = filteredTxHashes; this.filteredTxn = filteredTxn; } } // Holds blocks that we have received but can't plug into the chain yet, eg because they were created whilst we
class OrphanBlock { final Block block; final List<Sha256Hash> filteredTxHashes; final Map<Sha256Hash, Transaction> filteredTxn; OrphanBlock(Block block, @Nullable List<Sha256Hash> filteredTxHashes, @Nullable Map<Sha256Hash, Transaction> filteredTxn) { final boolean filtered = filteredTxHashes != null && filteredTxn != null; Preconditions.checkArgument((block.transactions == null && filtered) || (block.transactions != null && !filtered)); this.block = block; this.filteredTxHashes = filteredTxHashes; this.filteredTxn = filteredTxn; } } // Holds blocks that we have received but can't plug into the chain yet, eg because they were created whilst we
class OrphanBlock { final Block block; final List<Sha256Hash> filteredTxHashes; final Map<Sha256Hash, Transaction> filteredTxn; OrphanBlock(Block block, @Nullable List<Sha256Hash> filteredTxHashes, @Nullable Map<Sha256Hash, Transaction> filteredTxn) { final boolean filtered = filteredTxHashes != null && filteredTxn != null; Preconditions.checkArgument((block.transactions == null && filtered) || (block.transactions != null && !filtered)); this.block = block; this.filteredTxHashes = filteredTxHashes; this.filteredTxn = filteredTxn; } } // Holds blocks that we have received but can't plug into the chain yet, eg because they were created whilst we
@Test public void deserializeFilteredBlock() throws Exception { // Random real block (000000000000dab0130bbcc991d3d7ae6b81aa6f50a798888dfe62337458dc45) // With one tx FilteredBlock block = new FilteredBlock(PARAMS, HEX.decode("0100000079cda856b143d9db2c1caff01d1aecc8630d30625d10e8b4b8b0000000000000b50cc069d6a3e33e3ff84a5c41d9d3febe7c770fdcc96b2c3ff60abe184f196367291b4d4c86041b8fa45d630100000001b50cc069d6a3e33e3ff84a5c41d9d3febe7c770fdcc96b2c3ff60abe184f19630101")); // Check that the header was properly deserialized assertTrue(block.getBlockHeader().getHash().equals(Sha256Hash.wrap("000000000000dab0130bbcc991d3d7ae6b81aa6f50a798888dfe62337458dc45"))); // Check that the partial merkle tree is correct List<Sha256Hash> txesMatched = block.getTransactionHashes(); assertTrue(txesMatched.size() == 1); assertTrue(txesMatched.contains(Sha256Hash.wrap("63194f18be0af63f2c6bc9dc0f777cbefed3d9415c4af83f3ee3a3d669c00cb5"))); // Check round tripping. assertEquals(block, new FilteredBlock(PARAMS, block.bitcoinSerialize())); }
@Test public void createFilteredBlock() throws Exception { ECKey key1 = new ECKey(); ECKey key2 = new ECKey(); Transaction tx1 = FakeTxBuilder.createFakeTx(PARAMS, Coin.COIN, key1); Transaction tx2 = FakeTxBuilder.createFakeTx(PARAMS, Coin.FIFTY_COINS, key2.toAddress(PARAMS)); Block block = FakeTxBuilder.makeSolvedTestBlock(PARAMS.getGenesisBlock(), Address.fromBase58(PARAMS, "msg2t2V2sWNd85LccoddtWysBTR8oPnkzW"), tx1, tx2); BloomFilter filter = new BloomFilter(4, 0.1, 1); filter.insert(key1); filter.insert(key2); FilteredBlock filteredBlock = filter.applyAndUpdate(block); assertEquals(4, filteredBlock.getTransactionCount()); // This call triggers verification of the just created data. List<Sha256Hash> txns = filteredBlock.getTransactionHashes(); assertTrue(txns.contains(tx1.getHash())); assertTrue(txns.contains(tx2.getHash())); }
assertTrue(filteredBlock.getHash().equals(block.getHash())); List<Sha256Hash> txHashList = filteredBlock.getTransactionHashes(); assertTrue(txHashList.size() == 4);