protected void putUpdateStoredBlock(StoredBlock storedBlock, boolean wasUndoable) { // We put as one record as then the get is much faster. if (instrument) beginMethod("putUpdateStoredBlock"); Sha256Hash hash = storedBlock.getHeader().getHash(); ByteBuffer bb = ByteBuffer.allocate(97); storedBlock.serializeCompact(bb); bb.put((byte) (wasUndoable ? 1 : 0)); batchPut(getKey(KeyType.HEADERS_ALL, hash), bb.array()); if (instrument) endMethod("putUpdateStoredBlock"); }
protected void putUpdateStoredBlock(StoredBlock storedBlock, boolean wasUndoable) { // We put as one record as then the get is much faster. if (instrument) beginMethod("putUpdateStoredBlock"); Sha256Hash hash = storedBlock.getHeader().getHash(); ByteBuffer bb = ByteBuffer.allocate(97); storedBlock.serializeCompact(bb); bb.put((byte) (wasUndoable ? 1 : 0)); batchPut(getKey(KeyType.HEADERS_ALL, hash), bb.array()); if (instrument) endMethod("putUpdateStoredBlock"); }
void removeUndoableBlocksWhereHeightIsLessThan(int height) { if (height < 0) return; DBIterator iterator = db.iterator(); ByteBuffer keyBuf = ByteBuffer.allocate(5); keyBuf.put((byte) KeyType.HEIGHT_UNDOABLEBLOCKS.ordinal()); keyBuf.putInt(height); for (iterator.seek(keyBuf.array()); iterator.hasNext(); iterator.next()) { byte[] bytekey = iterator.peekNext().getKey(); ByteBuffer buff = ByteBuffer.wrap(bytekey); buff.get(); // Just remove byte from buffer. int keyHeight = buff.getInt(); byte[] hashbytes = new byte[32]; buff.get(hashbytes, 4, 28); if (keyHeight > height) break; batchDelete(getKey(KeyType.UNDOABLEBLOCKS_ALL, hashbytes)); batchDelete(bytekey); } try { iterator.close(); } catch (IOException e) { log.error("Error closing iterator", e); } }
protected void putUpdateStoredBlock(StoredBlock storedBlock, boolean wasUndoable) { // We put as one record as then the get is much faster. if (instrument) beginMethod("putUpdateStoredBlock"); Sha256Hash hash = storedBlock.getHeader().getHash(); ByteBuffer bb = ByteBuffer.allocate(97); storedBlock.serializeCompact(bb); bb.put((byte) (wasUndoable ? 1 : 0)); batchPut(getKey(KeyType.HEADERS_ALL, hash), bb.array()); if (instrument) endMethod("putUpdateStoredBlock"); }
protected void putUpdateStoredBlock(StoredBlock storedBlock, boolean wasUndoable) { // We put as one record as then the get is much faster. if (instrument) beginMethod("putUpdateStoredBlock"); Sha256Hash hash = storedBlock.getHeader().getHash(); ByteBuffer bb = ByteBuffer.allocate(97); storedBlock.serializeCompact(bb); bb.put((byte) (wasUndoable ? 1 : 0)); batchPut(getKey(KeyType.HEADERS_ALL, hash), bb.array()); if (instrument) endMethod("putUpdateStoredBlock"); }
private void initFromDb() throws BlockStoreException { Sha256Hash hash = Sha256Hash.wrap(batchGet(getKey(KeyType.CHAIN_HEAD_SETTING))); this.chainHeadBlock = get(hash); this.chainHeadHash = hash; if (this.chainHeadBlock == null) { throw new BlockStoreException("corrupt database block store - head block not found"); } hash = Sha256Hash.wrap(batchGet(getKey(KeyType.VERIFIED_CHAIN_HEAD_SETTING))); this.verifiedChainHeadBlock = get(hash); this.verifiedChainHeadHash = hash; if (this.verifiedChainHeadBlock == null) { throw new BlockStoreException("corrupt databse block store - verified head block not found"); } }
private void initFromDb() throws BlockStoreException { Sha256Hash hash = Sha256Hash.wrap(batchGet(getKey(KeyType.CHAIN_HEAD_SETTING))); this.chainHeadBlock = get(hash); this.chainHeadHash = hash; if (this.chainHeadBlock == null) { throw new BlockStoreException("corrupt database block store - head block not found"); } hash = Sha256Hash.wrap(batchGet(getKey(KeyType.VERIFIED_CHAIN_HEAD_SETTING))); this.verifiedChainHeadBlock = get(hash); this.verifiedChainHeadHash = hash; if (this.verifiedChainHeadBlock == null) { throw new BlockStoreException("corrupt databse block store - verified head block not found"); } }
private void initFromDb() throws BlockStoreException { Sha256Hash hash = Sha256Hash.wrap(batchGet(getKey(KeyType.CHAIN_HEAD_SETTING))); this.chainHeadBlock = get(hash); this.chainHeadHash = hash; if (this.chainHeadBlock == null) { throw new BlockStoreException("corrupt database block store - head block not found"); } hash = Sha256Hash.wrap(batchGet(getKey(KeyType.VERIFIED_CHAIN_HEAD_SETTING))); this.verifiedChainHeadBlock = get(hash); this.verifiedChainHeadHash = hash; if (this.verifiedChainHeadBlock == null) { throw new BlockStoreException("corrupt databse block store - verified head block not found"); } }
private void initFromDb() throws BlockStoreException { Sha256Hash hash = Sha256Hash.wrap(batchGet(getKey(KeyType.CHAIN_HEAD_SETTING))); this.chainHeadBlock = get(hash); this.chainHeadHash = hash; if (this.chainHeadBlock == null) { throw new BlockStoreException("corrupt database block store - head block not found"); } hash = Sha256Hash.wrap(batchGet(getKey(KeyType.VERIFIED_CHAIN_HEAD_SETTING))); this.verifiedChainHeadBlock = get(hash); this.verifiedChainHeadHash = hash; if (this.verifiedChainHeadBlock == null) { throw new BlockStoreException("corrupt databse block store - verified head block not found"); } }
@Override public void setChainHead(StoredBlock chainHead) throws BlockStoreException { if (instrument) beginMethod("setChainHead"); Sha256Hash hash = chainHead.getHeader().getHash(); this.chainHeadHash = hash; this.chainHeadBlock = chainHead; batchPut(getKey(KeyType.CHAIN_HEAD_SETTING), hash.getBytes()); if (instrument) endMethod("setChainHead"); }
@Override public void setChainHead(StoredBlock chainHead) throws BlockStoreException { if (instrument) beginMethod("setChainHead"); Sha256Hash hash = chainHead.getHeader().getHash(); this.chainHeadHash = hash; this.chainHeadBlock = chainHead; batchPut(getKey(KeyType.CHAIN_HEAD_SETTING), hash.getBytes()); if (instrument) endMethod("setChainHead"); }
@Override public void setChainHead(StoredBlock chainHead) throws BlockStoreException { if (instrument) beginMethod("setChainHead"); Sha256Hash hash = chainHead.getHeader().getHash(); this.chainHeadHash = hash; this.chainHeadBlock = chainHead; batchPut(getKey(KeyType.CHAIN_HEAD_SETTING), hash.getBytes()); if (instrument) endMethod("setChainHead"); }
private void openDB() { Options options = new Options(); options.createIfMissing(true); // options.compressionType(CompressionType.NONE); options.cacheSize(leveldbReadCache); options.writeBufferSize(leveldbWriteCache); options.maxOpenFiles(10000); // options.blockSize(1024*1024*50); try { db = factory.open(new File(filename), options); } catch (IOException e) { throw new RuntimeException("Can not open DB", e); } utxoCache = new LRUCache(openOutCache, 0.75f); try { if (batchGet(getKey(KeyType.CREATED)) == null) { createNewStore(params); } else { initFromDb(); } } catch (BlockStoreException e) { throw new RuntimeException("Can not init/load db", e); } }
@Override public void setChainHead(StoredBlock chainHead) throws BlockStoreException { if (instrument) beginMethod("setChainHead"); Sha256Hash hash = chainHead.getHeader().getHash(); this.chainHeadHash = hash; this.chainHeadBlock = chainHead; batchPut(getKey(KeyType.CHAIN_HEAD_SETTING), hash.getBytes()); if (instrument) endMethod("setChainHead"); }
private void openDB() { Options options = new Options(); options.createIfMissing(true); // options.compressionType(CompressionType.NONE); options.cacheSize(leveldbReadCache); options.writeBufferSize(leveldbWriteCache); options.maxOpenFiles(10000); // options.blockSize(1024*1024*50); try { db = factory.open(new File(filename), options); } catch (IOException e) { throw new RuntimeException("Can not open DB", e); } utxoCache = new LRUCache(openOutCache, 0.75f); try { if (batchGet(getKey(KeyType.CREATED)) == null) { createNewStore(params); } else { initFromDb(); } } catch (BlockStoreException e) { throw new RuntimeException("Can not init/load db", e); } }
private void createNewStore(NetworkParameters params) throws BlockStoreException { try { // Set up the genesis block. When we start out fresh, it is by // definition the top of the chain. StoredBlock storedGenesisHeader = new StoredBlock(params.getGenesisBlock().cloneAsHeader(), params.getGenesisBlock().getWork(), 0); // The coinbase in the genesis block is not spendable. This is // because of how the reference client inits // its database - the genesis transaction isn't actually in the db // so its spent flags can never be updated. List<Transaction> genesisTransactions = Lists.newLinkedList(); StoredUndoableBlock storedGenesis = new StoredUndoableBlock(params.getGenesisBlock().getHash(), genesisTransactions); beginDatabaseBatchWrite(); put(storedGenesisHeader, storedGenesis); setChainHead(storedGenesisHeader); setVerifiedChainHead(storedGenesisHeader); batchPut(getKey(KeyType.CREATED), bytes("done")); commitDatabaseBatchWrite(); } catch (VerificationException e) { throw new RuntimeException(e); // Cannot happen. } }
@Override public void setVerifiedChainHead(StoredBlock chainHead) throws BlockStoreException { if (instrument) beginMethod("setVerifiedChainHead"); Sha256Hash hash = chainHead.getHeader().getHash(); this.verifiedChainHeadHash = hash; this.verifiedChainHeadBlock = chainHead; batchPut(getKey(KeyType.VERIFIED_CHAIN_HEAD_SETTING), hash.getBytes()); if (this.chainHeadBlock.getHeight() < chainHead.getHeight()) setChainHead(chainHead); removeUndoableBlocksWhereHeightIsLessThan(chainHead.getHeight() - fullStoreDepth); if (instrument) endMethod("setVerifiedChainHead"); }
@Override public void setVerifiedChainHead(StoredBlock chainHead) throws BlockStoreException { if (instrument) beginMethod("setVerifiedChainHead"); Sha256Hash hash = chainHead.getHeader().getHash(); this.verifiedChainHeadHash = hash; this.verifiedChainHeadBlock = chainHead; batchPut(getKey(KeyType.VERIFIED_CHAIN_HEAD_SETTING), hash.getBytes()); if (this.chainHeadBlock.getHeight() < chainHead.getHeight()) setChainHead(chainHead); removeUndoableBlocksWhereHeightIsLessThan(chainHead.getHeight() - fullStoreDepth); if (instrument) endMethod("setVerifiedChainHead"); }
@Override public void setVerifiedChainHead(StoredBlock chainHead) throws BlockStoreException { if (instrument) beginMethod("setVerifiedChainHead"); Sha256Hash hash = chainHead.getHeader().getHash(); this.verifiedChainHeadHash = hash; this.verifiedChainHeadBlock = chainHead; batchPut(getKey(KeyType.VERIFIED_CHAIN_HEAD_SETTING), hash.getBytes()); if (this.chainHeadBlock.getHeight() < chainHead.getHeight()) setChainHead(chainHead); removeUndoableBlocksWhereHeightIsLessThan(chainHead.getHeight() - fullStoreDepth); if (instrument) endMethod("setVerifiedChainHead"); }
@Override public void setVerifiedChainHead(StoredBlock chainHead) throws BlockStoreException { if (instrument) beginMethod("setVerifiedChainHead"); Sha256Hash hash = chainHead.getHeader().getHash(); this.verifiedChainHeadHash = hash; this.verifiedChainHeadBlock = chainHead; batchPut(getKey(KeyType.VERIFIED_CHAIN_HEAD_SETTING), hash.getBytes()); if (this.chainHeadBlock.getHeight() < chainHead.getHeight()) setChainHead(chainHead); removeUndoableBlocksWhereHeightIsLessThan(chainHead.getHeight() - fullStoreDepth); if (instrument) endMethod("setVerifiedChainHead"); }