@lambeta
2018-03-23T04:32:12.000000Z
字数 8703
阅读 351
blockchain
git clone https://github.com/corda/cordapp-example
open class TemplateContract : Contract {override fun verify(tx: LedgerTransaction) {TODO()}}
2. 定义 Contract ID,用于构建 tx 的 output: StateAndContract
companion object {@JvmStaticval TEMPLATE_CONTRACT_ID = "com.example.contract.TemplateContract"}
3. 定义 Contract 中的 command, command 提供了 tx 的意图和交易双方的签名,用于 contract 中的验证
interface Commands : CommandData {class Action : TypeOnlyCommandData(), Commands}
如:
val command = tx.commands.requireSingleCommand<TemplateContract.Commands>()...when (command.value) {is Commands.Action -> {val input = tx.inputs.single()requireThat {"the transaction is signed by the owner" using (input.owner.owningKey in command.signers)"the state is propagated" using (outputs.size == 1)}}
WebServerPluginRegistry
// List of lambdas returning JAX-RS objectsval webApis: List<Function<CordaRPCOps, out Any>> get() = emptyList()// Map of static serving endpoints to the matching resource directory.val staticServeDirs: Map<String, String> get() = emptyMap()
重写其中的变量webApis
class TemplateWebPlugin : WebServerPluginRegistry {override val webApis: List<Function<CordaRPCOps, out Any>> = listOf(Function(::TemplateApi))...
注意: 实例化Plugin之后,rpcOps 依赖注入会在这里发生。
这个例子中的TemplateApi即是实现了JAX-RS的类,如下:
@Path("template")class TemplateApi(val rpcOps: CordaRPCOps) {// Accessible at /api/template/templateGetEndpoint.@GET@Path("templateGetEndpoint")@Produces(MediaType.APPLICATION_JSON)fun templateGetEndpoint(): Response {return Response.ok("Template GET endpoint.").build()}}
2. 注册Plugin
在路径META-INF/services/net.corda.webserver.services.WebServerPluginRegistry文件中填写类TemplateWebPlugin的限定名,这里是com.template.TemplateWebPlugin.
3. 对象的序列化白名单
class TemplateSerializationWhitelist : SerializationWhitelist {override val whitelist: List<Class<*>> = listOf(TemplateData::class.java)}// This class is not annotated with @CordaSerilizable, so it must be added to the serialization whitelist.data class TemplateData(val payload: String)
注意这里提到,如果Class没有@CordaSerilizable注解,那么需要放到白名单中。
4. 注册序列化白名单
在路径META-INF/services/net.corda.core.serialization.SerializationWhitelist文件中填写白名单的类限定名com.template.TemplateSerializationWhitelist.
cordaRPCOps可以用来操作 Party Node。
rpcOps.wellKnownPartyFromX500Name(partyName);
2. Start flow
rpcOps.startTrackedFlowDynamic(ExampleFlow.Initiator.class, iouValue, otherParty);
3. Vault query
$ git clone https://github.com/qianyan/corda-docker.git
$ docker-compose build
./gradlew deployNodescordapps under any node of build/nodes, e.g. NetworkMapAndNotary/cordapps, to corda-docker/cordapps in first step.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.confAfter these operations, you will see directories in corda-docker as below:
nodes├── NetworkMapAndNotary.conf├── PartyA.conf└── PartyB.conf
5. download network-bootstrapper-corda-3.0.jar in corda-docker, refer to corda network
aria2c -x3 http://downloads.corda.net/network-bootstrapper-corda-3.0.jar
6. generate real nodes in corda-docker
java -jar network-bootstrapper-corda-3.0.jar nodes
The command will generate a directory structures as below:
nodes├── NetworkMapAndNotary│ ├── additional-node-infos│ ├── certificates│ ├── corda.jar│ ├── cordapps│ ├── logs│ ├── network-parameters│ ├── node.conf│ ├── nodeInfo-9D193AC8883388E96664F3B0CC0AD3D162114F85F1633A69EF8CB30DA0993065│ └── persistence.mv.db├── PartyA│ ├── additional-node-infos│ ├── certificates│ ├── corda.jar│ ├── cordapps│ ├── logs│ ├── network-parameters│ ├── node.conf│ ├── nodeInfo-E4477B559304AADFC0638772C0956A38FA2E2A7A5EB0E65D0D83E5884831879A│ └── persistence.mv.db├── PartyB│ ├── additional-node-infos│ ├── certificates│ ├── corda.jar│ ├── cordapps│ ├── logs│ ├── network-parameters│ ├── node.conf│ ├── nodeInfo-B1F1FD9F15FCFDE1C9EE7A59504A44D669AB67B655597D60B691138E9C12E93C│ └── persistence.mv.db└── whitelist.txt
7. replace each localhost in node.conf with its container name, for eaxmple:
myLegalName="O=NetworkMapAndNotary,L=London,C=GB"notary {validating=true}p2pAddress="localhost:10002"rpcUsers=[]webAddress="localhost:10004"--> #change to container namemyLegalName="O=NetworkMapAndNotary,L=London,C=GB"notary {validating=true}p2pAddress="NetworkMapAndNotary:10002"rpcUsers=[]webAddress="NetworkMapAndNotary:10004"
8. start containers with docker-compose up
$ docker-compose up...NetworkMapAndNotary | Loaded CorDapps : corda-core-corda-3.0NetworkMapAndNotary | Node for "NetworkMapAndNotary" started up and registered in 31.61 secPartyB | Advertised P2P messaging addresses : PartyB:10008PartyB | RPC connection address : PartyB:10009PartyB | RPC admin connection address : PartyB:10049PartyA | Advertised P2P messaging addresses : PartyA:10005PartyA | RPC connection address : PartyA:10006PartyA | RPC admin connection address : PartyA:10046PartyB | Loaded CorDapps : cordapp-template-kotlin-0.1, cordapp-contracts-states-0.1, cordapp-0.1, corda-finance-corda-3.0, corda-core-corda-3.0PartyB | Node for "PartyB" started up and registered in 40.21 secPartyA | Loaded CorDapps : cordapp-template-kotlin-0.1, cordapp-contracts-states-0.1, cordapp-0.1, corda-finance-corda-3.0, corda-core-corda-3.0PartyA | Node for "PartyA" started up and registered in 40.88 secPartyB | Webserver started up in 46.92 secPartyA | Webserver started up in 47.23 sec
9. Access each node via web server
localhost:10007orlocalhost:10010
总结
启动docker-compose时,发现了network parameters有问题
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 {}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 {}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 {}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 {}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 {}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 {}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 {}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 {}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 {}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 {}PartyA | E 01:41:50+0000 [main] cordapp.CordappProviderImpl.verifyInstalledCordapps - Con