Hyperledger Fabric: peer chaincode invoke: pass JSON object to chaincode function? - hyperledger-fabric

I am able to call the chaincode functionality by peer chaincode invoke as:
`peer chaincode invoke -o localhost:7050 -C mychannel -n pmc -c '{"function":"initLedger","Args":["one", "two"]}`
But I want to pass javascript object as argument like:
`peer chaincode invoke -o localhost:7050 -C mychannel -n pmc -c '{"function":"initLedger","Args":[ {property1:"one"}, {property2:"two"}]}`
but this fails with invalid character 'p' looking for beginning of object key string. I have tried few other permutations but all without success.
the documentation says about -c as:
-c, --ctor string Constructor message for the chaincode in JSON format (default "{}")
Can come one tell correct syntax to pass the object.
EDIT 1:
For the sake of completeness following
peer chaincode invoke -o localhost:7050 -C mychannel -n pmc -c '{"function":"initLedger","Args": [{"property1":"one", "property2":"two"} ]}'
returns Error: Chaincode argument error: json: cannot unmarshal array into Go struct field strArgs.Args of type string

I think you might be using an old format of the invoke command, so I'm suggesting a new format like this including your stringified JSON object:
peer chaincode invoke -o localhost:7050 -C mychannel -n pmc -c '{"Args":["initLedger","{\"property1\":\"one\",\"property2\":\"two\"}"]}'
But if you need to use your format then perhaps:
peer chaincode invoke -o localhost:7050 -C mychannel -n pmc -c '{"function":"initLedger", "Args": ["{\"property1\":\"one\",\"property2\":\"two\"}"]}'

Related

Why Invoke query does not performed valid transaction?

I have setup the byfn from here. What I am going through that is invoking by this command.
peer chaincode invoke -o orderer.example.com:7050 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n mycc -c '{"Args":["invoke","a","b","10"]}
It give me an error that iI found by docker logs {peer0_container_id}. I also set CORE_PEER_ADDRESSAUTODETECT=true
2020-01-06 08:08:39.972 UTC [vscc] Validate -> ERRO 0ad VSCC error: stateBasedValidator.Validate failed, err validation of endorsement policy for chaincode mycc in tx 9:0 failed: signature set did not satisfy policy
2020-01-06 08:08:39.973 UTC [committer.txvalidator] validateTx -> ERRO 0ae VSCCValidateTx for transaction txId = ea10c982be12ab3762b4199a349c1409fb22bde5230667be78b2394ccdb05d37 returned error: validation of endorsement policy for chaincode mycc in tx 9:0 failed: signature set did not satisfy policy
But for this command it working fine I mean transaction is valid.
peer chaincode invoke -o orderer.example.com:7050 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n mycc --peerAddresses peer0.org1.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses peer0.org2.example.com:9051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c '{"Args":["invoke","a","b","10"]}'
Is there anything to configure for invoking with the first one I mean except passing the --peerAddresses param?
I have also added the third organization from here. And here the invoke command also work i mean valid transaction.
peer chaincode invoke -o orderer.example.com:7050 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n mycc -c '{"Args":["invoke","a","b","10"]}'
So, why does not the same invoke is not working for the byfn?
Note for byfn I have removed the all images and done it from the beginning. So here is no conflict actually.

Hyperledger Fabric - Peer unable to join channel

We are getting an error while trying a peer node to join a channel. Both members have separate peer nodes and separate EC2 machines acting as clients for the network. Member1 has created a channel and joined its peer node to the channel. Member2 has pulled the genesis block from their client but when trying to join the channel, we get the error:
Error: genesis block file not found open ourchannel.block: no such file or directory
despite seeing ourchannel.block in the directory we run the command. The command we're using to join the channel is:
docker exec -e "CORE_PEER_TLS_ENABLED=true" \
-e "CORE_PEER_TLS_ROOTCERT_FILE=/opt/home/managedblockchain-tls-chain.pem" \
-e "CORE_PEER_ADDRESS=$PEER" \
-e "CORE_PEER_LOCALMSPID=$MSP" \
-e "CORE_PEER_MSPCONFIGPATH=$MSP_PATH" \
cli peer channel join -b ourchannel.block \
-o $ORDERER --cafile /opt/home/managedblockchain-tls-chain.pem --tls
You only have the genesis block (the file with .block extension) if you create a channel in a separate container, which is cli container in your case. Please check by using ls command inside the cli container.
A simple command to jump into cli container:
docker exec -it cli bash
Just in case you can't find genesis block, you can get it from the orderer (remember to modify the path to the cafile fit your project):
peer channel fetch 0 ourchannel.block -o orderer.example.com:7050 -c ourchannel --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
After this, you can join the channel with peer channel join command.
Simple issue, we had to specify the absolute path to the block file.
Instead of
cli peer channel join -b ourchannel.block
we used
cli peer channel join -b /opt/home/ourchannel.block \
and it worked.

Hyperledger fabric: unable to Invoke chaincode in the chainecode_example02 for?

I have my own network on and try to use the chaincode_example02 chaincode to test it out, following the first-network example. When it comes to chaincodeInvoke in my shellscipt, the console shows
Sending invoke transaction on peer0.bank peer0.caseManager...
--tlsRootCertFiles
--peerAddresses peer0.bank.snts.com:7051 --tlsRootCertFiles
--tlsRootCertFiles
--peerAddresses peer0.bank.snts.com:7051 --tlsRootCertFiles --peerAddresses peer0.caseManager.snts.com:7051 --tlsRootCertFiles
+ peer chaincode invoke -o orderer.snts.com:7050 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/snts.com/orderers/orderer.snts.com/msp/tlscacerts/tlsca.snts.com-cert.pem -C sntschannel -n mycc --peerAddresses peer0.bank.snts.com:7051 --tlsRootCertFiles --peerAddresses peer0.caseManager.snts.com:7051 --tlsRootCertFiles -c '{"Args":["invoke","a","b","10"]}'
+ res=1
+ set +x
2019-01-01 16:38:40.670 UTC [chaincodeCmd] validatePeerConnectionParameters -> WARN 001 received more TLS root cert files (2) than peer addresses (1)
Error: error validating peer connection parameters: number of peer addresses (1) does not match the number of TLS root cert files (2)
!!!!!!!!!!!!!!! Invoke execution on peer0.bank peer0.caseManager failed !!!!!!!!!!!!!!!!
========= ERROR !!! FAILED to execute End-2-End Scenario ===========
bank has 2 peers and caseManager has 3. What can lead to this problem? In first-network example, both orgs has 2 peers, and it works perfectly. Can someone help? Thank you.
In your request, you have used --tlsRootCertFiles flag but did not specify any path for it? are you setting this externally? if not try specifying it like
--tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
The problem is with the way TLSINFO is fetched.
Below command is used in utils.sh file where we are mentioning the TLSCert file. You need to modify it according to your requirement.
TLSINFO=$(eval echo "--tlsRootCertFiles \$PEER$1_Org$2_CA")
You need to modify the global variables so that they are generic.
It is necessary to change, in the setOrderererGlobals() method of the file utils.sh :
setGlobals() {
PEER=$1
ORG=$2
### ADD THESES LINES !!! ###
CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org$ORG.supplychainnet.ch/peers/peer$PEER.org$ORG.supplychainnet.ch/tls/ca.crt
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org$ORG.supplychainnet.ch/users/Admin\#org$ORG.supplychainnet.ch/msp
### ADD THESES LINES !!! ###
if [ $ORG -eq 1 ]; then
CORE_PEER_LOCALMSPID="Org1MSP"
### COMMENT THESES LINES !!! ###
#CORE_PEER_TLS_ROOTCERT_FILE=$PEER0_ORG1_CA
#CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.supplychainnet.ch/users/Admin\#org1.supplychainnet.ch/msp
### COMMENT THESES LINES !!! ###
if [ $PEER -eq 0 ]; then
CORE_PEER_ADDRESS=peer0.org1.supplychainnet.ch:7051
else
CORE_PEER_ADDRESS=peer1.org1.supplychainnet.ch:8051
fi
else
echo "================== ERROR !!! ORG Unknown =================="
fi
As a result, the environment paths for each peer will automatically be updated with the numbers $PEER and $ORG instead of retrieving the global PEERx_ORGy_CA paths defined at the top of the file.
pass ca.crt file path for respective peer in --tlsRootCertFiles
Try This and it works
docker exec -it cli bash
peer chaincode invoke -o orderer.example.com:7050 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C testchannel -n mycc --peerAddresses peer0.org1.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses peer0.org2.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c '{"Args":["invoke","a","b","20"]}'

Endorsement from multiple peers one arg

I have a multiple peer, one org Org1MSP network where I want the transactions to be endorsed by multiple peers.
Is there a way to specify endorsement policy using CLI for e.g. during instantiate chaincode
For e.g.
peer chaincode instantiate -o orderer.example.com:7050 -C mychannel -n
mycc -v 1.0 -c '{"Args":[]}' -P "OutOf(2, 'Org1MSP.peer')"
Just specify the same org twice with an AND:
AND('Org1MSP.peer', 'Org1MSP.peer')

Error in starting Hyperledger fabcar sample application

I am trying to run fab car sample application from http://hyperledger-fabric.readthedocs.io/en/release-1.1/write_first_app.html
when ./startFabric.sh node trying to create channel with below command
docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/msp/users/Admin#org1.example.com/msp" peer0.org1.example.com peer channel create -o orderer.example.com:7050 -c mychannel -f /etc/hyperledger/configtx/channel.tx
It gives error as
flag provided but not defined: -e
What am I doing wrong here?
The Fabric samples require Docker 17.06.2-ce or later

Resources