/** * Returns a {@link StoredBlock} representing the last checkpoint before the given time, for example, normally * you would want to know the checkpoint before the earliest wallet birthday. */ public StoredBlock getCheckpointBefore(long time) { try { checkArgument(time > params.getGenesisBlock().getTimeSeconds()); // This is thread safe because the map never changes after creation. Map.Entry<Long, StoredBlock> entry = checkpoints.floorEntry(time); if (entry != null) return entry.getValue(); Block genesis = params.getGenesisBlock().cloneAsHeader(); return new StoredBlock(genesis, genesis.getWork(), 0); } catch (VerificationException e) { throw new RuntimeException(e); // Cannot happen. } }
/** * Returns a {@link StoredBlock} representing the last checkpoint before the given time, for example, normally * you would want to know the checkpoint before the earliest wallet birthday. */ public StoredBlock getCheckpointBefore(long time) { try { checkArgument(time > params.getGenesisBlock().getTimeSeconds()); // This is thread safe because the map never changes after creation. Map.Entry<Long, StoredBlock> entry = checkpoints.floorEntry(time); if (entry != null) return entry.getValue(); Block genesis = params.getGenesisBlock().cloneAsHeader(); return new StoredBlock(genesis, genesis.getWork(), 0); } catch (VerificationException e) { throw new RuntimeException(e); // Cannot happen. } }
/** * Returns a {@link StoredBlock} representing the last checkpoint before the given time, for example, normally * you would want to know the checkpoint before the earliest wallet birthday. */ public StoredBlock getCheckpointBefore(long time) { try { checkArgument(time > params.getGenesisBlock().getTimeSeconds()); // This is thread safe because the map never changes after creation. Map.Entry<Long, StoredBlock> entry = checkpoints.floorEntry(time); if (entry != null) return entry.getValue(); Block genesis = params.getGenesisBlock().cloneAsHeader(); return new StoredBlock(genesis, genesis.getWork(), 0); } catch (VerificationException e) { throw new RuntimeException(e); // Cannot happen. } }
public MemoryBlockStore(NetworkParameters params) { // Insert the genesis block. try { Block genesisHeader = params.getGenesisBlock().cloneAsHeader(); StoredBlock storedGenesis = new StoredBlock(genesisHeader, genesisHeader.getWork(), 0); put(storedGenesis); setChainHead(storedGenesis); this.params = params; } catch (BlockStoreException e) { throw new RuntimeException(e); // Cannot happen. } catch (VerificationException e) { throw new RuntimeException(e); // Cannot happen. } }
public MemoryBlockStore(NetworkParameters params) { // Insert the genesis block. try { Block genesisHeader = params.getGenesisBlock().cloneAsHeader(); StoredBlock storedGenesis = new StoredBlock(genesisHeader, genesisHeader.getWork(), 0); put(storedGenesis); setChainHead(storedGenesis); this.params = params; } catch (BlockStoreException e) { throw new RuntimeException(e); // Cannot happen. } catch (VerificationException e) { throw new RuntimeException(e); // Cannot happen. } }
public MemoryBlockStore(NetworkParameters params) { // Insert the genesis block. try { Block genesisHeader = params.getGenesisBlock().cloneAsHeader(); StoredBlock storedGenesis = new StoredBlock(genesisHeader, genesisHeader.getWork(), 0); put(storedGenesis); setChainHead(storedGenesis); this.params = params; } catch (BlockStoreException e) { throw new RuntimeException(e); // Cannot happen. } catch (VerificationException e) { throw new RuntimeException(e); // Cannot happen. } }
public MemoryBlockStore(NetworkParameters params) { // Insert the genesis block. try { Block genesisHeader = params.getGenesisBlock().cloneAsHeader(); StoredBlock storedGenesis = new StoredBlock(genesisHeader, genesisHeader.getWork(), 0); put(storedGenesis); setChainHead(storedGenesis); this.params = params; } catch (BlockStoreException e) { throw new RuntimeException(e); // Cannot happen. } catch (VerificationException e) { throw new RuntimeException(e); // Cannot happen. } }
private void initNewStore(NetworkParameters params) throws Exception { byte[] header; header = HEADER_MAGIC.getBytes("US-ASCII"); buffer.put(header); // Insert the genesis block. lock.lock(); try { setRingCursor(buffer, FILE_PROLOGUE_BYTES); } finally { lock.unlock(); } Block genesis = params.getGenesisBlock().cloneAsHeader(); StoredBlock storedGenesis = new StoredBlock(genesis, genesis.getWork(), 0); put(storedGenesis); setChainHead(storedGenesis); }
private void initNewStore(NetworkParameters params) throws Exception { byte[] header; header = HEADER_MAGIC.getBytes("US-ASCII"); buffer.put(header); // Insert the genesis block. lock.lock(); try { setRingCursor(buffer, FILE_PROLOGUE_BYTES); } finally { lock.unlock(); } Block genesis = params.getGenesisBlock().cloneAsHeader(); StoredBlock storedGenesis = new StoredBlock(genesis, genesis.getWork(), 0); put(storedGenesis); setChainHead(storedGenesis); }
private void initNewStore(NetworkParameters params) throws Exception { byte[] header; header = HEADER_MAGIC.getBytes("US-ASCII"); buffer.put(header); // Insert the genesis block. lock.lock(); try { setRingCursor(buffer, FILE_PROLOGUE_BYTES); } finally { lock.unlock(); } Block genesis = params.getGenesisBlock().cloneAsHeader(); StoredBlock storedGenesis = new StoredBlock(genesis, genesis.getWork(), 0); put(storedGenesis); setChainHead(storedGenesis); }
@Test public void testWork() throws Exception { BigInteger work = PARAMS.getGenesisBlock().getWork(); // This number is printed by Bitcoin Core at startup as the calculated value of chainWork on testnet: // // SetBestChain: new best=00000007199508e34a9f height=0 work=536879104 assertEquals(BigInteger.valueOf(536879104L), work); }
private synchronized void initStoreIfNeeded() throws BlockStoreException { if (db.get(CHAIN_HEAD_KEY) != null) return; // Already initialised. Block genesis = context.getParams().getGenesisBlock().cloneAsHeader(); StoredBlock storedGenesis = new StoredBlock(genesis, genesis.getWork(), 0); put(storedGenesis); setChainHead(storedGenesis); }
private synchronized void initStoreIfNeeded() throws BlockStoreException { if (db.get(CHAIN_HEAD_KEY) != null) return; // Already initialised. Block genesis = context.getParams().getGenesisBlock().cloneAsHeader(); StoredBlock storedGenesis = new StoredBlock(genesis, genesis.getWork(), 0); put(storedGenesis); setChainHead(storedGenesis); }
private synchronized void initStoreIfNeeded() throws BlockStoreException { if (db.get(CHAIN_HEAD_KEY) != null) return; // Already initialised. Block genesis = context.getParams().getGenesisBlock().cloneAsHeader(); StoredBlock storedGenesis = new StoredBlock(genesis, genesis.getWork(), 0); put(storedGenesis); setChainHead(storedGenesis); }
private synchronized void initStoreIfNeeded() throws BlockStoreException { if (db.get(CHAIN_HEAD_KEY) != null) return; // Already initialised. Block genesis = context.getParams().getGenesisBlock().cloneAsHeader(); StoredBlock storedGenesis = new StoredBlock(genesis, genesis.getWork(), 0); put(storedGenesis); setChainHead(storedGenesis); }
@Test public void nonStandardDust() { Transaction standardTx = new Transaction(PARAMS); standardTx.addInput(PARAMS.getGenesisBlock().getTransactions().get(0).getOutput(0)); standardTx.addOutput(COIN, key1); assertEquals(RiskAnalysis.Result.OK, DefaultRiskAnalysis.FACTORY.create(wallet, standardTx, NO_DEPS).analyze()); Transaction dustTx = new Transaction(PARAMS); dustTx.addInput(PARAMS.getGenesisBlock().getTransactions().get(0).getOutput(0)); dustTx.addOutput(Coin.SATOSHI, key1); // 1 Satoshi assertEquals(RiskAnalysis.Result.NON_STANDARD, DefaultRiskAnalysis.FACTORY.create(wallet, dustTx, NO_DEPS).analyze()); Transaction edgeCaseTx = new Transaction(PARAMS); edgeCaseTx.addInput(PARAMS.getGenesisBlock().getTransactions().get(0).getOutput(0)); edgeCaseTx.addOutput(DefaultRiskAnalysis.MIN_ANALYSIS_NONDUST_OUTPUT, key1); // Dust threshold assertEquals(RiskAnalysis.Result.OK, DefaultRiskAnalysis.FACTORY.create(wallet, edgeCaseTx, NO_DEPS).analyze()); }
@Test public void nonFinalDependency() { // Final tx has a dependency that is non-final. Transaction tx1 = new Transaction(PARAMS); tx1.addInput(PARAMS.getGenesisBlock().getTransactions().get(0).getOutput(0)).setSequenceNumber(1); TransactionOutput output = tx1.addOutput(COIN, key1); tx1.setLockTime(TIMESTAMP + 86400); Transaction tx2 = new Transaction(PARAMS); tx2.addInput(output); tx2.addOutput(COIN, new ECKey()); DefaultRiskAnalysis analysis = DefaultRiskAnalysis.FACTORY.create(wallet, tx2, ImmutableList.of(tx1)); assertEquals(RiskAnalysis.Result.NON_FINAL, analysis.analyze()); assertEquals(tx1, analysis.getNonFinal()); }
@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())); }
@Test public void coinbaseTxns() throws Exception { // Covers issue 420 where the outpoint index of a coinbase tx input was being mis-serialized. Block b = PARAMS.getGenesisBlock().createNextBlockWithCoinbase(Block.BLOCK_VERSION_GENESIS, myKey.getPubKey(), FIFTY_COINS, Block.BLOCK_HEIGHT_GENESIS); Transaction coinbase = b.getTransactions().get(0); assertTrue(coinbase.isCoinBase()); BlockChain chain = new BlockChain(PARAMS, myWallet, new MemoryBlockStore(PARAMS)); assertTrue(chain.add(b)); // Wallet now has a coinbase tx in it. assertEquals(1, myWallet.getTransactions(true).size()); assertTrue(myWallet.getTransaction(coinbase.getHash()).isCoinBase()); Wallet wallet2 = roundTrip(myWallet); assertEquals(1, wallet2.getTransactions(true).size()); assertTrue(wallet2.getTransaction(coinbase.getHash()).isCoinBase()); }
@Test public void unconnectedBlocks() throws Exception { Block b1 = PARAMS.getGenesisBlock().createNextBlock(coinbaseTo); Block b2 = b1.createNextBlock(coinbaseTo); Block b3 = b2.createNextBlock(coinbaseTo); // Connected. assertTrue(chain.add(b1)); // Unconnected but stored. The head of the chain is still b1. assertFalse(chain.add(b3)); assertEquals(chain.getChainHead().getHeader(), b1.cloneAsHeader()); // Add in the middle block. assertTrue(chain.add(b2)); assertEquals(chain.getChainHead().getHeader(), b3.cloneAsHeader()); }