# Команда peer chaincode Команда `peer chaincode` позволяет администраторам выполнять такие операции с чейнкодом на одноранговых узлах, как установка, создание нового экземпляра, вызов, упаковка, запрос и обновление чейнкода. ## Синтаксис Команда `peer chaincode` имеет следующие подкоманды: * install * instantiate * invoke * list * package * query * signpackage * upgrade Различные подкоманды (install, instatiate...) соответствуют различным операциям с чейнкодом на одноранговых узлах. Например, подкоманда `peer chaincode install` используется для установки чейнкода на одноранговом узле, а подкоманда `peer chaincode query` — для обращения к чейнкоду для получения текущего значения из копии реестра на одноранговом узле. Каждая подкоманда `peer chaincode` описана вместе с ее параметрами в отдельном разделе этой статьи. ## Флаги Для каждой подкоманды `peer chaincode` предусмотрен соответствующий набор флагов. Также существуют глобальные флаги, которые подходят ко всем подкомандам `peer chaincode`. Однако использовать эти флаги не все всегда нужно. Например, подкоманда `query` не требует использование флага `--orderer`. Отдельные флаги описаны в подразделах соответствующих подкоманд. Ниже приведены глобальные флаги команды: * `--cafile ` Путь к файлу, содержащему доверенный сертификат службы упорядочения, кодированный в формат PEM. * `--certfile ` Путь к файлу, содержащему открытый ключ X509 в формате PEM для взаимного TLS-соединения со службой упорядочения. * `--keyfile ` Путь к файлу, содержащему закрытый ключ в формате PEM для взаимного TLS-соединения со службой упорядочения. * `-o` или `--orderer ` Адрес точки службы упорядочения в формате `<имя хоста или IP-адрес>:<порт>`. * `--ordererTLSHostnameOverride ` Переопределение имени хоста для проверки TLS-соединения со службой упорядочения. * `--tls` Использовать TLS для связи со службой упорядочения. * `--transient ` Временная коллекция аргументов в формате JSON. ## peer chaincode install Установка чейнкода на одноранговый узел. Команда устанавливает пакет чейнкода (если он предоставлен) или упаковывает чейнкод перед установкой. ``` Install a chaincode on a peer. This installs a chaincode deployment spec package (if provided) or packages the specified chaincode before subsequently installing it. Usage: peer chaincode install [flags] Flags: --connectionProfile string Connection profile that provides the necessary connection information for the network. Note: currently only supported for providing peer connection information -c, --ctor string Constructor message for the chaincode in JSON format (default "{}") -h, --help help for install -l, --lang string Language the chaincode is written in (default "golang") -n, --name string Name of the chaincode -p, --path string Path to chaincode --peerAddresses stringArray The addresses of the peers to connect to --tlsRootCertFiles stringArray If TLS is enabled, the paths to the TLS root cert files of the peers to connect to. The order and number of certs specified should match the --peerAddresses flag -v, --version string Version of the chaincode specified in install/instantiate/upgrade commands Global Flags: --cafile string Path to file containing PEM-encoded trusted certificate(s) for the ordering endpoint --certfile string Path to file containing PEM-encoded X509 public key to use for mutual TLS communication with the orderer endpoint --clientauth Use mutual TLS when communicating with the orderer endpoint --connTimeout duration Timeout for client to connect (default 3s) --keyfile string Path to file containing PEM-encoded private key to use for mutual TLS communication with the orderer endpoint -o, --orderer string Ordering service endpoint --ordererTLSHostnameOverride string The hostname override to use when validating the TLS connection to the orderer. --tls Use TLS when communicating with the orderer endpoint --transient string Transient map of arguments in JSON encoding ``` ## peer chaincode instantiate Развертывание чейнкода в сети. ``` Deploy the specified chaincode to the network. Usage: peer chaincode instantiate [flags] Flags: -C, --channelID string The channel on which this command should be executed --collections-config string The fully qualified path to the collection JSON file including the file name --connectionProfile string Connection profile that provides the necessary connection information for the network. Note: currently only supported for providing peer connection information -c, --ctor string Constructor message for the chaincode in JSON format (default "{}") -E, --escc string The name of the endorsement system chaincode to be used for this chaincode -h, --help help for instantiate -l, --lang string Language the chaincode is written in (default "golang") -n, --name string Name of the chaincode --peerAddresses stringArray The addresses of the peers to connect to -P, --policy string The endorsement policy associated to this chaincode --tlsRootCertFiles stringArray If TLS is enabled, the paths to the TLS root cert files of the peers to connect to. The order and number of certs specified should match the --peerAddresses flag -v, --version string Version of the chaincode specified in install/instantiate/upgrade commands -V, --vscc string The name of the verification system chaincode to be used for this chaincode Global Flags: --cafile string Path to file containing PEM-encoded trusted certificate(s) for the ordering endpoint --certfile string Path to file containing PEM-encoded X509 public key to use for mutual TLS communication with the orderer endpoint --clientauth Use mutual TLS when communicating with the orderer endpoint --connTimeout duration Timeout for client to connect (default 3s) --keyfile string Path to file containing PEM-encoded private key to use for mutual TLS communication with the orderer endpoint -o, --orderer string Ordering service endpoint --ordererTLSHostnameOverride string The hostname override to use when validating the TLS connection to the orderer. --tls Use TLS when communicating with the orderer endpoint --transient string Transient map of arguments in JSON encoding ``` ## peer chaincode invoke Вызов указанного чейнкода. После вызова чейнкода команда отправит одобренную транзакцию в сеть. ``` Invoke the specified chaincode. It will try to commit the endorsed transaction to the network. Usage: peer chaincode invoke [flags] Flags: -C, --channelID string The channel on which this command should be executed --connectionProfile string Connection profile that provides the necessary connection information for the network. Note: currently only supported for providing peer connection information -c, --ctor string Constructor message for the chaincode in JSON format (default "{}") -h, --help help for invoke -I, --isInit Is this invocation for init (useful for supporting legacy chaincodes in the new lifecycle) -n, --name string Name of the chaincode --peerAddresses stringArray The addresses of the peers to connect to --tlsRootCertFiles stringArray If TLS is enabled, the paths to the TLS root cert files of the peers to connect to. The order and number of certs specified should match the --peerAddresses flag --waitForEvent Whether to wait for the event from each peer's deliver filtered service signifying that the 'invoke' transaction has been committed successfully --waitForEventTimeout duration Time to wait for the event from each peer's deliver filtered service signifying that the 'invoke' transaction has been committed successfully (default 30s) Global Flags: --cafile string Path to file containing PEM-encoded trusted certificate(s) for the ordering endpoint --certfile string Path to file containing PEM-encoded X509 public key to use for mutual TLS communication with the orderer endpoint --clientauth Use mutual TLS when communicating with the orderer endpoint --connTimeout duration Timeout for client to connect (default 3s) --keyfile string Path to file containing PEM-encoded private key to use for mutual TLS communication with the orderer endpoint -o, --orderer string Ordering service endpoint --ordererTLSHostnameOverride string The hostname override to use when validating the TLS connection to the orderer. --tls Use TLS when communicating with the orderer endpoint --transient string Transient map of arguments in JSON encoding ``` ## peer chaincode list Получение списка развернутых в канале чейнкодов или установленных на одноганговом узле чейнкодов. ``` Get the instantiated chaincodes in the channel if specify channel, or get installed chaincodes on the peer Usage: peer chaincode list [flags] Flags: -C, --channelID string The channel on which this command should be executed --connectionProfile string Connection profile that provides the necessary connection information for the network. Note: currently only supported for providing peer connection information -h, --help help for list --installed Get the installed chaincodes on a peer --instantiated Get the instantiated chaincodes on a channel --peerAddresses stringArray The addresses of the peers to connect to --tlsRootCertFiles stringArray If TLS is enabled, the paths to the TLS root cert files of the peers to connect to. The order and number of certs specified should match the --peerAddresses flag Global Flags: --cafile string Path to file containing PEM-encoded trusted certificate(s) for the ordering endpoint --certfile string Path to file containing PEM-encoded X509 public key to use for mutual TLS communication with the orderer endpoint --clientauth Use mutual TLS when communicating with the orderer endpoint --connTimeout duration Timeout for client to connect (default 3s) --keyfile string Path to file containing PEM-encoded private key to use for mutual TLS communication with the orderer endpoint -o, --orderer string Ordering service endpoint --ordererTLSHostnameOverride string The hostname override to use when validating the TLS connection to the orderer. --tls Use TLS when communicating with the orderer endpoint --transient string Transient map of arguments in JSON encoding ``` ## peer chaincode package Упаковка чейнкода и сохранение пакета в файл. ``` Package a chaincode and write the package to a file. Usage: peer chaincode package [outputfile] [flags] Flags: -s, --cc-package create CC deployment spec for owner endorsements instead of raw CC deployment spec -c, --ctor string Constructor message for the chaincode in JSON format (default "{}") -h, --help help for package -i, --instantiate-policy string instantiation policy for the chaincode -l, --lang string Language the chaincode is written in (default "golang") -n, --name string Name of the chaincode -p, --path string Path to chaincode -S, --sign if creating CC deployment spec package for owner endorsements, also sign it with local MSP -v, --version string Version of the chaincode specified in install/instantiate/upgrade commands Global Flags: --cafile string Path to file containing PEM-encoded trusted certificate(s) for the ordering endpoint --certfile string Path to file containing PEM-encoded X509 public key to use for mutual TLS communication with the orderer endpoint --clientauth Use mutual TLS when communicating with the orderer endpoint --connTimeout duration Timeout for client to connect (default 3s) --keyfile string Path to file containing PEM-encoded private key to use for mutual TLS communication with the orderer endpoint -o, --orderer string Ordering service endpoint --ordererTLSHostnameOverride string The hostname override to use when validating the TLS connection to the orderer. --tls Use TLS when communicating with the orderer endpoint --transient string Transient map of arguments in JSON encoding ``` ## peer chaincode query Получение одобренного результата выполнения функции чейнкода. Команда не создает транзакцию. ``` Get endorsed result of chaincode function call and print it. It won't generate transaction. Usage: peer chaincode query [flags] Flags: -C, --channelID string The channel on which this command should be executed --connectionProfile string Connection profile that provides the necessary connection information for the network. Note: currently only supported for providing peer connection information -c, --ctor string Constructor message for the chaincode in JSON format (default "{}") -h, --help help for query -x, --hex If true, output the query value byte array in hexadecimal. Incompatible with --raw -n, --name string Name of the chaincode --peerAddresses stringArray The addresses of the peers to connect to -r, --raw If true, output the query value as raw bytes, otherwise format as a printable string --tlsRootCertFiles stringArray If TLS is enabled, the paths to the TLS root cert files of the peers to connect to. The order and number of certs specified should match the --peerAddresses flag Global Flags: --cafile string Path to file containing PEM-encoded trusted certificate(s) for the ordering endpoint --certfile string Path to file containing PEM-encoded X509 public key to use for mutual TLS communication with the orderer endpoint --clientauth Use mutual TLS when communicating with the orderer endpoint --connTimeout duration Timeout for client to connect (default 3s) --keyfile string Path to file containing PEM-encoded private key to use for mutual TLS communication with the orderer endpoint -o, --orderer string Ordering service endpoint --ordererTLSHostnameOverride string The hostname override to use when validating the TLS connection to the orderer. --tls Use TLS when communicating with the orderer endpoint --transient string Transient map of arguments in JSON encoding ``` ## peer chaincode signpackage Подписывание пакета чейнкода. ``` Sign the specified chaincode package Usage: peer chaincode signpackage [flags] Flags: -h, --help help for signpackage Global Flags: --cafile string Path to file containing PEM-encoded trusted certificate(s) for the ordering endpoint --certfile string Path to file containing PEM-encoded X509 public key to use for mutual TLS communication with the orderer endpoint --clientauth Use mutual TLS when communicating with the orderer endpoint --connTimeout duration Timeout for client to connect (default 3s) --keyfile string Path to file containing PEM-encoded private key to use for mutual TLS communication with the orderer endpoint -o, --orderer string Ordering service endpoint --ordererTLSHostnameOverride string The hostname override to use when validating the TLS connection to the orderer. --tls Use TLS when communicating with the orderer endpoint --transient string Transient map of arguments in JSON encoding ``` ## peer chaincode upgrade Обновление существующего чейнкода. Новая версия чейнкода заменяет существующую при проведении транзакции. ``` Upgrade an existing chaincode with the specified one. The new chaincode will immediately replace the existing chaincode upon the transaction committed. Usage: peer chaincode upgrade [flags] Flags: -C, --channelID string The channel on which this command should be executed --collections-config string The fully qualified path to the collection JSON file including the file name --connectionProfile string Connection profile that provides the necessary connection information for the network. Note: currently only supported for providing peer connection information -c, --ctor string Constructor message for the chaincode in JSON format (default "{}") -E, --escc string The name of the endorsement system chaincode to be used for this chaincode -h, --help help for upgrade -l, --lang string Language the chaincode is written in (default "golang") -n, --name string Name of the chaincode -p, --path string Path to chaincode --peerAddresses stringArray The addresses of the peers to connect to -P, --policy string The endorsement policy associated to this chaincode --tlsRootCertFiles stringArray If TLS is enabled, the paths to the TLS root cert files of the peers to connect to. The order and number of certs specified should match the --peerAddresses flag -v, --version string Version of the chaincode specified in install/instantiate/upgrade commands -V, --vscc string The name of the verification system chaincode to be used for this chaincode Global Flags: --cafile string Path to file containing PEM-encoded trusted certificate(s) for the ordering endpoint --certfile string Path to file containing PEM-encoded X509 public key to use for mutual TLS communication with the orderer endpoint --clientauth Use mutual TLS when communicating with the orderer endpoint --connTimeout duration Timeout for client to connect (default 3s) --keyfile string Path to file containing PEM-encoded private key to use for mutual TLS communication with the orderer endpoint -o, --orderer string Ordering service endpoint --ordererTLSHostnameOverride string The hostname override to use when validating the TLS connection to the orderer. --tls Use TLS when communicating with the orderer endpoint --transient string Transient map of arguments in JSON encoding ``` ## Примеры использования ### Примеры использования команды peer chaincode instantiate Ниже приведены примеры использования команды `peer chaincode instantiate`, которая создает чейнкод с названием `mycc` версии `1.0` в канале `mychannel`: * Использование глобальных флагов `--tls` и `--cafile` для создания экземпляра чейнкода в сети с включенным TLS: ``` export ORDERER_CA=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem peer chaincode instantiate -o orderer.example.com:7050 --tls --cafile $ORDERER_CA -C mychannel -n mycc -v 1.0 -c '{"Args":["init","a","100","b","200"]}' -P "AND ('Org1MSP.peer','Org2MSP.peer')" 2018-02-22 16:33:53.324 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc 2018-02-22 16:33:53.324 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc 2018-02-22 16:34:08.698 UTC [main] main -> INFO 003 Exiting..... ``` * Использование только соответствующих команде опций для создания чейнкода в сети с отключенным TLS: ``` peer chaincode instantiate -o orderer.example.com:7050 -C mychannel -n mycc -v 1.0 -c '{"Args":["init","a","100","b","200"]}' -P "AND ('Org1MSP.peer','Org2MSP.peer')" 2018-02-22 16:34:09.324 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc 2018-02-22 16:34:09.324 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc 2018-02-22 16:34:24.698 UTC [main] main -> INFO 003 Exiting..... ``` ### Пример использования команды peer chaincode invoke Ниже приведен пример команды `peer chaincode invoke`: * Вызов чейнкода с именем `mycc` версии `1.0` в канале `mychannel` на одноранговых узлах `peer0.org1.example.com:7051` и `peer0.org2.example.com:9051` (узлы указаны флагом `--peerAddresses`) с запросом переместить 10 единиц из переменной `a` в переменную `b`: ``` peer chaincode invoke -o orderer.example.com:7050 -C mychannel -n mycc --peerAddresses peer0.org1.example.com:7051 --peerAddresses peer0.org2.example.com:9051 -c '{"Args":["invoke","a","b","10"]}' 2018-02-22 16:34:27.069 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc 2018-02-22 16:34:27.069 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc . . . 2018-02-22 16:34:27.106 UTC [chaincodeCmd] chaincodeInvokeOrQuery -> DEBU 00a ESCC invoke result: version:1 response: payload:"\n \237mM\376? [\214\002 \332\204\035\275q\227\2132A\n\204&\2106\037W|\346#\3413\274\022Y\nE\022\024\n\004lscc\022\014\n\n\n\004mycc\022\002\010\003\022-\n\004mycc\022%\n\007\n\001a\022\002\010\003\n\007\n\001b\022\002\010\003\032\007\n\001a\032\00290\032\010\n\001b\032\003210\032\003\010\310\001\"\013\022\004mycc\032\0031.0" endorsement: 2018-02-22 16:34:27.107 UTC [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 00b Chaincode invoke successful. result: status:200 2018-02-22 16:34:27.107 UTC [main] main -> INFO 00c Exiting..... ``` В этом сообщении из журнала можно увидеть, что вызов был успешен: ``` 2018-02-22 16:34:27.107 UTC [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 00b Chaincode invoke successful. result: status:200 ``` Такой ответ указывает на то, что транзакция была успешно отправлена в службу упорядочения. Транзакция будет добавлена в блок и, наконец, утверждена или не утверждена одноранговыми узлами в канале. ### Пример использования команды peer chaincode list Ниже приведены примеры использования команды `peer chaincode list`: * Использование флага `--installed` для перечисления всех чейнкодов, установленных в канале. ``` peer chaincode list --installed Get installed chaincodes on peer: Name: mycc, Version: 1.0, Path: github.com/hyperledger/fabric-samples/chaincode/abstore/go, Id: 8cc2730fdafd0b28ef734eac12b29df5fc98ad98bdb1b7e0ef96265c3d893d61 2018-02-22 17:07:13.476 UTC [main] main -> INFO 001 Exiting..... ``` Можно видеть, что на одноранговом узле установлен чейнкод с именем `mycc` и версией `1.0`. * Использование флага `--instantiated` вместе с `-C` (идентификатор канала) для вывода экземпляров чейнкода, запущенных в канале. ``` peer chaincode list --instantiated -C mychannel Возвращает экземпляры чейнкода, запущенных в канале mychannel: Name: mycc, Version: 1.0, Path: github.com/hyperledger/fabric-samples/chaincode/abstore/go, Escc: escc, Vscc: vscc 2018-02-22 17:07:42.969 UTC [main] main -> INFO 001 Exiting..... ``` Можно видеть, что чейнкод с названием `mycc` и версией `1.0` запущен в канале `mychannel`. ### Пример использования команды peer chaincode package Ниже приводится пример использование команды `peer chaincode package`, которая упаковывает чейнкод с именем `mycc` и версией `1.1`, создает данные развертывания чейнкода, подписывает пакет с помощью локального провайдера службы членства и выводит результат в файл `ccpack.out`: ``` peer chaincode package ccpack.out -n mycc -p github.com/hyperledger/fabric-samples/chaincode/abstore/go -v 1.1 -s -S . . . 2018-02-22 17:27:01.404 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 003 Using default escc 2018-02-22 17:27:01.405 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 004 Using default vscc . . . 2018-02-22 17:27:01.879 UTC [chaincodeCmd] chaincodePackage -> DEBU 011 Packaged chaincode into deployment spec of size <3426>, with args = [ccpack.out] 2018-02-22 17:27:01.879 UTC [main] main -> INFO 012 Exiting..... ``` ### Пример использования команды peer chaincode query Ниже приведен пример использования команды `peer chaincode query`, которая обращается к копии реестра на одноранговом узле, для получения переменной `a` чейнкода `mycc` версии `1.0`: * Из журнала видно, что на момент вызова переменная `a` имеет значение `90`. ``` peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}' 2018-02-22 16:34:30.816 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc 2018-02-22 16:34:30.816 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc Query Result: 90 ``` ### Пример использования команды peer chaincode signpackage Ниже приведен пример использования команды `peer chaincode signpackage`, которая принимает существующий подписанный пакет и создает новый с добавленной подписью локального провайдера службы членства. ``` peer chaincode signpackage ccwith1sig.pak ccwith2sig.pak Wrote signed package to ccwith2sig.pak successfully 2018-02-24 19:32:47.189 EST [main] main -> INFO 002 Exiting..... ``` ### Пример использования команды peer chaincode upgrade Ниже приведен пример использования команды `peer chaincode upgrade`, которая обновляет чейнкод с именем `mycc` версии `1.1` в канале `mychannel` до версии `1.2`, в которой добавляется новая переменная `c`: * Использование глобальных флагов `--tls` и `--cafile` для обновления чейнкода в сети с включенным TLS: ``` export ORDERER_CA=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem peer chaincode upgrade -o orderer.example.com:7050 --tls --cafile $ORDERER_CA -C mychannel -n mycc -v 1.2 -c '{"Args":["init","a","100","b","200","c","300"]}' -P "AND ('Org1MSP.peer','Org2MSP.peer')" . . . 2018-02-22 18:26:31.433 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 003 Using default escc 2018-02-22 18:26:31.434 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 004 Using default vscc 2018-02-22 18:26:31.435 UTC [chaincodeCmd] getChaincodeSpec -> DEBU 005 java chaincode enabled 2018-02-22 18:26:31.435 UTC [chaincodeCmd] upgrade -> DEBU 006 Get upgrade proposal for chaincode . . . 2018-02-22 18:26:46.687 UTC [chaincodeCmd] upgrade -> DEBU 009 endorse upgrade proposal, get response . . . 2018-02-22 18:26:46.693 UTC [chaincodeCmd] upgrade -> DEBU 00c Get Signed envelope 2018-02-22 18:26:46.693 UTC [chaincodeCmd] chaincodeUpgrade -> DEBU 00d Send signed envelope to orderer 2018-02-22 18:26:46.908 UTC [main] main -> INFO 00e Exiting..... ``` * Использование соответствующих команде опции используются для создания чейнкода в сети с отключенным TLS: ``` peer chaincode upgrade -o orderer.example.com:7050 -C mychannel -n mycc -v 1.2 -c '{"Args":["init","a","100","b","200","c","300"]}' -P "AND ('Org1MSP.peer','Org2MSP.peer')" . . . 2018-02-22 18:28:31.433 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 003 Using default escc 2018-02-22 18:28:31.434 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 004 Using default vscc 2018-02-22 18:28:31.435 UTC [chaincodeCmd] getChaincodeSpec -> DEBU 005 java chaincode enabled 2018-02-22 18:28:31.435 UTC [chaincodeCmd] upgrade -> DEBU 006 Get upgrade proposal for chaincode . . . 2018-02-22 18:28:46.687 UTC [chaincodeCmd] upgrade -> DEBU 009 endorse upgrade proposal, get response . . . 2018-02-22 18:28:46.693 UTC [chaincodeCmd] upgrade -> DEBU 00c Get Signed envelope 2018-02-22 18:28:46.693 UTC [chaincodeCmd] chaincodeUpgrade -> DEBU 00d Send signed envelope to orderer 2018-02-22 18:28:46.908 UTC [main] main -> INFO 00e Exiting..... ```