corda/docs/source/tutorial-clientrpc-api.rst
2016-11-29 12:38:47 +00:00

6.7 KiB

Client RPC API tutorial

In this tutorial we will build a simple command line utility that connects to a node, creates some Cash transactions and meanwhile dumps the transaction graph to the standard output. We will then put some simple visualisation on top. For an explanation on how the RPC works see clientrpc.

We start off by connecting to the node itself. For the purposes of the tutorial we will use the Driver to start up a notary and a node that issues/exits and moves Cash around for herself. To authenticate we will use the certificates of the nodes directly.

Note how we configure the node to create a user that has permission to start the CashFlow.

example-code/src/main/kotlin/net/corda/docs/ClientRpcTutorial.kt

Now we can connect to the node itself using a valid RPC login. We login using the configured user.

example-code/src/main/kotlin/net/corda/docs/ClientRpcTutorial.kt

We start generating transactions in a different thread (generateTransactions to be defined later) using proxy, which exposes the full RPC interface of the node:

../../node/src/main/kotlin/net/corda/node/services/messaging/CordaRPCOps.kt

Warning

This API is evolving and will continue to grow as new functionality and features added to Corda are made available to RPC clients.

The one we need in order to dump the transaction graph is verifiedTransactions. The type signature tells us that the RPC will return a list of transactions and an Observable stream. This is a general pattern, we query some data and the node will return the current snapshot and future updates done to it. Observables are described in further detail in clientrpc

example-code/src/main/kotlin/net/corda/docs/ClientRpcTutorial.kt

The graph will be defined by nodes and edges between them. Each node represents a transaction and edges represent output-input relations. For now let's just print NODE <txhash> for the former and EDGE <txhash> <txhash> for the latter.

example-code/src/main/kotlin/net/corda/docs/ClientRpcTutorial.kt

Now we just need to create the transactions themselves!

example-code/src/main/kotlin/net/corda/docs/ClientRpcTutorial.kt

We utilise several RPC functions here to query things like the notaries in the node cluster or our own vault.

Then in a loop we generate randomly either an Issue, a Pay or an Exit transaction.

The RPC we need to initiate a Cash transaction is startFlowDynamic which may start an arbitrary flow, given sufficient permissions to do so. We won't use this function directly, but rather a type-safe wrapper around it startFlow that type-checks the arguments for us.

Finally we have everything in place: we start a couple of nodes, connect to them, and start creating transactions while listening on successfully created ones, which are dumped to the console. We just need to run it!:

# Build the example

./gradlew docs/source/example-code:installDist

# Start it

./docs/source/example-code/build/install/docs/source/example-code/bin/client-rpc-tutorial Print

Now let's try to visualise the transaction graph. We will use a graph drawing library called graphstream

example-code/src/main/kotlin/net/corda/docs/ClientRpcTutorial.kt

If we run the client with Visualise we should see a simple random graph being drawn as new transactions are being created.

Registering classes from your CorDapp with RPC Kryo

As described in clientrpc, you currently have to register any additional classes you add that are needed in RPC requests or responses with the Kryo instance RPC uses. Here's an example of how you do this for an example class.

example-code/src/main/kotlin/net/corda/docs/ClientRpcTutorial.kt

See more on plugins in creating-a-cordapp.

Warning

We will be replacing the use of Kryo in RPC with a stable message format and this will mean that this plugin customisation point will either go away completely or change.

Security

RPC credentials associated with a Client must match the permission set configured on the server Node. This refers to both authentication (username and password) and role-based authorisation (the set of flows an authenticated user is entitled to run).

In the instructions above the server node permissions are configured programmatically in the driver code:

driver(driverDirectory = baseDirectory) {
    val user = User("user", "password", permissions = setOf(startProtocolPermission<CashProtocol>()))
    val node = startNode("Alice", rpcUsers = listOf(user)).get()

When starting a standalone node using a configuration file we must supply the RPC credentials as follows:

rpcUsers : [
    { user=user, password=password, permissions=[ StartFlow.net.corda.flows.CashFlow ] }
]

When using the gradle Cordformation plugin to configure and deploy a node you must supply the RPC credentials in a similar manner:

rpcUsers = [
        ['user' : "user",
         'password' : "password",
         'permissions' : ["StartFlow.net.corda.flows.CashFlow"]]
]

You can then deploy and launch the nodes (Notary and Alice) as follows:

# to create a set of configs and installs under ``docs/source/example-code/build/nodes`` run
./gradlew docs/source/example-code:deployNodes
# to open up two new terminals with the two nodes run
./docs/source/example-code/build/nodes/runnodes
# followed by the same commands as before:
./docs/source/example-code/build/install/docs/source/example-code/bin/client-rpc-tutorial Print
./docs/source/example-code/build/install/docs/source/example-code/bin/client-rpc-tutorial Visualise

See more on security in secure-coding-guidelines, node configuration in corda-configuration-files and Cordformation in creating-a-cordapp