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()
Creates a new context object.Creates a new custom context object.Context
(NetworkParameters params) Deprecated.Context
(NetworkParameters params, int eventHorizon, Coin feePerKb, boolean ensureMinRequiredFee) Deprecated. -
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
static Context
getOrCreate
(NetworkParameters params) Deprecated.UsegetOrCreate()
boolean
Whether to ensure the minimum required fee by default when completing transactions.boolean
If this is set to true, proof of work is not enforced.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
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.UseContext()
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. 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
.relaxProofOfWork
- If true, proof of work is not enforced. This is useful for unit-testing. SeeBlock.checkProofOfWork(boolean)
.
-
Context
@Deprecated public Context(NetworkParameters params, int eventHorizon, Coin feePerKb, boolean ensureMinRequiredFee) Deprecated.Note that NetworkParameters have been removed from this class. Thus, this constructor just swallows them.
-
-
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
-
getOrCreate
Deprecated.UsegetOrCreate()
Note that NetworkParameters have been removed from this class. Thus, this method just swallows them. -
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. -
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
. -
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.
-
Context()