Class InMemoryStorage

java.lang.Object
com.privatechain.storage.memory.InMemoryStorage
All Implemented Interfaces:
BlockchainStorage

public final class InMemoryStorage extends Object implements BlockchainStorage
HashMap-backed in-memory implementation of BlockchainStorage.

This implementation is intended for testing and demos only. All data is lost when the JVM exits. For durable storage use LevelDBStorage, RocksDBStorage, or FileSystemStorage.

Thread safety

Concurrent reads are allowed via a ReentrantReadWriteLock read lock. All writes acquire the exclusive write lock, serializing mutations as required by FR-STOR-06 and NFR-REL-01.

Usage


 BlockchainNode node = BlockchainConfig.builder()
     .storage(new InMemoryStorage())
     .build();
 
Since:
1.0.0
See Also:
  • Constructor Details

    • InMemoryStorage

      public InMemoryStorage()
  • Method Details

    • saveBlock

      public void saveBlock(Block block)
      Persists a block to the in-memory store.

      Saving the same block index twice overwrites the previous entry — this satisfies the idempotent contract of BlockchainStorage.saveBlock(com.privatechain.core.model.Block).

      Specified by:
      saveBlock in interface BlockchainStorage
      Parameters:
      block - the block to persist (non-null)
      Throws:
      NullPointerException - if block is null
      BlockValidationException - if storage fails unexpectedly
    • loadBlock

      public Block loadBlock(int index)
      Retrieves a block by its zero-based index.
      Specified by:
      loadBlock in interface BlockchainStorage
      Parameters:
      index - block index (≥ 0)
      Returns:
      the stored Block
      Throws:
      NoSuchElementException - if no block exists at index
    • loadBlockByHash

      public Optional<Block> loadBlockByHash(String hash)
      Searches for a block by its SHA-256 hash using a constant-time comparison to prevent hash timing attacks (NFR-SEC-03).
      Specified by:
      loadBlockByHash in interface BlockchainStorage
      Parameters:
      hash - hex-encoded block hash (non-null)
      Returns:
      an Optional containing the matching block, or empty if absent
      Throws:
      NullPointerException - if hash is null
    • loadAll

      public List<Block> loadAll()
      Returns all stored blocks in ascending index order.

      Because the backing store is a TreeMap keyed by block index, TreeMap.values() iterates in ascending key order by definition — no explicit sort is needed even when blocks were saved out of sequence.

      The returned list is a defensive copy; mutations do not affect the store.

      Specified by:
      loadAll in interface BlockchainStorage
      Returns:
      non-null, index-ordered list (maybe empty)
    • exists

      public boolean exists(String hash)
      Returns true if a block with the given hash is present in the store.

      Uses constant-time comparison to prevent timing side-channel attacks.

      Specified by:
      exists in interface BlockchainStorage
      Parameters:
      hash - hex-encoded block hash (non-null)
      Returns:
      true if a matching block exists
      Throws:
      NullPointerException - if hash is null
    • chainHeight

      public int chainHeight()
      Returns the number of blocks currently in the store.
      Specified by:
      chainHeight in interface BlockchainStorage
      Returns:
      block count (≥ 0)
    • deleteAll

      public void deleteAll()
      Removes all blocks from the in-memory store.

      Warning: This operation is irreversible and intended for testing / chain-reset scenarios only.

      Specified by:
      deleteAll in interface BlockchainStorage
    • toString

      public String toString()
      Returns a human-readable description of this storage instance.
      Overrides:
      toString in class Object
      Returns:
      string representation