Exemple #1
0
 public synchronized void put(StoredBlock block) throws BlockStoreException {
   try {
     Sha256Hash hash = block.getHeader().getHash();
     assert blockMap.get(hash) == null : "Attempt to insert duplicate";
     // Append to the end of the file. The other fields in StoredBlock will be recalculated when
     // it's reloaded.
     byte[] bytes = block.getHeader().bitcoinSerialize();
     stream.write(bytes);
     stream.flush();
     blockMap.put(hash, block);
   } catch (IOException e) {
     throw new BlockStoreException(e);
   }
 }
Exemple #2
0
 public synchronized void setChainHead(StoredBlock chainHead) throws BlockStoreException {
   try {
     this.chainHead = chainHead.getHeader().getHash();
     // Write out new hash to the first 32 bytes of the file past one (first byte is version
     // number).
     stream.getChannel().write(ByteBuffer.wrap(this.chainHead.getBytes()), 1);
   } catch (IOException e) {
     throw new BlockStoreException(e);
   }
 }
  @Override
  public void setChainHead(StoredBlock chainHead) throws BlockStoreException {
    final MappedByteBuffer buffer = this.buffer;
    if (buffer == null) throw new BlockStoreException("Store closed");

    lock.lock();
    try {
      lastChainHead = chainHead;
      byte[] headHash = chainHead.getHeader().getHash().getBytes();
      buffer.position(8);
      buffer.put(headHash);
    } 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()) {
        // 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();
    }
  }
Exemple #5
0
 private void createNewStore(NetworkParameters params, File file) throws BlockStoreException {
   // Create a new block store if the file wasn't found or anything went wrong whilst reading.
   blockMap.clear();
   try {
     stream = new FileOutputStream(file, false); // Do not append, create fresh.
     stream.write(1); // Version.
   } catch (IOException e1) {
     // We could not load a block store nor could we create a new one!
     throw new BlockStoreException(e1);
   }
   try {
     // Set up the genesis block. When we start out fresh, it is by definition the top of the
     // chain.
     Block genesis = params.genesisBlock.cloneAsHeader();
     StoredBlock storedGenesis = new StoredBlock(genesis, genesis.getWork(), 0);
     this.chainHead = storedGenesis.getHeader().getHash();
     stream.write(this.chainHead.getBytes());
     put(storedGenesis);
   } catch (VerificationException e1) {
     throw new RuntimeException(e1); // Cannot happen.
   } catch (IOException e) {
     throw new BlockStoreException(e);
   }
 }