/** * Override this to use a {@link BlockStore} that isn't the default of {@link SPVBlockStore}. */ protected BlockStore provideBlockStore(File file) throws BlockStoreException { return new SPVBlockStore(params, file); }
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 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 void put(StoredBlock block) throws BlockStoreException { final MappedByteBuffer buffer = this.buffer; if (buffer == null) throw new BlockStoreException("Store closed"); lock.lock(); try { int cursor = getRingCursor(buffer); if (cursor == getFileSize()) { // Wrapped around. cursor = FILE_PROLOGUE_BYTES; } buffer.position(cursor); Sha256Hash hash = block.getHeader().getHash(); notFoundCache.remove(hash); buffer.put(hash.getBytes()); block.serializeCompact(buffer); setRingCursor(buffer, buffer.position()); blockCache.put(hash, block); } finally { lock.unlock(); } }
blockStore = new SPVBlockStore(Constants.NETWORK_PARAMETERS, blockChainFile); blockStore.getChainHead(); // detect corruptions as early as possible
int cursor = getRingCursor(buffer); final int startingPoint = cursor; final int fileSize = getFileSize(capacity); final byte[] targetHashBytes = hash.getBytes(); byte[] scratch = new byte[32];
long fileSize = getFileSize(capacity); if (!exists) { log.info("Creating new SPV block chain file " + file); throw new BlockStoreException("Header bytes do not equal " + HEADER_MAGIC); } else { initNewStore(params);
@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 void put(StoredBlock block) throws BlockStoreException { final MappedByteBuffer buffer = this.buffer; if (buffer == null) throw new BlockStoreException("Store closed"); lock.lock(); try { int cursor = getRingCursor(buffer); if (cursor == getFileSize(capacity)) { // Wrapped around. cursor = FILE_PROLOGUE_BYTES; } buffer.position(cursor); Sha256Hash hash = block.getHeader().getHash(); notFoundCache.remove(hash); buffer.put(hash.getBytes()); block.serializeCompact(buffer); setRingCursor(buffer, buffer.position()); blockCache.put(hash, block); } finally { lock.unlock(); } }
int cursor = getRingCursor(buffer); final int startingPoint = cursor; final int fileSize = getFileSize(); final byte[] targetHashBytes = hash.getBytes(); byte[] scratch = new byte[32];
long fileSize = getFileSize(); if (!exists) { log.info("Creating new SPV block chain file " + file); throw new BlockStoreException("Header bytes do not equal " + HEADER_MAGIC); } else { initNewStore(params);
@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 this to use a {@link BlockStore} that isn't the default of {@link SPVBlockStore}. */ protected BlockStore provideBlockStore(File file) throws BlockStoreException { return new SPVBlockStore(params, file); }
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); }
@Override public void put(StoredBlock block) throws BlockStoreException { final MappedByteBuffer buffer = this.buffer; if (buffer == null) throw new BlockStoreException("Store closed"); lock.lock(); try { int cursor = getRingCursor(buffer); if (cursor == getFileSize(capacity)) { // Wrapped around. cursor = FILE_PROLOGUE_BYTES; } buffer.position(cursor); Sha256Hash hash = block.getHeader().getHash(); notFoundCache.remove(hash); buffer.put(hash.getBytes()); block.serializeCompact(buffer); setRingCursor(buffer, buffer.position()); blockCache.put(hash, block); } finally { lock.unlock(); } }
int cursor = getRingCursor(buffer); final int startingPoint = cursor; final int fileSize = getFileSize(capacity); final byte[] targetHashBytes = hash.getBytes(); byte[] scratch = new byte[32];
long fileSize = getFileSize(capacity); if (!exists) { log.info("Creating new SPV block chain file " + file); throw new BlockStoreException("Header bytes do not equal " + HEADER_MAGIC); } else { initNewStore(params);
@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 this to use a {@link BlockStore} that isn't the default of {@link SPVBlockStore}. */ protected BlockStore provideBlockStore(File file) throws BlockStoreException { return new SPVBlockStore(params, file); }
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); }