corda/docs/source/creating-a-cordapp.rst

171 lines
6.9 KiB
ReStructuredText
Raw Normal View History

2016-08-23 09:25:06 +00:00
Creating a Cordapp
==================
2016-08-24 20:03:20 +00:00
A Cordapp is an application that runs on the Corda platform using the platform APIs and plugin system. They are self
contained in separate JARs from the node server JAR that are created and distributed.
2016-08-23 09:25:06 +00:00
2016-08-24 15:19:11 +00:00
App Plugins
-----------
2016-08-23 09:25:06 +00:00
2016-08-24 15:19:11 +00:00
.. note:: Currently apps are only supported for JVM languages.
2016-08-23 09:25:06 +00:00
2016-08-24 15:19:11 +00:00
To create an app plugin you must you must extend from `CordaPluginRegistry`_. The JavaDoc contains
2016-08-24 14:33:43 +00:00
specific details of the implementation, but you can extend the server in the following ways:
2016-08-23 09:25:06 +00:00
2016-08-24 14:33:43 +00:00
1. Required protocols: Specify which protocols will be whitelisted for use in your web APIs.
2. Service plugins: Register your services (see below).
2016-08-24 14:33:43 +00:00
3. Web APIs: You may register your own endpoints under /api/ of the built-in web server.
2016-08-23 09:25:06 +00:00
4. Static web endpoints: You may register your own static serving directories for serving web content.
2016-08-24 15:19:11 +00:00
Services
--------
2016-08-23 09:25:06 +00:00
2016-08-24 15:19:11 +00:00
Services are classes which are constructed after the node has started. It is provided a `ServiceHubInternal`_ which
2016-08-24 20:03:20 +00:00
allows a richer API than the `ServiceHub`_ exposed to contracts. It enables adding protocols, registering
message handlers and more. The service does not run in a separate thread, so the only entry point to the service is during
2016-08-24 15:19:11 +00:00
construction, where message handlers should be registered and threads started.
2016-08-23 09:25:06 +00:00
2016-08-24 15:19:11 +00:00
Starting Nodes
--------------
2016-08-23 09:25:06 +00:00
To use an app you must also have a node server. To create a node server run the gradle installTemplateNodes task.
2016-08-23 09:25:06 +00:00
This will output the node JAR to ``build/libs/corda.jar`` and several sample/standard
2016-08-24 20:03:20 +00:00
node setups to ``build/nodes``. For now you can use the ``build/nodes/nodea`` configuration as a template.
2016-08-23 09:25:06 +00:00
Each node server by default must have a ``node.conf`` file in the current working directory. After first
execution of the node server there will be many other configuration and persistence files created in a node workspace directory. This is specified as the basedir property of the node.conf file, or else can be overidden using ``--base-directory=<workspace>``.
2016-08-23 09:25:06 +00:00
2016-08-24 15:19:11 +00:00
.. note:: Outside of development environments do not store your node directories in the build folder.
2016-08-23 09:25:06 +00:00
.. warning:: Also note that the bootstrapping process of the ``corda.jar`` unpacks the Corda dependencies into a temporary folder. It is therefore suggested that the CAPSULE_CACHE_DIR environment variable be set before starting the process to control this location.
2016-08-24 15:19:11 +00:00
Installing Apps
---------------
2016-08-23 09:25:06 +00:00
2016-08-24 15:19:11 +00:00
Once you have created your app JAR you can install it to a node by adding it to ``<node_dir>/plugins/``. In this
case the ``node_dir`` is the location where your node server's JAR and configuration file is.
2016-08-23 09:25:06 +00:00
2016-08-24 15:19:11 +00:00
.. note:: If the directory does not exist you can create it manually.
2016-08-23 09:25:06 +00:00
Starting your Node
------------------
2016-08-24 20:03:20 +00:00
Now you have a node server with your app installed, you can run it by navigating to ``<node_dir>`` and running
2016-08-23 09:25:06 +00:00
java -jar corda.jar
2016-08-23 09:25:06 +00:00
The plugin should automatically be registered and the configuration file used.
.. warning:: If your working directory is not ``<node_dir>`` your plugins and configuration will not be used.
The configuration file and workspace paths can be overidden on the command line e.g.
``java -jar corda.jar --config-file=test.conf --base-directory=/opt/r3corda/nodes/test``.
Otherwise the workspace folder for the node is created based upon the ``basedir`` property in the ``node.conf`` file and if this is relative it is applied relative to the current working path.
Debugging your Node
-------------------
To enable remote debugging of the corda process use a command line such as:
``java -Dcapsule.jvm.args="-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005" -jar corda.jar``
This command line will start the debugger on port 5005 and pause the process awaiting debugger attachment.
Viewing persisted state of your Node
------------------------------------
To make examining the persisted contract states of your node or the internal node database tables easier, and providing you are
using the default database configuration used for demos, you should be able to connect to the internal node database over
a JDBC connection at the URL that is output to the logs at node start up. That URL will be of the form ``jdbc:h2:tcp://<host>:<port>/node``.
The user name and password for the login are as per the node data source configuration.
The name and column layout of the internal node tables is in a state of flux and should not be relied upon to remain static
at the present time, and should certainly be treated as read-only.
2016-08-24 20:03:20 +00:00
.. _CordaPluginRegistry: api/com.r3corda.core.node/-corda-plugin-registry/index.html
.. _ServiceHubInternal: api/com.r3corda.node.services.api/-service-hub-internal/index.html
.. _ServiceHub: api/com.r3corda.node.services.api/-service-hub/index.html
Building Against Corda
----------------------
.. warning:: This feature is subject to rapid change
Corda now supports publishing to Maven local to build against it. To publish to Maven local run the following in the
root directory of Corda
.. code-block:: shell
./gradlew publishToMavenLocal
This will publish corda-$version.jar, contracts-$version.jar, core-$version.jar and node-$version.jar to the
group com.r3corda. You can now depend on these as you normally would a Maven dependency.
In Gradle you can depend on these by adding/modifying your build.gradle file to contain the following:
.. code-block:: groovy
repositories {
mavenLocal()
... other repositories here ...
}
dependencies {
compile "com.r3corda:core:$corda_version"
compile "com.r3corda:contracts:$corda_version"
compile "com.r3corda:node:$corda_version"
compile "com.r3corda:corda:$corda_version"
... other dependencies here ...
}
Gradle Plugins for Cordapps
===========================
There are several Gradle plugins that reduce your build.gradle boilerplate and make development of Cordapps easier.
The available plugins are in the gradle-plugins directory of the Corda repository.
Building Gradle Plugins
-----------------------
To install to your local Maven repository the plugins that Cordapp gradle files require, run the following from the
root of the Corda project:
.. code-block:: text
./gradlew publishToMavenLocal
The plugins will now be installed to your local Maven repository in ~/.m2 on Unix and %HOMEPATH%\.m2 on Windows.
Using Gradle Plugins
--------------------
To use the plugins, if you are not already using the Cordapp template project, you must modify your build.gradle. Add
the following segments to the relevant part of your build.gradle.
.. code-block::
buildscript {
ext.corda_version = '<enter the corda version you build against here>'
... (your buildscript)
repositories {
... (other repositories)
mavenLocal()
}
dependencies {
... (your dependencies)
classpath "com.r3corda.plugins:<plugin-maven-name>:$corda_version"
}
}
apply plugin: 'com.r3corda.plugins.<plugin-maven-name>'
...