使用 CLI 测试链代码

如果链代码在网络上运行,则可以测试生成的任何方法。此外,如果您选择在链代码开发期间创建 executeQuery 方法,则可以在链代码部署到 Oracle Blockchain Platform 网络时运行 SQL 丰富的查询。

在本地超级账本架构网络上测试您的链代码

在本地网络上运行链代码项目后,您可以对其进行测试。

打开新的 shell 并导航到项目目录以与链代码进行交互。安装并部署链代码后,您可以使用 ochain invokeochain query 命令将事务处理提交到链代码内的函数。

ochain invoke

用法:ochain invoke <methodName> <methodArguments>

以下参数和选项可用于 ochain invoke 命令:
ochain invoke -h 
Usage: invoke [options] <methodName> [...args] 
Invoke a Chaincode transaction

Arguments :
    <methodName>  (required) Name of chaincode method to invoke.
    [...args]     (optional) Chaincode method input parameters if any. Parameters should be space separated strings/JSON strings for objects.

Options:
    -h, --help                output command usage information
    -D, --debug               enable debug logging
    -P, --project <path>      (optional) Path to Chaincode project to deploy. If not specified, it defaults to current directory.
    -c, --channel <channel>   (optional) Blockchain Channel to deploy chaincode to. If not specified, it defaults to the 'default' channel.
    -u, --username <username> (optional, if -r option is applied) A user name that has install chaincode privileges. Contact your administrator for more details.
    -s, --sign <password>     (optional) Password to authenticate user.
    -r, --url <url>           (required) Restproxy Url of the remote OBP instance.
    For example,
    OBP Cloud Services: https://test-xyz-abc.blockchain.ocp.oraclecloud.com:7443/restproxy
    OBP Enterprise Edition: https://restproxy.test-xyz-abc.blockchain.platform.com:443/restproxy

Examples:
$> ochain invoke <method>
(without chaincode initial arguments)  
$> ochain invoke <method> '{"manufacturerId":"m01","rawMaterialAvailable":9,"productsAvailable":4,"completionDate":"05-26-2020"}'
(for a single parameter)
$> ochain invoke <method> 's01' 's10'
$> ochain invoke <method> 's01' '{"manufacturerId":"m01","rawMaterialAvailable":9,"productsAvailable":4,"completionDate":"05-26-2020"}'
(for multiple parameters)
$> ochain invoke <method> 's01' 's10' -r <REST proxy URL of the remote instance> -u <username> -s <password>
(for remote invocation)

Linux 和 macOS

如果方法接受一个参数,则将其输入为字符串,如以下示例中所示:
ochain invoke createSupplier
'{"supplierId":"s01","rawMaterialAvailable":5,"license":"valid supplier","expiryDate":"2020-05-30","active":true}'
ochain invoke getSupplierDetails 's01'
'{"supplierId":"s01","rawMaterialAvailable":5,"license":"valid supplier","expiryDate":"2020-05-30","active":true}'
如果方法接受多个参数,则参数必须用空格分隔。例如:
ochain invoke getSupplierByRange 's01' 's03'
必须使用不同的格式在链代码中指定嵌入资产,例如使用嵌入式地址资产的员工资产,如以下示例所示:
name: employee
  properties:
     name: employeeId
     type: string
     mandatory: true
     id: true

     name: firstName
     type: string
     validate: max(30)
     mandatory: true

     name: lastName
     type: string
     validate: max(30)
     mandatory: true

     name: age
     type: number
     validate: positive(),min(18)

     name: address
     type: address
name: address

type: embedded

properties:
   name: street
   type: string

   name: city
   type: string

   name: state
   type: string

   name: country
   type: string
对于这些嵌入式资产,您可以使用类似于以下内容的格式来调用链代码:
ochain invoke createEmployee '{"employeeID":"e01", "firstName":"John", "lastName":"Doe", 
"age":35, "address":{"street":"Elm Ave", "city":"LA", "state":"California", "country":"US"}}'

Microsoft Windows

Windows 命令提示符不接受单引号 ('),因此必须以双引号 ("") 指定所有参数。必须转义包含双引号的任何参数,如以下示例中所示。

ochain invoke createSupplier
"{\"supplierId\":\"s01\",\"rawMaterialAvailable\":5,\"license\":\"valid
supplier\",\"expiryDate\":\"2020-05-30\",\"active\":true}"
如果方法接受多个参数,则必须用空格分隔这些参数。例如:
ochain invoke getSupplierByRange "s01" "s03"
如果您的链代码中有嵌入资产,例如使用嵌入式地址资产的员工资产(如前面所示),则可以使用类似于以下内容的格式来调用链代码:
ochain invoke createEmployee "{\"employeeID\":\"e01\", \"firstName\":\"John\", 
\"lastName\":\"Doe\", \"age\":35, \"address\":{\"street\":\"Elm Ave\", \"city\":\"LA\", 
\"state\":\"California\", \"country\":\"US\"}}"

验证

方法参数将根据规范文件中指定的验证进行验证。如果任何验证失败,将在输出中列出错误。

成功调用链代码后,将显示类似于以下文本的日志:
========== Started Invoke Chaincode ==========
[2020-06-23T18:37:54.563] [INFO] default - Successfully sent Proposal and received ProposalResponse
[2020-06-23T18:37:56.619] [INFO default - The chaincode invoke transaction has been committed on peer localhost:7051
[2020-06-23T18:37:56.619] [INFO] default - The chaincode invoke transaction was valid.
[2020-06-23T18:37:56.620] [INFO default - Successfully sent transaction to the orderer.
[2020-06-23T18:37:56.620] [INFO] default - Successfully invoked method "createSupplier" on chaincode "TSProject" on channel "mychannel"
[2020-06-23T18:37:56.620] [INFO] default - 
========== Finished Invoke Chaincode ==========

ochain query 命令

用法:ochain query <methodName> <methodArguments>

以下参数和选项可用于 ochain query 命令:
ochain query -h 
Usage: query [options] <methodName> [...args] 
Invoke a Chaincode Query.

Arguments :
    <methodName>     (required) Name of chaincode method to invoke.
    [...args]        (optional) Chaincode method input parameters if any. Parameters should be space separated strings/JSON strings for objects.

Options:
    -h, --help                output command usage information
    -D, --debug               enable debug logging
    -P, --project <path>      (optional) Path to Chaincode project to deploy. If not specified, it defaults to current directory.
    -c, --channel <channel>   (optional) Blockchain Channel to deploy chaincode to. If not specified, it defaults to the 'default' channel.
    -u, --username <username> (optional, if -r option is applied) A user name that has install chaincode privileges. Contact your administrator for more details.
    -s, --sign <password>     (optional) Password to authenticate user.
    -r, --url <url>           (required) Restproxy Url of the remote OBP instance.
    For example,
    OBP Cloud Services: https://test-xyz-abc.blockchain.ocp.oraclecloud.com:7443/restproxy  
    OBP Enterprise Edition: https://restproxy.test-xyz-abc.blockchain.platform.com:443/restproxy

Examples:
$> ochain query <method>
(without chaincode initial arguments)
$> ochain query <method> s01
(for a single parameter)
$> ochain query <method> 's01' 's10'
$> ochain query <method> 's01' '{"manufacturerId":"m01"}'
(for multiple parameters)
$> ochain query <method> 's01' 's10' -r <REST proxy URL of the remote instance> -u <username> -s <password>
(for remote query)
ochain query 命令遵循与 ochain invoke 命令相同的传递 <methodName><methodArguments> 的规则。
  • 在 macOS 和 Linux 上,可以使用单引号,并且无需在参数内对引号进行转义。
  • 在 Microsoft Windows 上,所有参数必须用双引号括起来,并且必须对参数中的任何引号进行转义。

本地测试多个令牌用户

要在本地测试具有多个用户的令牌项目,可以使用 tokenUser 属性更改每个事务处理的调用方。每个基架链代码项目都包含一个 .ochain.json 文件,用于存储链代码的元数据。通过更新 .ochain.json 文件中 tokenUser 字段的值来更改调用方。

{
  "name": "digiCurrCC",
  "description": "Chaincode package for digiCurrCC",
  "chaincodeName": "digiCurrCC",
  "chaincodeType": "node",
  "configFileLocation": "/Users/user1/token.yml",
  "appBuilderVersion": "21.2.3",
  "nodeVersion": "v12.18.1",
  "tokenUser": "admin"
}

支架项目后,tokenUser 属性将设置为本地网络的默认 admin 用户。要更改事务处理的调用方,请更改 tokenUser 属性以匹配在调用 createAccount (TypeScript) 或 CreateAccount (Go) 方法时创建帐户时设置的 user_id 属性。

更新后自动安装和部署

每当您更新链代码时,所做的更改都会在部署到本地网络时自动进行编译、安装和部署。无需关闭或重新启动本地网络。所有项目都将在每次更改时自动编译和部署。

在远程 Oracle Blockchain Platform 网络上测试您的链代码

将链代码项目成功部署到远程 Oracle Blockchain Platform 网络后,您可以对其进行测试。

可以测试在远程网络上运行的链代码,类似于在本地网络上测试链代码,如 Test Your Chaincode on a Local Hyperledger Fabric Network 中所述。

如果您手动部署链代码,而不是使用区块链应用程序构建器,则必须在测试链代码之前手动调用 init 函数。

您可以使用相同的 ochain invokeochain query 命令在远程 Oracle Blockchain Platform Cloud 或 Enterprise Edition 网络上运行所有方法事务;远程网络上也支持本地网络上支持的所有内容。将远程 Oracle Blockchain Platform 实例 (-r)、用户名 (-u) 和密码 (-s) 选项的 REST 代理 URL 传递给命令。确保指定正确的 REST 代理端口,通常为 7443 或 443。

示例

ochain invoke createSupplier 
'{"supplierId":"s01","rawMaterialAvailable":5,"license":"valid 
supplier","expiryDate":"2020-05-30","active":true}' -r 
[https://%3cblockchain-restproxy-url%3e:7443/]https://<blockchain-proxy-url>:7443/ 
-u idcqa -s password

在远程 Oracle Blockchain Platform 网络上测试令牌项目

您可以使用区块链应用构建器、Oracle Blockchain Platform REST 代理或超级账本架构 SDK 测试使用令牌的链代码项目。

区块链应用构建器

您可以使用区块链应用构建器 CLI 完成与多个用户的交易,以测试令牌链代码。

要对多个用户进行测试,请在调用和查询命令中更改授权参数(用户名和密码选项)。

Oracle Blockchain Platform REST 代理

您可以在 Oracle Blockchain Platform 中使用 REST 代理在远程 Oracle Blockchain Platform 网络上运行令牌链代码。使用任何 REST 代理客户端(例如 Postman REST 客户端)测试链代码项目。

要测试多个用户,请在 REST 客户端中更改授权参数(用户名和密码),或者连接到 Oracle Blockchain Platform 的其他实例。

执行 Berkeley DB SQL 富查询

如果您选择在链代码开发期间创建 executeQuery 方法,则可以在链代码部署到 Oracle Blockchain Platform 网络时运行 SQL 富查询。

如果在规范文件的 customMethods 部分中指定了 executeQuery,将在控制器中创建相应的 executeQuery 方法。

规格文件:
customMethods:
    - executeQuery
    - "fetchRawMaterial(supplierid: string, rawMaterialSupply: number)"
    - "getRawMaterialFromSupplier(manufacturerId: string, supplierld: string, rawMaterialSupply: number)"
    - "createProducts(manufacturerId: string, rawMaterialConsumed: number, productsCreated: number)"
    - "sendProductsToDistribution()"
控制器文件:
**
*
* BDB sql rich queries can be executed in OBP CS/EE.
* This method can be invoked only when connected to remote OBP CS/EE network.
*
*/
@Validator(yup.string())
public async executeQuery(query: string) {
    const result = await this.query(query);
    return result;
    }

您可以调用此方法,通过使用 -r-u-s 选项分别指定远程 Oracle Blockchain Platform REST 代理 URL、用户名和密码,在 Oracle Blockchain Platform 网络上运行 Berkeley DB SQL 丰富的查询。

TypeScript 的查询示例:
ochain query  executeQuery "SELECT key, valueJson FROM <STATE> WHERE 
json_extract(valueJson, '$.rawMaterialAvailable') = 4" -r 
[https://%3cblockchain-restproxy-url%3e:7443/]https://<blockchain-restproxy-url>:7443/ 
-u idcqa -s password
Go 的示例查询:
ochain query  executeQuery "SELECT key, valueJson FROM <STATE> WHERE 
json_extract(valueJson, \"$.rawMaterialAvailable\") = 4" -r 
[https://%3cblockchain-restproxy-url%3e:7443/]https://<blockchain-restproxy-url>:7443/ 
-u idcqa -s password

整个 SQL 查询在参数中执行,因此您可以即时更改查询。确保为 REST 代理(通常为 7443 或 443)指定正确的端口。TypeScript 和 Go 链代码的语法不同。如前面的示例所示,Go 查询对查询参数使用双引号,而不是单引号。双引号前面必须有反斜杠字符。