Interface ContractInterface
-
public interface ContractInterface
All Contracts should implement this interface, in addition to the Contract annotation.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 aContext
. 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)
toafterTransaction(Context, Object)
or between separate transaction functions when called directly then a subclass of theContext
should be provided.
-
-
Method Summary
All Methods Instance Methods Default Methods Modifier and Type Method 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 fromChaincodeStub
.default void
unknownTransaction(Context ctx)
Invoked for any transaction that does not exist.
-
-
-
Method Detail
-
createContext
default Context createContext(ChaincodeStub stub)
Create context fromChaincodeStub
. Default impl provided, but can be overwritten by contract- Parameters:
stub
- Instance of the ChaincodeStub to use for this transaction- Returns:
- instance of the context to use for the current transaction being executed
-
unknownTransaction
default void unknownTransaction(Context ctx)
Invoked for any transaction that does not exist. This will throw an exception. If you wish to alter the exception thrown or if you wish to consider requests for transactions that don't exist as not an error, subclass this method.- Parameters:
ctx
- the context as created bycreateContext(ChaincodeStub)
.
-
beforeTransaction
default void beforeTransaction(Context ctx)
Invoked once before each transaction. Any exceptions thrown will fail the transaction, and neither the required transaction or theafterTransaction(Context, Object)
will be called- Parameters:
ctx
- the context as created bycreateContext(ChaincodeStub)
.
-
afterTransaction
default void afterTransaction(Context ctx, java.lang.Object result)
Invoked once after each transaction. Any exceptions thrown will fail the transaction.- Parameters:
ctx
- the context as created bycreateContext(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.
-
-