[关闭]
@lambeta 2018-03-23T04:32:12.000000Z 字数 8703 阅读 351

Corda 开发流程

blockchain


  1. git clone https://github.com/corda/cordapp-example

定义 Contract

  1. 继承 Contract 并覆盖其中的 verify 方法
  1. open class TemplateContract : Contract {
  2. override fun verify(tx: LedgerTransaction) {
  3. TODO()
  4. }
  5. }

2. 定义 Contract ID,用于构建 tx 的 output: StateAndContract

  1. companion object {
  2. @JvmStatic
  3. val TEMPLATE_CONTRACT_ID = "com.example.contract.TemplateContract"
  4. }

3. 定义 Contract 中的 command, command 提供了 tx 的意图和交易双方的签名,用于 contract 中的验证

  1. interface Commands : CommandData {
  2. class Action : TypeOnlyCommandData(), Commands
  3. }

如:

  1. val command = tx.commands.requireSingleCommand<TemplateContract.Commands>()
  2. ...
  3. when (command.value) {
  4. is Commands.Action -> {
  5. val input = tx.inputs.single()
  6. requireThat {
  7. "the transaction is signed by the owner" using (input.owner.owningKey in command.signers)
  8. "the state is propagated" using (outputs.size == 1)
  9. }
  10. }

定义 API

  1. 继承 WebServerPluginRegistry
  1. // List of lambdas returning JAX-RS objects
  2. val webApis: List<Function<CordaRPCOps, out Any>> get() = emptyList()
  3. // Map of static serving endpoints to the matching resource directory.
  4. val staticServeDirs: Map<String, String> get() = emptyMap()

重写其中的变量webApis

  1. class TemplateWebPlugin : WebServerPluginRegistry {
  2. override val webApis: List<Function<CordaRPCOps, out Any>> = listOf(Function(::TemplateApi))
  3. ...

注意: 实例化Plugin之后,rpcOps 依赖注入会在这里发生。
这个例子中的TemplateApi即是实现了JAX-RS的类,如下:

  1. @Path("template")
  2. class TemplateApi(val rpcOps: CordaRPCOps) {
  3. // Accessible at /api/template/templateGetEndpoint.
  4. @GET
  5. @Path("templateGetEndpoint")
  6. @Produces(MediaType.APPLICATION_JSON)
  7. fun templateGetEndpoint(): Response {
  8. return Response.ok("Template GET endpoint.").build()
  9. }
  10. }

2. 注册Plugin
在路径META-INF/services/net.corda.webserver.services.WebServerPluginRegistry文件中填写类TemplateWebPlugin的限定名,这里是com.template.TemplateWebPlugin.

3. 对象的序列化白名单

  1. class TemplateSerializationWhitelist : SerializationWhitelist {
  2. override val whitelist: List<Class<*>> = listOf(TemplateData::class.java)
  3. }
  4. // This class is not annotated with @CordaSerilizable, so it must be added to the serialization whitelist.
  5. data class TemplateData(val payload: String)

注意这里提到,如果Class没有@CordaSerilizable注解,那么需要放到白名单中。

4. 注册序列化白名单
在路径META-INF/services/net.corda.core.serialization.SerializationWhitelist文件中填写白名单的类限定名com.template.TemplateSerializationWhitelist.

使用 CordaRPCOps

cordaRPCOps可以用来操作 Party Node。

  1. Retrieve party
  1. rpcOps.wellKnownPartyFromX500Name(partyName);

2. Start flow

  1. rpcOps.startTrackedFlowDynamic(ExampleFlow.Initiator.class, iouValue, otherParty);

3. Vault query

Corda 部署流程

1. git clone corda docker

  1. $ git clone https://github.com/qianyan/corda-docker.git

2. Build docker image

  1. $ docker-compose build

3. Deploy your Cordapp

  1. download the project cordapp-template-kotlin
  2. run ./gradlew deployNodes
  3. copy cordapps under any node of build/nodes, e.g. NetworkMapAndNotary/cordapps, to corda-docker/cordapps in first step.
  4. copy each node.conf under build/nodes, e.g. NetworkMapAndNotary/node.conf, to corda-docker/nodes and rename as NetworkMapAndNotary.conf. so do PartyA/node.conf to PartyA.conf

After these operations, you will see directories in corda-docker as below:

  1. nodes
  2. ├── NetworkMapAndNotary.conf
  3. ├── PartyA.conf
  4. └── PartyB.conf

5. download network-bootstrapper-corda-3.0.jar in corda-docker, refer to corda network

  1. aria2c -x3 http://downloads.corda.net/network-bootstrapper-corda-3.0.jar

6. generate real nodes in corda-docker

  1. java -jar network-bootstrapper-corda-3.0.jar nodes

The command will generate a directory structures as below:

  1. nodes
  2. ├── NetworkMapAndNotary
  3.    ├── additional-node-infos
  4.    ├── certificates
  5.    ├── corda.jar
  6.    ├── cordapps
  7.    ├── logs
  8.    ├── network-parameters
  9.    ├── node.conf
  10.    ├── nodeInfo-9D193AC8883388E96664F3B0CC0AD3D162114F85F1633A69EF8CB30DA0993065
  11.    └── persistence.mv.db
  12. ├── PartyA
  13.    ├── additional-node-infos
  14.    ├── certificates
  15.    ├── corda.jar
  16.    ├── cordapps
  17.    ├── logs
  18.    ├── network-parameters
  19.    ├── node.conf
  20.    ├── nodeInfo-E4477B559304AADFC0638772C0956A38FA2E2A7A5EB0E65D0D83E5884831879A
  21.    └── persistence.mv.db
  22. ├── PartyB
  23.    ├── additional-node-infos
  24.    ├── certificates
  25.    ├── corda.jar
  26.    ├── cordapps
  27.    ├── logs
  28.    ├── network-parameters
  29.    ├── node.conf
  30.    ├── nodeInfo-B1F1FD9F15FCFDE1C9EE7A59504A44D669AB67B655597D60B691138E9C12E93C
  31.    └── persistence.mv.db
  32. └── whitelist.txt

7. replace each localhost in node.conf with its container name, for eaxmple:

  1. myLegalName="O=NetworkMapAndNotary,L=London,C=GB"
  2. notary {
  3. validating=true
  4. }
  5. p2pAddress="localhost:10002"
  6. rpcUsers=[]
  7. webAddress="localhost:10004"
  8. --> #change to container name
  9. myLegalName="O=NetworkMapAndNotary,L=London,C=GB"
  10. notary {
  11. validating=true
  12. }
  13. p2pAddress="NetworkMapAndNotary:10002"
  14. rpcUsers=[]
  15. webAddress="NetworkMapAndNotary:10004"

8. start containers with docker-compose up

  1. $ docker-compose up
  2. ...
  3. NetworkMapAndNotary | Loaded CorDapps : corda-core-corda-3.0
  4. NetworkMapAndNotary | Node for "NetworkMapAndNotary" started up and registered in 31.61 sec
  5. PartyB | Advertised P2P messaging addresses : PartyB:10008
  6. PartyB | RPC connection address : PartyB:10009
  7. PartyB | RPC admin connection address : PartyB:10049
  8. PartyA | Advertised P2P messaging addresses : PartyA:10005
  9. PartyA | RPC connection address : PartyA:10006
  10. PartyA | RPC admin connection address : PartyA:10046
  11. PartyB | Loaded CorDapps : cordapp-template-kotlin-0.1, cordapp-contracts-states-0.1, cordapp-0.1, corda-finance-corda-3.0, corda-core-corda-3.0
  12. PartyB | Node for "PartyB" started up and registered in 40.21 sec
  13. PartyA | Loaded CorDapps : cordapp-template-kotlin-0.1, cordapp-contracts-states-0.1, cordapp-0.1, corda-finance-corda-3.0, corda-core-corda-3.0
  14. PartyA | Node for "PartyA" started up and registered in 40.88 sec
  15. PartyB | Webserver started up in 46.92 sec
  16. PartyA | Webserver started up in 47.23 sec

9. Access each node via web server

  1. localhost:10007
  2. or
  3. localhost:10010

总结

异常

启动docker-compose时,发现了network parameters有问题

  1. PartyB | E 01:41:49+0000 [main] cordapp.CordappProviderImpl.verifyInstalledCordapps - Contract com.template.TemplateContract found in attachment 514EA81F872492BD293B8B60B924D52B4F2A850AA90C9298732F1CF66EE7B420 is not whitelisted in the network parameters. If this is a production node contact your zone operator. See https://docs.corda.net/network-map.html {}
  2. PartyB | E 01:41:49+0000 [main] cordapp.CordappProviderImpl.verifyInstalledCordapps - Contract net.corda.finance.contracts.JavaCommercialPaper found in attachment C26A176F7448571B553D96D8EB61B44B10F6CB18A7E5C862AC7794705F07A417 is not whitelisted in the network parameters. If this is a production node contact your zone operator. See https://docs.corda.net/network-map.html {}
  3. PartyB | E 01:41:49+0000 [main] cordapp.CordappProviderImpl.verifyInstalledCordapps - Contract net.corda.finance.contracts.asset.Cash found in attachment C26A176F7448571B553D96D8EB61B44B10F6CB18A7E5C862AC7794705F07A417 is not whitelisted in the network parameters. If this is a production node contact your zone operator. See https://docs.corda.net/network-map.html {}
  4. PartyB | E 01:41:49+0000 [main] cordapp.CordappProviderImpl.verifyInstalledCordapps - Contract net.corda.finance.contracts.asset.CommodityContract found in attachment C26A176F7448571B553D96D8EB61B44B10F6CB18A7E5C862AC7794705F07A417 is not whitelisted in the network parameters. If this is a production node contact your zone operator. See https://docs.corda.net/network-map.html {}
  5. PartyB | E 01:41:49+0000 [main] cordapp.CordappProviderImpl.verifyInstalledCordapps - Contract net.corda.finance.contracts.asset.Obligation found in attachment C26A176F7448571B553D96D8EB61B44B10F6CB18A7E5C862AC7794705F07A417 is not whitelisted in the network parameters. If this is a production node contact your zone operator. See https://docs.corda.net/network-map.html {}
  6. PartyB | E 01:41:49+0000 [main] cordapp.CordappProviderImpl.verifyInstalledCordapps - Contract net.corda.finance.contracts.CommercialPaper found in attachment C26A176F7448571B553D96D8EB61B44B10F6CB18A7E5C862AC7794705F07A417 is not whitelisted in the network parameters. If this is a production node contact your zone operator. See https://docs.corda.net/network-map.html {}
  7. PartyA | E 01:41:50+0000 [main] cordapp.CordappProviderImpl.verifyInstalledCordapps - Contract com.template.TemplateContract found in attachment 514EA81F872492BD293B8B60B924D52B4F2A850AA90C9298732F1CF66EE7B420 is not whitelisted in the network parameters. If this is a production node contact your zone operator. See https://docs.corda.net/network-map.html {}
  8. PartyA | E 01:41:50+0000 [main] cordapp.CordappProviderImpl.verifyInstalledCordapps - Contract net.corda.finance.contracts.JavaCommercialPaper found in attachment C26A176F7448571B553D96D8EB61B44B10F6CB18A7E5C862AC7794705F07A417 is not whitelisted in the network parameters. If this is a production node contact your zone operator. See https://docs.corda.net/network-map.html {}
  9. PartyA | E 01:41:50+0000 [main] cordapp.CordappProviderImpl.verifyInstalledCordapps - Contract net.corda.finance.contracts.asset.Cash found in attachment C26A176F7448571B553D96D8EB61B44B10F6CB18A7E5C862AC7794705F07A417 is not whitelisted in the network parameters. If this is a production node contact your zone operator. See https://docs.corda.net/network-map.html {}
  10. PartyA | E 01:41:50+0000 [main] cordapp.CordappProviderImpl.verifyInstalledCordapps - Contract net.corda.finance.contracts.asset.CommodityContract found in attachment C26A176F7448571B553D96D8EB61B44B10F6CB18A7E5C862AC7794705F07A417 is not whitelisted in the network parameters. If this is a production node contact your zone operator. See https://docs.corda.net/network-map.html {}
  11. PartyA | E 01:41:50+0000 [main] cordapp.CordappProviderImpl.verifyInstalledCordapps - Con

Corda很不错的解释文章,后面有例子

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注