corda/docs/build/html/tutorial-contract-clauses.html
Clinton Alexander f92ef3d9cf Regen docsite
2017-01-03 13:07:48 +00:00

538 lines
45 KiB
HTML

<!-- If you edit this, then please make the same changes to layout_for_doc_website.html, as that is used for the web
doc site generation which we put analytics tracking on to identify any potential problem pages -->
<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Writing a contract using clauses &mdash; R3 Corda latest documentation</title>
<link rel="stylesheet" href="_static/css/custom.css" type="text/css" />
<link rel="top" title="R3 Corda latest documentation" href="index.html"/>
<link rel="next" title="Writing a contract test" href="tutorial-test-dsl.html"/>
<link rel="prev" title="Writing a contract" href="tutorial-contract.html"/>
<script src="_static/js/modernizr.min.js"></script>
</head>
<body class="wy-body-for-nav" role="document">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-scroll">
<div class="wy-side-nav-search">
<a href="index.html" class="icon icon-home"> R3 Corda
</a>
<div class="version">
latest
</div>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="search.html" method="get">
<input type="text" name="q" placeholder="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
<br>
<a href="api/index.html">API reference</a>
<br>
<a href="https://discourse.corda.net">Discourse Forums</a>
<br>
<a href="http://slack.corda.net">Slack</a>
<br>
</div>
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
<p class="caption"><span class="caption-text">Getting started</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="inthebox.html">What&#8217;s included?</a></li>
<li class="toctree-l1"><a class="reference internal" href="getting-set-up.html">Getting set up</a></li>
<li class="toctree-l1"><a class="reference internal" href="getting-set-up-fault-finding.html">Getting set up: troubleshooting</a></li>
<li class="toctree-l1"><a class="reference internal" href="running-the-demos.html">Running the demos</a></li>
<li class="toctree-l1"><a class="reference internal" href="CLI-vs-IDE.html">CLI vs IDE</a></li>
</ul>
<p class="caption"><span class="caption-text">Key concepts</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="data-model.html">Data model</a></li>
<li class="toctree-l1"><a class="reference internal" href="transaction-data-types.html">Data types</a></li>
<li class="toctree-l1"><a class="reference internal" href="merkle-trees.html">Transaction tear-offs</a></li>
<li class="toctree-l1"><a class="reference internal" href="consensus.html">Consensus model</a></li>
<li class="toctree-l1"><a class="reference internal" href="clauses.html">Clauses key concepts</a></li>
</ul>
<p class="caption"><span class="caption-text">CorDapps</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="creating-a-cordapp.html">CorDapps Background</a></li>
<li class="toctree-l1"><a class="reference internal" href="creating-a-cordapp.html#gradle-plugins-for-cordapps">Gradle plugins for CorDapps</a></li>
<li class="toctree-l1"><a class="reference internal" href="tutorial-cordapp.html">The CorDapp Template</a></li>
<li class="toctree-l1"><a class="reference internal" href="tutorial-cordapp.html#building-the-cordapp-template">Building the CorDapp template</a></li>
<li class="toctree-l1"><a class="reference internal" href="tutorial-cordapp.html#running-the-cordapp-template">Running the CorDapp template</a></li>
<li class="toctree-l1"><a class="reference internal" href="tutorial-cordapp.html#interacting-with-the-cordapp-template">Interacting with the CorDapp template</a></li>
<li class="toctree-l1"><a class="reference internal" href="tutorial-cordapp.html#extending-the-cordapp-template">Extending the CorDapp template</a></li>
</ul>
<p class="caption"><span class="caption-text">The Corda node</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="clientrpc.html">Client RPC</a></li>
<li class="toctree-l1"><a class="reference internal" href="messaging.html">Networking and messaging</a></li>
<li class="toctree-l1"><a class="reference internal" href="persistence.html">Persistence</a></li>
<li class="toctree-l1"><a class="reference internal" href="node-administration.html">Node administration</a></li>
<li class="toctree-l1"><a class="reference internal" href="corda-configuration-file.html">Node configuration</a></li>
<li class="toctree-l1"><a class="reference internal" href="corda-plugins.html">The Corda plugin framework</a></li>
<li class="toctree-l1"><a class="reference internal" href="node-services.html">Brief introduction to the node services</a></li>
<li class="toctree-l1"><a class="reference internal" href="node-explorer.html">Node Explorer</a></li>
<li class="toctree-l1"><a class="reference internal" href="permissioning.html">Network permissioning</a></li>
</ul>
<p class="caption"><span class="caption-text">Tutorials</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="tutorial-contract.html">Writing a contract</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="#">Writing a contract using clauses</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#what-are-clauses-and-why-to-use-them">What are clauses and why to use them?</a></li>
<li class="toctree-l2"><a class="reference internal" href="#how-clauses-work">How clauses work?</a></li>
<li class="toctree-l2"><a class="reference internal" href="#commercial-paper-class">Commercial paper class</a></li>
<li class="toctree-l2"><a class="reference internal" href="#simple-clauses">Simple Clauses</a></li>
<li class="toctree-l2"><a class="reference internal" href="#group-clause">Group clause</a></li>
<li class="toctree-l2"><a class="reference internal" href="#summary">Summary</a></li>
<li class="toctree-l2"><a class="reference internal" href="#debugging">Debugging</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="tutorial-test-dsl.html">Writing a contract test</a></li>
<li class="toctree-l1"><a class="reference internal" href="tutorial-integration-testing.html">Integration Test Tutorial</a></li>
<li class="toctree-l1"><a class="reference internal" href="tutorial-clientrpc-api.html">Client RPC API tutorial</a></li>
<li class="toctree-l1"><a class="reference internal" href="tutorial-building-transactions.html">Building Transactions</a></li>
<li class="toctree-l1"><a class="reference internal" href="flow-state-machines.html">Writing flows</a></li>
<li class="toctree-l1"><a class="reference internal" href="flow-testing.html">Writing flow tests</a></li>
<li class="toctree-l1"><a class="reference internal" href="running-a-notary.html">Running a notary service</a></li>
<li class="toctree-l1"><a class="reference internal" href="using-a-notary.html">Using a notary service</a></li>
<li class="toctree-l1"><a class="reference internal" href="oracles.html">Writing oracle services</a></li>
<li class="toctree-l1"><a class="reference internal" href="oracles.html#implementing-an-oracle-with-continuously-varying-data">Implementing an oracle with continuously varying data</a></li>
<li class="toctree-l1"><a class="reference internal" href="oracles.html#using-an-oracle">Using an oracle</a></li>
<li class="toctree-l1"><a class="reference internal" href="tutorial-attachments.html">Using attachments</a></li>
<li class="toctree-l1"><a class="reference internal" href="event-scheduling.html">Event scheduling</a></li>
</ul>
<p class="caption"><span class="caption-text">Other</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="network-simulator.html">Network Simulator</a></li>
<li class="toctree-l1"><a class="reference internal" href="initial-margin-agreement.html">Initial margin agreements</a></li>
</ul>
<p class="caption"><span class="caption-text">Component library</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="contract-catalogue.html">Contract catalogue</a></li>
<li class="toctree-l1"><a class="reference internal" href="contract-irs.html">Interest rate swaps</a></li>
</ul>
<p class="caption"><span class="caption-text">Appendix</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="loadtesting.html">Load testing</a></li>
<li class="toctree-l1"><a class="reference internal" href="setting-up-a-corda-network.html">Introduction - What is a corda network?</a></li>
<li class="toctree-l1"><a class="reference internal" href="setting-up-a-corda-network.html#setting-up-your-own-network">Setting up your own network</a></li>
<li class="toctree-l1"><a class="reference internal" href="secure-coding-guidelines.html">Secure coding guidelines</a></li>
<li class="toctree-l1"><a class="reference internal" href="release-process.html">Release process</a></li>
<li class="toctree-l1"><a class="reference internal" href="release-process.html#steps-to-cut-a-release">Steps to cut a release</a></li>
<li class="toctree-l1"><a class="reference internal" href="release-notes.html">Release notes</a></li>
<li class="toctree-l1"><a class="reference internal" href="codestyle.html">Code style guide</a></li>
<li class="toctree-l1"><a class="reference internal" href="building-the-docs.html">Building the documentation</a></li>
<li class="toctree-l1"><a class="reference internal" href="publishing-corda.html">Publishing Corda</a></li>
<li class="toctree-l1"><a class="reference internal" href="azure-vm.html">Working with the Corda Demo on Azure Marketplace</a></li>
</ul>
<p class="caption"><span class="caption-text">Glossary</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="glossary.html">Glossary</a></li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
<nav class="wy-nav-top" role="navigation" aria-label="top navigation">
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="index.html">R3 Corda</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content">
<div role="navigation" aria-label="breadcrumbs navigation">
<ul class="wy-breadcrumbs">
<li><a href="index.html">Docs</a> &raquo;</li>
<li>Writing a contract using clauses</li>
<li class="wy-breadcrumbs-aside">
<a href="_sources/tutorial-contract-clauses.txt" rel="nofollow"> View page source</a>
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/codesets.js"></script><div class="section" id="writing-a-contract-using-clauses">
<h1>Writing a contract using clauses<a class="headerlink" href="#writing-a-contract-using-clauses" title="Permalink to this headline"></a></h1>
<p>This tutorial will take you through restructuring the commercial paper contract to use clauses. You should have
already completed &#8220;<a class="reference internal" href="tutorial-contract.html"><span class="doc">Writing a contract</span></a>&#8221;.
As before, the example is focused on basic implementation of commercial paper, which is essentially a simpler version of a corporate
bond. A company issues CP with a particular face value, say $100, but sells it for less, say $90. The paper can be redeemed
for cash at a given date in the future. Thus this example would have a 10% interest rate with a single repayment.
Whole Kotlin code can be found in <code class="docutils literal"><span class="pre">CommercialPaper.kt</span></code>.</p>
<div class="section" id="what-are-clauses-and-why-to-use-them">
<h2>What are clauses and why to use them?<a class="headerlink" href="#what-are-clauses-and-why-to-use-them" title="Permalink to this headline"></a></h2>
<p>Clauses are essentially micro-contracts which contain independent verification logic, and can be logically composed
together to form a contract. Clauses are designed to enable re-use of common verification parts, for example issuing state objects
is generally the same for all fungible contracts, so a common issuance clause can be inherited for each contract&#8217;s
issue clause. This cuts down on scope for error, and improves consistency of behaviour. By splitting verification logic
into smaller chunks, they can also be readily tested in isolation.</p>
</div>
<div class="section" id="how-clauses-work">
<h2>How clauses work?<a class="headerlink" href="#how-clauses-work" title="Permalink to this headline"></a></h2>
<p>We have different types of clauses, the most basic are the ones that define verification logic for particular command set.
We will see them later as elementary building blocks that commercial paper consist of - <code class="docutils literal"><span class="pre">Move</span></code>, <code class="docutils literal"><span class="pre">Issue</span></code> and <code class="docutils literal"><span class="pre">Redeem</span></code>.
As a developer you need to identify reusable parts of your contract and decide how they should be combined. It is where
composite clauses become useful. They gather many clause subcomponents and resolve how and which of them should be checked.</p>
<p>For example, assume that we want to verify a transaction using all constraints defined in separate clauses. We need to
wrap classes that define them into <code class="docutils literal"><span class="pre">AllComposition</span></code> composite clause. It assures that all clauses from that combination
match with commands in a transaction - only then verification logic can be executed.
It may be a little confusing, but composite clause is also a clause and you can even wrap it in the special grouping clause.
In <code class="docutils literal"><span class="pre">CommercialPaper</span></code> it looks like that:</p>
<img alt="_images/commPaperClauses.png" src="_images/commPaperClauses.png" />
<p>The most basic types of composite clauses are <code class="docutils literal"><span class="pre">AllComposition</span></code>, <code class="docutils literal"><span class="pre">AnyComposition</span></code> and <code class="docutils literal"><span class="pre">FirstComposition</span></code>.
In this tutorial we will use <code class="docutils literal"><span class="pre">GroupClauseVerifier</span></code> and <code class="docutils literal"><span class="pre">AnyComposition</span></code>. It&#8217;s important to understand how they work.
Charts showing execution and more detailed information can be found in <a class="reference internal" href="clauses.html"><span class="doc">Clauses key concepts</span></a>.</p>
</div>
<div class="section" id="commercial-paper-class">
<span id="verify-ref"></span><h2>Commercial paper class<a class="headerlink" href="#commercial-paper-class" title="Permalink to this headline"></a></h2>
<p>We start from defining <code class="docutils literal"><span class="pre">CommercialPaper</span></code> class. As in previous tutorial we need some elementary parts: <code class="docutils literal"><span class="pre">Commands</span></code> interface,
<code class="docutils literal"><span class="pre">generateMove</span></code>, <code class="docutils literal"><span class="pre">generateIssue</span></code>, <code class="docutils literal"><span class="pre">generateRedeem</span></code> - so far so good that stays the same. The new part is verification and
<code class="docutils literal"><span class="pre">Clauses</span></code> interface (you will see them later in code). Let&#8217;s start from the basic structure:</p>
<div class="codeset container">
<div class="highlight-kotlin"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">CommercialPaper</span> <span class="p">:</span> <span class="n">Contract</span> <span class="p">{</span>
<span class="k">override</span> <span class="k">val</span> <span class="py">legalContractReference</span><span class="p">:</span> <span class="n">SecureHash</span> <span class="p">=</span> <span class="n">SecureHash</span><span class="p">.</span><span class="n">sha256</span><span class="p">(</span><span class="s">&quot;https://en.wikipedia.org/wiki/Commercial_paper&quot;</span><span class="p">)</span>
<span class="k">override</span> <span class="k">fun</span> <span class="nf">verify</span><span class="p">(</span><span class="n">tx</span><span class="p">:</span> <span class="n">TransactionForContract</span><span class="p">)</span> <span class="p">=</span> <span class="n">verifyClause</span><span class="p">(</span><span class="n">tx</span><span class="p">,</span> <span class="n">Clauses</span><span class="p">.</span><span class="n">Group</span><span class="p">(),</span> <span class="n">tx</span><span class="p">.</span><span class="n">commands</span><span class="p">.</span><span class="n">select</span><span class="p">&lt;</span><span class="n">Commands</span><span class="p">&gt;())</span>
<span class="k">interface</span> <span class="nc">Commands</span> <span class="p">:</span> <span class="n">CommandData</span> <span class="p">{</span>
<span class="k">data</span> <span class="k">class</span> <span class="nc">Move</span><span class="p">(</span><span class="k">override</span> <span class="k">val</span> <span class="py">contractHash</span><span class="p">:</span> <span class="n">SecureHash</span><span class="p">?</span> <span class="p">=</span> <span class="k">null</span><span class="p">)</span> <span class="p">:</span> <span class="n">FungibleAsset</span><span class="p">.</span><span class="n">Commands</span><span class="p">.</span><span class="n">Move</span><span class="p">,</span> <span class="n">Commands</span>
<span class="k">class</span> <span class="nc">Redeem</span> <span class="p">:</span> <span class="n">TypeOnlyCommandData</span><span class="p">(),</span> <span class="n">Commands</span>
<span class="k">data</span> <span class="k">class</span> <span class="nc">Issue</span><span class="p">(</span><span class="k">override</span> <span class="k">val</span> <span class="py">nonce</span><span class="p">:</span> <span class="n">Long</span> <span class="p">=</span> <span class="n">random63BitValue</span><span class="p">())</span> <span class="p">:</span> <span class="n">IssueCommand</span><span class="p">,</span> <span class="n">Commands</span>
<span class="p">}</span>
</pre></div>
</div>
<div class="highlight-java"><div class="highlight"><pre><span></span><span class="kd">public</span> <span class="kd">class</span> <span class="nc">CommercialPaper</span> <span class="kd">implements</span> <span class="n">Contract</span> <span class="o">{</span>
<span class="nd">@Override</span>
<span class="kd">public</span> <span class="n">SecureHash</span> <span class="nf">getLegalContractReference</span><span class="o">()</span> <span class="o">{</span>
<span class="k">return</span> <span class="n">SecureHash</span><span class="o">.</span><span class="na">Companion</span><span class="o">.</span><span class="na">sha256</span><span class="o">(</span><span class="s">&quot;https://en.wikipedia.org/wiki/Commercial_paper&quot;</span><span class="o">);</span>
<span class="o">}</span>
<span class="nd">@Override</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">verify</span><span class="o">(</span><span class="nd">@NotNull</span> <span class="n">TransactionForContract</span> <span class="n">tx</span><span class="o">)</span> <span class="kd">throws</span> <span class="n">IllegalArgumentException</span> <span class="o">{</span>
<span class="n">ClauseVerifier</span><span class="o">.</span><span class="na">verifyClause</span><span class="o">(</span><span class="n">tx</span><span class="o">,</span> <span class="k">new</span> <span class="n">Clauses</span><span class="o">.</span><span class="na">Group</span><span class="o">(),</span> <span class="n">extractCommands</span><span class="o">(</span><span class="n">tx</span><span class="o">));</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="kd">interface</span> <span class="nc">Commands</span> <span class="kd">extends</span> <span class="n">CommandData</span> <span class="o">{</span>
<span class="kd">class</span> <span class="nc">Move</span> <span class="kd">implements</span> <span class="n">Commands</span> <span class="o">{</span>
<span class="nd">@Override</span>
<span class="kd">public</span> <span class="kt">boolean</span> <span class="nf">equals</span><span class="o">(</span><span class="n">Object</span> <span class="n">obj</span><span class="o">)</span> <span class="o">{</span> <span class="k">return</span> <span class="n">obj</span> <span class="k">instanceof</span> <span class="n">Move</span><span class="o">;</span> <span class="o">}</span>
<span class="o">}</span>
<span class="kd">class</span> <span class="nc">Redeem</span> <span class="kd">implements</span> <span class="n">Commands</span> <span class="o">{</span>
<span class="nd">@Override</span>
<span class="kd">public</span> <span class="kt">boolean</span> <span class="nf">equals</span><span class="o">(</span><span class="n">Object</span> <span class="n">obj</span><span class="o">)</span> <span class="o">{</span> <span class="k">return</span> <span class="n">obj</span> <span class="k">instanceof</span> <span class="n">Redeem</span><span class="o">;</span> <span class="o">}</span>
<span class="o">}</span>
<span class="kd">class</span> <span class="nc">Issue</span> <span class="kd">implements</span> <span class="n">Commands</span> <span class="o">{</span>
<span class="nd">@Override</span>
<span class="kd">public</span> <span class="kt">boolean</span> <span class="nf">equals</span><span class="o">(</span><span class="n">Object</span> <span class="n">obj</span><span class="o">)</span> <span class="o">{</span> <span class="k">return</span> <span class="n">obj</span> <span class="k">instanceof</span> <span class="n">Issue</span><span class="o">;</span> <span class="o">}</span>
<span class="o">}</span>
<span class="o">}</span>
</pre></div>
</div>
</div>
<p>As you can see we used <code class="docutils literal"><span class="pre">verifyClause</span></code> function with <code class="docutils literal"><span class="pre">Clauses.Group()</span></code> in place of previous verification.
It&#8217;s an entry point to running clause logic. <code class="docutils literal"><span class="pre">verifyClause</span></code> takes the transaction, a clause (usually a composite one)
to verify, and a collection of commands the clause is expected to handle all of. This list of commands is important because
<code class="docutils literal"><span class="pre">verifyClause</span></code> checks that none of the commands are left unprocessed at the end, and raises an error if they are.</p>
</div>
<div class="section" id="simple-clauses">
<h2>Simple Clauses<a class="headerlink" href="#simple-clauses" title="Permalink to this headline"></a></h2>
<p>Let&#8217;s move to constructing contract logic in terms of clauses language. Commercial paper contract has three commands and
three corresponding behaviours: <code class="docutils literal"><span class="pre">Issue</span></code>, <code class="docutils literal"><span class="pre">Move</span></code> and <code class="docutils literal"><span class="pre">Redeem</span></code>. Each of them has a specific set of requirements that must be satisfied -
perfect material for defining clauses. For brevity we will show only <code class="docutils literal"><span class="pre">Move</span></code> clause, rest is constructed in similar manner
and included in the <code class="docutils literal"><span class="pre">CommercialPaper.kt</span></code> code.</p>
<div class="codeset container">
<div class="highlight-kotlin"><div class="highlight"><pre><span></span><span class="k">interface</span> <span class="nc">Clauses</span> <span class="p">{</span>
<span class="k">class</span> <span class="nc">Move</span><span class="p">:</span> <span class="n">Clause</span><span class="p">&lt;</span><span class="n">State</span><span class="p">,</span> <span class="n">Commands</span><span class="p">,</span> <span class="n">Issued</span><span class="p">&lt;</span><span class="n">Terms</span><span class="p">&gt;&gt;()</span> <span class="p">{</span>
<span class="k">override</span> <span class="k">val</span> <span class="py">requiredCommands</span><span class="p">:</span> <span class="n">Set</span><span class="p">&lt;</span><span class="n">Class</span><span class="p">&lt;</span><span class="k">out</span> <span class="n">CommandData</span><span class="p">&gt;&gt;</span>
<span class="k">get</span><span class="p">()</span> <span class="p">=</span> <span class="n">setOf</span><span class="p">(</span><span class="n">Commands</span><span class="p">.</span><span class="n">Move</span><span class="o">::</span><span class="k">class</span><span class="p">.</span><span class="n">java</span><span class="p">)</span>
<span class="k">override</span> <span class="k">fun</span> <span class="nf">verify</span><span class="p">(</span><span class="n">tx</span><span class="p">:</span> <span class="n">TransactionForContract</span><span class="p">,</span>
<span class="n">inputs</span><span class="p">:</span> <span class="n">List</span><span class="p">&lt;</span><span class="n">State</span><span class="p">&gt;,</span>
<span class="n">outputs</span><span class="p">:</span> <span class="n">List</span><span class="p">&lt;</span><span class="n">State</span><span class="p">&gt;,</span>
<span class="n">commands</span><span class="p">:</span> <span class="n">List</span><span class="p">&lt;</span><span class="n">AuthenticatedObject</span><span class="p">&lt;</span><span class="n">Commands</span><span class="p">&gt;&gt;,</span>
<span class="n">groupingKey</span><span class="p">:</span> <span class="n">Issued</span><span class="p">&lt;</span><span class="n">Terms</span><span class="p">&gt;?):</span> <span class="n">Set</span><span class="p">&lt;</span><span class="n">Commands</span><span class="p">&gt;</span> <span class="p">{</span>
<span class="k">val</span> <span class="py">command</span> <span class="p">=</span> <span class="n">commands</span><span class="p">.</span><span class="n">requireSingleCommand</span><span class="p">&lt;</span><span class="n">Commands</span><span class="p">.</span><span class="n">Move</span><span class="p">&gt;()</span>
<span class="k">val</span> <span class="py">input</span> <span class="p">=</span> <span class="n">inputs</span><span class="p">.</span><span class="n">single</span><span class="p">()</span>
<span class="n">requireThat</span> <span class="p">{</span>
<span class="s">&quot;the transaction is signed by the owner of the CP&quot;</span> <span class="k">by</span> <span class="p">(</span><span class="n">input</span><span class="p">.</span><span class="n">owner</span> <span class="k">in</span> <span class="n">command</span><span class="p">.</span><span class="n">signers</span><span class="p">)</span>
<span class="s">&quot;the state is propagated&quot;</span> <span class="k">by</span> <span class="p">(</span><span class="n">outputs</span><span class="p">.</span><span class="n">size</span> <span class="p">==</span> <span class="m">1</span><span class="p">)</span>
<span class="c1">// Don&#39;t need to check anything else, as if outputs.size == 1 then the output is equal to</span>
<span class="c1">// the input ignoring the owner field due to the grouping.</span>
<span class="p">}</span>
<span class="k">return</span> <span class="n">setOf</span><span class="p">(</span><span class="n">command</span><span class="p">.</span><span class="n">value</span><span class="p">)</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="p">...</span>
</pre></div>
</div>
<div class="highlight-java"><div class="highlight"><pre><span></span><span class="kd">public</span> <span class="kd">interface</span> <span class="nc">Clauses</span> <span class="o">{</span>
<span class="kd">class</span> <span class="nc">Move</span> <span class="kd">extends</span> <span class="n">Clause</span><span class="o">&lt;</span><span class="n">State</span><span class="o">,</span> <span class="n">Commands</span><span class="o">,</span> <span class="n">State</span><span class="o">&gt;</span> <span class="o">{</span>
<span class="nd">@NotNull</span>
<span class="nd">@Override</span>
<span class="kd">public</span> <span class="n">Set</span><span class="o">&lt;</span><span class="n">Class</span><span class="o">&lt;?</span> <span class="kd">extends</span> <span class="n">CommandData</span><span class="o">&gt;&gt;</span> <span class="nf">getRequiredCommands</span><span class="o">()</span> <span class="o">{</span>
<span class="k">return</span> <span class="n">Collections</span><span class="o">.</span><span class="na">singleton</span><span class="o">(</span><span class="n">Commands</span><span class="o">.</span><span class="na">Move</span><span class="o">.</span><span class="na">class</span><span class="o">);</span>
<span class="o">}</span>
<span class="nd">@NotNull</span>
<span class="nd">@Override</span>
<span class="kd">public</span> <span class="n">Set</span><span class="o">&lt;</span><span class="n">Commands</span><span class="o">&gt;</span> <span class="nf">verify</span><span class="o">(</span><span class="nd">@NotNull</span> <span class="n">TransactionForContract</span> <span class="n">tx</span><span class="o">,</span>
<span class="nd">@NotNull</span> <span class="n">List</span><span class="o">&lt;?</span> <span class="kd">extends</span> <span class="n">State</span><span class="o">&gt;</span> <span class="n">inputs</span><span class="o">,</span>
<span class="nd">@NotNull</span> <span class="n">List</span><span class="o">&lt;?</span> <span class="kd">extends</span> <span class="n">State</span><span class="o">&gt;</span> <span class="n">outputs</span><span class="o">,</span>
<span class="nd">@NotNull</span> <span class="n">List</span><span class="o">&lt;?</span> <span class="kd">extends</span> <span class="n">AuthenticatedObject</span><span class="o">&lt;?</span> <span class="kd">extends</span> <span class="n">Commands</span><span class="o">&gt;&gt;</span> <span class="n">commands</span><span class="o">,</span>
<span class="nd">@NotNull</span> <span class="n">State</span> <span class="n">groupingKey</span><span class="o">)</span> <span class="o">{</span>
<span class="n">AuthenticatedObject</span><span class="o">&lt;</span><span class="n">Commands</span><span class="o">.</span><span class="na">Move</span><span class="o">&gt;</span> <span class="n">cmd</span> <span class="o">=</span> <span class="n">requireSingleCommand</span><span class="o">(</span><span class="n">tx</span><span class="o">.</span><span class="na">getCommands</span><span class="o">(),</span> <span class="n">Commands</span><span class="o">.</span><span class="na">Move</span><span class="o">.</span><span class="na">class</span><span class="o">);</span>
<span class="c1">// There should be only a single input due to aggregation above</span>
<span class="n">State</span> <span class="n">input</span> <span class="o">=</span> <span class="n">single</span><span class="o">(</span><span class="n">inputs</span><span class="o">);</span>
<span class="k">if</span> <span class="o">(!</span><span class="n">cmd</span><span class="o">.</span><span class="na">getSigners</span><span class="o">().</span><span class="na">contains</span><span class="o">(</span><span class="n">input</span><span class="o">.</span><span class="na">getOwner</span><span class="o">()))</span>
<span class="k">throw</span> <span class="k">new</span> <span class="n">IllegalStateException</span><span class="o">(</span><span class="s">&quot;Failed requirement: the transaction is signed by the owner of the CP&quot;</span><span class="o">);</span>
<span class="c1">// Check the output CP state is the same as the input state, ignoring the owner field.</span>
<span class="k">if</span> <span class="o">(</span><span class="n">outputs</span><span class="o">.</span><span class="na">size</span><span class="o">()</span> <span class="o">!=</span> <span class="mi">1</span><span class="o">)</span> <span class="o">{</span>
<span class="k">throw</span> <span class="k">new</span> <span class="n">IllegalStateException</span><span class="o">(</span><span class="s">&quot;the state is propagated&quot;</span><span class="o">);</span>
<span class="o">}</span>
<span class="c1">// Don&#39;t need to check anything else, as if outputs.size == 1 then the output is equal to</span>
<span class="c1">// the input ignoring the owner field due to the grouping.</span>
<span class="k">return</span> <span class="n">Collections</span><span class="o">.</span><span class="na">singleton</span><span class="o">(</span><span class="n">cmd</span><span class="o">.</span><span class="na">getValue</span><span class="o">());</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="o">...</span>
</pre></div>
</div>
</div>
<p>We took part of code for <code class="docutils literal"><span class="pre">Command.Move</span></code> verification from previous tutorial and put it into the verify function
of <code class="docutils literal"><span class="pre">Move</span></code> class. Notice that this class must extend the <code class="docutils literal"><span class="pre">Clause</span></code> abstract class, which defines
the <code class="docutils literal"><span class="pre">verify</span></code> function, and the <code class="docutils literal"><span class="pre">requiredCommands</span></code> property used to determine the conditions under which a clause
is triggered. In the above example it means that the clause will run verification when the <code class="docutils literal"><span class="pre">Commands.Move</span></code> is present in a transaction.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Notice that commands refer to all input and output states in a transaction. For clause to be executed, transaction has
to include all commands from <code class="docutils literal"><span class="pre">requiredCommands</span></code> set.</p>
</div>
<p>Few important changes:</p>
<ul class="simple">
<li><code class="docutils literal"><span class="pre">verify</span></code> function returns the set of commands which it has processed. Normally this returned set is identical to the
<code class="docutils literal"><span class="pre">requiredCommands</span></code> used to trigger the clause, however in some cases the clause may process further optional commands
which it needs to report that it has handled.</li>
<li>Verification takes new parameters. Usually inputs and outputs are some subset of the original transaction entries
passed to the clause by outer composite or grouping clause. <code class="docutils literal"><span class="pre">groupingKey</span></code> is a key used to group original states.</li>
</ul>
<p>As a simple example imagine input states:</p>
<ol class="arabic simple">
<li>1000 GBP issued by Bank of England</li>
<li>500 GBP issued by Bank of England</li>
<li>1000 GBP issued by Bank of Scotland</li>
</ol>
<p>We will group states by Issuer so in the first group we have inputs 1 and 2, in second group input number 3. Grouping keys are:
&#8216;GBP issued by Bank of England&#8217; and &#8216;GBP issued by Bank of Scotland&#8217;.</p>
<p>How the states can be grouped and passed in that form to the <code class="docutils literal"><span class="pre">Move</span></code> clause? That leads us to the concept of <code class="docutils literal"><span class="pre">GroupClauseVerifier</span></code>.</p>
</div>
<div class="section" id="group-clause">
<h2>Group clause<a class="headerlink" href="#group-clause" title="Permalink to this headline"></a></h2>
<p>We may have a transaction with similar but unrelated state evolutions which need to be validated independently. It
makes sense to check <code class="docutils literal"><span class="pre">Move</span></code> command on groups of related inputs and outputs (see example above). Thus, we need to collect
relevant states together.
For this we extend the standard <code class="docutils literal"><span class="pre">GroupClauseVerifier</span></code> and specify how to group input/output states, as well as the top-level
clause to run on each group. In our example a top-level is a composite clause - <code class="docutils literal"><span class="pre">AnyCompostion</span></code> that delegates verification to
it&#8217;s subclasses (wrapped move, issue, redeem). Any in this case means that it will take 0 or more clauses that match transaction commands.</p>
<div class="codeset container">
<div class="highlight-kotlin"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Group</span> <span class="p">:</span> <span class="n">GroupClauseVerifier</span><span class="p">&lt;</span><span class="n">State</span><span class="p">,</span> <span class="n">Commands</span><span class="p">,</span> <span class="n">Issued</span><span class="p">&lt;</span><span class="n">Terms</span><span class="p">&gt;&gt;(</span>
<span class="n">AnyComposition</span><span class="p">(</span>
<span class="n">Redeem</span><span class="p">(),</span>
<span class="n">Move</span><span class="p">(),</span>
<span class="n">Issue</span><span class="p">()))</span> <span class="p">{</span>
<span class="k">override</span> <span class="k">fun</span> <span class="nf">groupStates</span><span class="p">(</span><span class="n">tx</span><span class="p">:</span> <span class="n">TransactionForContract</span><span class="p">):</span> <span class="n">List</span><span class="p">&lt;</span><span class="n">TransactionForContract</span><span class="p">.</span><span class="n">InOutGroup</span><span class="p">&lt;</span><span class="n">State</span><span class="p">,</span> <span class="n">Issued</span><span class="p">&lt;</span><span class="n">Terms</span><span class="p">&gt;&gt;&gt;</span>
<span class="p">=</span> <span class="n">tx</span><span class="p">.</span><span class="n">groupStates</span><span class="p">&lt;</span><span class="n">State</span><span class="p">,</span> <span class="n">Issued</span><span class="p">&lt;</span><span class="n">Terms</span><span class="p">&gt;&gt;</span> <span class="p">{</span> <span class="n">it</span><span class="p">.</span><span class="n">token</span> <span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
<div class="highlight-java"><div class="highlight"><pre><span></span><span class="kd">class</span> <span class="nc">Group</span> <span class="kd">extends</span> <span class="n">GroupClauseVerifier</span><span class="o">&lt;</span><span class="n">State</span><span class="o">,</span> <span class="n">Commands</span><span class="o">,</span> <span class="n">State</span><span class="o">&gt;</span> <span class="o">{</span>
<span class="kd">public</span> <span class="nf">Group</span><span class="o">()</span> <span class="o">{</span>
<span class="kd">super</span><span class="o">(</span><span class="k">new</span> <span class="n">AnyComposition</span><span class="o">&lt;&gt;(</span>
<span class="k">new</span> <span class="n">Clauses</span><span class="o">.</span><span class="na">Redeem</span><span class="o">(),</span>
<span class="k">new</span> <span class="n">Clauses</span><span class="o">.</span><span class="na">Move</span><span class="o">(),</span>
<span class="k">new</span> <span class="n">Clauses</span><span class="o">.</span><span class="na">Issue</span><span class="o">()</span>
<span class="o">));</span>
<span class="o">}</span>
<span class="nd">@NotNull</span>
<span class="nd">@Override</span>
<span class="kd">public</span> <span class="n">List</span><span class="o">&lt;</span><span class="n">InOutGroup</span><span class="o">&lt;</span><span class="n">State</span><span class="o">,</span> <span class="n">State</span><span class="o">&gt;&gt;</span> <span class="nf">groupStates</span><span class="o">(</span><span class="nd">@NotNull</span> <span class="n">TransactionForContract</span> <span class="n">tx</span><span class="o">)</span> <span class="o">{</span>
<span class="k">return</span> <span class="n">tx</span><span class="o">.</span><span class="na">groupStates</span><span class="o">(</span><span class="n">State</span><span class="o">.</span><span class="na">class</span><span class="o">,</span> <span class="n">State</span><span class="o">::</span><span class="n">withoutOwner</span><span class="o">);</span>
<span class="o">}</span>
<span class="o">}</span>
</pre></div>
</div>
</div>
<p>For the <code class="docutils literal"><span class="pre">CommercialPaper</span></code> contract, <code class="docutils literal"><span class="pre">Group</span></code> is the main clause for the contract, and is passed directly into
<code class="docutils literal"><span class="pre">verifyClause</span></code> (see the example code at the top of this tutorial). We used <code class="docutils literal"><span class="pre">groupStates</span></code> function here, it&#8217;s worth reminding
how it works: <a class="reference internal" href="tutorial-contract.html#state-ref"><span class="std std-ref">Using state groups</span></a>.</p>
</div>
<div class="section" id="summary">
<h2>Summary<a class="headerlink" href="#summary" title="Permalink to this headline"></a></h2>
<p>In summary the top level contract <code class="docutils literal"><span class="pre">CommercialPaper</span></code> specifies a single grouping clause of type
<code class="docutils literal"><span class="pre">CommercialPaper.Clauses.Group</span></code> which in turn specifies <code class="docutils literal"><span class="pre">GroupClause</span></code> implementations for each type of command
(<code class="docutils literal"><span class="pre">Redeem</span></code>, <code class="docutils literal"><span class="pre">Move</span></code> and <code class="docutils literal"><span class="pre">Issue</span></code>). This reflects the flow of verification: in order to verify a <code class="docutils literal"><span class="pre">CommercialPaper</span></code>
we first group states, check which commands are specified, and run command-specific verification logic accordingly.</p>
<img alt="_images/commPaperExecution.png" src="_images/commPaperExecution.png" />
</div>
<div class="section" id="debugging">
<h2>Debugging<a class="headerlink" href="#debugging" title="Permalink to this headline"></a></h2>
<p>Debugging clauses which have been composed together can be complicated due to the difficulty in knowing which clauses
have been matched, whether specific clauses failed to match or passed verification, etc. There is &#8220;trace&#8221; level
logging code in the clause verifier which evaluates which clauses will be matched and logs them, before actually
performing the validation. To enable this, ensure trace level logging is enabled on the <code class="docutils literal"><span class="pre">Clause</span></code> interface.</p>
</div>
</div>
</div>
</div>
<footer>
<div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
<a href="tutorial-test-dsl.html" class="btn btn-neutral float-right" title="Writing a contract test" accesskey="n">Next <span class="fa fa-arrow-circle-right"></span></a>
<a href="tutorial-contract.html" class="btn btn-neutral" title="Writing a contract" accesskey="p"><span class="fa fa-arrow-circle-left"></span> Previous</a>
</div>
<hr/>
<div role="contentinfo">
<p>
&copy; Copyright 2016, R3 Limited.
</p>
</div>
Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT:'./',
VERSION:'latest',
COLLAPSE_INDEX:false,
FILE_SUFFIX:'.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<script type="text/javascript" src="_static/js/theme.js"></script>
<script type="text/javascript">
jQuery(function () {
SphinxRtdTheme.StickyNav.enable();
});
</script>
</body>
</html>