corda/docs/build/html/tutorial-contract-clauses.html
Clinton Alexander 9a2963bca9 Docs regen.
2017-02-22 10:59:02 +00:00

545 lines
46 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>
API reference: <a href="api/kotlin/corda/index.html">Kotlin</a>/ <a href="api/javadoc/index.html">JavaDoc</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">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="key-concepts.html">Overview</a></li>
<li class="toctree-l1"><a class="reference internal" href="key-concepts-ecosystem.html">Corda ecosystem</a></li>
<li class="toctree-l1"><a class="reference internal" href="key-concepts-data-model.html">Data model</a></li>
<li class="toctree-l1"><a class="reference internal" href="key-concepts-core-types.html">Core types</a></li>
<li class="toctree-l1"><a class="reference internal" href="key-concepts-financial-model.html">Financial model</a></li>
<li class="toctree-l1"><a class="reference internal" href="key-concepts-flow-framework.html">Flow framework</a></li>
<li class="toctree-l1"><a class="reference internal" href="key-concepts-consensus-notaries.html">Consensus and notaries</a></li>
<li class="toctree-l1"><a class="reference internal" href="key-concepts-vault.html">Vault</a></li>
<li class="toctree-l1"><a class="reference internal" href="key-concepts-security-model.html">Security model</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">CorDapp basics</a></li>
<li class="toctree-l1"><a class="reference internal" href="tutorial-cordapp.html">The example CorDapp</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-use-them">What are clauses and why use them?</a></li>
<li class="toctree-l2"><a class="reference internal" href="#how-do-clauses-work">How do 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="contract-upgrade.html">Upgrading Contracts</a></li>
<li class="toctree-l1"><a class="reference internal" href="tutorial-integration-testing.html">Integration testing</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="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="clauses.html">Clauses</a></li>
<li class="toctree-l1"><a class="reference internal" href="merkle-trees.html">Transaction tear-offs</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">What is a corda 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-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="further-notes-on-kotlin.html">Further notes on Kotlin</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>In this tutorial, we will restructure 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;.</p>
<p>As before, this example is focused on a basic implementation of commercial paper (CP), which is essentially a simpler version of a corporate
bond. A company issues commercial paper with a particular face value, say $100, but sells it for less, say $90. The paper can be redeemed
for cash at a given future date. In our example, the commercial paper has a 10% interest rate, with a single repayment.
The full 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-use-them">
<h2>What are clauses and why use them?<a class="headerlink" href="#what-are-clauses-and-why-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
to form a complete 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 used 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, these can also be readily tested in isolation.</p>
</div>
<div class="section" id="how-do-clauses-work">
<h2>How do clauses work?<a class="headerlink" href="#how-do-clauses-work" title="Permalink to this headline"></a></h2>
<p>There are different types of clauses. The most basic are those that define the verification logic for a single command
(e.g. <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>, in the case of commercial paper), or even run without any commands at all (e.g. <code class="docutils literal"><span class="pre">Timestamp</span></code>).</p>
<p>These basic clauses can then be combined using a <code class="docutils literal"><span class="pre">CompositeClause</span></code>. The goal of composite clauses is to determine
which individual clauses need to be matched and verified for a given transaction
to be considered valid. We refer to a clause as being &#8220;matched&#8221; when the transaction has the required commands present for the clause
in question to trigger. Meanwhile, we talk about a clause &#8220;verifying&#8221; when its <code class="docutils literal"><span class="pre">verify()</span></code> function returns <code class="docutils literal"><span class="pre">True</span></code>.</p>
<p>As an example, let&#8217;s say we want a transaction to be valid only when every single one of its clauses matches and verifies. We implement this
by wrapping the individual clauses into an <code class="docutils literal"><span class="pre">AllOf</span></code> composite clause, which ensures that a transaction is
only considered valid if all of its clauses are both matched and verify.</p>
<p>There are two other basic composite clauses that you should be aware of:</p>
<blockquote>
<div><ul class="simple">
<li><code class="docutils literal"><span class="pre">AnyOf</span></code>, whereby 1 or more clauses may match, and every matched clause must verify</li>
<li><code class="docutils literal"><span class="pre">FirstOf</span></code>, whereby at least one clause must match, and the first such clause must verify</li>
</ul>
</div></blockquote>
<p>In turn, composite clauses are themselves <code class="docutils literal"><span class="pre">Clause</span></code> s, and can, for example, be wrapped in the special <code class="docutils literal"><span class="pre">GroupClauseVerifier</span></code> grouping clause.
For <code class="docutils literal"><span class="pre">CommercialPaper</span></code>, this would look as follows:</p>
<img alt="_images/commPaperClauses.png" src="_images/commPaperClauses.png" />
<p>For this tutorial, we will be using <code class="docutils literal"><span class="pre">GroupClauseVerifier</span></code> and <code class="docutils literal"><span class="pre">AnyOf</span></code>. Since it&#8217;s important to understand how these work,
charts showing their execution and other details can be found in <a class="reference internal" href="clauses.html"><span class="doc">Clauses</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 by defining the <code class="docutils literal"><span class="pre">CommercialPaper</span></code> class. As in the previous tutorial, we need some elementary parts: a <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 - these stay the same. The new part is verification and the
<code class="docutils literal"><span class="pre">Clauses</span></code> interface (which we will see 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 our previous verification logic.
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 all of the commands the clause is expected to handle. 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, raising 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. The 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 only show the <code class="docutils literal"><span class="pre">Move</span></code> clause. The rest is constructed in similar manner,
and is 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 the code for <code class="docutils literal"><span class="pre">Command.Move</span></code> verification from the 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, this means that the clause will run its verification logic when <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 a clause to be executed, the transaction has
to include all commands from the <code class="docutils literal"><span class="pre">requiredCommands</span></code> set.</p>
</div>
<p>A few important changes:</p>
<ul class="simple">
<li>The <code class="docutils literal"><span class="pre">verify</span></code> function returns the set of commands which it has processed. Normally this 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 the following 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, meaning that we have inputs 1 and 2 in one group, and input 3 in another group. The grouping keys are
&#8216;GBP issued by Bank of England&#8217; and &#8216;GBP issued by Bank of Scotland&#8217;.</p>
<p>How are the states grouped and passed in this form to the <code class="docutils literal"><span class="pre">Move</span></code> clause? Answering that question 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 the <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, the top level is a composite clause - <code class="docutils literal"><span class="pre">AnyCompostion</span></code> - that delegates verification to
its subclauses (wrapped move, issue, redeem). &#8220;Any&#8221; in this case means that it will take 0 or more clauses that match the 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">AnyOf</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">AnyOf</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 also used <code class="docutils literal"><span class="pre">groupStates</span></code> function here - it
may be worth reminding yourself how it works here: <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 verification flow: in order to verify <code class="docutils literal"><span class="pre">CommercialPaper</span></code>,
we first group states, then we check which commands are specified, and finally we 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>