使用 CLI 測試您的 Chaincode

如果您的鏈碼是在網路上執行,您可以測試任何產生的方法。此外,如果您選擇在鏈碼開發期間建立 executeQuery 方法,如果您的鏈碼部署到 Oracle Blockchain Platform 網路,則可以執行 SQL Rich Query。

在本機 Hyperledger Fabric 網路上測試您的鏈碼

當您的鏈碼專案在本機網路上執行之後,您就可以進行測試。

開啟新的 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 指令遵循傳送 <methodName><methodArguments>ochain invoke 指令相同的規則。
  • 在 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 網路後,您可以進行測試。

您可以測試在遠端網路上執行的鏈碼 (類似於在本機網路上測試),如在本機 Hyperledger Fabric 網路上測試您的鏈碼中所述。

如果您手動部署鏈碼,而不是使用區塊鏈 App 產生器,則必須先手動呼叫 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 網路上測試權杖專案

您可以使用 Blockchain App Builder、Oracle Blockchain Platform REST 代理主機或 Hyperledger Fabric SDK 來測試使用權杖的鏈碼專案。

區塊鏈 App 產生器

您可以使用 Blockchain App Builder CLI 來完成與多位使用者的交易,以測試權杖鏈碼。

若要與多位使用者進行測試,請變更呼叫和查詢命令中的授權參數 (使用者名稱和密碼選項)。

Oracle Blockchain Platform REST 代理主機

您可以使用 Oracle Blockchain Platform 中的 REST 代理主機,在遠端 Oracle Blockchain Platform 網路上執行您的權杖鏈碼。使用任何 REST 代理主機從屬端 (例如 Postman REST 從屬端) 來測試您的鏈碼專案。

若要測試多個使用者,請變更 REST 從屬端中的授權參數 (使用者名稱和密碼),或連線至其他 Oracle Blockchain Platform 執行處理。

執行 Berkeley 資料庫 SQL 豐富查詢

如果您選擇在鏈碼開發期間建立 executeQuery 方法,如果您的鏈碼部署到 Oracle Blockchain Platform 網路,則可以執行 SQL Rich Query。

如果您在規格檔案的 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 RTF 查詢。

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 查詢在查詢參數中使用雙引號,而非單引號。雙引號前面必須有反斜線字元。