Class InMemoryStorage
- All Implemented Interfaces:
BlockchainStorage
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionintReturns the number of blocks currently in the store.voidRemoves all blocks from the in-memory store.booleanReturnstrueif a block with the given hash is present in the store.loadAll()Returns all stored blocks in ascending index order.loadBlock(int index) Retrieves a block by its zero-based index.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).voidPersists a block to the in-memory store.toString()Returns a human-readable description of this storage instance.
-
Constructor Details
-
InMemoryStorage
public InMemoryStorage()
-
-
Method Details
-
saveBlock
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:
saveBlockin interfaceBlockchainStorage- Parameters:
block- the block to persist (non-null)- Throws:
NullPointerException- ifblockis nullBlockValidationException- if storage fails unexpectedly
-
loadBlock
Retrieves a block by its zero-based index.- Specified by:
loadBlockin interfaceBlockchainStorage- Parameters:
index- block index (≥ 0)- Returns:
- the stored
Block - Throws:
NoSuchElementException- if no block exists atindex
-
loadBlockByHash
Searches for a block by its SHA-256 hash using a constant-time comparison to prevent hash timing attacks (NFR-SEC-03).- Specified by:
loadBlockByHashin interfaceBlockchainStorage- Parameters:
hash- hex-encoded block hash (non-null)- Returns:
- an
Optionalcontaining the matching block, or empty if absent - Throws:
NullPointerException- ifhashis null
-
loadAll
Returns all stored blocks in ascending index order.Because the backing store is a
TreeMapkeyed 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:
loadAllin interfaceBlockchainStorage- Returns:
- non-null, index-ordered list (maybe empty)
-
exists
Returnstrueif a block with the given hash is present in the store.Uses constant-time comparison to prevent timing side-channel attacks.
- Specified by:
existsin interfaceBlockchainStorage- Parameters:
hash- hex-encoded block hash (non-null)- Returns:
trueif a matching block exists- Throws:
NullPointerException- ifhashis null
-
chainHeight
public int chainHeight()Returns the number of blocks currently in the store.- Specified by:
chainHeightin interfaceBlockchainStorage- 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:
deleteAllin interfaceBlockchainStorage
-
toString
Returns a human-readable description of this storage instance.
-