Class TestKit

    • Field Detail

      • MAX_VALIDATOR_COUNT_WITH_ENABLED_TIME_SERVICE

        public static final short MAX_VALIDATOR_COUNT_WITH_ENABLED_TIME_SERVICE
        The maximum number of validators supported by TestKit when a time oracle is enabled. The time oracle does not work in a TestKit with a higher number of validators because the time oracle requires the majority of those validators to submit transactions with time updates, but only a single emulated node submits them.
        See Also:
        Constant Field Values
    • Method Detail

      • forService

        public static TestKit forService​(Class<? extends ServiceModule> serviceModule)
        Creates a TestKit network with a single validator node for a single service.
      • getService

        public <T extends Service> T getService​(short serviceId,
                                                Class<T> serviceClass)
        Returns an instance of a service with the given service id and service class. Only user-defined services can be requested, i.e., it is not possible to get an instance of a built-in service such as the time oracle.
        Returns:
        the service instance or null if there is no service with such id
        Throws:
        IllegalArgumentException - if the service with given id was not found or could not be cast to given class
      • createBlockWithTransactions

        public Block createBlockWithTransactions​(TransactionMessage... transactions)
        Creates a block with the given transaction(s). Transactions are applied in the lexicographical order of their hashes. In-pool transactions will be ignored.
        Returns:
        created block
        Throws:
        IllegalArgumentException - if transactions are malformed or don't belong to this service
      • createBlockWithTransactions

        public Block createBlockWithTransactions​(Iterable<TransactionMessage> transactions)
        Creates a block with the given transactions. Transactions are applied in the lexicographical order of their hashes. In-pool transactions will be ignored.
        Returns:
        created block
        Throws:
        IllegalArgumentException - if transactions are malformed or don't belong to this service
      • createBlock

        public Block createBlock()
        Creates a block with all in-pool transactions. Transactions are applied in the lexicographical order of their hashes.
        Returns:
        created block
      • getTransactionPool

        public List<TransactionMessage> getTransactionPool()
        Returns a list of in-pool transactions. Please note that the order of transactions in pool does not necessarily match the order in which the clients submitted the messages.
      • withSnapshot

        public void withSnapshot​(Consumer<Snapshot> snapshotFunction)
        Performs the given function with a snapshot of the current database state (i.e., the one that corresponds to the latest committed block). In-pool (not yet processed) transactions are also accessible with it in blockchain.

        This method destroys the snapshot once the passed closure completes, compared to getSnapshot(), which disposes created snapshots only when TestKit is closed.

        Parameters:
        snapshotFunction - a function to execute
        See Also:
        applySnapshot(Function)
      • applySnapshot

        public <ResultT> ResultT applySnapshot​(Function<Snapshot,​ResultT> snapshotFunction)
        Performs the given function with a snapshot of the current database state (i.e., the one that corresponds to the latest committed block) and returns a result of its execution. In-pool (not yet processed) transactions are also accessible with it in blockchain.

        This method destroys the snapshot once the passed closure completes, compared to getSnapshot(), which disposes created snapshots only when TestKit is closed.

        Consider using withSnapshot(Consumer) when returning the result of given function is not needed.

        Type Parameters:
        ResultT - a type the function returns
        Parameters:
        snapshotFunction - a function to execute
        Returns:
        the result of applying the given function to the database state
      • getSnapshot

        public Snapshot getSnapshot()
        Returns a snapshot of the current database state (i.e., the one that corresponds to the latest committed block). In-pool (not yet processed) transactions are also accessible with it in blockchain.

        All created snapshots are deleted when this TestKit is closed. It is forbidden to access the snapshots once the TestKit is closed.

        If you need to create a large number (e.g. more than a hundred) of snapshots, it is recommended to use withSnapshot(Consumer) or applySnapshot(Function), which destroy the snapshots once the passed closure completes.

      • getEmulatedNode

        public EmulatedNode getEmulatedNode()
        Returns the context of the node that the TestKit emulates (i.e., on which it instantiates and executes services).
      • builder

        public static TestKit.Builder builder()
        Creates a new builder for the TestKit. Note that this builder creates a single validator network by default.