CLI를 사용하여 체인코드 테스트

체인 코드가 네트워크에서 실행 중인 경우 생성된 메소드를 테스트할 수 있습니다. 또한 체인코드 개발 중에 executeQuery 메소드를 생성하도록 선택한 경우 체인코드가 Oracle Blockchain Platform 네트워크에 배포된 경우 SQL 리치 쿼리를 실행할 수 있습니다.

로컬 Hyperledger Fabric 네트워크에서 체인코드 테스트

체인코드 프로젝트가 로컬 네트워크에서 실행된 후 테스트할 수 있습니다.

새 셸을 열고 프로젝트 디렉토리로 이동하여 체인 코드와 상호 작용합니다. 체인코드가 설치되고 배포되면 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 사용자로 설정됩니다. 트랜잭션 호출자를 변경하려면 createAccount(TypeScript) 또는 CreateAccount(Go) 메소드가 호출될 때 계정이 생성될 때 설정된 user_id 속성과 일치하도록 tokenUser 속성을 변경합니다.

업데이트 후 자동 설치 및 배포

체인코드를 업데이트할 때마다 변경사항이 로컬 네트워크에 배포될 때 자동으로 컴파일, 설치 및 배포됩니다. 로컬 네트워크를 다시 작동 중지하거나 작동 중지할 필요가 없습니다. 모든 프로젝트는 모든 변경 시 자동으로 컴파일되고 배포됩니다.

원격 Oracle Blockchain Platform 네트워크에서 체인코드 테스트

체인코드 프로젝트가 원격 Oracle Blockchain Platform 네트워크에 성공적으로 배포된 후 테스트할 수 있습니다.

You test chaincode running on a remote network similar to testing it on a local network, as described in Test Your Chaincode on a Local Hyperledger Fabric Network.

블록체인 앱 빌더를 사용하는 대신 체인코드를 수동으로 배포한 경우 체인코드를 테스트하기 전에 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 네트워크에서 토큰 프로젝트 테스트

블록체인 앱 빌더, Oracle Blockchain Platform REST 프록시 또는 Hyperledger Fabric SDK를 사용하여 토큰으로 작동하는 체인코드 프로젝트를 테스트할 수 있습니다.

블록체인 앱 빌더

Blockchain App Builder CLI를 사용하여 여러 사용자가 토큰 체인코드를 테스트하는 거래를 완료할 수 있습니다.

여러 사용자로 테스트하려면 invoke 및 query 명령에서 권한 부여 매개변수(사용자 이름 및 암호 옵션)를 변경합니다.

Oracle Blockchain Platform REST 프록시

Oracle Blockchain Platform에서 REST 프록시를 사용하여 원격 Oracle Blockchain Platform 네트워크에서 토큰 체인 코드를 실행할 수 있습니다. Postman REST Client와 같은 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;
    }

이 메소드를 호출하면 -r, -u-s 옵션을 사용하여 원격 Oracle Blockchain Platform REST 프록시 URL, 사용자 이름 및 비밀번호를 각각 지정하여 Oracle Blockchain Platform 네트워크에서 Berkeley DB SQL 리치 질의를 실행할 수 있습니다.

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
이동에 대한 예제 질의:
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 query는 작은 따옴표 대신 query 파라미터에 큰 따옴표를 사용합니다. 큰따옴표 앞에는 백슬래시 문자가 와야 합니다.