使用 CLI 測試您的 Chaincode

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

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

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

開啟新的 Shell 並導覽至專案目錄以與鏈碼互動。安裝並部署鏈碼後,您可以使用 ochain invokeochain 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) 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)

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) 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 的相同規則。
  • 在 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 網路上測試您的鏈碼

在您的鏈碼專案成功部署到遠端 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;
    }

您可以呼叫此方法,在 Oracle Blockchain Platform 網路上執行 Berkeley DB SQL RTF 查詢,方法是分別使用 -r-u-s 選項來指定遠端 Oracle Blockchain Platform REST 代理主機 URL、使用者名稱和密碼。

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