Class Blockchain
- java.lang.Object
-
- com.exonum.binding.core.blockchain.Blockchain
-
public final class Blockchain extends Object
Provides read-only access to the subset of blockchain::Schema features in the Core API: blocks, transaction messages, execution results.All method arguments are non-null by default.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleancontainsBlock(Block block)Returns true if the blockchain contains exactly the same block as the passed value; false if it does not contain such block.Optional<Block>findBlock(HashCode blockHash)Returns a block object for given block hash.StoredConfigurationgetActualConfiguration()Returns the configuration for the latest height of the blockchain, including services and their parameters.BlockgetBlock(long height)Returns the block at the given height.ListIndex<HashCode>getBlockHashes()Returns a list of all block hashes, indexed by the block height.MapIndex<HashCode,Block>getBlocks()Returns a map that stores a block object for every block hash.ProofListIndexProxy<HashCode>getBlockTransactions(long height)Returns a proof list of transaction hashes committed in the block at the given height.ProofListIndexProxy<HashCode>getBlockTransactions(HashCode blockId)Returns a proof list of transaction hashes committed in the block with the given id.ProofListIndexProxy<HashCode>getBlockTransactions(Block block)Returns a proof list of transaction hashes committed in the given block.longgetHeight()Returns the blockchain height which is the height of the latest committed block in the blockchain.BlockgetLastBlock()Returns the latest committed block.KeySetIndexProxy<HashCode>getTransactionPool()Returns a set of uncommitted (in-pool) transaction hashes; empty in case of no transactions.Optional<TransactionLocation>getTxLocation(HashCode messageHash)Returns transaction position inside the blockchain for given message hash.MapIndex<HashCode,TransactionLocation>getTxLocations()Returns a map that keeps the transaction position inside the blockchain for every transaction hash.MapIndex<HashCode,TransactionMessage>getTxMessages()Returns a map of transaction messages identified by their SHA-256 hashes.Optional<TransactionResult>getTxResult(HashCode messageHash)Returns a transaction execution result for given message hash.ProofMapIndexProxy<HashCode,TransactionResult>getTxResults()Returns a map with a key-value pair of a transaction hash and execution result.static BlockchainnewInstance(View view)Constructs a new blockchain instance for the given database view.
-
-
-
Method Detail
-
newInstance
public static Blockchain newInstance(View view)
Constructs a new blockchain instance for the given database view.
-
containsBlock
public boolean containsBlock(Block block)
Returns true if the blockchain contains exactly the same block as the passed value; false if it does not contain such block. Please note that all block fields are compared, not only its hash.- Parameters:
block- a value to check for presence in the blockchain
-
getHeight
public long getHeight()
Returns the blockchain height which is the height of the latest committed block in the blockchain. The block height is a distance between the last block and the "genesis", or initial, block. Therefore, the blockchain height is equal to the number of blocks plus one.For example, the "genesis" block has height
h = 0. The latest committed block has heighth = getBlockHashes().size() - 1.- Throws:
RuntimeException- if the "genesis block" was not created
-
getBlockHashes
public ListIndex<HashCode> getBlockHashes()
Returns a list of all block hashes, indexed by the block height. For example, the "genesis block" will be at index 0, the block at heighth = 10— at index 10. The last committed block will be at heighth = getBlockHashes().size() - 1.
-
getBlockTransactions
public ProofListIndexProxy<HashCode> getBlockTransactions(long height)
Returns a proof list of transaction hashes committed in the block at the given height.- Parameters:
height- block height starting from 0- Throws:
IllegalArgumentException- if the height is invalid: negative or exceeding the blockchain height
-
getBlockTransactions
public ProofListIndexProxy<HashCode> getBlockTransactions(HashCode blockId)
Returns a proof list of transaction hashes committed in the block with the given id.- Parameters:
blockId- id of the block- Throws:
IllegalArgumentException- if there is no block with given id
-
getBlockTransactions
public ProofListIndexProxy<HashCode> getBlockTransactions(Block block)
Returns a proof list of transaction hashes committed in the given block. The given block must match exactly the block that is stored in the database.- Parameters:
block- block of which list of transaction hashes should be returned- Throws:
IllegalArgumentException- if there is no such block in the blockchain
-
getTxMessages
public MapIndex<HashCode,TransactionMessage> getTxMessages()
Returns a map of transaction messages identified by their SHA-256 hashes. Both committed and in-pool (not yet processed) transactions are returned.
-
getTxResults
public ProofMapIndexProxy<HashCode,TransactionResult> getTxResults()
Returns a map with a key-value pair of a transaction hash and execution result.
-
getTxResult
public Optional<TransactionResult> getTxResult(HashCode messageHash)
Returns a transaction execution result for given message hash.- Returns:
- a transaction execution result, or
Optional.empty()if this transaction is unknown or was not yet executed
-
getTxLocations
public MapIndex<HashCode,TransactionLocation> getTxLocations()
Returns a map that keeps the transaction position inside the blockchain for every transaction hash.
-
getTxLocation
public Optional<TransactionLocation> getTxLocation(HashCode messageHash)
Returns transaction position inside the blockchain for given message hash.- Returns:
- a transaction execution result, or
Optional.empty()if this transaction is unknown or was not yet executed
-
getBlocks
public MapIndex<HashCode,Block> getBlocks()
Returns a map that stores a block object for every block hash.
-
getBlock
public Block getBlock(long height)
Returns the block at the given height.- Parameters:
height- the height of the block; must be non-negative and less than or equal to the current blockchain height- Returns:
- a block at the height
- Throws:
IndexOutOfBoundsException- if the height is not valid
-
findBlock
public Optional<Block> findBlock(HashCode blockHash)
Returns a block object for given block hash.- Returns:
- a corresponding block, or
Optional.empty()if there is no block with given block hash
-
getLastBlock
public Block getLastBlock()
Returns the latest committed block.- Throws:
RuntimeException- if the "genesis block" was not created
-
getActualConfiguration
public StoredConfiguration getActualConfiguration()
Returns the configuration for the latest height of the blockchain, including services and their parameters.- Throws:
RuntimeException- if the "genesis block" was not created
-
getTransactionPool
public KeySetIndexProxy<HashCode> getTransactionPool()
Returns a set of uncommitted (in-pool) transaction hashes; empty in case of no transactions. Note that this pool represents the state as of the current snapshot, and its state is volatile even between block commits.- See Also:
- Pool of Unconfirmed Transactions
-
-