使用 CLI 測試您的 Chaincode
如果您的鏈碼是在網路上執行,則可以測試任何產生的方法。此外,如果您選擇在鏈碼開發期間建立 executeQuery
方法,如果您的鏈碼部署到 Oracle Blockchain Platform 網路,則可以執行 SQL Rich Query。
在本機 Hyperledger Fabric 網路上測試您的 Chaincode
當您的 chaincode 專案在區域網路執行後,即可進行測試。
開啟新的 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
指令遵循傳送 <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 網路上測試您的鏈碼中的描述進行測試。
如果您手動部署鏈碼,而不是使用 Blockchain App Builder,則必須在測試鏈碼之前手動呼叫 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 上的權杖專案
您可以使用 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 DB SQL Rich Queries
如果您選擇在鏈碼開發期間建立 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、使用者名稱和密碼。
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 鏈碼的語法不同。如上例所示,「執行」查詢會使用雙引號作為查詢參數,而非單引號。雙引號前面必須加上反斜線字元。