@Override public synchronized StoredBlock getChainHead() throws BlockStoreException { return get(Sha256Hash.wrap(db.get(CHAIN_HEAD_KEY))); }
private synchronized void tryOpen(File directory, DBFactory dbFactory, Options options) throws IOException, BlockStoreException { db = dbFactory.open(directory, options); initStoreIfNeeded(); }
/** * Override this to use a {@link BlockStore} that isn't the default of {@link SPVBlockStore}. */ protected BlockStore provideBlockStore(File file) throws BlockStoreException { return new LevelDBBlockStore(context, file); } }
LevelDBBlockStore store = new LevelDBBlockStore(context, f); store.reset(); StoredBlock genesis = store.getChainHead(); assertEquals(params.getGenesisBlock(), genesis.getHeader()); assertEquals(0, genesis.getHeight()); store.put(b1); store.setChainHead(b1); store.close(); store = new LevelDBBlockStore(context, f); try { StoredBlock b2 = store.get(b1.getHeader().getHash()); assertEquals(b1, b2); StoredBlock chainHead = store.getChainHead(); assertEquals(b1, chainHead); } finally { store.close(); store.destroy();
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); }
/** Creates a LevelDB SPV block store using the given factory, which is useful if you want a pure Java version. */ public LevelDBBlockStore(Context context, File directory, DBFactory dbFactory) throws BlockStoreException { this.context = context; this.path = directory; Options options = new Options(); options.createIfMissing(); try { tryOpen(directory, dbFactory, options); } catch (IOException e) { try { dbFactory.repair(directory, options); tryOpen(directory, dbFactory, options); } catch (IOException e1) { throw new BlockStoreException(e1); } } }
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); }
/** Creates a LevelDB SPV block store using the given factory, which is useful if you want a pure Java version. */ public LevelDBBlockStore(Context context, File directory, DBFactory dbFactory) throws BlockStoreException { this.context = context; this.path = directory; Options options = new Options(); options.createIfMissing(); try { tryOpen(directory, dbFactory, options); } catch (IOException e) { try { dbFactory.repair(directory, options); tryOpen(directory, dbFactory, options); } catch (IOException e1) { throw new BlockStoreException(e1); } } }
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 tryOpen(File directory, DBFactory dbFactory, Options options) throws IOException, BlockStoreException { db = dbFactory.open(directory, options); initStoreIfNeeded(); }
@Override public synchronized StoredBlock getChainHead() throws BlockStoreException { return get(Sha256Hash.wrap(db.get(CHAIN_HEAD_KEY))); }
/** Creates a LevelDB SPV block store using the given factory, which is useful if you want a pure Java version. */ public LevelDBBlockStore(Context context, File directory, DBFactory dbFactory) throws BlockStoreException { this.context = context; this.path = directory; Options options = new Options(); options.createIfMissing(); try { tryOpen(directory, dbFactory, options); } catch (IOException e) { try { dbFactory.repair(directory, options); tryOpen(directory, dbFactory, options); } catch (IOException e1) { throw new BlockStoreException(e1); } } }
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 tryOpen(File directory, DBFactory dbFactory, Options options) throws IOException, BlockStoreException { db = dbFactory.open(directory, options); initStoreIfNeeded(); }
@Override public synchronized StoredBlock getChainHead() throws BlockStoreException { return get(Sha256Hash.wrap(db.get(CHAIN_HEAD_KEY))); }
/** Creates a LevelDB SPV block store using the given factory, which is useful if you want a pure Java version. */ public LevelDBBlockStore(Context context, File directory, DBFactory dbFactory) throws BlockStoreException { this.context = context; this.path = directory; Options options = new Options(); options.createIfMissing(); try { tryOpen(directory, dbFactory, options); } catch (IOException e) { try { dbFactory.repair(directory, options); tryOpen(directory, dbFactory, options); } catch (IOException e1) { throw new BlockStoreException(e1); } } }
private synchronized void tryOpen(File directory, DBFactory dbFactory, Options options) throws IOException, BlockStoreException { db = dbFactory.open(directory, options); initStoreIfNeeded(); }
@Override public synchronized StoredBlock getChainHead() throws BlockStoreException { return get(Sha256Hash.wrap(db.get(CHAIN_HEAD_KEY))); }
/** Erases the contents of the database (but NOT the underlying files themselves) and then reinitialises with the genesis block. */ public synchronized void reset() throws BlockStoreException { try { WriteBatch batch = db.createWriteBatch(); try { DBIterator it = db.iterator(); try { it.seekToFirst(); while (it.hasNext()) batch.delete(it.next().getKey()); db.write(batch); } finally { it.close(); } } finally { batch.close(); } initStoreIfNeeded(); } catch (IOException e) { throw new BlockStoreException(e); } }
/** Erases the contents of the database (but NOT the underlying files themselves) and then reinitialises with the genesis block. */ public synchronized void reset() throws BlockStoreException { try { WriteBatch batch = db.createWriteBatch(); try { DBIterator it = db.iterator(); try { it.seekToFirst(); while (it.hasNext()) batch.delete(it.next().getKey()); db.write(batch); } finally { it.close(); } } finally { batch.close(); } initStoreIfNeeded(); } catch (IOException e) { throw new BlockStoreException(e); } }