2017-01-03 13:07:48 +00:00
<!-- 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 -->
2016-11-11 11:52:29 +00:00
<!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 test — 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" / >
2017-01-06 17:38:23 +00:00
< link rel = "next" title = "Integration testing" href = "tutorial-integration-testing.html" / >
2016-11-11 11:52:29 +00:00
< link rel = "prev" title = "Writing a contract using clauses" href = "tutorial-contract-clauses.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 >
2017-01-19 17:11:18 +00:00
API reference: < a href = "api/kotlin/corda/index.html" > Kotlin< / a > / < a href = "api/javadoc/index.html" > JavaDoc< / a >
2017-01-03 13:07:48 +00:00
< br >
< a href = "https://discourse.corda.net" > Discourse Forums< / a >
< br >
< a href = "http://slack.corda.net" > Slack< / a >
< br >
2016-11-11 11:52:29 +00:00
< / div >
< div class = "wy-menu wy-menu-vertical" data-spy = "affix" role = "navigation" aria-label = "main navigation" >
2016-11-23 12:50:02 +00:00
< p class = "caption" > < span class = "caption-text" > Getting started< / span > < / p >
2016-11-11 11:52:29 +00:00
< ul >
< li class = "toctree-l1" > < a class = "reference internal" href = "inthebox.html" > What’ s included?< / a > < / li >
< li class = "toctree-l1" > < a class = "reference internal" href = "getting-set-up.html" > Getting set up< / a > < / li >
2017-01-06 17:38:23 +00:00
< li class = "toctree-l1" > < a class = "reference internal" href = "getting-set-up-fault-finding.html" > Troubleshooting< / a > < / li >
2016-11-23 12:50:02 +00:00
< li class = "toctree-l1" > < a class = "reference internal" href = "running-the-demos.html" > Running the demos< / a > < / li >
2016-11-29 18:44:59 +00:00
< li class = "toctree-l1" > < a class = "reference internal" href = "CLI-vs-IDE.html" > CLI vs IDE< / a > < / li >
2016-11-23 12:50:02 +00:00
< / ul >
< p class = "caption" > < span class = "caption-text" > Key concepts< / span > < / p >
< ul >
2016-11-11 11:52:29 +00:00
< 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 >
2016-11-29 18:44:59 +00:00
< li class = "toctree-l1" > < a class = "reference internal" href = "merkle-trees.html" > Transaction tear-offs< / a > < / li >
2016-11-11 11:52:29 +00:00
< li class = "toctree-l1" > < a class = "reference internal" href = "consensus.html" > Consensus model< / a > < / li >
2017-01-06 17:38:23 +00:00
< li class = "toctree-l1" > < a class = "reference internal" href = "clauses.html" > Clauses< / a > < / li >
2016-11-29 18:44:59 +00:00
< / ul >
< p class = "caption" > < span class = "caption-text" > CorDapps< / span > < / p >
< ul >
2017-01-06 17:38:23 +00:00
< 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 CorDapp template< / a > < / li >
2016-11-23 12:50:02 +00:00
< / 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 >
2016-11-11 11:52:29 +00:00
< 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 >
2016-11-29 18:44:59 +00:00
< 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 >
2016-11-11 11:52:29 +00:00
< / 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" > < a class = "reference internal" href = "tutorial-contract-clauses.html" > Writing a contract using clauses< / a > < / li >
< li class = "toctree-l1 current" > < a class = "current reference internal" href = "#" > Writing a contract test< / a > < ul >
< li class = "toctree-l2" > < a class = "reference internal" href = "#testing-single-transactions" > Testing single transactions< / a > < / li >
< li class = "toctree-l2" > < a class = "reference internal" href = "#chaining-transactions" > Chaining transactions< / a > < / li >
< / ul >
< / li >
2017-01-06 17:38:23 +00:00
< li class = "toctree-l1" > < a class = "reference internal" href = "tutorial-integration-testing.html" > Integration testing< / a > < / li >
2016-11-29 18:44:59 +00:00
< li class = "toctree-l1" > < a class = "reference internal" href = "tutorial-clientrpc-api.html" > Client RPC API tutorial< / a > < / li >
2017-01-06 17:38:23 +00:00
< li class = "toctree-l1" > < a class = "reference internal" href = "tutorial-building-transactions.html" > Building transactions< / a > < / li >
2016-11-29 18:44:59 +00:00
< 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 >
2016-11-11 11:52:29 +00:00
< 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 >
2016-11-23 12:50:02 +00:00
< p class = "caption" > < span class = "caption-text" > Other< / span > < / p >
2016-11-11 11:52:29 +00:00
< ul >
2016-11-23 12:50:02 +00:00
< li class = "toctree-l1" > < a class = "reference internal" href = "network-simulator.html" > Network Simulator< / a > < / li >
2016-11-11 11:52:29 +00:00
< / ul >
2016-11-23 12:50:02 +00:00
< p class = "caption" > < span class = "caption-text" > Component library< / span > < / p >
2016-11-11 11:52:29 +00:00
< ul >
2016-11-23 12:50:02 +00:00
< li class = "toctree-l1" > < a class = "reference internal" href = "contract-catalogue.html" > Contract catalogue< / a > < / li >
2016-11-29 18:44:59 +00:00
< li class = "toctree-l1" > < a class = "reference internal" href = "contract-irs.html" > Interest rate swaps< / a > < / li >
2016-11-11 11:52:29 +00:00
< / ul >
< p class = "caption" > < span class = "caption-text" > Appendix< / span > < / p >
< ul >
2016-11-18 12:57:39 +00:00
< li class = "toctree-l1" > < a class = "reference internal" href = "loadtesting.html" > Load testing< / a > < / li >
2017-01-06 17:38:23 +00:00
< li class = "toctree-l1" > < a class = "reference internal" href = "setting-up-a-corda-network.html" > What is a corda network?< / a > < / li >
2016-11-11 11:52:29 +00:00
< 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 >
2017-01-03 13:07:48 +00:00
< 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 >
2016-11-11 11:52:29 +00:00
< / ul >
2016-11-23 12:50:02 +00:00
< 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 >
2016-11-11 11:52:29 +00:00
< / 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 > » < / li >
< li > Writing a contract test< / li >
< li class = "wy-breadcrumbs-aside" >
< a href = "_sources/tutorial-test-dsl.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-test" >
< h1 > Writing a contract test< a class = "headerlink" href = "#writing-a-contract-test" title = "Permalink to this headline" > ¶< / a > < / h1 >
2017-01-03 13:07:48 +00:00
< p > This tutorial will take you through the steps required to write a contract test using Kotlin and Java.< / p >
2016-11-11 11:52:29 +00:00
< p > The testing DSL allows one to define a piece of the ledger with transactions referring to each other, and ways of
verifying their correctness.< / p >
< div class = "section" id = "testing-single-transactions" >
< h2 > Testing single transactions< a class = "headerlink" href = "#testing-single-transactions" title = "Permalink to this headline" > ¶< / a > < / h2 >
< p > We start with the empty ledger:< / p >
< div class = "codeset container" >
2017-01-03 13:07:48 +00:00
< div class = "highlight-kotlin" > < div class = "highlight" > < pre > < span > < / span > < span class = "k" > class< / span > < span class = "nc" > CommercialPaperTest< / span > < span class = "p" > {< / span >
< span class = "n" > @Test< / span >
< span class = "k" > fun< / span > < span class = "nf" > emptyLedger< / span > < span class = "p" > ()< / span > < span class = "p" > {< / span >
< span class = "n" > ledger< / span > < span class = "p" > {< / span >
< span class = "p" > }< / span >
2016-11-11 11:52:29 +00:00
< span class = "p" > }< / span >
2017-01-03 13:07:48 +00:00
< span class = "p" > ...< / span >
2016-11-11 11:52:29 +00:00
< span class = "p" > }< / span >
< / pre > < / div >
< / div >
< div class = "highlight-java" > < div class = "highlight" > < pre > < span > < / span > < span class = "kn" > import< / span > < span class = "nn" > static< / span > < span class = "n" > net< / span > < span class = "o" > .< / span > < span class = "na" > corda< / span > < span class = "o" > .< / span > < span class = "na" > core< / span > < span class = "o" > .< / span > < span class = "na" > testing< / span > < span class = "o" > .< / span > < span class = "na" > JavaTestHelpers< / span > < span class = "o" > .*;< / span >
< span class = "kn" > import< / span > < span class = "nn" > static< / span > < span class = "n" > net< / span > < span class = "o" > .< / span > < span class = "na" > corda< / span > < span class = "o" > .< / span > < span class = "na" > core< / span > < span class = "o" > .< / span > < span class = "na" > contracts< / span > < span class = "o" > .< / span > < span class = "na" > JavaTestHelpers< / span > < span class = "o" > .*;< / span >
< span class = "nd" > @Test< / span >
< span class = "kd" > public< / span > < span class = "kt" > void< / span > < span class = "nf" > emptyLedger< / span > < span class = "o" > ()< / span > < span class = "o" > {< / span >
< span class = "n" > ledger< / span > < span class = "o" > (< / span > < span class = "n" > l< / span > < span class = "o" > -> < / span > < span class = "o" > {< / span >
< span class = "k" > return< / span > < span class = "n" > Unit< / span > < span class = "o" > .< / span > < span class = "na" > INSTANCE< / span > < span class = "o" > ;< / span > < span class = "c1" > // We need to return this explicitly< / span >
< span class = "o" > });< / span >
< span class = "o" > }< / span >
< / pre > < / div >
< / div >
< / div >
< p > The DSL keyword < code class = "docutils literal" > < span class = "pre" > ledger< / span > < / code > takes a closure that can build up several transactions and may verify their overall
correctness. A ledger is effectively a fresh world with no pre-existing transactions or services within it.< / p >
2017-01-03 13:07:48 +00:00
< p > We will start with defining helper function that returns a < code class = "docutils literal" > < span class = "pre" > CommercialPaper< / span > < / code > state:< / p >
2016-11-11 11:52:29 +00:00
< div class = "codeset container" >
2017-01-03 13:07:48 +00:00
< div class = "highlight-kotlin" > < div class = "highlight" > < pre > < span > < / span > fun getPaper(): ICommercialPaperState = CommercialPaper.State(
issuance = MEGA_CORP.ref(123),
owner = MEGA_CORP_PUBKEY,
faceValue = 1000.DOLLARS `issued by` MEGA_CORP.ref(123),
maturityDate = TEST_TX_TIME + 7.days
)
2016-11-11 11:52:29 +00:00
< / pre > < / div >
< / div >
2017-01-03 13:07:48 +00:00
< div class = "highlight-java" > < div class = "highlight" > < pre > < span > < / span > < span class = "kd" > private< / span > < span class = "kd" > final< / span > < span class = "n" > OpaqueBytes< / span > < span class = "n" > defaultRef< / span > < span class = "o" > =< / span > < span class = "k" > new< / span > < span class = "n" > OpaqueBytes< / span > < span class = "o" > (< / span > < span class = "k" > new< / span > < span class = "kt" > byte< / span > < span class = "o" > []{< / span > < span class = "mi" > 123< / span > < span class = "o" > });< / span >
< span class = "kd" > private< / span > < span class = "n" > ICommercialPaperState< / span > < span class = "nf" > getPaper< / span > < span class = "o" > ()< / span > < span class = "o" > {< / span >
< span class = "k" > return< / span > < span class = "k" > new< / span > < span class = "n" > JavaCommercialPaper< / span > < span class = "o" > .< / span > < span class = "na" > State< / span > < span class = "o" > (< / span >
< span class = "n" > getMEGA_CORP< / span > < span class = "o" > ().< / span > < span class = "na" > ref< / span > < span class = "o" > (< / span > < span class = "n" > defaultRef< / span > < span class = "o" > ),< / span >
< span class = "n" > getMEGA_CORP_PUBKEY< / span > < span class = "o" > (),< / span >
< span class = "n" > issuedBy< / span > < span class = "o" > (< / span > < span class = "n" > DOLLARS< / span > < span class = "o" > (< / span > < span class = "mi" > 1000< / span > < span class = "o" > ),< / span > < span class = "n" > getMEGA_CORP< / span > < span class = "o" > ().< / span > < span class = "na" > ref< / span > < span class = "o" > (< / span > < span class = "n" > defaultRef< / span > < span class = "o" > )),< / span >
< span class = "n" > getTEST_TX_TIME< / span > < span class = "o" > ().< / span > < span class = "na" > plus< / span > < span class = "o" > (< / span > < span class = "mi" > 7< / span > < span class = "o" > ,< / span > < span class = "n" > ChronoUnit< / span > < span class = "o" > .< / span > < span class = "na" > DAYS< / span > < span class = "o" > )< / span >
2016-11-11 11:52:29 +00:00
< span class = "o" > );< / span >
2017-01-03 13:07:48 +00:00
< span class = "o" > }< / span >
< / pre > < / div >
< / div >
< / div >
< p > It’ s a < code class = "docutils literal" > < span class = "pre" > CommercialPaper< / span > < / code > issued by < code class = "docutils literal" > < span class = "pre" > MEGA_CORP< / span > < / code > with face value of $1000 and maturity date in 7 days.< / p >
< p > Let’ s add a < code class = "docutils literal" > < span class = "pre" > CommercialPaper< / span > < / code > transaction:< / p >
< div class = "codeset container" >
< div class = "highlight-kotlin" > < div class = "highlight" > < pre > < span > < / span > < span class = "n" > @Test< / span >
< span class = "k" > fun< / span > < span class = "nf" > simpleCPDoesntCompile< / span > < span class = "p" > ()< / span > < span class = "p" > {< / span >
< span class = "k" > val< / span > < span class = "py" > inState< / span > < span class = "p" > =< / span > < span class = "n" > getPaper< / span > < span class = "p" > ()< / span >
< span class = "n" > ledger< / span > < span class = "p" > {< / span >
< span class = "n" > transaction< / span > < span class = "p" > {< / span >
< span class = "n" > input< / span > < span class = "p" > (< / span > < span class = "n" > inState< / 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 = "nd" > @Test< / span >
< span class = "kd" > public< / span > < span class = "kt" > void< / span > < span class = "nf" > simpleCPDoesntCompile< / span > < span class = "o" > ()< / span > < span class = "o" > {< / span >
< span class = "n" > ICommercialPaperState< / span > < span class = "n" > inState< / span > < span class = "o" > =< / span > < span class = "n" > getPaper< / span > < span class = "o" > ();< / span >
2016-11-11 11:52:29 +00:00
< span class = "n" > ledger< / span > < span class = "o" > (< / span > < span class = "n" > l< / span > < span class = "o" > -> < / span > < span class = "o" > {< / span >
< span class = "n" > l< / span > < span class = "o" > .< / span > < span class = "na" > transaction< / span > < span class = "o" > (< / span > < span class = "n" > tx< / span > < span class = "o" > -> < / span > < span class = "o" > {< / span >
< span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > input< / span > < span class = "o" > (< / span > < span class = "n" > inState< / span > < span class = "o" > );< / span >
< span class = "o" > });< / span >
< span class = "k" > return< / span > < span class = "n" > Unit< / span > < span class = "o" > .< / span > < span class = "na" > INSTANCE< / span > < span class = "o" > ;< / span >
< span class = "o" > });< / span >
< span class = "o" > }< / span >
< / pre > < / div >
< / div >
< / div >
< p > We can add a transaction to the ledger using the < code class = "docutils literal" > < span class = "pre" > transaction< / span > < / code > primitive. The transaction in turn may be defined by
specifying < code class = "docutils literal" > < span class = "pre" > input< / span > < / code > -s, < code class = "docutils literal" > < span class = "pre" > output< / span > < / code > -s, < code class = "docutils literal" > < span class = "pre" > command< / span > < / code > -s and < code class = "docutils literal" > < span class = "pre" > attachment< / span > < / code > -s.< / p >
2017-01-03 13:07:48 +00:00
< p > The above < code class = "docutils literal" > < span class = "pre" > input< / span > < / code > call is a bit special; transactions don’ t actually contain input states, just references
2016-11-11 11:52:29 +00:00
to output states of other transactions. Under the hood the above < code class = "docutils literal" > < span class = "pre" > input< / span > < / code > call creates a dummy transaction in the
ledger (that won’ t be verified) which outputs the specified state, and references that from this transaction.< / p >
< p > The above code however doesn’ t compile:< / p >
< div class = "codeset container" >
2017-01-03 13:07:48 +00:00
< div class = "highlight-kotlin" > < div class = "highlight" > < pre > < span > < / span > < span class = "n" > Error< / span > < span class = "p" > :(< / span > < span class = "m" > 29< / span > < span class = "p" > ,< / span > < span class = "m" > 17< / span > < span class = "p" > )< / span > < span class = "n" > Kotlin< / span > < span class = "p" > :< / span > < span class = "n" > Type< / span > < span class = "n" > mismatch< / span > < span class = "p" > :< / span > < span class = "n" > inferred< / span > < span class = "n" > type< / span > < span class = "k" > is< / span > < span class = "n" > Unit< / span > < span class = "n" > but< / span > < span class = "n" > EnforceVerifyOrFail< / span > < span class = "n" > was< / span > < span class = "n" > expected< / span >
2016-11-11 11:52:29 +00:00
< / pre > < / div >
< / div >
2017-01-03 13:07:48 +00:00
< div class = "highlight-java" > < div class = "highlight" > < pre > < span > < / span > < span class = "nl" > Error:< / span > < span class = "o" > (< / span > < span class = "mi" > 35< / span > < span class = "o" > ,< / span > < span class = "mi" > 27< / span > < span class = "o" > )< / span > < span class = "n" > java< / span > < span class = "o" > :< / span > < span class = "n" > incompatible< / span > < span class = "n" > types< / span > < span class = "o" > :< / span > < span class = "n" > bad< / span > < span class = "k" > return< / span > < span class = "n" > type< / span > < span class = "n" > in< / span > < span class = "n" > lambda< / span > < span class = "n" > expression< / span > < span class = "n" > missing< / span > < span class = "k" > return< / span > < span class = "n" > value< / span >
2016-11-11 11:52:29 +00:00
< / pre > < / div >
< / div >
< / div >
< p > This is deliberate: The DSL forces us to specify either < code class = "docutils literal" > < span class = "pre" > this.verifies()< / span > < / code > or < code class = "docutils literal" > < span class = "pre" > this< / span > < span class = "pre" > `fails< / span > < span class = "pre" > with`< / span > < span class = "pre" > " some< / span > < span class = "pre" > text" < / span > < / code > on the
last line of < code class = "docutils literal" > < span class = "pre" > transaction< / span > < / code > :< / p >
< div class = "codeset container" >
2017-01-03 13:07:48 +00:00
< div class = "highlight-kotlin" > < div class = "highlight" > < pre > < span > < / span > < span class = "n" > @Test< / span >
< span class = "k" > fun< / span > < span class = "nf" > simpleCP< / span > < span class = "p" > ()< / span > < span class = "p" > {< / span >
< span class = "k" > val< / span > < span class = "py" > inState< / span > < span class = "p" > =< / span > < span class = "n" > getPaper< / span > < span class = "p" > ()< / span >
< span class = "n" > ledger< / span > < span class = "p" > {< / span >
< span class = "n" > transaction< / span > < span class = "p" > {< / span >
< span class = "n" > input< / span > < span class = "p" > (< / span > < span class = "n" > inState< / span > < span class = "p" > )< / span >
< span class = "k" > this< / span > < span class = "p" > .< / span > < span class = "n" > verifies< / span > < span class = "p" > ()< / span >
< span class = "p" > }< / span >
< span class = "p" > }< / span >
< span class = "p" > }< / span >
2016-11-11 11:52:29 +00:00
< / pre > < / div >
< / div >
< div class = "highlight-java" > < div class = "highlight" > < pre > < span > < / span > < span class = "nd" > @Test< / span >
2017-01-03 13:07:48 +00:00
< span class = "kd" > public< / span > < span class = "kt" > void< / span > < span class = "nf" > simpleCP< / span > < span class = "o" > ()< / span > < span class = "o" > {< / span >
< span class = "n" > ICommercialPaperState< / span > < span class = "n" > inState< / span > < span class = "o" > =< / span > < span class = "n" > getPaper< / span > < span class = "o" > ();< / span >
2016-11-11 11:52:29 +00:00
< span class = "n" > ledger< / span > < span class = "o" > (< / span > < span class = "n" > l< / span > < span class = "o" > -> < / span > < span class = "o" > {< / span >
< span class = "n" > l< / span > < span class = "o" > .< / span > < span class = "na" > transaction< / span > < span class = "o" > (< / span > < span class = "n" > tx< / span > < span class = "o" > -> < / span > < span class = "o" > {< / span >
< span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > input< / span > < span class = "o" > (< / span > < span class = "n" > inState< / span > < span class = "o" > );< / span >
< span class = "k" > return< / span > < span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > verifies< / span > < span class = "o" > ();< / span >
< span class = "o" > });< / span >
< span class = "k" > return< / span > < span class = "n" > Unit< / span > < span class = "o" > .< / span > < span class = "na" > INSTANCE< / span > < span class = "o" > ;< / span >
< span class = "o" > });< / span >
< span class = "o" > }< / span >
< / pre > < / div >
< / div >
< / div >
2017-01-03 13:07:48 +00:00
< p > Let’ s take a look at a transaction that fails.< / p >
< div class = "codeset container" >
< div class = "highlight-kotlin" > < div class = "highlight" > < pre > < span > < / span > < span class = "n" > @Test< / span >
< span class = "k" > fun< / span > < span class = "nf" > simpleCPMove< / span > < span class = "p" > ()< / span > < span class = "p" > {< / span >
< span class = "k" > val< / span > < span class = "py" > inState< / span > < span class = "p" > =< / span > < span class = "n" > getPaper< / span > < span class = "p" > ()< / span >
< span class = "n" > ledger< / span > < span class = "p" > {< / span >
< span class = "n" > transaction< / span > < span class = "p" > {< / span >
< span class = "n" > input< / span > < span class = "p" > (< / span > < span class = "n" > inState< / span > < span class = "p" > )< / span >
< span class = "n" > command< / span > < span class = "p" > (< / span > < span class = "n" > MEGA_CORP_PUBKEY< / span > < span class = "p" > )< / span > < span class = "p" > {< / span > < span class = "n" > CommercialPaper< / 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 = "p" > }< / span >
< span class = "k" > this< / span > < span class = "p" > .< / span > < span class = "n" > verifies< / 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 = "nd" > @Test< / span >
< span class = "kd" > public< / span > < span class = "kt" > void< / span > < span class = "nf" > simpleCPMove< / span > < span class = "o" > ()< / span > < span class = "o" > {< / span >
< span class = "n" > ICommercialPaperState< / span > < span class = "n" > inState< / span > < span class = "o" > =< / span > < span class = "n" > getPaper< / span > < span class = "o" > ();< / span >
< span class = "n" > ledger< / span > < span class = "o" > (< / span > < span class = "n" > l< / span > < span class = "o" > -> < / span > < span class = "o" > {< / span >
< span class = "n" > l< / span > < span class = "o" > .< / span > < span class = "na" > transaction< / span > < span class = "o" > (< / span > < span class = "n" > tx< / span > < span class = "o" > -> < / span > < span class = "o" > {< / span >
< span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > input< / span > < span class = "o" > (< / span > < span class = "n" > inState< / span > < span class = "o" > );< / span >
< span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > command< / span > < span class = "o" > (< / span > < span class = "n" > getMEGA_CORP_PUBKEY< / span > < span class = "o" > (),< / span > < span class = "k" > new< / span > < span class = "n" > JavaCommercialPaper< / span > < span class = "o" > .< / span > < span class = "na" > Commands< / span > < span class = "o" > .< / span > < span class = "na" > Move< / span > < span class = "o" > ());< / span >
< span class = "k" > return< / span > < span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > verifies< / span > < span class = "o" > ();< / span >
< span class = "o" > });< / span >
< span class = "k" > return< / span > < span class = "n" > Unit< / span > < span class = "o" > .< / span > < span class = "na" > INSTANCE< / span > < span class = "o" > ;< / span >
< span class = "o" > });< / span >
< span class = "o" > }< / span >
< / pre > < / div >
< / div >
< / div >
< p > When run, that code produces the following error:< / p >
< div class = "codeset container" >
< div class = "highlight-kotlin" > < div class = "highlight" > < pre > < span > < / span > net.corda.core.contracts.TransactionVerificationException$ContractRejection: java.lang.IllegalArgumentException: Failed requirement: the state is propagated
< / pre > < / div >
< / div >
< div class = "highlight-java" > < div class = "highlight" > < pre > < span > < / span > < span class = "n" > net< / span > < span class = "o" > .< / span > < span class = "na" > corda< / span > < span class = "o" > .< / span > < span class = "na" > core< / span > < span class = "o" > .< / span > < span class = "na" > contracts< / span > < span class = "o" > .< / span > < span class = "na" > TransactionVerificationException$ContractRejection< / span > < span class = "o" > :< / span > < span class = "n" > java< / span > < span class = "o" > .< / span > < span class = "na" > lang< / span > < span class = "o" > .< / span > < span class = "na" > IllegalStateException< / span > < span class = "o" > :< / span > < span class = "n" > the< / span > < span class = "n" > state< / span > < span class = "n" > is< / span > < span class = "n" > propagated< / span >
2016-11-11 11:52:29 +00:00
< / pre > < / div >
< / div >
< / div >
2017-01-03 13:07:48 +00:00
< p > The transaction verification failed, because we wanted to move paper but didn’ t specify an output - but the state should be propagated.
However we can specify that this is an intended behaviour by changing < code class = "docutils literal" > < span class = "pre" > this.verifies()< / span > < / code > to < code class = "docutils literal" > < span class = "pre" > this< / span > < span class = "pre" > `fails< / span > < span class = "pre" > with`< / span > < span class = "pre" > " the< / span > < span class = "pre" > state< / span > < span class = "pre" > is< / span > < span class = "pre" > propagated" < / span > < / code > :< / p >
2016-11-11 11:52:29 +00:00
< div class = "codeset container" >
< div class = "highlight-kotlin" > < div class = "highlight" > < pre > < span > < / span > @Test
2017-01-03 13:07:48 +00:00
fun simpleCPMoveFails() {
val inState = getPaper()
2016-11-11 11:52:29 +00:00
ledger {
transaction {
input(inState)
2017-01-03 13:07:48 +00:00
command(MEGA_CORP_PUBKEY) { CommercialPaper.Commands.Move() }
this `fails with` " the state is propagated"
2016-11-11 11:52:29 +00:00
}
}
}
< / pre > < / div >
< / div >
< div class = "highlight-java" > < div class = "highlight" > < pre > < span > < / span > < span class = "nd" > @Test< / span >
2017-01-03 13:07:48 +00:00
< span class = "kd" > public< / span > < span class = "kt" > void< / span > < span class = "nf" > simpleCPMoveFails< / span > < span class = "o" > ()< / span > < span class = "o" > {< / span >
< span class = "n" > ICommercialPaperState< / span > < span class = "n" > inState< / span > < span class = "o" > =< / span > < span class = "n" > getPaper< / span > < span class = "o" > ();< / span >
2016-11-11 11:52:29 +00:00
< span class = "n" > ledger< / span > < span class = "o" > (< / span > < span class = "n" > l< / span > < span class = "o" > -> < / span > < span class = "o" > {< / span >
< span class = "n" > l< / span > < span class = "o" > .< / span > < span class = "na" > transaction< / span > < span class = "o" > (< / span > < span class = "n" > tx< / span > < span class = "o" > -> < / span > < span class = "o" > {< / span >
< span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > input< / span > < span class = "o" > (< / span > < span class = "n" > inState< / span > < span class = "o" > );< / span >
2017-01-03 13:07:48 +00:00
< span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > command< / span > < span class = "o" > (< / span > < span class = "n" > getMEGA_CORP_PUBKEY< / span > < span class = "o" > (),< / span > < span class = "k" > new< / span > < span class = "n" > JavaCommercialPaper< / span > < span class = "o" > .< / span > < span class = "na" > Commands< / span > < span class = "o" > .< / span > < span class = "na" > Move< / span > < span class = "o" > ());< / span >
< span class = "k" > return< / span > < span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > failsWith< / span > < span class = "o" > (< / span > < span class = "s" > " the state is propagated" < / span > < span class = "o" > );< / span >
2016-11-11 11:52:29 +00:00
< span class = "o" > });< / span >
< span class = "k" > return< / span > < span class = "n" > Unit< / span > < span class = "o" > .< / span > < span class = "na" > INSTANCE< / span > < span class = "o" > ;< / span >
< span class = "o" > });< / span >
< span class = "o" > }< / span >
< / pre > < / div >
< / div >
< / div >
< p > We can continue to build the transaction until it < code class = "docutils literal" > < span class = "pre" > verifies< / span > < / code > :< / p >
< div class = "codeset container" >
< div class = "highlight-kotlin" > < div class = "highlight" > < pre > < span > < / span > @Test
2017-01-03 13:07:48 +00:00
fun simpleCPMoveSuccess() {
val inState = getPaper()
2016-11-11 11:52:29 +00:00
ledger {
transaction {
input(inState)
2017-01-03 13:07:48 +00:00
command(MEGA_CORP_PUBKEY) { CommercialPaper.Commands.Move() }
this `fails with` " the state is propagated"
output(" alice' s paper" ) { inState `owned by` ALICE_PUBKEY }
2016-11-11 11:52:29 +00:00
this.verifies()
}
}
}
< / pre > < / div >
< / div >
< div class = "highlight-java" > < div class = "highlight" > < pre > < span > < / span > < span class = "nd" > @Test< / span >
2017-01-03 13:07:48 +00:00
< span class = "kd" > public< / span > < span class = "kt" > void< / span > < span class = "nf" > simpleCPMoveSuccess< / span > < span class = "o" > ()< / span > < span class = "o" > {< / span >
< span class = "n" > ICommercialPaperState< / span > < span class = "n" > inState< / span > < span class = "o" > =< / span > < span class = "n" > getPaper< / span > < span class = "o" > ();< / span >
2016-11-11 11:52:29 +00:00
< span class = "n" > ledger< / span > < span class = "o" > (< / span > < span class = "n" > l< / span > < span class = "o" > -> < / span > < span class = "o" > {< / span >
< span class = "n" > l< / span > < span class = "o" > .< / span > < span class = "na" > transaction< / span > < span class = "o" > (< / span > < span class = "n" > tx< / span > < span class = "o" > -> < / span > < span class = "o" > {< / span >
< span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > input< / span > < span class = "o" > (< / span > < span class = "n" > inState< / span > < span class = "o" > );< / span >
2017-01-03 13:07:48 +00:00
< span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > command< / span > < span class = "o" > (< / span > < span class = "n" > getMEGA_CORP_PUBKEY< / span > < span class = "o" > (),< / span > < span class = "k" > new< / span > < span class = "n" > JavaCommercialPaper< / span > < span class = "o" > .< / span > < span class = "na" > Commands< / span > < span class = "o" > .< / span > < span class = "na" > Move< / span > < span class = "o" > ());< / span >
< span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > failsWith< / span > < span class = "o" > (< / span > < span class = "s" > " the state is propagated" < / span > < span class = "o" > );< / span >
< span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > output< / span > < span class = "o" > (< / span > < span class = "s" > " alice' s paper" < / span > < span class = "o" > ,< / span > < span class = "n" > inState< / span > < span class = "o" > .< / span > < span class = "na" > withOwner< / span > < span class = "o" > (< / span > < span class = "n" > getALICE_PUBKEY< / span > < span class = "o" > ()));< / span >
2016-11-11 11:52:29 +00:00
< span class = "k" > return< / span > < span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > verifies< / span > < span class = "o" > ();< / span >
< span class = "o" > });< / span >
< span class = "k" > return< / span > < span class = "n" > Unit< / span > < span class = "o" > .< / span > < span class = "na" > INSTANCE< / span > < span class = "o" > ;< / span >
< span class = "o" > });< / span >
< span class = "o" > }< / span >
< / pre > < / div >
< / div >
< / div >
2017-01-03 13:07:48 +00:00
< p > < code class = "docutils literal" > < span class = "pre" > output< / span > < / code > specifies that we want the input state to be transferred to < code class = "docutils literal" > < span class = "pre" > ALICE< / span > < / code > and < code class = "docutils literal" > < span class = "pre" > command< / span > < / code > adds the
< code class = "docutils literal" > < span class = "pre" > Move< / span > < / code > command itself, signed by the current owner of the input state, < code class = "docutils literal" > < span class = "pre" > MEGA_CORP_PUBKEY< / span > < / code > .< / p >
< p > We constructed a complete signed commercial paper transaction and verified it. Note how we left in the < code class = "docutils literal" > < span class = "pre" > fails< / span > < span class = "pre" > with< / span > < / code >
line - this is fine, the failure will be tested on the partially constructed transaction.< / p >
2016-11-11 11:52:29 +00:00
< p > What should we do if we wanted to test what happens when the wrong party signs the transaction? If we simply add a
2017-01-03 13:07:48 +00:00
< code class = "docutils literal" > < span class = "pre" > command< / span > < / code > it will permanently ruin the transaction... Enter < code class = "docutils literal" > < span class = "pre" > tweak< / span > < / code > :< / p >
2016-11-11 11:52:29 +00:00
< div class = "codeset container" >
< div class = "highlight-kotlin" > < div class = "highlight" > < pre > < span > < / span > @Test
2017-01-03 13:07:48 +00:00
fun `simple issuance with tweak`() {
2016-11-11 11:52:29 +00:00
ledger {
transaction {
2017-01-03 13:07:48 +00:00
output(" paper" ) { getPaper() } // Some CP is issued onto the ledger by MegaCorp.
2016-11-11 11:52:29 +00:00
tweak {
2017-01-03 13:07:48 +00:00
command(DUMMY_PUBKEY_1) { CommercialPaper.Commands.Issue() }
timestamp(TEST_TX_TIME)
this `fails with` " output states are issued by a command signer"
2016-11-11 11:52:29 +00:00
}
2017-01-03 13:07:48 +00:00
command(MEGA_CORP_PUBKEY) { CommercialPaper.Commands.Issue() }
timestamp(TEST_TX_TIME)
2016-11-11 11:52:29 +00:00
this.verifies()
}
}
}
< / pre > < / div >
< / div >
< div class = "highlight-java" > < div class = "highlight" > < pre > < span > < / span > < span class = "nd" > @Test< / span >
2017-01-03 13:07:48 +00:00
< span class = "kd" > public< / span > < span class = "kt" > void< / span > < span class = "nf" > simpleIssuanceWithTweak< / span > < span class = "o" > ()< / span > < span class = "o" > {< / span >
2016-11-11 11:52:29 +00:00
< span class = "n" > ledger< / span > < span class = "o" > (< / span > < span class = "n" > l< / span > < span class = "o" > -> < / span > < span class = "o" > {< / span >
< span class = "n" > l< / span > < span class = "o" > .< / span > < span class = "na" > transaction< / span > < span class = "o" > (< / span > < span class = "n" > tx< / span > < span class = "o" > -> < / span > < span class = "o" > {< / span >
2017-01-03 13:07:48 +00:00
< span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > output< / span > < span class = "o" > (< / span > < span class = "s" > " paper" < / span > < span class = "o" > ,< / span > < span class = "n" > getPaper< / span > < span class = "o" > ());< / span > < span class = "c1" > // Some CP is issued onto the ledger by MegaCorp.< / span >
2016-11-11 11:52:29 +00:00
< span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > tweak< / span > < span class = "o" > (< / span > < span class = "n" > tw< / span > < span class = "o" > -> < / span > < span class = "o" > {< / span >
2017-01-03 13:07:48 +00:00
< span class = "n" > tw< / span > < span class = "o" > .< / span > < span class = "na" > command< / span > < span class = "o" > (< / span > < span class = "n" > getDUMMY_PUBKEY_1< / span > < span class = "o" > (),< / span > < span class = "k" > new< / span > < span class = "n" > JavaCommercialPaper< / span > < span class = "o" > .< / span > < span class = "na" > Commands< / span > < span class = "o" > .< / span > < span class = "na" > Issue< / span > < span class = "o" > ());< / span >
< span class = "n" > tw< / span > < span class = "o" > .< / span > < span class = "na" > timestamp< / span > < span class = "o" > (< / span > < span class = "n" > getTEST_TX_TIME< / span > < span class = "o" > ());< / span >
< span class = "k" > return< / span > < span class = "n" > tw< / span > < span class = "o" > .< / span > < span class = "na" > failsWith< / span > < span class = "o" > (< / span > < span class = "s" > " output states are issued by a command signer" < / span > < span class = "o" > );< / span >
2016-11-11 11:52:29 +00:00
< span class = "o" > });< / span >
2017-01-03 13:07:48 +00:00
< span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > command< / span > < span class = "o" > (< / span > < span class = "n" > getMEGA_CORP_PUBKEY< / span > < span class = "o" > (),< / span > < span class = "k" > new< / span > < span class = "n" > JavaCommercialPaper< / span > < span class = "o" > .< / span > < span class = "na" > Commands< / span > < span class = "o" > .< / span > < span class = "na" > Issue< / span > < span class = "o" > ());< / span >
< span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > timestamp< / span > < span class = "o" > (< / span > < span class = "n" > getTEST_TX_TIME< / span > < span class = "o" > ());< / span >
2016-11-11 11:52:29 +00:00
< span class = "k" > return< / span > < span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > verifies< / span > < span class = "o" > ();< / span >
< span class = "o" > });< / span >
< span class = "k" > return< / span > < span class = "n" > Unit< / span > < span class = "o" > .< / span > < span class = "na" > INSTANCE< / span > < span class = "o" > ;< / span >
< span class = "o" > });< / span >
< span class = "o" > }< / span >
< / pre > < / div >
< / div >
< / div >
2017-01-03 13:07:48 +00:00
< p > < code class = "docutils literal" > < span class = "pre" > tweak< / span > < / code > creates a local copy of the transaction. This makes possible to locally “ ruin” the transaction while not
modifying the original one, allowing testing of different error conditions.< / p >
2016-11-11 11:52:29 +00:00
< p > We now have a neat little test that tests a single transaction. This is already useful, and in fact testing of a single
2017-01-03 13:07:48 +00:00
transaction in this way is very common. There is even a shorthand top-level < code class = "docutils literal" > < span class = "pre" > transaction< / span > < / code > primitive that creates a
2016-11-11 11:52:29 +00:00
ledger with a single transaction:< / p >
< div class = "codeset container" >
< div class = "highlight-kotlin" > < div class = "highlight" > < pre > < span > < / span > @Test
2017-01-03 13:07:48 +00:00
fun `simple issuance with tweak and top level transaction`() {
2016-11-11 11:52:29 +00:00
transaction {
2017-01-03 13:07:48 +00:00
output(" paper" ) { getPaper() } // Some CP is issued onto the ledger by MegaCorp.
2016-11-11 11:52:29 +00:00
tweak {
2017-01-03 13:07:48 +00:00
command(DUMMY_PUBKEY_1) { CommercialPaper.Commands.Issue() }
timestamp(TEST_TX_TIME)
this `fails with` " output states are issued by a command signer"
2016-11-11 11:52:29 +00:00
}
2017-01-03 13:07:48 +00:00
command(MEGA_CORP_PUBKEY) { CommercialPaper.Commands.Issue() }
timestamp(TEST_TX_TIME)
2016-11-11 11:52:29 +00:00
this.verifies()
}
}
< / pre > < / div >
< / div >
< div class = "highlight-java" > < div class = "highlight" > < pre > < span > < / span > < span class = "nd" > @Test< / span >
2017-01-03 13:07:48 +00:00
< span class = "kd" > public< / span > < span class = "kt" > void< / span > < span class = "nf" > simpleIssuanceWithTweakTopLevelTx< / span > < span class = "o" > ()< / span > < span class = "o" > {< / span >
2016-11-11 11:52:29 +00:00
< span class = "n" > transaction< / span > < span class = "o" > (< / span > < span class = "n" > tx< / span > < span class = "o" > -> < / span > < span class = "o" > {< / span >
2017-01-03 13:07:48 +00:00
< span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > output< / span > < span class = "o" > (< / span > < span class = "s" > " paper" < / span > < span class = "o" > ,< / span > < span class = "n" > getPaper< / span > < span class = "o" > ());< / span > < span class = "c1" > // Some CP is issued onto the ledger by MegaCorp.< / span >
2016-11-11 11:52:29 +00:00
< span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > tweak< / span > < span class = "o" > (< / span > < span class = "n" > tw< / span > < span class = "o" > -> < / span > < span class = "o" > {< / span >
2017-01-03 13:07:48 +00:00
< span class = "n" > tw< / span > < span class = "o" > .< / span > < span class = "na" > command< / span > < span class = "o" > (< / span > < span class = "n" > getDUMMY_PUBKEY_1< / span > < span class = "o" > (),< / span > < span class = "k" > new< / span > < span class = "n" > JavaCommercialPaper< / span > < span class = "o" > .< / span > < span class = "na" > Commands< / span > < span class = "o" > .< / span > < span class = "na" > Issue< / span > < span class = "o" > ());< / span >
< span class = "n" > tw< / span > < span class = "o" > .< / span > < span class = "na" > timestamp< / span > < span class = "o" > (< / span > < span class = "n" > getTEST_TX_TIME< / span > < span class = "o" > ());< / span >
< span class = "k" > return< / span > < span class = "n" > tw< / span > < span class = "o" > .< / span > < span class = "na" > failsWith< / span > < span class = "o" > (< / span > < span class = "s" > " output states are issued by a command signer" < / span > < span class = "o" > );< / span >
2016-11-11 11:52:29 +00:00
< span class = "o" > });< / span >
2017-01-03 13:07:48 +00:00
< span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > command< / span > < span class = "o" > (< / span > < span class = "n" > getMEGA_CORP_PUBKEY< / span > < span class = "o" > (),< / span > < span class = "k" > new< / span > < span class = "n" > JavaCommercialPaper< / span > < span class = "o" > .< / span > < span class = "na" > Commands< / span > < span class = "o" > .< / span > < span class = "na" > Issue< / span > < span class = "o" > ());< / span >
< span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > timestamp< / span > < span class = "o" > (< / span > < span class = "n" > getTEST_TX_TIME< / span > < span class = "o" > ());< / span >
2016-11-11 11:52:29 +00:00
< span class = "k" > return< / span > < span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > verifies< / span > < span class = "o" > ();< / span >
< span class = "o" > });< / span >
< span class = "o" > }< / span >
< / pre > < / div >
< / div >
< / div >
< / div >
< div class = "section" id = "chaining-transactions" >
< h2 > Chaining transactions< a class = "headerlink" href = "#chaining-transactions" title = "Permalink to this headline" > ¶< / a > < / h2 >
< p > Now that we know how to define a single transaction, let’ s look at how to define a chain of them:< / p >
< div class = "codeset container" >
< div class = "highlight-kotlin" > < div class = "highlight" > < pre > < span > < / span > @Test
2017-01-03 13:07:48 +00:00
fun `chain commercial paper`() {
val issuer = MEGA_CORP.ref(123)
2016-11-11 11:52:29 +00:00
ledger {
unverifiedTransaction {
2017-01-03 13:07:48 +00:00
output(" alice' s $900" , 900.DOLLARS.CASH `issued by` issuer `owned by` ALICE_PUBKEY)
2016-11-11 11:52:29 +00:00
}
2017-01-03 13:07:48 +00:00
// Some CP is issued onto the ledger by MegaCorp.
transaction(" Issuance" ) {
output(" paper" ) { getPaper() }
command(MEGA_CORP_PUBKEY) { CommercialPaper.Commands.Issue() }
timestamp(TEST_TX_TIME)
this.verifies()
}
transaction(" Trade" ) {
input(" paper" )
input(" alice' s $900" )
output(" borrowed $900" ) { 900.DOLLARS.CASH `issued by` issuer `owned by` MEGA_CORP_PUBKEY }
output(" alice' s paper" ) { " paper" .output< ICommercialPaperState> () `owned by` ALICE_PUBKEY }
command(ALICE_PUBKEY) { Cash.Commands.Move() }
command(MEGA_CORP_PUBKEY) { CommercialPaper.Commands.Move() }
2016-11-11 11:52:29 +00:00
this.verifies()
}
}
}
< / pre > < / div >
< / div >
< div class = "highlight-java" > < div class = "highlight" > < pre > < span > < / span > < span class = "nd" > @Test< / span >
2017-01-03 13:07:48 +00:00
< span class = "kd" > public< / span > < span class = "kt" > void< / span > < span class = "nf" > chainCommercialPaper< / span > < span class = "o" > ()< / span > < span class = "o" > {< / span >
< span class = "n" > PartyAndReference< / span > < span class = "n" > issuer< / span > < span class = "o" > =< / span > < span class = "n" > getMEGA_CORP< / span > < span class = "o" > ().< / span > < span class = "na" > ref< / span > < span class = "o" > (< / span > < span class = "n" > defaultRef< / span > < span class = "o" > );< / span >
2016-11-11 11:52:29 +00:00
< span class = "n" > ledger< / span > < span class = "o" > (< / span > < span class = "n" > l< / span > < span class = "o" > -> < / span > < span class = "o" > {< / span >
< span class = "n" > l< / span > < span class = "o" > .< / span > < span class = "na" > unverifiedTransaction< / span > < span class = "o" > (< / span > < span class = "n" > tx< / span > < span class = "o" > -> < / span > < span class = "o" > {< / span >
2017-01-03 13:07:48 +00:00
< span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > output< / span > < span class = "o" > (< / span > < span class = "s" > " alice' s $900" < / span > < span class = "o" > ,< / span >
< span class = "k" > new< / span > < span class = "n" > Cash< / span > < span class = "o" > .< / span > < span class = "na" > State< / span > < span class = "o" > (< / span > < span class = "n" > issuedBy< / span > < span class = "o" > (< / span > < span class = "n" > DOLLARS< / span > < span class = "o" > (< / span > < span class = "mi" > 900< / span > < span class = "o" > ),< / span > < span class = "n" > issuer< / span > < span class = "o" > ),< / span > < span class = "n" > getALICE_PUBKEY< / span > < span class = "o" > (),< / span > < span class = "kc" > null< / span > < span class = "o" > ));< / span >
< span class = "k" > return< / span > < span class = "n" > Unit< / span > < span class = "o" > .< / span > < span class = "na" > INSTANCE< / span > < span class = "o" > ;< / span >
< span class = "o" > });< / span >
< span class = "c1" > // Some CP is issued onto the ledger by MegaCorp.< / span >
< span class = "n" > l< / span > < span class = "o" > .< / span > < span class = "na" > transaction< / span > < span class = "o" > (< / span > < span class = "s" > " Issuance" < / span > < span class = "o" > ,< / span > < span class = "n" > tx< / span > < span class = "o" > -> < / span > < span class = "o" > {< / span >
< span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > output< / span > < span class = "o" > (< / span > < span class = "s" > " paper" < / span > < span class = "o" > ,< / span > < span class = "n" > getPaper< / span > < span class = "o" > ());< / span >
< span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > command< / span > < span class = "o" > (< / span > < span class = "n" > getMEGA_CORP_PUBKEY< / span > < span class = "o" > (),< / span > < span class = "k" > new< / span > < span class = "n" > JavaCommercialPaper< / span > < span class = "o" > .< / span > < span class = "na" > Commands< / span > < span class = "o" > .< / span > < span class = "na" > Issue< / span > < span class = "o" > ());< / span >
< span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > timestamp< / span > < span class = "o" > (< / span > < span class = "n" > getTEST_TX_TIME< / span > < span class = "o" > ());< / span >
< span class = "k" > return< / span > < span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > verifies< / span > < span class = "o" > ();< / span >
2016-11-11 11:52:29 +00:00
< span class = "o" > });< / span >
2017-01-03 13:07:48 +00:00
< span class = "n" > l< / span > < span class = "o" > .< / span > < span class = "na" > transaction< / span > < span class = "o" > (< / span > < span class = "s" > " Trade" < / span > < span class = "o" > ,< / span > < span class = "n" > tx< / span > < span class = "o" > -> < / span > < span class = "o" > {< / span >
< span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > input< / span > < span class = "o" > (< / span > < span class = "s" > " paper" < / span > < span class = "o" > );< / span >
< span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > input< / span > < span class = "o" > (< / span > < span class = "s" > " alice' s $900" < / span > < span class = "o" > );< / span >
< span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > output< / span > < span class = "o" > (< / span > < span class = "s" > " borrowed $900" < / span > < span class = "o" > ,< / span > < span class = "k" > new< / span > < span class = "n" > Cash< / span > < span class = "o" > .< / span > < span class = "na" > State< / span > < span class = "o" > (< / span > < span class = "n" > issuedBy< / span > < span class = "o" > (< / span > < span class = "n" > DOLLARS< / span > < span class = "o" > (< / span > < span class = "mi" > 900< / span > < span class = "o" > ),< / span > < span class = "n" > issuer< / span > < span class = "o" > ),< / span > < span class = "n" > getMEGA_CORP_PUBKEY< / span > < span class = "o" > (),< / span > < span class = "kc" > null< / span > < span class = "o" > ));< / span >
< span class = "n" > JavaCommercialPaper< / span > < span class = "o" > .< / span > < span class = "na" > State< / span > < span class = "n" > inputPaper< / span > < span class = "o" > =< / span > < span class = "n" > l< / span > < span class = "o" > .< / span > < span class = "na" > retrieveOutput< / span > < span class = "o" > (< / span > < span class = "n" > JavaCommercialPaper< / span > < span class = "o" > .< / span > < span class = "na" > State< / span > < span class = "o" > .< / span > < span class = "na" > class< / span > < span class = "o" > ,< / span > < span class = "s" > " paper" < / span > < span class = "o" > );< / span >
< span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > output< / span > < span class = "o" > (< / span > < span class = "s" > " alice' s paper" < / span > < span class = "o" > ,< / span > < span class = "n" > inputPaper< / span > < span class = "o" > .< / span > < span class = "na" > withOwner< / span > < span class = "o" > (< / span > < span class = "n" > getALICE_PUBKEY< / span > < span class = "o" > ()));< / span >
< span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > command< / span > < span class = "o" > (< / span > < span class = "n" > getALICE_PUBKEY< / span > < span class = "o" > (),< / span > < span class = "k" > new< / span > < span class = "n" > Cash< / span > < span class = "o" > .< / span > < span class = "na" > Commands< / span > < span class = "o" > .< / span > < span class = "na" > Move< / span > < span class = "o" > ());< / span >
< span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > command< / span > < span class = "o" > (< / span > < span class = "n" > getMEGA_CORP_PUBKEY< / span > < span class = "o" > (),< / span > < span class = "k" > new< / span > < span class = "n" > JavaCommercialPaper< / span > < span class = "o" > .< / span > < span class = "na" > Commands< / span > < span class = "o" > .< / span > < span class = "na" > Move< / span > < span class = "o" > ());< / span >
2016-11-11 11:52:29 +00:00
< span class = "k" > return< / span > < span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > verifies< / span > < span class = "o" > ();< / span >
< span class = "o" > });< / span >
< span class = "k" > return< / span > < span class = "n" > Unit< / span > < span class = "o" > .< / span > < span class = "na" > INSTANCE< / span > < span class = "o" > ;< / span >
< span class = "o" > });< / span >
< span class = "o" > }< / span >
< / pre > < / div >
< / div >
< / div >
2017-01-03 13:07:48 +00:00
< p > In this example we declare that < code class = "docutils literal" > < span class = "pre" > ALICE< / span > < / code > has $900 but we don’ t care where from. For this we can use
2016-11-11 11:52:29 +00:00
< code class = "docutils literal" > < span class = "pre" > unverifiedTransaction< / span > < / code > . Note how we don’ t need to specify < code class = "docutils literal" > < span class = "pre" > this.verifies()< / span > < / code > .< / p >
2017-01-03 13:07:48 +00:00
< p > Notice that we labelled output with < code class = "docutils literal" > < span class = "pre" > " alice's< / span > < span class = "pre" > $900" < / span > < / code > , also in transaction named < code class = "docutils literal" > < span class = "pre" > " Issuance" < / span > < / code >
we labelled a commercial paper with < code class = "docutils literal" > < span class = "pre" > " paper" < / span > < / code > . Now we can subsequently refer to them in other transactions, e.g.
by < code class = "docutils literal" > < span class = "pre" > input(" alice's< / span > < span class = "pre" > $900" )< / span > < / code > or < code class = "docutils literal" > < span class = "pre" > " paper" .output< ICommercialPaperState> ()< / span > < / code > .< / p >
< p > The last transaction named < code class = "docutils literal" > < span class = "pre" > " Trade" < / span > < / code > exemplifies simple fact of selling the < code class = "docutils literal" > < span class = "pre" > CommercialPaper< / span > < / code > to Alice for her $900,
$100 less than the face value at 10% interest after only 7 days.< / p >
< p > We can also test whole ledger calling < code class = "docutils literal" > < span class = "pre" > this.verifies()< / span > < / code > and < code class = "docutils literal" > < span class = "pre" > this.fails()< / span > < / code > on the ledger level.
To do so let’ s create a simple example that uses the same input twice:< / p >
2016-11-11 11:52:29 +00:00
< div class = "codeset container" >
< div class = "highlight-kotlin" > < div class = "highlight" > < pre > < span > < / span > @Test
2017-01-03 13:07:48 +00:00
fun `chain commercial paper double spend`() {
val issuer = MEGA_CORP.ref(123)
2016-11-11 11:52:29 +00:00
ledger {
unverifiedTransaction {
2017-01-03 13:07:48 +00:00
output(" alice' s $900" , 900.DOLLARS.CASH `issued by` issuer `owned by` ALICE_PUBKEY)
2016-11-11 11:52:29 +00:00
}
2017-01-03 13:07:48 +00:00
// Some CP is issued onto the ledger by MegaCorp.
transaction(" Issuance" ) {
output(" paper" ) { getPaper() }
command(MEGA_CORP_PUBKEY) { CommercialPaper.Commands.Issue() }
timestamp(TEST_TX_TIME)
this.verifies()
}
transaction(" Trade" ) {
input(" paper" )
input(" alice' s $900" )
output(" borrowed $900" ) { 900.DOLLARS.CASH `issued by` issuer `owned by` MEGA_CORP_PUBKEY }
output(" alice' s paper" ) { " paper" .output< ICommercialPaperState> () `owned by` ALICE_PUBKEY }
command(ALICE_PUBKEY) { Cash.Commands.Move() }
command(MEGA_CORP_PUBKEY) { CommercialPaper.Commands.Move() }
2016-11-11 11:52:29 +00:00
this.verifies()
}
transaction {
2017-01-03 13:07:48 +00:00
input(" paper" )
// We moved a paper to another pubkey.
output(" bob' s paper" ) { " paper" .output< ICommercialPaperState> () `owned by` BOB_PUBKEY }
command(MEGA_CORP_PUBKEY) { CommercialPaper.Commands.Move() }
2016-11-11 11:52:29 +00:00
this.verifies()
}
2017-01-03 13:07:48 +00:00
this.fails()
2016-11-11 11:52:29 +00:00
}
}
< / pre > < / div >
< / div >
< div class = "highlight-java" > < div class = "highlight" > < pre > < span > < / span > < span class = "nd" > @Test< / span >
2017-01-03 13:07:48 +00:00
< span class = "kd" > public< / span > < span class = "kt" > void< / span > < span class = "nf" > chainCommercialPaperDoubleSpend< / span > < span class = "o" > ()< / span > < span class = "o" > {< / span >
< span class = "n" > PartyAndReference< / span > < span class = "n" > issuer< / span > < span class = "o" > =< / span > < span class = "n" > getMEGA_CORP< / span > < span class = "o" > ().< / span > < span class = "na" > ref< / span > < span class = "o" > (< / span > < span class = "n" > defaultRef< / span > < span class = "o" > );< / span >
2016-11-11 11:52:29 +00:00
< span class = "n" > ledger< / span > < span class = "o" > (< / span > < span class = "n" > l< / span > < span class = "o" > -> < / span > < span class = "o" > {< / span >
< span class = "n" > l< / span > < span class = "o" > .< / span > < span class = "na" > unverifiedTransaction< / span > < span class = "o" > (< / span > < span class = "n" > tx< / span > < span class = "o" > -> < / span > < span class = "o" > {< / span >
2017-01-03 13:07:48 +00:00
< span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > output< / span > < span class = "o" > (< / span > < span class = "s" > " alice' s $900" < / span > < span class = "o" > ,< / span >
< span class = "k" > new< / span > < span class = "n" > Cash< / span > < span class = "o" > .< / span > < span class = "na" > State< / span > < span class = "o" > (< / span > < span class = "n" > issuedBy< / span > < span class = "o" > (< / span > < span class = "n" > DOLLARS< / span > < span class = "o" > (< / span > < span class = "mi" > 900< / span > < span class = "o" > ),< / span > < span class = "n" > issuer< / span > < span class = "o" > ),< / span > < span class = "n" > getALICE_PUBKEY< / span > < span class = "o" > (),< / span > < span class = "kc" > null< / span > < span class = "o" > ));< / span >
2016-11-11 11:52:29 +00:00
< span class = "k" > return< / span > < span class = "n" > Unit< / span > < span class = "o" > .< / span > < span class = "na" > INSTANCE< / span > < span class = "o" > ;< / span >
< span class = "o" > });< / span >
2017-01-03 13:07:48 +00:00
< span class = "c1" > // Some CP is issued onto the ledger by MegaCorp.< / span >
< span class = "n" > l< / span > < span class = "o" > .< / span > < span class = "na" > transaction< / span > < span class = "o" > (< / span > < span class = "s" > " Issuance" < / span > < span class = "o" > ,< / span > < span class = "n" > tx< / span > < span class = "o" > -> < / span > < span class = "o" > {< / span >
< span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > output< / span > < span class = "o" > (< / span > < span class = "s" > " paper" < / span > < span class = "o" > ,< / span > < span class = "n" > getPaper< / span > < span class = "o" > ());< / span >
< span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > command< / span > < span class = "o" > (< / span > < span class = "n" > getMEGA_CORP_PUBKEY< / span > < span class = "o" > (),< / span > < span class = "k" > new< / span > < span class = "n" > JavaCommercialPaper< / span > < span class = "o" > .< / span > < span class = "na" > Commands< / span > < span class = "o" > .< / span > < span class = "na" > Issue< / span > < span class = "o" > ());< / span >
< span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > timestamp< / span > < span class = "o" > (< / span > < span class = "n" > getTEST_TX_TIME< / span > < span class = "o" > ());< / span >
2016-11-11 11:52:29 +00:00
< span class = "k" > return< / span > < span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > verifies< / span > < span class = "o" > ();< / span >
< span class = "o" > });< / span >
2017-01-03 13:07:48 +00:00
< span class = "n" > l< / span > < span class = "o" > .< / span > < span class = "na" > transaction< / span > < span class = "o" > (< / span > < span class = "s" > " Trade" < / span > < span class = "o" > ,< / span > < span class = "n" > tx< / span > < span class = "o" > -> < / span > < span class = "o" > {< / span >
< span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > input< / span > < span class = "o" > (< / span > < span class = "s" > " paper" < / span > < span class = "o" > );< / span >
< span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > input< / span > < span class = "o" > (< / span > < span class = "s" > " alice' s $900" < / span > < span class = "o" > );< / span >
< span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > output< / span > < span class = "o" > (< / span > < span class = "s" > " borrowed $900" < / span > < span class = "o" > ,< / span > < span class = "k" > new< / span > < span class = "n" > Cash< / span > < span class = "o" > .< / span > < span class = "na" > State< / span > < span class = "o" > (< / span > < span class = "n" > issuedBy< / span > < span class = "o" > (< / span > < span class = "n" > DOLLARS< / span > < span class = "o" > (< / span > < span class = "mi" > 900< / span > < span class = "o" > ),< / span > < span class = "n" > issuer< / span > < span class = "o" > ),< / span > < span class = "n" > getMEGA_CORP_PUBKEY< / span > < span class = "o" > (),< / span > < span class = "kc" > null< / span > < span class = "o" > ));< / span >
< span class = "n" > JavaCommercialPaper< / span > < span class = "o" > .< / span > < span class = "na" > State< / span > < span class = "n" > inputPaper< / span > < span class = "o" > =< / span > < span class = "n" > l< / span > < span class = "o" > .< / span > < span class = "na" > retrieveOutput< / span > < span class = "o" > (< / span > < span class = "n" > JavaCommercialPaper< / span > < span class = "o" > .< / span > < span class = "na" > State< / span > < span class = "o" > .< / span > < span class = "na" > class< / span > < span class = "o" > ,< / span > < span class = "s" > " paper" < / span > < span class = "o" > );< / span >
< span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > output< / span > < span class = "o" > (< / span > < span class = "s" > " alice' s paper" < / span > < span class = "o" > ,< / span > < span class = "n" > inputPaper< / span > < span class = "o" > .< / span > < span class = "na" > withOwner< / span > < span class = "o" > (< / span > < span class = "n" > getALICE_PUBKEY< / span > < span class = "o" > ()));< / span >
< span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > command< / span > < span class = "o" > (< / span > < span class = "n" > getALICE_PUBKEY< / span > < span class = "o" > (),< / span > < span class = "k" > new< / span > < span class = "n" > Cash< / span > < span class = "o" > .< / span > < span class = "na" > Commands< / span > < span class = "o" > .< / span > < span class = "na" > Move< / span > < span class = "o" > ());< / span >
< span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > command< / span > < span class = "o" > (< / span > < span class = "n" > getMEGA_CORP_PUBKEY< / span > < span class = "o" > (),< / span > < span class = "k" > new< / span > < span class = "n" > JavaCommercialPaper< / span > < span class = "o" > .< / span > < span class = "na" > Commands< / span > < span class = "o" > .< / span > < span class = "na" > Move< / span > < span class = "o" > ());< / span >
2016-11-11 11:52:29 +00:00
< span class = "k" > return< / span > < span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > verifies< / span > < span class = "o" > ();< / span >
< span class = "o" > });< / span >
2017-01-03 13:07:48 +00:00
< span class = "n" > l< / span > < span class = "o" > .< / span > < span class = "na" > transaction< / span > < span class = "o" > (< / span > < span class = "n" > tx< / span > < span class = "o" > -> < / span > < span class = "o" > {< / span >
< span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > input< / span > < span class = "o" > (< / span > < span class = "s" > " paper" < / span > < span class = "o" > );< / span >
< span class = "n" > JavaCommercialPaper< / span > < span class = "o" > .< / span > < span class = "na" > State< / span > < span class = "n" > inputPaper< / span > < span class = "o" > =< / span > < span class = "n" > l< / span > < span class = "o" > .< / span > < span class = "na" > retrieveOutput< / span > < span class = "o" > (< / span > < span class = "n" > JavaCommercialPaper< / span > < span class = "o" > .< / span > < span class = "na" > State< / span > < span class = "o" > .< / span > < span class = "na" > class< / span > < span class = "o" > ,< / span > < span class = "s" > " paper" < / span > < span class = "o" > );< / span >
< span class = "c1" > // We moved a paper to other pubkey.< / span >
< span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > output< / span > < span class = "o" > (< / span > < span class = "s" > " bob' s paper" < / span > < span class = "o" > ,< / span > < span class = "n" > inputPaper< / span > < span class = "o" > .< / span > < span class = "na" > withOwner< / span > < span class = "o" > (< / span > < span class = "n" > getBOB_PUBKEY< / span > < span class = "o" > ()));< / span >
< span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > command< / span > < span class = "o" > (< / span > < span class = "n" > getMEGA_CORP_PUBKEY< / span > < span class = "o" > (),< / span > < span class = "k" > new< / span > < span class = "n" > JavaCommercialPaper< / span > < span class = "o" > .< / span > < span class = "na" > Commands< / span > < span class = "o" > .< / span > < span class = "na" > Move< / span > < span class = "o" > ());< / span >
< span class = "k" > return< / span > < span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > verifies< / span > < span class = "o" > ();< / span >
< span class = "o" > });< / span >
< span class = "n" > l< / span > < span class = "o" > .< / span > < span class = "na" > fails< / span > < span class = "o" > ();< / span >
2016-11-11 11:52:29 +00:00
< span class = "k" > return< / span > < span class = "n" > Unit< / span > < span class = "o" > .< / span > < span class = "na" > INSTANCE< / span > < span class = "o" > ;< / span >
< span class = "o" > });< / span >
< span class = "o" > }< / span >
< / pre > < / div >
< / div >
< / div >
2017-01-03 13:07:48 +00:00
< p > The transactions < code class = "docutils literal" > < span class = "pre" > verifies()< / span > < / code > individually, however the state was spent twice! That’ s why we need the global ledger
verification (< code class = "docutils literal" > < span class = "pre" > this.fails()< / span > < / code > at the end). As in previous examples we can use < code class = "docutils literal" > < span class = "pre" > tweak< / span > < / code > to create a local copy of the whole ledger:< / p >
2016-11-11 11:52:29 +00:00
< div class = "codeset container" >
< div class = "highlight-kotlin" > < div class = "highlight" > < pre > < span > < / span > @Test
2017-01-03 13:07:48 +00:00
fun `chain commercial tweak`() {
val issuer = MEGA_CORP.ref(123)
2016-11-11 11:52:29 +00:00
ledger {
unverifiedTransaction {
2017-01-03 13:07:48 +00:00
output(" alice' s $900" , 900.DOLLARS.CASH `issued by` issuer `owned by` ALICE_PUBKEY)
2016-11-11 11:52:29 +00:00
}
2017-01-03 13:07:48 +00:00
// Some CP is issued onto the ledger by MegaCorp.
transaction(" Issuance" ) {
output(" paper" ) { getPaper() }
command(MEGA_CORP_PUBKEY) { CommercialPaper.Commands.Issue() }
timestamp(TEST_TX_TIME)
this.verifies()
}
transaction(" Trade" ) {
input(" paper" )
input(" alice' s $900" )
output(" borrowed $900" ) { 900.DOLLARS.CASH `issued by` issuer `owned by` MEGA_CORP_PUBKEY }
output(" alice' s paper" ) { " paper" .output< ICommercialPaperState> () `owned by` ALICE_PUBKEY }
command(ALICE_PUBKEY) { Cash.Commands.Move() }
command(MEGA_CORP_PUBKEY) { CommercialPaper.Commands.Move() }
2016-11-11 11:52:29 +00:00
this.verifies()
}
tweak {
transaction {
2017-01-03 13:07:48 +00:00
input(" paper" )
// We moved a paper to another pubkey.
output(" bob' s paper" ) { " paper" .output< ICommercialPaperState> () `owned by` BOB_PUBKEY }
command(MEGA_CORP_PUBKEY) { CommercialPaper.Commands.Move() }
2016-11-11 11:52:29 +00:00
this.verifies()
}
this.fails()
}
this.verifies()
}
}
< / pre > < / div >
< / div >
< div class = "highlight-java" > < div class = "highlight" > < pre > < span > < / span > < span class = "nd" > @Test< / span >
2017-01-03 13:07:48 +00:00
< span class = "kd" > public< / span > < span class = "kt" > void< / span > < span class = "nf" > chainCommercialPaperTweak< / span > < span class = "o" > ()< / span > < span class = "o" > {< / span >
< span class = "n" > PartyAndReference< / span > < span class = "n" > issuer< / span > < span class = "o" > =< / span > < span class = "n" > getMEGA_CORP< / span > < span class = "o" > ().< / span > < span class = "na" > ref< / span > < span class = "o" > (< / span > < span class = "n" > defaultRef< / span > < span class = "o" > );< / span >
2016-11-11 11:52:29 +00:00
< span class = "n" > ledger< / span > < span class = "o" > (< / span > < span class = "n" > l< / span > < span class = "o" > -> < / span > < span class = "o" > {< / span >
< span class = "n" > l< / span > < span class = "o" > .< / span > < span class = "na" > unverifiedTransaction< / span > < span class = "o" > (< / span > < span class = "n" > tx< / span > < span class = "o" > -> < / span > < span class = "o" > {< / span >
2017-01-03 13:07:48 +00:00
< span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > output< / span > < span class = "o" > (< / span > < span class = "s" > " alice' s $900" < / span > < span class = "o" > ,< / span >
< span class = "k" > new< / span > < span class = "n" > Cash< / span > < span class = "o" > .< / span > < span class = "na" > State< / span > < span class = "o" > (< / span > < span class = "n" > issuedBy< / span > < span class = "o" > (< / span > < span class = "n" > DOLLARS< / span > < span class = "o" > (< / span > < span class = "mi" > 900< / span > < span class = "o" > ),< / span > < span class = "n" > issuer< / span > < span class = "o" > ),< / span > < span class = "n" > getALICE_PUBKEY< / span > < span class = "o" > (),< / span > < span class = "kc" > null< / span > < span class = "o" > ));< / span >
2016-11-11 11:52:29 +00:00
< span class = "k" > return< / span > < span class = "n" > Unit< / span > < span class = "o" > .< / span > < span class = "na" > INSTANCE< / span > < span class = "o" > ;< / span >
< span class = "o" > });< / span >
2017-01-03 13:07:48 +00:00
< span class = "c1" > // Some CP is issued onto the ledger by MegaCorp.< / span >
< span class = "n" > l< / span > < span class = "o" > .< / span > < span class = "na" > transaction< / span > < span class = "o" > (< / span > < span class = "s" > " Issuance" < / span > < span class = "o" > ,< / span > < span class = "n" > tx< / span > < span class = "o" > -> < / span > < span class = "o" > {< / span >
< span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > output< / span > < span class = "o" > (< / span > < span class = "s" > " paper" < / span > < span class = "o" > ,< / span > < span class = "n" > getPaper< / span > < span class = "o" > ());< / span >
< span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > command< / span > < span class = "o" > (< / span > < span class = "n" > getMEGA_CORP_PUBKEY< / span > < span class = "o" > (),< / span > < span class = "k" > new< / span > < span class = "n" > JavaCommercialPaper< / span > < span class = "o" > .< / span > < span class = "na" > Commands< / span > < span class = "o" > .< / span > < span class = "na" > Issue< / span > < span class = "o" > ());< / span >
< span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > timestamp< / span > < span class = "o" > (< / span > < span class = "n" > getTEST_TX_TIME< / span > < span class = "o" > ());< / span >
< span class = "k" > return< / span > < span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > verifies< / span > < span class = "o" > ();< / span >
< span class = "o" > });< / span >
< span class = "n" > l< / span > < span class = "o" > .< / span > < span class = "na" > transaction< / span > < span class = "o" > (< / span > < span class = "s" > " Trade" < / span > < span class = "o" > ,< / span > < span class = "n" > tx< / span > < span class = "o" > -> < / span > < span class = "o" > {< / span >
< span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > input< / span > < span class = "o" > (< / span > < span class = "s" > " paper" < / span > < span class = "o" > );< / span >
< span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > input< / span > < span class = "o" > (< / span > < span class = "s" > " alice' s $900" < / span > < span class = "o" > );< / span >
< span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > output< / span > < span class = "o" > (< / span > < span class = "s" > " borrowed $900" < / span > < span class = "o" > ,< / span > < span class = "k" > new< / span > < span class = "n" > Cash< / span > < span class = "o" > .< / span > < span class = "na" > State< / span > < span class = "o" > (< / span > < span class = "n" > issuedBy< / span > < span class = "o" > (< / span > < span class = "n" > DOLLARS< / span > < span class = "o" > (< / span > < span class = "mi" > 900< / span > < span class = "o" > ),< / span > < span class = "n" > issuer< / span > < span class = "o" > ),< / span > < span class = "n" > getMEGA_CORP_PUBKEY< / span > < span class = "o" > (),< / span > < span class = "kc" > null< / span > < span class = "o" > ));< / span >
< span class = "n" > JavaCommercialPaper< / span > < span class = "o" > .< / span > < span class = "na" > State< / span > < span class = "n" > inputPaper< / span > < span class = "o" > =< / span > < span class = "n" > l< / span > < span class = "o" > .< / span > < span class = "na" > retrieveOutput< / span > < span class = "o" > (< / span > < span class = "n" > JavaCommercialPaper< / span > < span class = "o" > .< / span > < span class = "na" > State< / span > < span class = "o" > .< / span > < span class = "na" > class< / span > < span class = "o" > ,< / span > < span class = "s" > " paper" < / span > < span class = "o" > );< / span >
< span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > output< / span > < span class = "o" > (< / span > < span class = "s" > " alice' s paper" < / span > < span class = "o" > ,< / span > < span class = "n" > inputPaper< / span > < span class = "o" > .< / span > < span class = "na" > withOwner< / span > < span class = "o" > (< / span > < span class = "n" > getALICE_PUBKEY< / span > < span class = "o" > ()));< / span >
< span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > command< / span > < span class = "o" > (< / span > < span class = "n" > getALICE_PUBKEY< / span > < span class = "o" > (),< / span > < span class = "k" > new< / span > < span class = "n" > Cash< / span > < span class = "o" > .< / span > < span class = "na" > Commands< / span > < span class = "o" > .< / span > < span class = "na" > Move< / span > < span class = "o" > ());< / span >
< span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > command< / span > < span class = "o" > (< / span > < span class = "n" > getMEGA_CORP_PUBKEY< / span > < span class = "o" > (),< / span > < span class = "k" > new< / span > < span class = "n" > JavaCommercialPaper< / span > < span class = "o" > .< / span > < span class = "na" > Commands< / span > < span class = "o" > .< / span > < span class = "na" > Move< / span > < span class = "o" > ());< / span >
2016-11-11 11:52:29 +00:00
< span class = "k" > return< / span > < span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > verifies< / span > < span class = "o" > ();< / span >
< span class = "o" > });< / span >
< span class = "n" > l< / span > < span class = "o" > .< / span > < span class = "na" > tweak< / span > < span class = "o" > (< / span > < span class = "n" > lw< / span > < span class = "o" > -> < / span > < span class = "o" > {< / span >
< span class = "n" > lw< / span > < span class = "o" > .< / span > < span class = "na" > transaction< / span > < span class = "o" > (< / span > < span class = "n" > tx< / span > < span class = "o" > -> < / span > < span class = "o" > {< / span >
2017-01-03 13:07:48 +00:00
< span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > input< / span > < span class = "o" > (< / span > < span class = "s" > " paper" < / span > < span class = "o" > );< / span >
< span class = "n" > JavaCommercialPaper< / span > < span class = "o" > .< / span > < span class = "na" > State< / span > < span class = "n" > inputPaper< / span > < span class = "o" > =< / span > < span class = "n" > l< / span > < span class = "o" > .< / span > < span class = "na" > retrieveOutput< / span > < span class = "o" > (< / span > < span class = "n" > JavaCommercialPaper< / span > < span class = "o" > .< / span > < span class = "na" > State< / span > < span class = "o" > .< / span > < span class = "na" > class< / span > < span class = "o" > ,< / span > < span class = "s" > " paper" < / span > < span class = "o" > );< / span >
< span class = "c1" > // We moved a paper to another pubkey.< / span >
< span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > output< / span > < span class = "o" > (< / span > < span class = "s" > " bob' s paper" < / span > < span class = "o" > ,< / span > < span class = "n" > inputPaper< / span > < span class = "o" > .< / span > < span class = "na" > withOwner< / span > < span class = "o" > (< / span > < span class = "n" > getBOB_PUBKEY< / span > < span class = "o" > ()));< / span >
< span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > command< / span > < span class = "o" > (< / span > < span class = "n" > getMEGA_CORP_PUBKEY< / span > < span class = "o" > (),< / span > < span class = "k" > new< / span > < span class = "n" > JavaCommercialPaper< / span > < span class = "o" > .< / span > < span class = "na" > Commands< / span > < span class = "o" > .< / span > < span class = "na" > Move< / span > < span class = "o" > ());< / span >
2016-11-11 11:52:29 +00:00
< span class = "k" > return< / span > < span class = "n" > tx< / span > < span class = "o" > .< / span > < span class = "na" > verifies< / span > < span class = "o" > ();< / span >
< span class = "o" > });< / span >
< span class = "n" > lw< / span > < span class = "o" > .< / span > < span class = "na" > fails< / span > < span class = "o" > ();< / span >
< span class = "k" > return< / span > < span class = "n" > Unit< / span > < span class = "o" > .< / span > < span class = "na" > INSTANCE< / span > < span class = "o" > ;< / span >
< span class = "o" > });< / span >
< span class = "n" > l< / span > < span class = "o" > .< / span > < span class = "na" > verifies< / span > < span class = "o" > ();< / span >
< span class = "k" > return< / span > < span class = "n" > Unit< / span > < span class = "o" > .< / span > < span class = "na" > INSTANCE< / span > < span class = "o" > ;< / span >
< span class = "o" > });< / span >
< span class = "o" > }< / span >
< / pre > < / div >
< / div >
< / div >
< / div >
< / div >
< / div >
< / div >
< footer >
< div class = "rst-footer-buttons" role = "navigation" aria-label = "footer navigation" >
2017-01-06 17:38:23 +00:00
< a href = "tutorial-integration-testing.html" class = "btn btn-neutral float-right" title = "Integration testing" accesskey = "n" > Next < span class = "fa fa-arrow-circle-right" > < / span > < / a >
2016-11-11 11:52:29 +00:00
< a href = "tutorial-contract-clauses.html" class = "btn btn-neutral" title = "Writing a contract using clauses" accesskey = "p" > < span class = "fa fa-arrow-circle-left" > < / span > Previous< / a >
< / div >
< hr / >
< div role = "contentinfo" >
< p >
2016-11-29 18:44:59 +00:00
© Copyright 2016, R3 Limited.
2016-11-11 11:52:29 +00:00
< / 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 >