使用 CLI 测试您的链代码
如果您的链代码在网络上运行,您可以测试任何生成的方法。此外,如果您选择在链代码开发期间创建 executeQuery
方法,则可以在链代码部署到 Oracle Blockchain Platform 网络时运行 SQL 富查询。
在本地超级账本架构网络上测试您的链代码
链代码项目在本地网络上运行后,您可以对其进行测试。
打开新的 shell 并导航到项目目录以与链代码交互。安装并部署链代码后,您可以使用 ochain invoke
和 ochain query
命令将事务处理提交到链代码内的函数。
ochain invoke
用法:ochain invoke <methodName> <methodArguments>
ochain invoke
命令所采用的参数和选项:
my-mac:TSProject myname$ 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) URL of the remote OBP instance. Example: https://<blockchain-instance-url>:7443/
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 <url of the remote OBP instance> -u <username> -s <password>
(for remote invocation)
Mac OSX 和 Linux
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"}}'
窗口
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
命令所采用的参数和选项:my-mac:TSProject myname$ 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) URL of the remote OBP instance. Example: https://<blockchain-instance-url>:7443/
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 <url of the remote OBP instance> -u <username> -s <password>
(for remote query)
ochain query
命令遵循与 ochain invoke
相同的传递 <methodName>
和 <methodArguments>
的规则。
- 在 Mac OSX 和 Linux 上,可以使用单引号,并且不需要在参数中转义引号。
- 在 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 Network 上测试您的链代码
在您的链代码项目成功部署到远程 Oracle Blockchain Platform 网络后,您可以按在本地超级账本架构网络上测试您的链代码中所述进行测试。
如果您手动部署了链代码,而不是使用区块链应用程序构建器,则必须在测试链代码之前手动调用 init
函数。
可以使用相同的 ochain invoke
和 ochain query
命令在远程 Oracle Blockchain Platform Cloud 或 Enterprise Edition 网络上执行所有方法事务;远程网络上也支持本地网络上支持的所有事务。将远程 Oracle Blockchain Platform 实例 (-r)、用户名 (-u) 和密码 (-s) 选项的 URL 传递给命令。
示例
ochain invoke createSupplier
'{"supplierId":"s01","rawMaterialAvailable":5,"license":"valid
supplier","expiryDate":"2020-05-30","active":true}' -r
[https://%3cblockchain-instance-url%3e:7443/]https://<blockchain-instance-url>:7443/
-u idcqa -s password
在远程 Oracle Blockchain Platform Network 上测试令牌项目
您可以使用区块链应用构建器、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;
}
您可以调用此方法在 Oracle Blockchain Platform 网络上执行 Berkeley DB SQL 富查询,确保分别使用 -r、-u 和 -s 选项来指定远程 Oracle Blockchain Platform 实例 URL、用户名和密码。
ochain query executeQuery "SELECT key, valueJson FROM <STATE> WHERE
json_extract(valueJson, '$.rawMaterialAvailable') = 4" -r
[https://%3cblockchain-instance-url%3e:7443/]https://<blockchain-instance-url>:7443/
-u idcqa -s password
ochain query executeQuery "SELECT key, valueJson FROM <STATE> WHERE
json_extract(valueJson, \"$.rawMaterialAvailable\") = 4" -r
[https://%3cblockchain-instance-url%3e:7443/]https://<blockchain-instance-url>:7443/
-u idcqa -s password
整个 SQL 查询都包含在参数中,因此您可以即时更改查询。TypeScript 和 Go 链代码的语法不同。如上例所示,Go 查询对查询参数使用双引号,而不是单引号。双引号前面必须带有反斜杠字符。