From 0ffd44aef744637cc6d81bbaa59c22ab8c576856 Mon Sep 17 00:00:00 2001 From: CaiHQ Date: Fri, 20 May 2022 23:31:40 +0800 Subject: [PATCH] add readme add bdserver-docker add yjs plugin --- README.md | 76 ++++++++++++++++--- backend/.gitignore | 1 + backend/build.gradle | 8 +- backend/debugconf.json | 10 +++ backend/deployconfig.json | 9 --- backend/src/test/java/HelloTest.java | 10 +++ backend/yjs/main.yjs | 17 ++--- bdserver-docker-compose/README.md | 32 ++++++++ .../bdcontract/cmconfig.json | 23 ++++++ .../bdcontract/manager.key | 1 + .../bdcontract/manager.keypair | 2 + bdserver-docker-compose/bdledger/config.yml | 42 ++++++++++ bdserver-docker-compose/docker-compose.yml | 34 +++++++++ bdserver-docker-compose/start.sh | 13 ++++ 14 files changed, 245 insertions(+), 33 deletions(-) create mode 100644 backend/debugconf.json delete mode 100644 backend/deployconfig.json create mode 100644 backend/src/test/java/HelloTest.java create mode 100644 bdserver-docker-compose/README.md create mode 100644 bdserver-docker-compose/bdcontract/cmconfig.json create mode 100644 bdserver-docker-compose/bdcontract/manager.key create mode 100644 bdserver-docker-compose/bdcontract/manager.keypair create mode 100644 bdserver-docker-compose/bdledger/config.yml create mode 100644 bdserver-docker-compose/docker-compose.yml create mode 100755 bdserver-docker-compose/start.sh diff --git a/README.md b/README.md index aeefeb6..94aff8f 100644 --- a/README.md +++ b/README.md @@ -1,31 +1,85 @@ # contract-java-example #### 介绍 + idea开发合约的示例。 -#### 配置cp 的lib -做一个软链接,将cp-bundle的build/output 软链接到 -backend/cplibs。 +#### 测试环境配置 -#### 配置grepcp.list -用于手动指定不打包的jar。 格式为每行一个jar名。 +1. 在本地准备docker测试环境。 +```bash +cd bdserver-docker-compose +docker-compose up -d +cat bdcontract/manager.keypair +#并复制这组{"publickey".."privateKey"} +``` + +2. 打开浏览器`http://127.0.0.1:21030/NodePortal.html`。点击右上角`欢迎04xx`按钮,将步骤1中的keyPair复制,并点`导入密钥`。 + +### 开发环境配置 + +可安装idea插件:`ideaplugin/yjs-formater-1.0-SNAPSHOT-1653060411648.zip` +用于格式化yjs文件和自动补全。 + +#### 调试运行 + +1修改debugconf.json,将cpHost改为从docker可访问的域名或ip。 + +``` +在MAC/Linux环境下,可使用ifconfig | grep inet ,查看本地局域网ip,并配置到cpHost字段。 +在Windows环境下,可使用"cpHost":"host.docker.internal"。 +其中host.docker.internal是docker中内置的一个域名,可用于访问host。 +https://docs.docker.com/desktop/windows/networking/ +``` + +示例的debugconf.json配置: + +```json +{ + "agentAddress": "127.0.0.1:21030", + "publicKey": "04180354fdb6507f8ab98ccfbe165ce11da74ba733f81af86ad6d32216b32cf4f797c559d50ceeefbf4c760c3483840471c67471b90acdffb388cd7d496d9a1610", + "privateKey": "1d4196947f59532db6f8f4055e58474a48db8f30b476ae3edc66406464521b3b", + "ypkPath": "./backend/build/contractexample-1.3.0.ypk", + "killBeforeStart": "ContractExample", + "createParam": { + }, + "cpHost": "host.docker.internal" +} +``` + +2.启动测试用例: + +``` +test/java/HelloTest.run +``` + +注意run方法中的`for (;;);`用于防止测试用例退出。 +在windows中可能影响其他测试用例运行。可独立写个`public static void main(String[] args)`去运行合约。 + +#### 部署运行 + +部署至远程服务端执行":backend:deploy"任务: +即执行./backend/build.gradle 中的 deploy。 #### 前端sdk说明 -1. 如何使用javascript sdk -2. 如何使用typescript sdk -3. 示例:使用vite/vue/...等框架 -3. 如何配置成前后端独立连接的 -4. 如何配置成前后端一体化 + +1. 如何使用javascript sdk +2. 如何使用typescript sdk +3. 示例:使用vite/vue/...等框架 +3. 如何配置成前后端独立连接的 +4. 如何配置成前后端一体化 ##### 模拟后端数据 - #### 后端常用方法说明 ##### 调用相同CP的yjs代码 + 参考: Hello.callYJSInSameCP + ##### 调用其他CP的yjs代码 + 参考: Hello.callYJSInOtherCP diff --git a/backend/.gitignore b/backend/.gitignore index 2643730..e5c2fe5 100644 --- a/backend/.gitignore +++ b/backend/.gitignore @@ -1,4 +1,5 @@ /.DS_Store/ +ContractDB /build/ /cplibs/ cplibs diff --git a/backend/build.gradle b/backend/build.gradle index c574140..7ec0c61 100644 --- a/backend/build.gradle +++ b/backend/build.gradle @@ -5,7 +5,7 @@ buildscript { } dependencies { classpath "org.bdware.bdcontract:simple-ypk-packer:0.5.0" - classpath "org.bdware.bdcontract:ypk-deploy-tool:0.5.0" + classpath "org.bdware.bdcontract:ypk-deploy-tool:0.5.2" } } plugins { @@ -40,7 +40,7 @@ sourceSets { dependencies { api 'org.apache.logging.log4j:log4j-core:2.17.2' api 'org.apache.logging.log4j:log4j-api:2.17.2' - implementation 'org.bdware.sc:cp:1.5.4' + implementation 'org.bdware.sc:cp:1.5.7' testImplementation 'junit:junit:4.13.2' } @@ -73,7 +73,7 @@ task copyJar(type: Copy, dependsOn: [":backend:jar", ":backend:copyLibs"]) { } def reltivePath = "./backend" //reltivePath="." -def currVersion = "1.2.0" +def currVersion = "1.3.0" task grepCP(dependsOn: ["copyJar"]) { doLast { org.bdware.datanet.YPKPacker.grepJarByCPVersion("${reltivePath}/build/output/libs", org.bdware.datanet.CPVersion.cp_1_5_3) @@ -94,6 +94,6 @@ task buildYPK(dependsOn: ["buildZip"]) { } task deploy(dependsOn: ["buildYPK"]) { doLast { - org.bdware.ypkdeploy.HTTPTool.deployWithYpk("./backend/deployconfig.json", "${reltivePath}/build/contractexample-${currVersion}.ypk") + org.bdware.ypkdeploy.HTTPTool.deployWithYpk("${reltivePath}/debugconf.json", "${reltivePath}/build/contractexample-${currVersion}.ypk") } } \ No newline at end of file diff --git a/backend/debugconf.json b/backend/debugconf.json new file mode 100644 index 0000000..11bd1b5 --- /dev/null +++ b/backend/debugconf.json @@ -0,0 +1,10 @@ +{ + "agentAddress": "127.0.0.1:21030", + "publicKey": "04180354fdb6507f8ab98ccfbe165ce11da74ba733f81af86ad6d32216b32cf4f797c559d50ceeefbf4c760c3483840471c67471b90acdffb388cd7d496d9a1610", + "privateKey": "1d4196947f59532db6f8f4055e58474a48db8f30b476ae3edc66406464521b3b", + "ypkPath": "./build/contractexample-1.3.0.ypk", + "killBeforeStart": "ContractExample", + "createParam": { + }, + "cpHost": "host.docker.internal" +} \ No newline at end of file diff --git a/backend/deployconfig.json b/backend/deployconfig.json deleted file mode 100644 index 7b1181a..0000000 --- a/backend/deployconfig.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "agentAddress": "ip:port", - "privateKey": "e85ce2f4d8...", - "publicKey": "04da01345770b7e09d4774bf6cd67bf90b32eaa09f517c79b2d1f9b8a926", - "ypkPath": "./backend/build/contractexample-1.2.0.ypk", - "killBeforeStart": "ContractID/ContractName", - "createParam": { - } -} \ No newline at end of file diff --git a/backend/src/test/java/HelloTest.java b/backend/src/test/java/HelloTest.java new file mode 100644 index 0000000..231ad5f --- /dev/null +++ b/backend/src/test/java/HelloTest.java @@ -0,0 +1,10 @@ +import org.bdware.sc.debugger.DebugMain; +import org.junit.Test; + +public class HelloTest { + @Test + public void run() { + DebugMain.runWithConf("./debugconf.json"); + for (; ; ) ; + } +} diff --git a/backend/yjs/main.yjs b/backend/yjs/main.yjs index d798434..fd61212 100644 --- a/backend/yjs/main.yjs +++ b/backend/yjs/main.yjs @@ -1,12 +1,11 @@ - oracle ContractExample { - function onCreate(){ - Global.owner = requester; + function onCreate() { + Global.owner = requester; + } + export function callHello(arg) { + return org.bdware.sc.contractexample.Hello.call()+requester; + } + export function getOwner() { + return Global.owner; } - export function callHello(arg){ - return Hello.call()+requester; - } - export function getOwner(){ - return Global.owner; - } } \ No newline at end of file diff --git a/bdserver-docker-compose/README.md b/bdserver-docker-compose/README.md new file mode 100644 index 0000000..890f9e1 --- /dev/null +++ b/bdserver-docker-compose/README.md @@ -0,0 +1,32 @@ +# 镜像准备 +请提前熟悉docker常用命令,主要包括`docker ps`、`docker image xx`、`docker kill`等。 +1. 下载或导入镜像: +```bash +#下载镜像 +docker pull bdware/bdcontract:latest +#导入镜像,bdcontract-x.x.x.tar为待导入镜像文件。 +docker load -i bdcontract-x.x.x.tar +``` +2. 检查是否有"bdware/bdcontract:latest"镜像。 +```bash +docker image ls | grep bdcontract +``` +如果没有名为"bdware/bdcontract latest"的镜像,就将其中的最新版本打上。 +docker image tag bdware/bdcontract:x.y.z bdware/bdcontract:latest + +# 修改配置 +1. 生成管理员密钥对。 将公钥配置到manager.key中。 格式为单行130个字符。类似于 +``` +04dad765858... +``` + +也可利用cp目录下的jar包自己生成: +```bash +#本操作需要依赖java 1.8以上环境。 +java -cp cp/libs:cp/yjs.jar org.bdware.sc.SM2Helper generateKeyToFile +``` +生成的文件请妥善保存。 + +2. 修改配置文件cmconfig.json(如果需要),配置文件参数详见配置说明`cmconfig.readme.md`。 + +3. 执行脚本`sh start.sh` \ No newline at end of file diff --git a/bdserver-docker-compose/bdcontract/cmconfig.json b/bdserver-docker-compose/bdcontract/cmconfig.json new file mode 100644 index 0000000..a30c2af --- /dev/null +++ b/bdserver-docker-compose/bdcontract/cmconfig.json @@ -0,0 +1,23 @@ +{ + "cmi": "_1650424170416", + "debug": "", + "disableDoRepo": false, + "disableLocalLhs": false, + "doipCertPath": "", + "doipLhsAddress": "", + "doipPort": 21032, + "doipUserHandle": "", + "enableEventPersistence": false, + "enableSsl": "./ssl/chained.pem:./ssl/domain.pem", + "ip": "127.0.0.1", + "isLAN": true, + "overwrite": false, + "servicePort": 21030, + "textFileSuffixes": ".yjs,.json,.txt,.css,.js,.html,.md,.conf,.csv", + "withBdledgerClient": "./runnable/bdledger_mac", + "withBdledgerServer": false, + "consistencyPlugins": "/bdcontract/nosuchlib/custom-plugin.jar", + "startContract": [ + ], + "datachainConf": "bdledger:18021" +} \ No newline at end of file diff --git a/bdserver-docker-compose/bdcontract/manager.key b/bdserver-docker-compose/bdcontract/manager.key new file mode 100644 index 0000000..9bfbd3b --- /dev/null +++ b/bdserver-docker-compose/bdcontract/manager.key @@ -0,0 +1 @@ +04180354fdb6507f8ab98ccfbe165ce11da74ba733f81af86ad6d32216b32cf4f797c559d50ceeefbf4c760c3483840471c67471b90acdffb388cd7d496d9a1610 \ No newline at end of file diff --git a/bdserver-docker-compose/bdcontract/manager.keypair b/bdserver-docker-compose/bdcontract/manager.keypair new file mode 100644 index 0000000..603017a --- /dev/null +++ b/bdserver-docker-compose/bdcontract/manager.keypair @@ -0,0 +1,2 @@ + { "publicKey": "04180354fdb6507f8ab98ccfbe165ce11da74ba733f81af86ad6d32216b32cf4f797c559d50ceeefbf4c760c3483840471c67471b90acdffb388cd7d496d9a1610", + "privateKey": "1d4196947f59532db6f8f4055e58474a48db8f30b476ae3edc66406464521b3b"} \ No newline at end of file diff --git a/bdserver-docker-compose/bdledger/config.yml b/bdserver-docker-compose/bdledger/config.yml new file mode 100644 index 0000000..6a8cc2e --- /dev/null +++ b/bdserver-docker-compose/bdledger/config.yml @@ -0,0 +1,42 @@ +node: + id: acb1cc2a7013ec8a7ea7eee0ec6f835851dd8e91 + +api: + grpc: + addr: :18021 + http: + enabled: true + +datastore: + source: /data/block + +p2p: + identity: + peerId: QmeG9PE8NQ7q8M6xiovVp9u1UjZZWc8Wom1K5MomJgLuWs + privKey: CAASqQkwggSlAgEAAoIBAQDAb0X3U2HjgoUniXH8EMs6L8FHGU63BahbY+/7GjnVFXe0bSuPlpaUC2Twmhulvad/1nSCuF178UYoYQ6Qz1AwBl4IiZmQxhSXdZPEXrN3WnC70rrkMMQxz7sjevyEXm9EIUo4Hepxlvd6PlTHUaYOW4TXSsv5JqoZW4+itxane/tOCCpFM/ppftpz3N5RxO181mczm6cljHcShZXkHSxo0nn3sHSqixqbzv+c0x/c5YP+aue3MJX2IjODGEY1a3x3HwCmwK3IgUSKWyZk480codWeO/5JFLNKzlXzWAu2GG7v2MF1h3Fa4lwEGdCLILY2Ux9d+M3wRvoz7ZWYn0CTAgMBAAECggEALLgvLEDGWNa2FvAL+yXz9HuwkNNCxamc7FAXLuVzVx98CAPuwZmfOJxFQtytXCDs0fqGbPJnVyxEv5F+jwx+eAqiw50mgMxncKM0ScgwMKZl2GAqamkizuiVdrNYB3LirJSH6O6a0v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PGEEJzrUlg== + addresses: + swarm: + - /ip4/0.0.0.0/tcp/18026 + - /ip4/0.0.0.0/udp/18026/quic + bootstrap: + - /ip4/127.0.0.1/tcp/18026/p2p/QmeG9PE8NQ7q8M6xiovVp9u1UjZZWc8Wom1K5MomJgLuWs + pubsub: + router: gossipsub + heartbeatInterval: 10000 + +query: + maxDuration: 3600 # seconds + maxRequestLifetime: 3.0 # seconds + waitReservedTime: 1.0 # seconds + +nrw: + numWitness: 0 + numBackup: 0 + txsPerBlock: 1000 + blockGenCycle: 30000 + requestTimeout: 16 + messageTimeout: 5 + +log: + format: text + level: INFO diff --git a/bdserver-docker-compose/docker-compose.yml b/bdserver-docker-compose/docker-compose.yml new file mode 100644 index 0000000..9e10323 --- /dev/null +++ b/bdserver-docker-compose/docker-compose.yml @@ -0,0 +1,34 @@ +version: "3" + +services: + bdledger: + image: bdware/bdledger:dev-210909.874f034d + command: "-c /etc/bdledger/config.yml" + restart: unless-stopped + volumes: + - ./bdledger/config.yml:/etc/bdledger/config.yml:ro + - ./bdledger/data:/data + ports: + - "2416:2416" # P2P + - "2401:2401" # API + bdcontract: + image: bdware/bdcontract:1.7.9 + command: "-Dfile.encoding=UTF-8 -Djava.library.path='./dynamicLibrary' -Dlog4j.configurationFile=./log4j2.properties -cp ./libs/*:bdagent.jar org.bdware.server.CMHttpServer" + restart: unless-stopped + depends_on: + - bdledger + volumes: + - ./bdcontract/rocksdb:/bdcontract/rocksdb + - ./bdcontract/ContractDB:/bdcontract/ContractDB + - ./bdcontract/ContractManagerDB:/bdcontract/ContractManagerDB + - ./bdcontract/BDWareProjectDir:/bdcontract/BDWareProjectDir + - ./bdcontract/cmconfig.json:/bdcontract/cmconfig.json:ro +# - ./bdcontract/cp:/bdcontract/cp + - ./bdcontract/tls:/bdcontract/tls:ro + - ./bdcontract/manager.key:/bdcontract/manager.key:ro + ports: + - "21030:21030" # Contract manager API + - "21031:21031" # Contract manager master + - "21032:21032" # DOA + - "21033:21033" # Prometheus exporter + - "21050-21100:21050-21100" diff --git a/bdserver-docker-compose/start.sh b/bdserver-docker-compose/start.sh new file mode 100755 index 0000000..ff9f730 --- /dev/null +++ b/bdserver-docker-compose/start.sh @@ -0,0 +1,13 @@ +#!/bin/bash +docker-compose up -d +#export CURDIR=`pwd`/data +# +#docker run -p 21030-21033:21030-21033 -p 2641:2641 -p 2041:2041 -v $CURDIR/BDWareProjectDir:/bdcontract/BDWareProjectDir \ +# -v $CURDIR/ContractDB:/bdcontract/ContractDB \ +# -v $CURDIR/ContractManagerDB:/bdcontract/ContractManagerDB \ +# -v $CURDIR/cmconfig.json:/bdcontract/cmconfig.json \ +# -v $CURDIR/log:/bdcontract/log \ +# -v $CURDIR/manager.key:/bdcontract/manager.key: \ +# bdware/bdcontract:1.7.9 +# -v $CURDIR/cp:/bdcontract/cp: \ +# -v $CURDIR/keys:/bdcontract/keys: \