使用 CLI 測試您的鏈碼

如果您的鏈碼是在網路上執行,您可以測試任何產生的方法。此外,如果您選擇在鏈碼開發期間建立 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) 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 網路上測試您的鏈碼

順利將鏈碼專案部署到遠端 Oracle Blockchain Platform 網路之後,您可以依照在本機 Hyperledger Fabric 網路測試您的鏈碼中的描述進行測試。

如果您手動部署鏈碼,而不是使用 Blockchain App Builder,則必須在測試鏈碼之前手動呼叫 init 函數。

您可以使用相同的 ochain invokeochain 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 網路上測試記號專案

您可以使用 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 Rich Query,確保您分別使用 -r-u-s 選項來指定遠端 Oracle Blockchain Platform 執行處理 URL、使用者名稱和密碼。

TypeScript 的範例查詢:
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
Go 的範例查詢:
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 鏈碼的語法不同。如上例所示,「執行」查詢為查詢參數使用雙引號,而非單引號。雙引號前必須加上反斜線字元。