Skip to content

Commit

Permalink
Merge branch 'develop' into feature/permissioning
Browse files Browse the repository at this point in the history
Signed-off-by: André Macedo <[email protected]>
  • Loading branch information
andremacedopv authored Jan 12, 2024
2 parents fc350de + 9661747 commit 5ceb371
Show file tree
Hide file tree
Showing 15 changed files with 206 additions and 94 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ fabric/ca
fabric/bin
fabric/builders
chaincode/vendor/*
chaincode/collections.json
ccapi/vendor/*
cc-tools-demo.tar.gz
package-lock.json
Expand Down
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ start all components of the project with 3 organizations.

If you want to deploy with 1 organization, run the command `./startDev.sh -n 1`.

To apply chaincode changes, run `$ ./upgradeCC2.sh <version> <sequence>` with a version higher than the current one (starts with 0.1). Append `-n 1` to the command if running with 1 organization.
To apply chaincode changes, run `$ ./upgradeCC.sh <version> <sequence>` with a version higher than the current one (starts with 0.1). Append `-n 1` to the command if running with 1 organization.

To apply CC API changes, run `$ ./reloadCCAPI.sh`.

Expand All @@ -44,10 +44,23 @@ To test transactions after starting all components, run `$ ./tryout.sh`.

To test transactions using the godog tool, run `$ ./godog.sh`.


## Generate TAR archive for the chaincode

The `generateTar.sh` script is available to generate a `tar.gz` archive of the chaincode.

By running `$ ./generateTar.sh` without any option, the script generates a `collections.json` file for the private data on the chaincode with all the organizations defined on the readers section of private asset types, and then archives the code without the CCAPI.

By using the `--org/-o` option along the script, it's possible to specify the organizations to be considered when generating the `collections.json` file. This option may be used multiple times to add all the organizations, ex: `$ ./generateTar.sh -o org1MSP -o org2MSP`.

To also archive the the CCAPI alongside the chaincode, the `--ccapi/-c` flag may be used. Example: `$ ./generateTar.sh -c`.

By standard the archive is created using the project name with *1.0* label, to change it the `--name/-n` and `--label/-l` flags may be used. Example: `$ ./generateTar.sh -n my-project -l 2.0`

## More

You can reach GoLedger developers and `cc-tools` maintainers at our Discord - [Join us!](https://discord.gg/GndkYHxNyQ)

More documentation and details on `cc-tools` can be found at [https://goledger-cc-tools.readthedocs.io/en/latest/](https://goledger-cc-tools.readthedocs.io/en/latest/)

For production deployment please consider using GoFabric - [https://gofabric.io](https://gofabric.io)
For production deployment please consider using GoFabric - [https://gofabric.io](https://gofabric.io)
26 changes: 6 additions & 20 deletions ccapi/chaincode/eventHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"os"

"github.com/hyperledger/fabric-sdk-go/pkg/client/channel"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
)

Expand Down Expand Up @@ -81,30 +80,17 @@ func (event EventHandler) Execute(ccEvent *fab.CCEvent) {
return
}

// Invoke executeEvent tx
var res *channel.Response
var err error
// Invoke tx
txName := "executeEvent"
if event.ReadOnly {
res, _, err = Invoke(os.Getenv("CHANNEL"), os.Getenv("CCNAME"), "runEvent", os.Getenv("USER"), [][]byte{args}, nil)
if err != nil {
fmt.Println("error invoking transaction: ", err)
return
}
} else {
res, _, err = Invoke(os.Getenv("CHANNEL"), os.Getenv("CCNAME"), "executeEvent", os.Getenv("USER"), [][]byte{args}, nil)
if err != nil {
fmt.Println("error invoking transaction: ", err)
return
}
txName = "runEvent"
}

var response map[string]interface{}
nerr := json.Unmarshal(res.Payload, &response)
if nerr != nil {
fmt.Println("error unmarshalling response: ", nerr)
_, _, err := Invoke(os.Getenv("CHANNEL"), os.Getenv("CCNAME"), txName, [][]byte{args}, nil)
if err != nil {
fmt.Println("error invoking transaction: ", err)
return
}
fmt.Println("Response: ", response)
} else {
fmt.Println("Event type not supported")
}
Expand Down
35 changes: 0 additions & 35 deletions chaincode/collections.json

This file was deleted.

10 changes: 0 additions & 10 deletions chaincode/collections2-org.json

This file was deleted.

10 changes: 0 additions & 10 deletions chaincode/collections2.json

This file was deleted.

82 changes: 82 additions & 0 deletions chaincode/generateCollections.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package main

import (
"encoding/json"
"fmt"
"io/ioutil"
)

type ArrayFlags []string

func (i *ArrayFlags) String() string {
return "my string representation"
}

func (i *ArrayFlags) Set(value string) error {
*i = append(*i, value)
return nil
}

type CollectionElem struct {
Name string `json:"name"`
RequiredPeerCount int `json:"requiredPeerCount"`
MaxPeerCount int `json:"maxPeerCount"`
BlockToLive int `json:"blockToLive"`
MemberOnlyRead bool `json:"memberOnlyRead"`
Policy string `json:"policy"`
}

func generateCollection(orgs ArrayFlags) {
collection := []CollectionElem{}

for _, a := range assetTypeList {
if len(a.Readers) > 0 {
elem := CollectionElem{
Name: a.Tag,
RequiredPeerCount: 0,
MaxPeerCount: 3,
BlockToLive: 1000000,
MemberOnlyRead: true,
Policy: generatePolicy(a.Readers, orgs),
}
collection = append(collection, elem)
}
}

b, err := json.MarshalIndent(collection, "", " ")
if err != nil {
fmt.Println(err)
return
}
err = ioutil.WriteFile("collections.json", b, 0644)
if err != nil {
fmt.Println(err)
return
}
}

func generatePolicy(readers []string, orgs ArrayFlags) string {
firstElem := true
policy := "OR("
for _, r := range readers {
if len(orgs) > 0 {
found := false
for _, o := range orgs {
if r == o {
found = true
break
}
}
if !found {
continue
}
}
if !firstElem {
policy += ", "
}
policy += fmt.Sprintf("'%s.member'", r)
firstElem = false
}
policy += ")"
return policy
}
11 changes: 11 additions & 0 deletions chaincode/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"flag"
"fmt"
"log"
"time"
Expand Down Expand Up @@ -45,6 +46,16 @@ func SetupCC() error {

// main function starts up the chaincode in the container during instantiate
func main() {
// Generate collection json
genFlag := flag.Bool("g", false, "Enable collection generation")
flag.Bool("orgs", false, "List of orgs to generate collection for")
flag.Parse()
if *genFlag {
listOrgs := flag.Args()
generateCollection(listOrgs)
return
}

log.Printf("Starting chaincode %s version %s\n", header.Name, header.Version)

err := SetupCC()
Expand Down
2 changes: 1 addition & 1 deletion fabric/network.sh
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ CC_SRC_PATH="NA"
# endorsement policy defaults to "NA". This would allow chaincodes to use the majority default policy.
CC_END_POLICY="NA"
# collection configuration defaults to "NA"
CC_COLL_CONFIG="../chaincode/collections2.json"
CC_COLL_CONFIG="../chaincode/collections.json"
# chaincode init function defaults to "NA"
CC_INIT_FCN="NA"
# use this as the default docker-compose yaml definition
Expand Down
7 changes: 1 addition & 6 deletions fabric/startDev.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,7 @@ then
ORG_QNTY=3
fi

if [ $ORG_QNTY == 1 ]
then
CCCG_PATH="../chaincode/collections2-org.json"
else
CCCG_PATH="../chaincode/collections2.json"
fi
CCCG_PATH="../chaincode/collections.json"

./network.sh down -n $ORG_QNTY
rm -rf organizations/peerOrganizations
Expand Down
25 changes: 25 additions & 0 deletions generateCollection.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/bash

while getopts "o:h" opt; do
case $opt in
o) orgs+=("$OPTARG");;
#...
h)
echo "Usage: ./generateCollection.sh [-o <org name>]"
echo " -o: Include an organization in the collection configuration file"
echo " This option can be used multiple times to include multiple organizations"
echo " If no organizations are specified, the default is to include any organization found in the readers"
echo ""
echo "Example: ./generateCollection.sh -o org1MSP -o org2MSP -o org3MSP"
exit 0
;;
esac
done
shift $((OPTIND -1))

if [ ${#orgs[@]} -gt 0 ]
then
cd ./chaincode; go run . -g --orgs ${orgs[@]}; cd ..
else
cd ./chaincode; go run . -g; cd ..
fi
31 changes: 30 additions & 1 deletion generateTar.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Default values for the flags
FLAG_CCAPI="none"
FLAG_LABEL="1.0"
SKIP_COLL_GEN=false

# You can change this if you want to avoid using the --name flag
FLAG_NAME="cc-tools-demo"
Expand Down Expand Up @@ -32,12 +33,29 @@ while [[ $# -gt 0 ]]; do
exit 1
fi
;;
--org | -o)
if [[ $# -gt 1 ]]; then
orgs+=("$2")
shift 2
else
echo "Error: --org flag requires a value."
exit 1
fi
;;
--skip | -s)
SKIP_COLL_GEN=true
shift 1
;;
--help | -h)
echo "Usage: ./generateTar.sh [--ccapi] [--label <label>] [--name <name>]"
echo " --ccapi, -c: Include rest-server in the tar file. Default is no ccapi."
echo " --help , -h: Show this help message."
echo " --label, -l: Label to be used for the chaincode package. Default is 1.0."
echo " --name , -n: Name of the chaincode package. Default is ${FLAG_NAME}."
echo " --org , -o: Include an organization in the collection configuration file"
echo " This option can be used multiple times to include multiple organizations"
echo " If no organizations are specified, the default is to include any organization found in the readers"
echo " --skip , -s: Skip the generation of the collections.json file"
exit 0
;;
*)
Expand All @@ -47,6 +65,17 @@ while [[ $# -gt 0 ]]; do
esac
done

# Generate collection configuration file
if [ "$SKIP_COLL_GEN" = false ]
then
if [ ${#orgs[@]} -gt 0 ]
then
cd ./chaincode; go run . -g --orgs ${orgs[@]}; cd ..
else
cd ./chaincode; go run . -g; cd ..
fi
fi

# Remove previous tar file
rm -f ${FLAG_NAME}.tar.gz

Expand All @@ -61,7 +90,7 @@ case $FLAG_CCAPI in
go)
# Create temporary directory for rest-server
mkdir -p tmp/rest-server
cp -r ccapi/* tmp/rest-server
cp -r ccapi/. tmp/rest-server

# Compress file with rest-server (GoFabric will use the one provided)
tar -c --exclude=vendor -zf ${FLAG_NAME}.tar.gz chaincode.tar.gz -C tmp rest-server
Expand Down
1 change: 1 addition & 0 deletions go.work
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ go 1.19
use (
./ccapi
./chaincode
./test
)
Loading

0 comments on commit 5ceb371

Please sign in to comment.