@Override public StoredBlock getChainHead() throws BlockStoreException { final MappedByteBuffer buffer = this.buffer; if (buffer == null) throw new BlockStoreException("Store closed"); lock.lock(); try { if (lastChainHead == null) { byte[] headHash = new byte[32]; buffer.position(8); buffer.get(headHash); Sha256Hash hash = Sha256Hash.wrap(headHash); StoredBlock block = get(hash); if (block == null) throw new BlockStoreException("Corrupted block store: could not find chain head: " + hash); lastChainHead = block; } return lastChainHead; } finally { lock.unlock(); } }
@Test public void basics() throws Exception { NetworkParameters params = UnitTestParams.get(); File f = File.createTempFile("spvblockstore", null); f.delete(); f.deleteOnExit(); SPVBlockStore store = new SPVBlockStore(params, f); Address to = new ECKey().toAddress(params); // Check the first block in a new store is the genesis block. StoredBlock genesis = store.getChainHead(); assertEquals(params.getGenesisBlock(), genesis.getHeader()); assertEquals(0, genesis.getHeight()); // Build a new block. StoredBlock b1 = genesis.build(genesis.getHeader().createNextBlock(to).cloneAsHeader()); store.put(b1); store.setChainHead(b1); store.close(); // Check we can get it back out again if we rebuild the store object. store = new SPVBlockStore(params, f); StoredBlock b2 = store.get(b1.getHeader().getHash()); assertEquals(b1, b2); // Check the chain head was stored correctly also. StoredBlock chainHead = store.getChainHead(); assertEquals(b1, chainHead); } }
@Override public StoredBlock getChainHead() throws BlockStoreException { final MappedByteBuffer buffer = this.buffer; if (buffer == null) throw new BlockStoreException("Store closed"); lock.lock(); try { if (lastChainHead == null) { byte[] headHash = new byte[32]; buffer.position(8); buffer.get(headHash); Sha256Hash hash = Sha256Hash.wrap(headHash); StoredBlock block = get(hash); if (block == null) throw new BlockStoreException("Corrupted block store: could not find chain head: " + hash); lastChainHead = block; } return lastChainHead; } finally { lock.unlock(); } }
@Override public StoredBlock getChainHead() throws BlockStoreException { final MappedByteBuffer buffer = this.buffer; if (buffer == null) throw new BlockStoreException("Store closed"); lock.lock(); try { if (lastChainHead == null) { byte[] headHash = new byte[32]; buffer.position(8); buffer.get(headHash); Sha256Hash hash = Sha256Hash.wrap(headHash); StoredBlock block = get(hash); if (block == null) throw new BlockStoreException("Corrupted block store: could not find chain head: " + hash); lastChainHead = block; } return lastChainHead; } finally { lock.unlock(); } }
@Override public StoredBlock getChainHead() throws BlockStoreException { final MappedByteBuffer buffer = this.buffer; if (buffer == null) throw new BlockStoreException("Store closed"); lock.lock(); try { if (lastChainHead == null) { byte[] headHash = new byte[32]; buffer.position(8); buffer.get(headHash); Sha256Hash hash = Sha256Hash.wrap(headHash); StoredBlock block = get(hash); if (block == null) throw new BlockStoreException("Corrupted block store: could not find chain head: " + hash); lastChainHead = block; } return lastChainHead; } finally { lock.unlock(); } }