首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

Hyperledger Composer 基础,第 1 部分 建模和测试您的区块链网络-5

Hyperledger Composer 基础,第 1 部分 建模和测试您的区块链网络-5

如何实现访问控制?访问控制由模型中的一个名为 permissions.acl 的文件管理。在部分,我将访问控制作为一个主要概念进行了介绍。在第 2 和第 3                部分中,我会更详细地介绍这个非常重要的主题,还会介绍如何控制对区块链应用程序的访问。
让我们看看您的模型的 Define 选项卡中的                permissions.acl。perishable-network 模板包含一个类似下面文件的访问控制列表 (ACL)                文件:
/**
* Sample access control list.
*/
rule Default {
    description: "Allow all participants access to all resources"
    participant: "ANY"
    operation: ALL
    resource: "org.acme.shipping.perishable.*"
    action: ALLOW
}

rule SystemACL {
  description:  "System ACL to permit all access"
  participant: "org.hyperledger.composer.system.Participant"
  operation: ALL
  resource: "org.hyperledger.composer.system.**"
  action: ALLOW
}




ACL 文件包含用于控制对区块链应用程序中的资源的访问的规则。一言以蔽之,Hyperledger Composer                在实施安全保护时已考虑到了您,我将在本教程系列后面详细介绍这个主题。
就现在而言,上面定义的访问控制规则授予了完全开放的访问权,这暂时没有问题,因为您刚开始使用 Hyperledger Composer。
测试模型既然已经将模型实例化,是时候测试它了,这意味着要运行代码!在本例中,这意味着运行 lib/logic.js 中的                JavaScript 代码。
在设置测试之前,让我们检查一下合约(已在 setupDemo() 函数中实例化),了解一下其中的条款。图 7                给出了用于实例化合约资产的 JavaScript 代码:
图 7. 智能合约:条款和条件
现在您已准备好测试智能合约。在浏览器中运行 Playground,单击 Playground UI 顶部的 Test 选项卡。
让我们测试一下以下场景:
  • IoT 温度传感器提供了以下读数(单位为摄氏度):
    • 5
    • 7
    • 1
    • 4
  • 货物已收到。
让我们逐个查看此场景的组成部分,先从温度传感器数据开始。
在真实的应用程序中,IoT 温度传感器会将此数据发送到 IBM Cloud,在这里会针对区块链来调用智能合约代码,以便记录这些交易。
在 Playground 中,区块链是在浏览器的本地存储中维护的,但无论区块链位于何处,执行的交易代码都是相同的(这使得 Playground                成为了一个完美的测试场所,对吧?)。
检查 lib/logic.js 中的 temperatureReading() 函数:
/**
* A temperature reading has been received for a shipment
* @param {org.acme.shipping.perishable.TemperatureReading} temperatureReading - the TemperatureReading transaction
* @transaction
*/
function temperatureReading(temperatureReading) {

    var shipment = temperatureReading.shipment;

    console.log('Adding temperature ' + temperatureReading.centigrade + ' to shipment ' + shipment.$identifier);

    if (shipment.temperatureReadings) {
        shipment.temperatureReadings.push(temperatureReading);
    } else {
        shipment.temperatureReadings = [temperatureReading];
    }

    return getAssetRegistry('org.acme.shipping.perishable.Shipment')
        .then(function (shipmentRegistry) {
            // add the temp reading to the shipment
            return shipmentRegistry.update(shipment);
        });
}




在真实的应用程序中,当船运集装箱中的 IoT 传感器想要发送读数时,它会(通过货船的网络)将读数发送到云端,在 OpenWhisk                中运行的一个无服务器函数(它将调用 temperatureReading() 函数)会在云中获取该读数。
要在 Playground 中模拟此过程:
  • 单击 Submit Transaction 按钮(就像调用                    setupDemo() 函数时所做的一样)。
  • 确保 TemperatureReading 出现在 Transaction                        Type 下拉列表中。
  • JSON Data Preview 窗口中,将“centigrade”读数从 0 更改为                    5(我们想要发送的第一个读数)。
  • 确保货物 ID 设置为 SHIP_001。
  • 单击 Submit
  • 对剩余 3 个读数重复此过程。
要在真实应用程序中接收货物,在进口商的手持设备上运行的应用程序能向在 IBM Cloud 中运行的应用程序(或 OpenWhisk                中运行的无服务器函数)表明货物已收到,后者随后会计算要汇给种植者的款项。
要在 Playground 中模拟货物接收,可在 Playground 中运行 ShipmentReceived                交易,确保提供了货物的 ID,并单击 Submit
我知道,此过程中要处理许多工作。但是不用担心:在下面的视频中,我将展示如何测试模型,并查看您在浏览器的 JavaScript                控制台中运行的所有交易的结果。
返回列表