jdchain是京東數科開源的區塊鏈平臺,目標是實現一個面向企業應用場景的通用區塊鏈框架系統,能夠作為企業級基礎設施,為業務創新提供高效、靈活和安全的解決方案。之所以選擇jdchain研究是因為jdchain是為數不多的底層也是採用java實現的一個區塊鏈平臺
項目地址:https://github.com/blockchain...
文檔地址:http://ledger.jd.com/setup.html
部署組件
- peer:區塊鏈主節點,參與共識、賬本操作等
- gateway:與Peer節點通信,負責區塊鏈瀏覽器及消息傳遞
- 客戶端:採用SDK和網關鏈接,通過網關發起交易
獲取部署包
有兩種途徑可以拿到部署包,一、直接從官網下載.二、從github上拉源碼,然後自己構建,在deployment目錄下會生成部署包,自己構建的需要注意,如果是在Windows系統上構建的包,包裡的啟動腳本在linux系統下運行會有轉義字符的問題,需要在assembly.xml裡設置lineEnding為unix,具體設置點我查看
解壓部署
部署參考文檔:《從零開始部署JDChain》
效果預覽
區塊鏈部署工具
區塊鏈瀏覽器
部署遇到的問題:
官方文檔算比較詳細,但是很多細節沒有寫明,一般體驗和開發時部署的環境比較簡單,可能在一個主機上部署4個節點的peer。這個時候端口的編排要非常注意,因為jdchain目前的共識算法採用的開源的bftsmart實現,共識端口在同一臺主機上不能連續,不然就會端口衝突。博主就遇到了這個問題,下面是詳細排錯過程:採用SDK創建用戶時拋如下異常,網關可以正常連接,秘鑰認證沒有問題:
Caused by: java.lang.IndexOutOfBoundsException: The accessing index is out of BytesSlice's bounds! at com.jd.blockchain.utils.io.BytesSlice.checkBoundary(BytesSlice.java:174) at com.jd.blockchain.utils.io.BytesSlice.getInt(BytesSlice.java:97) at com.jd.blockchain.utils.io.BytesSlice.getInt(BytesSlice.java:86) at com.jd.blockchain.binaryproto.impl.HeaderEncoder.resolveCode(HeaderEncoder.java:71) at com.jd.blockchain.binaryproto.BinaryProtocol.decode(BinaryProtocol.java:41) at com.jd.blockchain.sdk.converters.BinarySerializeResponseConverter.getResponse(BinarySerializeResponseConverter.java:33) at com.jd.blockchain.utils.http.agent.HttpServiceAgent.invoke(HttpServiceAgent.java:587) ... 7 more
網關裡的異常
11:57:05.537 ERROR com.jd.blockchain.gateway.web.GatewayGlobalExceptionHandler 34 json - Unexpected exception occurred! --[RequestURL=[POST] http://192.168.1.190:8081/rpc/tx][class java.lang.IllegalStateException]Returned object not currently part of this pool java.lang.IllegalStateException: Returned object not currently part of this pool at org.apache.commons.pool2.impl.GenericObjectPool.returnObject(GenericObjectPool.java:530) ~[commons-pool2-2.5.0.jar!/:2.5.0] at com.jd.blockchain.consensus.bftsmart.client.BftsmartMessageService.sendOrderedMessage(BftsmartMessageService.java:46) ~[consensus-bftsmart-1.1.1.RELEASE.jar!/:1.1.1.RELEASE] at com.jd.blockchain.consensus.bftsmart.client.BftsmartMessageService.sendOrdered(BftsmartMessageService.java:22) ~[consensus-bftsmart-1.1.1.RELEASE.jar!/:1.1.1.RELEASE] at com.jd.blockchain.sdk.service.NodeSigningAppender.process(NodeSigningAppender.java:84) ~[sdk-base-1.1.1.RELEASE.jar!/:1.1.1.RELEASE] at com.jd.blockchain.sdk.service.PeerServiceProxy.process(PeerServiceProxy.java:89) ~[sdk-base-1.1.1.RELEASE.jar!/:1.1.1.RELEASE] at com.jd.blockchain.gateway.web.TxProcessingController.process(TxProcessingController.java:70) ~[gateway-1.1.1.RELEASE.jar!/:1.1.1.RELEASE] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_231] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_231] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_231] at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_231]
最終查明異常是由於網關裡創建AsynchServiceProxy失敗導致的,這部分實現採用了Apache-commons-pool2。實現如下:
public class BftsmartPeerProxyFactory extends BasePooledObjectFactory { private BftsmartClientSettings bftsmartClientSettings; private int gatewayId; private AtomicInteger index = new AtomicInteger(1); public BftsmartPeerProxyFactory(BftsmartClientSettings bftsmartClientSettings, int gatewayId) { this.bftsmartClientSettings = bftsmartClientSettings; this.gatewayId = gatewayId; } @Override public AsynchServiceProxy create() throws Exception { BftsmartTopology topology = BinarySerializeUtils.deserialize(bftsmartClientSettings.getTopology()); MemoryBasedViewStorage viewStorage = new MemoryBasedViewStorage(topology.getView()); TOMConfiguration tomConfiguration = BinarySerializeUtils.deserialize(bftsmartClientSettings.getTomConfig()); //every proxy client has unique id; tomConfiguration.setProcessId(gatewayId + index.getAndIncrement()); AsynchServiceProxy peerProxy = new AsynchServiceProxy(tomConfiguration, viewStorage); return peerProxy; } @Override public PooledObject wrap(AsynchServiceProxy asynchServiceProxy) { return new DefaultPooledObject<>(asynchServiceProxy); } }
這個代碼BinarySerializeUtils.deserialize(bftsmartClientSettings.getTopology())返回的是null,沒有正確拿到Bftsmart的網絡拓撲。進而查看peer節點,發現只有節點0成功了,其他都拋如下異常,初步判斷bftsmart的副本服務因為端口占用啟動失敗了:
11:36:48.479 ERROR bftsmart.tom.ServiceReplica 247 init - null java.net.BindException: 地址已在使用 at sun.nio.ch.Net.bind0(Native Method) ~[?:1.8.0_231] at sun.nio.ch.Net.bind(Net.java:433) ~[?:1.8.0_231] at sun.nio.ch.Net.bind(Net.java:425) ~[?:1.8.0_231] at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223) ~[?:1.8.0_231] at io.netty.channel.socket.nio.NioServerSocketChannel.doBind(NioServerSocketChannel.java:130) ~[netty-all-4.1.29.Final.jar:4.1.29.Final] at io.netty.channel.AbstractChannel$AbstractUnsafe.bind(AbstractChannel.java:558) ~[netty-all-4.1.29.Final.jar:4.1.29.Final] at io.netty.channel.DefaultChannelPipeline$HeadContext.bind(DefaultChannelPipeline.java:1358) ~[netty-all-4.1.29.Final.jar:4.1.29.Final] at io.netty.channel.AbstractChannelHandlerContext.invokeBind(AbstractChannelHandlerContext.java:501) ~[netty-all-4.1.29.Final.jar:4.1.29.Final] at io.netty.channel.AbstractChannelHandlerContext.bind(AbstractChannelHandlerContext.java:486) ~[netty-all-4.1.29.Final.jar:4.1.29.Final] at io.netty.channel.DefaultChannelPipeline.bind(DefaultChannelPipeline.java:1019) ~[netty-all-4.1.29.Final.jar:4.1.29.Final] at io.netty.channel.AbstractChannel.bind(AbstractChannel.java:254) ~[netty-all-4.1.29.Final.jar:4.1.29.Final] at io.netty.bootstrap.AbstractBootstrap$2.run(AbstractBootstrap.java:366) ~[netty-all-4.1.29.Final.jar:4.1.29.Final] at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:163) ~[netty-all-4.1.29.Final.jar:4.1.29.Final] at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:404) ~[netty-all-4.1.29.Final.jar:4.1.29.Final] at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:446) ~[netty-all-4.1.29.Final.jar:4.1.29.Final] at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:884) ~[netty-all-4.1.29.Final.jar:4.1.29.Final] at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) ~[netty-all-4.1.29.Final.jar:4.1.29.Final] at java.lang.Thread.run(Thread.java:748) ~[?:1.8.0_231]
從bftsmart官方文檔得知,如果在同一個主機運行,不能使用順序的端口號,即一開始編排的peer的共識端口,6000、6001、6002、6003是不行的,原因如下:如果在同一臺計算機(127.0.0.1)中部署/執行了某些(或全部)副本,config/hosts.config則不能具有順序的端口號(例如10000、10001、10002、10003)。這是因為每個副本都綁定了兩個端口:一個用於接收來自客戶端的消息,另一個用於接收來自其他副本的消息(通過獲取下一個端口號選擇) 。更一般而言,如果為副本R分配了端口號P,它將嘗試將端口P(綁定到接收到的客戶端請求)和端口P + 1(綁定到其他副本)進行綁定。如果不執行此準則,則副本可能無法綁定所有需要的端口。
- 文檔地址:https://github.com/bft-smart/...
結語
jdchain是完整採用java實現的區塊鏈項目,是java開發者研究區塊鏈的一大福音,而且項目開源後一直在迭代,文檔和社區支持方面都比較友好。入門雖然很簡單,但是要深入到這個項目後面還有很多的東西要研究。樓主計劃,後面先把SDK和網關的交互搞清楚,然後在研究下最新的共識實現(基於RabbitMQ),然後在研究下智能合約的應用,最後深入代碼實現,可能後面還會有其他的關於jdchain的內容輸出。最後希望全部掌握後能給社區貢獻點代碼、提供點案例、解答點問題。為開源儘自己的綿薄之力
關鍵字: com blockchain 企業級