Class Context


  • public class Context
    extends java.lang.Object

    The Context object holds various objects and pieces of configuration that are scoped to a specific instantiation of bitcoinj for a specific network. You can get an instance of this class through calling get().

    Context is new in 0.13 and the library is currently in a transitional period: you should create a Context that wraps your chosen network parameters before using the rest of the library. However if you don't, things will still work as a Context will be created for you and stashed in thread local storage. The context is then propagated between library created threads as needed. This automagical propagation and creation is a temporary mechanism: one day it will be removed to avoid confusing edge cases that could occur if the developer does not fully understand it e.g. in the case where multiple instances of the library are in use simultaneously.

    • Field Detail

    • Constructor Detail

      • Context

        public Context()
        Creates a new context object. For now, this will be done for you by the framework. Eventually you will be expected to do this yourself in the same manner as fetching a NetworkParameters object (at the start of your app).
      • Context

        @Deprecated
        public Context​(NetworkParameters params)
        Deprecated.
        Note that NetworkParameters have been removed from this class. Thus, this constructor just swallows them.
      • Context

        public Context​(int eventHorizon,
                       Coin feePerKb,
                       boolean ensureMinRequiredFee,
                       boolean relaxProofOfWork)
        Creates a new custom context object. This is mainly meant for unit tests for now.
        Parameters:
        eventHorizon - Number of blocks after which the library will delete data and be unable to always process reorgs. See getEventHorizon().
        feePerKb - The default fee per 1000 virtual bytes of transaction data to pay when completing transactions. For details, see SendRequest.feePerKb.
        ensureMinRequiredFee - Whether to ensure the minimum required fee by default when completing transactions. For details, see SendRequest.ensureMinRequiredFee.
        relaxProofOfWork - If true, proof of work is not enforced. This is useful for unit-testing. See Block.checkProofOfWork(boolean).
      • Context

        @Deprecated
        public Context​(NetworkParameters params,
                       int eventHorizon,
                       Coin feePerKb,
                       boolean ensureMinRequiredFee)
        Note that NetworkParameters have been removed from this class. Thus, this constructor just swallows them.
    • Method Detail

      • get

        public static Context get()
        Returns the current context that is associated with the calling thread. BitcoinJ is an API that has thread affinity: much like OpenGL it expects each thread that accesses it to have been configured with a global Context object. This method returns that. Note that to help you develop, this method will also propagate whichever context was created last onto the current thread, if it's missing. However it will print an error when doing so because propagation of contexts is meant to be done manually: this is so two libraries or subsystems that independently use bitcoinj (or possibly alt coin forks of it) can operate correctly.
        Throws:
        java.lang.IllegalStateException - if no context exists at all or if we are in strict mode and there is no context.
      • getOrCreate

        public static Context getOrCreate()
      • getOrCreate

        @Deprecated
        public static Context getOrCreate​(NetworkParameters params)
        Deprecated.
        Note that NetworkParameters have been removed from this class. Thus, this method just swallows them.
      • propagate

        public static void propagate​(Context context)
        Sets the given context as the current thread context. You should use this if you create your own threads that want to create core BitcoinJ objects. Generally, if a class can accept a Context in its constructor and might be used (even indirectly) by a thread, you will want to call this first. Your task may be simplified by using a ContextPropagatingThreadFactory.
      • getConfidenceTable

        public TxConfidenceTable getConfidenceTable()
        Returns the TxConfidenceTable created by this context. The pool tracks advertised and downloaded transactions so their confidence can be measured as a proportion of how many peers announced it. With an un-tampered with internet connection, the more peers announce a transaction the more confidence you can have that it's really valid.
      • getEventHorizon

        public int getEventHorizon()
        The event horizon is the number of blocks after which various bits of the library consider a transaction to be so confirmed that it's safe to delete data. Re-orgs larger than the event horizon will not be correctly processed, so the default value is high (100).
      • getFeePerKb

        public Coin getFeePerKb()
        The default fee per 1000 virtual bytes of transaction data to pay when completing transactions. For details, see SendRequest.feePerKb.
      • isEnsureMinRequiredFee

        public boolean isEnsureMinRequiredFee()
        Whether to ensure the minimum required fee by default when completing transactions. For details, see SendRequest.ensureMinRequiredFee.
      • isRelaxProofOfWork

        public boolean isRelaxProofOfWork()
        If this is set to true, proof of work is not enforced. This is useful for unit-testing, as we need to create and solve fake blocks quite often.