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​(NetworkParameters params)
        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).
        Parameters:
        params - The network parameters that will be associated with this context.
      • Context

        public Context​(NetworkParameters params,
                       int eventHorizon,
                       Coin feePerKb,
                       boolean ensureMinRequiredFee)
        Creates a new custom context object. This is mainly meant for unit tests for now.
        Parameters:
        params - The network parameters that will be associated with this context.
        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.
    • 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.
      • 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.
      • getParams

        public NetworkParameters getParams()
        Returns the NetworkParameters specified when this context was (auto) created. The network parameters defines various hard coded constants for a specific instance of a Bitcoin network, such as main net, testnet, etc.
      • 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.