public interface ContractInterface
All methods on this interface have default implementations; for many contracts it may not be needed to sub-class these.
Each method on the Contract that is marked with the Transaction
annotation is considered a Transaction Function. This is eligible for
calling. Each transaction function is supplied with its first parameter
being a Context
. The other parameters
are supplied at the developer's discretion.
The sequence of calls is
createContext() -> beforeTransaction() -> the transaction function -> afterTransaction()
If any of these functions throws an exception it is considered an error case
and the whole transaction is failed. The
Context
is a very important object as
it provides transactional context for access to current transaction id,
ledger state, etc.
Note on Threading
All code should be 'Thread Friendly'. Each method must not rely on instance
fields or class side variables for storage. Nor should they use any
ThreadLocal Storage. Ledger data is stored via the ledger api available via
the Context
.
If information needs to be passed from
beforeTransaction(Context)
to
afterTransaction(Context, Object)
or between separate transaction
functions when called directly then a subclass of the Context
should be provided.
Modifier and Type | Method and Description |
---|---|
default void |
afterTransaction(Context ctx,
java.lang.Object result)
Invoked once after each transaction.
|
default void |
beforeTransaction(Context ctx)
Invoked once before each transaction.
|
default Context |
createContext(ChaincodeStub stub)
Create context from
ChaincodeStub . |
default void |
unknownTransaction(Context ctx)
Invoked for any transaction that does not exist.
|
default Context createContext(ChaincodeStub stub)
ChaincodeStub
.
Default impl provided, but can be
overwritten by contractstub
- Instance of the ChaincodeStub to use for this transactiondefault void unknownTransaction(Context ctx)
ctx
- the context as created by createContext(ChaincodeStub)
.default void beforeTransaction(Context ctx)
afterTransaction(Context, Object)
will be calledctx
- the context as created by createContext(ChaincodeStub)
.default void afterTransaction(Context ctx, java.lang.Object result)
ctx
- the context as created by
createContext(ChaincodeStub)
.result
- The object returned from the transaction function if any. As
this is a Java object and therefore pass-by-reference it is
possible to modify this object.