Support invoke command
Right now we have a buch of test scripts to verify if invoking chaincodes work (e2e-network/expect-invoke-cli.sh and similar). For the simplest Docker setup it is handled by executing some operations on the CLI container:
peerAddresses="--peerAddresses $(echo "$peer" | sed 's/,/ --peerAddresses /g')"
response="$(
# shellcheck disable=SC2086
docker exec "$cli" peer chaincode invoke \
$peerAddresses \
-C "$channel" \
-n "$chaincode" \
-c "$command" \
--transient "$transient" \
--waitForEvent \
--waitForEventTimeout 90s \
2>&1
)"
We want invoke command to be supported by Fablo, with the syntax similar to our channel query scripts. Within this issue we want to support invoke command for the simplest use case: no TLS, Docker setup, and using CLI container. Relevant script is expect-invoke-cli.sh. After this issue, the script, instead of invoking docker, should call (some path)/fablo.sh chaincode invoke (some params).
Probably the best way to implement this is to follow the similar approach as for channel query scripts - using templates and strong validation.
Desired syntax:
fablo chaincode invoke <channel_name> <chaincode_name> <peers_dmains_comma_separated> <command> <transient>
Sample:
fablo chaincode invoke my-channel1 chaincode1 'peer0.org1.example.com,peer0.org2.example.com' '{"Args":["KVContract:put", "name", "James Bond"]}'
No need for additional tests. In order to verify the code works we just want to replace the content of expect-invoke-cli.sh script.
This issue, however includes upgrade of README.
Further steps:
- Support chaincode query
- Suport invoke with TLS
- Support invoke for kubernetes
The second step for this command after #403 is to ensure we have consistent API - the same as for chaincode list #406 and we use generators to make it work with custom network topologies.
Checklist for the second step:
- [ ]
chaincodeInvokeinchaincode-scripts.shshould use generators as in the listing below - [ ]
peerChaincodeInvokeshould be added tochaincode-functions-v2.sh - [ ]
chaincodeInvokeinchaincode-scripts.shshould usepeerChaincodeInvoketo invoke chaincodes - [ ] ~~
peerChaincodeInvokeshould be able to call multiple peers (it should include the part:PEER_ADDRESSES="--peerAddresses $(echo "$PEERS" | sed 's/,/ --peerAddresses /g')")~~ => to be resolved in separate issue - [ ]
peerChaincodeInvokeTlsshould be added tochaincode-functions-v2.sh - [ ]
expect-invoke-cli-tls.shfile should be removed.test-02-raft-2orgs.shshould useexpect-invoke-cli.shinstead
Suggested implementation for chaincodeInvoke() in chaincode-scripts.sh:
# Function to perform chaincode invoke. Accepts 5 parameters:
# 1. comma-separated peers
# 2. channel name
# 3. chaincode name
# 4. chaincode command
# 5. transient data (optional)
chaincodeInvoke() {
if [ "$#" -ne 4 ] && [ "$#" -ne 5 ]; then
echo "Expected 4 or 5 parameters for chaincode list, but got: $*"
echo "Usage: fablo chaincode invoke <peer_domains_comma_separated> <channel_name> <chaincode_name> <command> [transient]"
exit 1
fi
cli=""
peer_addresses=""
<% if (!global.tls) { %>
peer_certs=""
<% } %>
<% orgs.forEach((org) => { -%>
<% org.peers.forEach((peer) => { -%>
if [[ "$1" == *"<%= peer.address %>"* ]]; then
cli="<%= org.cli.address %>"
peer_addresses="$peer_addresses,<%= peer.fullAddress %>"
<% if(!global.tls) { %>
peer_certs="$peer_certs,crypto/peers/<%= peer.address %>/tls/ca.crt"
<% } %>
fi
<% }) -%>
<% }) -%>
if [ -z "$peer_addresses" ]; then
echo "Unknown peers: $1"
exit 1
fi
<% if(!global.tls) { %>
peerChaincodeInvoke "$cli" "${peer_addresses:1}" "$2" "$3" "$4" "$5"
<% } else { %>
peerChaincodeInvokeTls "$cli" "${peer_addresses:1}" "$2" "$3" "$4" "$5" "${peer_certs:1}"
<% } %>
}