public void shutdown () throws BlockStoreException { peerGroup.stop(); blockStore.close(); }
/** * Removes the current wallet from the block chain and closes the block store */ private void closeBlockstore() { // Remove the wallet from the block chain before closing the blockstore if (WalletManager.INSTANCE.getCurrentWalletSummary().isPresent() && blockChain != null) { log.debug("Removing wallet from blockChain..."); blockChain.removeWallet(WalletManager.INSTANCE.getCurrentWalletSummary().get().getWallet()); blockChain = null; // need to recreate it when a new blockstore is created } // Close the blockstore if (blockStore != null) { try { // The blockstore can throw an NPE internally log.debug("When the blockstore was closed the height was {}", blockStore.getChainHead() == null ? "unknown" : blockStore.getChainHead().getHeight()); blockStore.close(); } catch (BlockStoreException e) { log.warn("BlockStoreException: Blockstore was already closed or not closed cleanly: {}", e.getMessage()); } catch (NullPointerException e) { // Internal bug in Bitcoinj } } else { log.debug("blockStore was not present"); } blockStore = null; }
@Override protected StoredBlock getStoredBlockInCurrentScope(Sha256Hash hash) throws BlockStoreException { return blockStore.get(hash); }
/** * <p>Convenience method that creates a CheckpointManager, loads the given data, gets the checkpoint for the given * time, then inserts it into the store and sets that to be the chain head. Useful when you have just created * a new store from scratch and want to use configure it all in one go.</p> * * <p>Note that time is adjusted backwards by a week to account for possible clock drift in the block headers.</p> */ public static void checkpoint(NetworkParameters params, InputStream checkpoints, BlockStore store, long time) throws IOException, BlockStoreException { checkNotNull(params); checkNotNull(store); checkArgument(!(store instanceof FullPrunedBlockStore), "You cannot use checkpointing with a full store."); time -= 86400 * 7; checkArgument(time > 0); log.info("Attempting to initialize a new block store with a checkpoint for time {} ({})", time, Utils.dateTimeFormat(time * 1000)); BufferedInputStream stream = new BufferedInputStream(checkpoints); CheckpointManager manager = new CheckpointManager(params, stream); StoredBlock checkpoint = manager.getCheckpointBefore(time); store.put(checkpoint); store.setChainHead(checkpoint); } }
StoredBlock cursor = blockStore.getChainHead(); if (cursor == null) {
@Override protected void rollbackBlockStore(int height) throws BlockStoreException { lock.lock(); try { int currentHeight = getBestChainHeight(); checkArgument(height >= 0 && height <= currentHeight, "Bad height: %s", height); if (height == currentHeight) return; // nothing to do // Look for the block we want to be the new chain head StoredBlock newChainHead = blockStore.getChainHead(); while (newChainHead.getHeight() > height) { newChainHead = newChainHead.getPrev(blockStore); if (newChainHead == null) throw new BlockStoreException("Unreachable height"); } // Modify store directly blockStore.put(newChainHead); this.setChainHead(newChainHead); } finally { lock.unlock(); } }
@Override protected StoredBlock addToBlockStore(StoredBlock storedPrev, Block blockHeader, TransactionOutputChanges txOutChanges) throws BlockStoreException, VerificationException { StoredBlock newBlock = storedPrev.build(blockHeader); blockStore.put(newBlock); return newBlock; }
@Override protected void doSetChainHead(StoredBlock chainHead) throws BlockStoreException { blockStore.setChainHead(chainHead); }
/** * <p>Convenience method that creates a CheckpointManager, loads the given data, gets the checkpoint for the given * time, then inserts it into the store and sets that to be the chain head. Useful when you have just created * a new store from scratch and want to use configure it all in one go.</p> * * <p>Note that time is adjusted backwards by a week to account for possible clock drift in the block headers.</p> */ public static void checkpoint(NetworkParameters params, InputStream checkpoints, BlockStore store, long time) throws IOException, BlockStoreException { checkNotNull(params); checkNotNull(store); checkArgument(!(store instanceof FullPrunedBlockStore), "You cannot use checkpointing with a full store."); time -= 86400 * 7; checkArgument(time > 0); log.info("Attempting to initialize a new block store with a checkpoint for time {} ({})", time, Utils.dateTimeFormat(time * 1000)); BufferedInputStream stream = new BufferedInputStream(checkpoints); CheckpointManager manager = new CheckpointManager(params, stream); StoredBlock checkpoint = manager.getCheckpointBefore(time); store.put(checkpoint); store.setChainHead(checkpoint); } }
/** Emulates receiving a valid block that builds on top of the chain. */ public static BlockPair createFakeBlock(BlockStore blockStore, long version, long timeSeconds, int height, Transaction... transactions) { try { return createFakeBlock(blockStore, blockStore.getChainHead(), version, timeSeconds, height, transactions); } catch (BlockStoreException e) { throw new RuntimeException(e); // Cannot happen. } }
@Override protected void rollbackBlockStore(int height) throws BlockStoreException { lock.lock(); try { int currentHeight = getBestChainHeight(); checkArgument(height >= 0 && height <= currentHeight, "Bad height: %s", height); if (height == currentHeight) return; // nothing to do // Look for the block we want to be the new chain head StoredBlock newChainHead = blockStore.getChainHead(); while (newChainHead.getHeight() > height) { newChainHead = newChainHead.getPrev(blockStore); if (newChainHead == null) throw new BlockStoreException("Unreachable height"); } // Modify store directly blockStore.put(newChainHead); this.setChainHead(newChainHead); } finally { lock.unlock(); } }
@Override protected StoredBlock addToBlockStore(StoredBlock storedPrev, Block blockHeader) throws BlockStoreException, VerificationException { StoredBlock newBlock = storedPrev.build(blockHeader); blockStore.put(newBlock); return newBlock; }
@Override protected void doSetChainHead(StoredBlock chainHead) throws BlockStoreException { blockStore.setChainHead(chainHead); }
@Override protected StoredBlock getStoredBlockInCurrentScope(Sha256Hash hash) throws BlockStoreException { return blockStore.get(hash); }
/** * <p>Convenience method that creates a CheckpointManager, loads the given data, gets the checkpoint for the given * time, then inserts it into the store and sets that to be the chain head. Useful when you have just created * a new store from scratch and want to use configure it all in one go.</p> * * <p>Note that time is adjusted backwards by a week to account for possible clock drift in the block headers.</p> */ public static void checkpoint(NetworkParameters params, InputStream checkpoints, BlockStore store, long time) throws IOException, BlockStoreException { checkNotNull(params); checkNotNull(store); checkArgument(!(store instanceof FullPrunedBlockStore), "You cannot use checkpointing with a full store."); time -= 86400 * 7; checkArgument(time > 0); log.info("Attempting to initialize a new block store with a checkpoint for time {} ({})", time, Utils.dateTimeFormat(time * 1000)); BufferedInputStream stream = new BufferedInputStream(checkpoints); CheckpointManager manager = new CheckpointManager(params, stream); StoredBlock checkpoint = manager.getCheckpointBefore(time); store.put(checkpoint); store.setChainHead(checkpoint); } }
/** * Constructs a BlockChain connected to the given list of listeners (eg, wallets) and a store. */ public AbstractBlockChain(Context context, List<? extends Wallet> wallets, BlockStore blockStore) throws BlockStoreException { this.blockStore = blockStore; chainHead = blockStore.getChainHead(); log.info("chain head is at height {}:\n{}", chainHead.getHeight(), chainHead.getHeader()); this.params = context.getParams(); this.newBestBlockListeners = new CopyOnWriteArrayList<>(); this.reorganizeListeners = new CopyOnWriteArrayList<>(); this.transactionReceivedListeners = new CopyOnWriteArrayList<>(); for (NewBestBlockListener l : wallets) addNewBestBlockListener(Threading.SAME_THREAD, l); for (ReorganizeListener l : wallets) addReorganizeListener(Threading.SAME_THREAD, l); for (TransactionReceivedInBlockListener l : wallets) addTransactionReceivedListener(Threading.SAME_THREAD, l); this.versionTally = new VersionTally(context.getParams()); this.versionTally.initialize(blockStore, chainHead); }
@Override protected void rollbackBlockStore(int height) throws BlockStoreException { lock.lock(); try { int currentHeight = getBestChainHeight(); checkArgument(height >= 0 && height <= currentHeight, "Bad height: %s", height); if (height == currentHeight) return; // nothing to do // Look for the block we want to be the new chain head StoredBlock newChainHead = blockStore.getChainHead(); while (newChainHead.getHeight() > height) { newChainHead = newChainHead.getPrev(blockStore); if (newChainHead == null) throw new BlockStoreException("Unreachable height"); } // Modify store directly blockStore.put(newChainHead); this.setChainHead(newChainHead); } finally { lock.unlock(); } }
private static void shutdown() { try { if (peers == null) return; // setup() never called so nothing to do. if (peers.isRunning()) peers.stop(); saveWallet(walletFile); store.close(); wallet = null; } catch (BlockStoreException e) { throw new RuntimeException(e); } }
StoredBlock chainHead = blockStore.getChainHead(); blockStoreBlockHeight = chainHead == null ? -2 : chainHead.getHeight(); blockStore.close(); } catch (BlockStoreException bse) { log.warn("Failed to close block store", bse);
@Override protected StoredBlock addToBlockStore(StoredBlock storedPrev, Block blockHeader) throws BlockStoreException, VerificationException { StoredBlock newBlock = storedPrev.build(blockHeader); blockStore.put(newBlock); return newBlock; }