Class Context
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 Summary
-
Constructor Summary
ConstructorDescriptionContext
(NetworkParameters params) Creates a new context object.Context
(NetworkParameters params, int eventHorizon, Coin feePerKb, boolean ensureMinRequiredFee) Creates a new custom context object. -
Method Summary
Modifier and TypeMethodDescriptionstatic void
Require that new threads usepropagate(Context)
orContextPropagatingThreadFactory
, rather than using a heuristic for the desired context.static Context
get()
Returns the current context that is associated with the calling thread.Returns theTxConfidenceTable
created by this context.int
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.The default fee per 1000 virtual bytes of transaction data to pay when completing transactions.static Context
getOrCreate
(NetworkParameters params) Returns theNetworkParameters
specified when this context was (auto) created.boolean
Whether to ensure the minimum required fee by default when completing transactions.static void
Sets the given context as the current thread context.
-
Field Details
-
DEFAULT_EVENT_HORIZON
public static final int DEFAULT_EVENT_HORIZON- See Also:
-
-
Constructor Details
-
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).- 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. SeegetEventHorizon()
.feePerKb
- The default fee per 1000 virtual bytes of transaction data to pay when completing transactions. For details, seeSendRequest.feePerKb
.ensureMinRequiredFee
- Whether to ensure the minimum required fee by default when completing transactions. For details, seeSendRequest.ensureMinRequiredFee
.
-
-
Method Details
-
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:
IllegalStateException
- if no context exists at all or if we are in strict mode and there is no context.
-
enableStrictMode
public static void enableStrictMode()Require that new threads usepropagate(Context)
orContextPropagatingThreadFactory
, rather than using a heuristic for the desired context. -
getOrCreate
-
propagate
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 aContextPropagatingThreadFactory
. -
getConfidenceTable
Returns theTxConfidenceTable
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
Returns theNetworkParameters
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
The default fee per 1000 virtual bytes of transaction data to pay when completing transactions. For details, seeSendRequest.feePerKb
. -
isEnsureMinRequiredFee
public boolean isEnsureMinRequiredFee()Whether to ensure the minimum required fee by default when completing transactions. For details, seeSendRequest.ensureMinRequiredFee
.
-