From 4d589074baba2f691940c00acd070e5bc577be54 Mon Sep 17 00:00:00 2001 From: Fernando Garzon Date: Wed, 22 Feb 2023 16:46:40 -0800 Subject: [PATCH] Update Feb 22 2023 - Testing PDC functionalities --- .../chaincode-go/chaincode/asset_transfer.go | 74 ++++++++++++++++++- test-network/QuickCCTest.sh | 38 +++++++++- 2 files changed, 106 insertions(+), 6 deletions(-) diff --git a/asset-transfer-private-data/chaincode-go/chaincode/asset_transfer.go b/asset-transfer-private-data/chaincode-go/chaincode/asset_transfer.go index 370f0ea9..96b12362 100644 --- a/asset-transfer-private-data/chaincode-go/chaincode/asset_transfer.go +++ b/asset-transfer-private-data/chaincode-go/chaincode/asset_transfer.go @@ -50,9 +50,9 @@ type PrivateSchemaContent struct { } type Schema struct { - Version int `json:"Version"` - Hash string `json:"Hash"` - JsonSchemaContent map[string]interface{} + JsonSchemaContent map[string]interface{} `json:"JsonSchemaContent"` + SchemaId string `json:"SchemaId"` + Project string `json:"Project` } type User struct { @@ -82,7 +82,7 @@ func main() { } // InitLedger adds a base set of Data entries to the ledger -func (s *SmartContract) InitLedger(ctx contractapi.TransactionContextInterface, InitSchema string, InitData string) error { +/*func (s *SmartContract) InitLedger(ctx contractapi.TransactionContextInterface, InitSchema string, InitData string) error { // We use the function jsonReader in order to read the content of the shcema Json File. The schema Json file is composed by us and inserted as a parameter in the invokation of the initialization function. schemaJsonFileContent, error_schema := s.JsonReader(ctx, InitSchema) @@ -146,6 +146,7 @@ func (s *SmartContract) InitLedger(ctx contractapi.TransactionContextInterface, } return nil } +*/ func (s *SmartContract) LastSchemaHash(ctx contractapi.TransactionContextInterface) string { return lastSchemaHash @@ -233,6 +234,7 @@ func (s *SmartContract) SchemaExists(ctx contractapi.TransactionContextInterface } } +/* func (s *SmartContract) CreateNewSchema(ctx contractapi.TransactionContextInterface, newSchemaContent string) error { jsonFileContent, err := s.JsonReader(ctx, newSchemaContent) @@ -279,6 +281,7 @@ func (s *SmartContract) CreateNewSchema(ctx contractapi.TransactionContextInterf return nil } } +*/ // GetAllSchemas returns all schemas found in world state @@ -913,3 +916,66 @@ func (s *SmartContract) WriteSchemaToPDC(ctx contractapi.TransactionContextInter return nil } + +// ReadAsset reads the information from collection +func (s *SmartContract) ReadSchemaFromPDC(ctx contractapi.TransactionContextInterface, SchemaID string) (*Schema, error) { + + log.Printf("ReadSchemaFromPDC: collection %v, ID %v", PDC1, SchemaID) + assetJSON, err := ctx.GetStub().GetPrivateData(PDC1, SchemaID) //get the asset from chaincode state + if err != nil { + return nil, fmt.Errorf("failed to read asset: %v", err) + } + + //No Asset found, return empty response + if assetJSON == nil { + log.Printf("%v does not exist in collection %v", SchemaID, PDC1) + return nil, nil + } + + var schema *Schema + err = json.Unmarshal(assetJSON, &schema) + if err != nil { + return nil, fmt.Errorf("failed to unmarshal JSON: %v", err) + } + + return schema, nil + +} + +func (s *SmartContract) GetAllPDCSchemas(ctx contractapi.TransactionContextInterface, SchemaID string) ([]*Schema, error) { + log.Printf("GetAllPDCSchemas: collection %v ", PDC1) + + resultsIterator, err := ctx.GetStub().GetPrivateDataByRange(PDC1, "", "") + + if err != nil { + return nil, err + } + defer resultsIterator.Close() + + if err != nil { + return nil, fmt.Errorf("failed to read Schemas: %v", err) + } + + var schemas []*Schema + for resultsIterator.HasNext() { + queryResponse, err := resultsIterator.Next() + if err != nil { + return nil, err + } + + var schema map[string]interface{} + err = json.Unmarshal(queryResponse.Value, &schema) + if err != nil { + return nil, err + } + var schemaStruct Schema + err = json.Unmarshal(queryResponse.Value, &schemaStruct) + if err != nil { + return nil, err + } else { + schemas = append(schemas, &schemaStruct) + } + } + + return schemas, nil +} diff --git a/test-network/QuickCCTest.sh b/test-network/QuickCCTest.sh index ae7504df..7b6c06a4 100644 --- a/test-network/QuickCCTest.sh +++ b/test-network/QuickCCTest.sh @@ -5,6 +5,7 @@ echo "========= Killing Previous network and Setting up New One ===========" #docker rm -f $(docker ps -q) ./network.sh down ./network.sh up createChannel +./monitordocker.sh fabric_test echo "========= Channel created and successfully joined ===========" cd $HOME/Projects/OSC-IS/fabric-samples/asset-transfer-basic/chaincode-go @@ -211,11 +212,44 @@ echo "========= CC Query: Get all DataSamples ===========" peer chaincode query -C mychannel -n basic -c '{"Args":["GetAllAssets"]}' +echo "========= Setting Up Network with PDC and SmartContract ===========" +./monitordocker.sh fabric_test + + +cd /Users/fernando/Projects/OSC-IS/fabric-samples/test-network +./network.sh down +./network.sh up createChannel -ca -s couchdb +./network.sh deployCC -ccn private -ccp ../asset-transfer-private-data/chaincode-go/ -ccl go -ccep "OR('Org1MSP.peer','Org2MSP.peer')" -cccg ../asset-transfer-private-data/chaincode-go/collections_config.json + +echo "========= Creating Identities ===========" + +export PATH=${PWD}/../bin:${PWD}:$PATH +export FABRIC_CFG_PATH=$PWD/../config/ +export FABRIC_CA_CLIENT_HOME=${PWD}/organizations/peerOrganizations/org1.example.com/ +fabric-ca-client register --caname ca-org1 --id.name owner --id.secret ownerpw --id.type client --tls.certfiles "${PWD}/organizations/fabric-ca/org1/tls-cert.pem" +fabric-ca-client enroll -u https://owner:ownerpw@localhost:7054 --caname ca-org1 -M "${PWD}/organizations/peerOrganizations/org1.example.com/users/owner@org1.example.com/msp" --tls.certfiles "${PWD}/organizations/fabric-ca/org1/tls-cert.pem" +cp "${PWD}/organizations/peerOrganizations/org1.example.com/msp/config.yaml" "${PWD}/organizations/peerOrganizations/org1.example.com/users/owner@org1.example.com/msp/config.yaml" +export FABRIC_CA_CLIENT_HOME=${PWD}/organizations/peerOrganizations/org2.example.com/ +fabric-ca-client register --caname ca-org2 --id.name buyer --id.secret buyerpw --id.type client --tls.certfiles "${PWD}/organizations/fabric-ca/org2/tls-cert.pem" +fabric-ca-client enroll -u https://buyer:buyerpw@localhost:8054 --caname ca-org2 -M "${PWD}/organizations/peerOrganizations/org2.example.com/users/buyer@org2.example.com/msp" --tls.certfiles "${PWD}/organizations/fabric-ca/org2/tls-cert.pem" +cp "${PWD}/organizations/peerOrganizations/org2.example.com/msp/config.yaml" "${PWD}/organizations/peerOrganizations/org2.example.com/users/buyer@org2.example.com/msp/config.yaml" echo "========= CC Invoke: Creation of Schema in PDC ===========" -export ASSET_PROPERTIES=$(echo -n "{\"SchemaId\":\"Project1.Schema1\",\"Project\":\"Project1\",\"JsonSchemaContent\":"{\"type\": \"object\", \"properties\": { \"number\": { \"type\": \"number\" }, \"street_name\": { \"type\": \"string\" }, \"street_type\": { \"enum\": [\"Street\", \"Avenue\",\"Boulevard\"] }}, \"additionalProperties\": false, \"required\": [ \"number\", \"street_name\"]}", "{ \"number\": 1600, \"street_name\": \"Pennsylvania\", \"street_type\": \"Avenue\" }"]}"}" | base64 | tr -d \\n) +export PATH=${PWD}/../bin:$PATH +export FABRIC_CFG_PATH=$PWD/../config/ +export CORE_PEER_TLS_ENABLED=true +export CORE_PEER_LOCALMSPID="Org1MSP" +export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt +export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/owner@org1.example.com/msp +export CORE_PEER_ADDRESS=localhost:7051 + +export ASSET_PROPERTIES=$(echo -n "{\"SchemaId\":\"Project1.Schema1\",\"Project\":\"Project1\",\"JsonSchemaContent\":{\"type\": \"object\", \"properties\": { \"number\": { \"type\": \"number\" }, \"street_name\": { \"type\": \"string\" }, \"street_type\": { \"enum\": [\"Street\", \"Avenue\",\"Boulevard\"] }}, \"additionalProperties\": false, \"required\": [ \"number\", \"street_name\"]}}" | base64 | tr -d \\n) + + peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile "${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem" -C mychannel -n private -c '{"function":"WriteSchemaToPDC","Args":[]}' --transient "{\"asset_properties\":\"$ASSET_PROPERTIES\"}" -'{\"type\": \"object\", \"properties\": { \"number\": { \"type\": \"number\" }, \"street_name\": { \"type\": \"string\" }, \"street_type\": { \"enum\": [\"Street\", \"Avenue\",\"Boulevard\"] }}, \"additionalProperties\": false, \"required\": [ \"number\", \"street_name\"]}", "{ \"number\": 1600, \"street_name\": \"Pennsylvania\", \"street_type\": \"Avenue\" }"]}' \ No newline at end of file +echo "========= CC Invoke: Reading of Schema in PDC ===========" + +peer chaincode query -C mychannel -n private -c '{"function":"ReadAsset","Args":["Project1.Schema1"]}'