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 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.

    • Method Detail

      • createContext

        default Context createContext​(ChaincodeStub stub)
        Create context from ChaincodeStub. Default impl provided, but can be overwritten by contract
        stub - Instance of the ChaincodeStub to use for this transaction
        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.
        ctx - the context as created by createContext(ChaincodeStub).
      • afterTransaction

        default void afterTransaction​(Context ctx,
                                      java.lang.Object result)
        Invoked once after each transaction. Any exceptions thrown will fail the transaction.
        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.