Marbles nodejs 示例中的 golang 链代码如何工作?

How does the golang chaincode in the marbles nodejs example works?

我有这个例子https://github.com/IBM-Blockchain/marbles run locally. I saw that the example downloaded the golang chaincode from https://github.com/ibm-blockchain/marbles-chaincode。链码存储在 /marbles/node_modules/ibm-blockchain-js/temp/unzip 的硬盘上。 您能否解释一下 golang 链代码是如何在 nodejs 代码中执行的?

我没有详细查看 Marbles 应用程序,但一般来说,nodejs 代码只是验证器网络的客户端,验证器正在以完全解耦的方式处理基于 golang 的链码基于 nodejs 的客户端。在此过程中,验证器 downloads/acquires 链代码并在隔离容器中对其进行本地编译。您可以查看 [golang::chaincode]->[nodejs::client]->(network)->[golang::validator]->[golang::container] 这样的过程。所以第一部分和最后一部分是 golang/chaincode 相关的,中间发生的东西或多或少是一种运输。 IE。客户端是 nodejs 而验证器是 golang 这一事实在这里无关紧要。

实现 Marbles 链代码(也称为智能合约)的 Golang 代码不会在 Node.js 应用程序内部执行。链代码是应用程序与之交互以修改存储在区块链中的状态变量的内容。这种情况下的状态是:存在什么弹珠,它们的所有者是谁,它是什么颜色等。但是链码本身(Golang 代码)被打包为 docker 容器,部署到区块链,并且是 up 运行 等待交易。 Node.js 代码构建这些交易并将其发送到 docker 容器,接收链代码执行的结果,并更新当前状态的应用程序视图。

仅供参考,实施 Marbles 应用程序是为了演示如何在 Hyperledger Fabric 项目之上实施应用程序 运行。 Hyperledger 目前仅完全支持 Golang 作为智能合约语言,但很快就会支持更多语言。

如所述here,

Interacting with the cc is done with a HTTP REST call to a peer on the network. The ibc-js SDK will abstract the details of the REST calls away. This allows us to use dot notation to call our GoLang functions (such as chaincode.invoke.init_marble(args)).

The user will interact with our Node.js application in their browser. This client side JS code will open a websocket to the backend Node.js application. The client JS will send messages to the backend when the user interacts with the site.

The backend Node.js will send HTTP requests (via the SDK) to a blockchain peer to carry out the user's actions. The peer will communicate to its chaincode container at its leisure. Note that the previous HTTP request was really a 'submission' of chaincode to be run, it will actually run at a later date (usually milliseconds).

The cc container will carry out the desired operation and record it to the ledger. ie create/transfer a marble.