Corda是Blockchain企业框架之一。
这周开始准备学习Corda, 首先先研究一下https://github.com/corda/samples 里面的例子。
这是Git下载好的代码列表
这个例子是支持java和kotlin
以Kotlin为例
这里包括Contract 智能合约和State 代码
IOUContract.kt 主要写智能合约,包括一些校验
IOUState.kt 定义一个账本一个事实,
IOUSchema.kt 是一个数据库的一个映射
因为公司的项目是基于Kotlin ,所以需要删除java build
- 删除java build 在gradle
1 cordapp-example\settings.gradle
//include 'contracts-java'
//include 'workflows-java'
include 'contracts-kotlin'
include 'workflows-kotlin'
include 'clients'
2 cordapp-example\clients\build.gradle
// cordapp project(":contracts-java")
// cordapp project(":workflows-java")
cordapp project(":contracts-kotlin")
cordapp project(":workflows-kotlin")
这样Java 就不会在gradle编译了 。
- 打包运行 gradlew.bat deployNodes
成功以后
- 运行节点 runnodes
会有4个java进程起来
分别是PartA PartB PartC Notary
Corda 默认的数据库是H2 ,我们可以连接 Corda H2数据库。
需要更改三个地方, persistence 是Corda默认的名字,需要Copy到对应目录里(Users下)
H2数据库默认名字是sa 密码为空
其中蓝色区域是corda IOUSchema.kt对应的表结构,
因为Corda 里面数据库是ORM框架,可以更改其他数据库比如SQL Server ,PostgreSQL,以后会更新博客怎么更改数据库在Corda上。
最后,这里面Client文件夹是Kotlin的springboot工程, 可以用网页形式访问。但是用的angular 是google的域名,需要提前替换。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example CorDapp</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css"
integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.1.1.min.js">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"
integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
crossorigin="anonymous"></script>
<script src="js/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/2.2.0/ui-bootstrap-tpls.min.js"></script>
<script src="js/angular-module.js"></script>
</head>
<body ng-app="demoAppModule" ng-controller="DemoAppController as demoApp">
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#">{{demoApp.thisNode}}</a>
</div>
<button ng-click="demoApp.openModal()" type="button" class="btn btn-primary navbar-btn">Create IOU</button>
</div>
</nav>
<script type="text/ng-template" id="demoAppModal.html">
<div class="modal-header">
<h4 class="modal-title">Add new IOU</h4>
</div>
<form>
<div class="modal-body">
<div class="form-group">
<label for="counterparty" class="control-label">Counter-party:</label>
<select ng-model="modalInstance.form.counterparty" class="form-control" id="counterparty"
ng-options="peer as peer for peer in modalInstance.peers">
</select>
</div>
<div class="form-group">
<label for="value" class="control-label">Value (Int):</label>
<input type="text" ng-model="modalInstance.form.value" class="form-control" id="value">
</div>
<div ng-show="modalInstance.formError" class="form-group">
<div class="alert alert-danger" role="alert">
<span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
<span class="sr-only">Error:</span>
Enter valid IOU parameters
</div>
</div>
</div>
<div class="modal-footer">
<button ng-click="modalInstance.cancel()" type="button" class="btn btn-default">Close</button>
<button ng-click="modalInstance.create()" type="button" class="btn btn-primary">Create IOU</button>
</div>
</form>
</script>
<script type="text/ng-template" id="messageContent.html">
<div class="modal-body" id="modal-body">
{{ modalInstanceTwo.message }}
</div>
</script>
<div class="row">
<div class="col-md-1"></div>
<div class="col-md-10">
<div class="panel-group" id="accordion">
<div class="panel">
<div class="panel-primary">
<div class="panel-heading">
<h4 class="panel-title">
Recorded IOUs:
</h4>
</div>
</div>
<div>
<div ng-show="!demoApp.ious.length" class="panel-body">Use the "Create IOU" button to send an IOU to a peer.</div>
<div ng-repeat="iou in demoApp.ious" class="panel panel-default">
<div class="panel-body">
<ul>
<li>Lender: {{iou.lender}}</li>
<li>Borrower: {{iou.borrower}}</li>
<li>Value: {{iou.value}}</li>
</ul>
</div>
</div>
</div>
</div>
<div class="panel">
<div class="panel-primary">
<div class="panel-heading">
<h4 class="panel-title">
My IOUs:
</h4>
</div>
</div>
<div>
<div ng-repeat="iou in demoApp.myious" class="panel panel-default">
<div class="panel-body">
<ul>
<li>Lender: {{iou.lender}}</li>
<li>Borrower: {{iou.borrower}}</li>
<li>Value: {{iou.value}}</li>
</ul>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="col-md-1"></div>
</div>
</body>
</html>
angular.min.js需要自己下载放在目录里。