Class Blockchain


  • public final class Blockchain
    extends java.lang.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
      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.
      java.util.Optional<Block> findBlock​(com.exonum.binding.common.hash.HashCode blockHash)
      Returns a block object for given block hash.
      com.exonum.binding.common.configuration.StoredConfiguration getActualConfiguration()
      Returns the configuration for the latest height of the blockchain, including services and their parameters.
      Block getBlock​(long height)
      Returns the block at the given height.
      ListIndex<com.exonum.binding.common.hash.HashCode> getBlockHashes()
      Returns a list of all block hashes, indexed by the block height.
      MapIndex<com.exonum.binding.common.hash.HashCode,​Block> getBlocks()
      Returns a map that stores a block object for every block hash.
      ProofListIndexProxy<com.exonum.binding.common.hash.HashCode> getBlockTransactions​(long height)
      Returns a proof list of transaction hashes committed in the block at the given height.
      ProofListIndexProxy<com.exonum.binding.common.hash.HashCode> getBlockTransactions​(Block block)
      Returns a proof list of transaction hashes committed in the given block.
      ProofListIndexProxy<com.exonum.binding.common.hash.HashCode> getBlockTransactions​(com.exonum.binding.common.hash.HashCode blockId)
      Returns a proof list of transaction hashes committed in the block with the given id.
      long getHeight()
      Returns the blockchain height which is the height of the latest committed block in the blockchain.
      Block getLastBlock()
      Returns the latest committed block.
      java.util.Optional<com.exonum.binding.common.blockchain.TransactionLocation> getTxLocation​(com.exonum.binding.common.hash.HashCode messageHash)
      Returns transaction position inside the blockchain for given message hash.
      MapIndex<com.exonum.binding.common.hash.HashCode,​com.exonum.binding.common.blockchain.TransactionLocation> getTxLocations()
      Returns a map that keeps the transaction position inside the blockchain for every transaction hash.
      MapIndex<com.exonum.binding.common.hash.HashCode,​com.exonum.binding.common.message.TransactionMessage> getTxMessages()
      Returns a map of transaction messages identified by their SHA-256 hashes.
      java.util.Optional<com.exonum.binding.common.blockchain.TransactionResult> getTxResult​(com.exonum.binding.common.hash.HashCode messageHash)
      Returns a transaction execution result for given message hash.
      ProofMapIndexProxy<com.exonum.binding.common.hash.HashCode,​com.exonum.binding.common.blockchain.TransactionResult> getTxResults()
      Returns a map with a key-value pair of a transaction hash and execution result.
      static Blockchain newInstance​(View view)
      Constructs a new blockchain instance for the given database view.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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 height h = getBlockHashes().size() - 1.

        Throws:
        java.lang.RuntimeException - if the "genesis block" was not created
      • getBlockHashes

        public ListIndex<com.exonum.binding.common.hash.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 height h = 10 — at index 10. The last committed block will be at height h = getBlockHashes().size() - 1.
      • getBlockTransactions

        public ProofListIndexProxy<com.exonum.binding.common.hash.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:
        java.lang.IllegalArgumentException - if the height is invalid: negative or exceeding the blockchain height
      • getBlockTransactions

        public ProofListIndexProxy<com.exonum.binding.common.hash.HashCode> getBlockTransactions​(com.exonum.binding.common.hash.HashCode blockId)
        Returns a proof list of transaction hashes committed in the block with the given id.
        Parameters:
        blockId - id of the block
        Throws:
        java.lang.IllegalArgumentException - if there is no block with given id
      • getBlockTransactions

        public ProofListIndexProxy<com.exonum.binding.common.hash.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:
        java.lang.IllegalArgumentException - if there is no such block in the blockchain
      • getTxMessages

        public MapIndex<com.exonum.binding.common.hash.HashCode,​com.exonum.binding.common.message.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<com.exonum.binding.common.hash.HashCode,​com.exonum.binding.common.blockchain.TransactionResult> getTxResults()
        Returns a map with a key-value pair of a transaction hash and execution result.
      • getTxResult

        public java.util.Optional<com.exonum.binding.common.blockchain.TransactionResult> getTxResult​(com.exonum.binding.common.hash.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<com.exonum.binding.common.hash.HashCode,​com.exonum.binding.common.blockchain.TransactionLocation> getTxLocations()
        Returns a map that keeps the transaction position inside the blockchain for every transaction hash.
      • getTxLocation

        public java.util.Optional<com.exonum.binding.common.blockchain.TransactionLocation> getTxLocation​(com.exonum.binding.common.hash.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<com.exonum.binding.common.hash.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:
        java.lang.IndexOutOfBoundsException - if the height is not valid
      • findBlock

        public java.util.Optional<Block> findBlock​(com.exonum.binding.common.hash.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:
        java.lang.RuntimeException - if the "genesis block" was not created
      • getActualConfiguration

        public com.exonum.binding.common.configuration.StoredConfiguration getActualConfiguration()
        Returns the configuration for the latest height of the blockchain, including services and their parameters.
        Throws:
        java.lang.RuntimeException - if the "genesis block" was not created