hyperledger-labs / fablo

Fablo is a simple tool to generate the Hyperledger Fabric blockchain network and run it on Docker. It supports RAFT and solo consensus protocols, multiple organizations and channels, chaincode installation and upgrade.
Apache License 2.0
188 stars 72 forks source link

Support invoke command #390

Open dzikowski opened 1 year ago

dzikowski commented 1 year ago

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:

  1. Support chaincode query
  2. Suport invoke with TLS
  3. Support invoke for kubernetes
dzikowski commented 10 months ago

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:

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}"
  <% } %>
}