mirror of
https://github.com/hyperledger/fabric-samples.git
synced 2026-06-24 18:45:09 +00:00
Schema Update: Schema can be updated and used to validate new json incoming files. Json contents are being stored in Data Structs
This commit is contained in:
parent
920ddf0777
commit
edff9ad2c1
6 changed files with 528 additions and 11 deletions
|
|
@ -0,0 +1,403 @@
|
||||||
|
package chaincode
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/hyperledger/fabric-contract-api-go/contractapi"
|
||||||
|
"github.com/xeipuuv/gojsonschema"
|
||||||
|
|
||||||
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
|
)
|
||||||
|
|
||||||
|
// SmartContract provides functions for managing an Asset
|
||||||
|
type SmartContract struct {
|
||||||
|
contractapi.Contract
|
||||||
|
}
|
||||||
|
|
||||||
|
// Asset describes basic details of what makes up a simple asset
|
||||||
|
// Insert struct field in alphabetic order => to achieve determinism across languages
|
||||||
|
// golang keeps the order when marshal to json but doesn't order automatically
|
||||||
|
|
||||||
|
/*type Data struct {
|
||||||
|
docType string `json:"docType"`
|
||||||
|
id string `json:"id"`
|
||||||
|
title string `json:"title"`
|
||||||
|
description string `json:"description"`
|
||||||
|
Type string `json:"Type"`
|
||||||
|
DOI string `json:"DOI"`
|
||||||
|
url string `json:"url"`
|
||||||
|
manifest string `json:"manifest"`
|
||||||
|
footprint string `json:"footprint"`
|
||||||
|
keywords string `json:"keywords"`
|
||||||
|
otherDataIdName string `json:"otherDataIdName"`
|
||||||
|
otherDataIdValue string `json:"otherDataIdValue"`
|
||||||
|
fundingAgencies string `json:"fundingAgencies"`
|
||||||
|
acknowledgment string `json:"acknowledgment"`
|
||||||
|
noteForChange string `json:"noteForChange"`
|
||||||
|
contributor string `json:"contributor"`
|
||||||
|
contributor_id string `json:"contributor_id"`
|
||||||
|
}*/
|
||||||
|
|
||||||
|
// Asset describes basic details of what makes up a simple asset
|
||||||
|
// Insert struct field in alphabetic order => to achieve determinism across languages
|
||||||
|
// golang keeps the order when marshal to json but doesn't order automatically
|
||||||
|
|
||||||
|
var schemas []Schema
|
||||||
|
|
||||||
|
type Data struct {
|
||||||
|
Contributor string `json:"Contributor"`
|
||||||
|
ContributorId string `json:"ContributorId"`
|
||||||
|
ContentHash string `json:"ContentHash"`
|
||||||
|
Id string `json:"Id"`
|
||||||
|
Owners []string `json:"Owners"`
|
||||||
|
JsonFileContent map[string]interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
type Schema struct {
|
||||||
|
Contributor string `json:"Contributor"`
|
||||||
|
ContributorId string `json:"ContributorId"`
|
||||||
|
ContentHash string `json:"ContentHash"`
|
||||||
|
Id string `json:"Id"`
|
||||||
|
Owners []string `json:"Owners"`
|
||||||
|
JsonFileContent map[string]interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// InitLedger adds a base set of Data entries to the ledger
|
||||||
|
func (s *SmartContract) InitLedger(ctx contractapi.TransactionContextInterface, pathSchema string, pathFirstJsonFile 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 into
|
||||||
|
// the docker container of the commited chaincode (For now)
|
||||||
|
schemaJsonFileContent, error_schema := s.JsonReader(ctx, pathSchema)
|
||||||
|
firstJsonFileContent, error_file := s.JsonReader(ctx, pathFirstJsonFile)
|
||||||
|
|
||||||
|
if error_schema != nil {
|
||||||
|
return fmt.Errorf("failed to read shcema.json file: %v", error_schema)
|
||||||
|
} else if error_file != nil {
|
||||||
|
return fmt.Errorf("failed to read 1st json files: %v", error_file)
|
||||||
|
} else {
|
||||||
|
fmt.Printf("Schema Content: %v", schemaJsonFileContent)
|
||||||
|
fmt.Printf("Test Json file Content: %v", firstJsonFileContent)
|
||||||
|
|
||||||
|
dataEntries := []Data{
|
||||||
|
{Contributor: "pepitoperes@email.com", ContributorId: "ABC123", ContentHash: "ZXCVBNM", Id: "00000", Owners: []string{"CIA", "DEA", "FBI"}, JsonFileContent: firstJsonFileContent},
|
||||||
|
}
|
||||||
|
|
||||||
|
//This is the definition of the Schema that we should use for validate all the JSON files from now on.
|
||||||
|
initSchema := Schema{Contributor: "pepitoperes@email.com", ContributorId: "ABC123", ContentHash: "ZXCVBNM", Id: "00000", Owners: []string{"CIA", "DEA", "FBI"}, JsonFileContent: schemaJsonFileContent}
|
||||||
|
schemas = append(schemas, initSchema)
|
||||||
|
|
||||||
|
for _, data := range schemas {
|
||||||
|
assetJSON, err := json.Marshal(data)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = ctx.GetStub().PutState(data.Id, assetJSON)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to put to world state. %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, data := range dataEntries {
|
||||||
|
assetJSON, err := json.Marshal(data)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = ctx.GetStub().PutState(data.Id, assetJSON)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to put to world state. %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *SmartContract) JsonReader(ctx contractapi.TransactionContextInterface, path string) (map[string]interface{}, error) {
|
||||||
|
|
||||||
|
content, err := ioutil.ReadFile(path)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("Error when opening file: ", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now let's unmarshall the data into `payload`
|
||||||
|
var payload map[string]interface{}
|
||||||
|
err = json.Unmarshal(content, &payload)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("Error during Unmarshal(): ", err)
|
||||||
|
}
|
||||||
|
return payload, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetAllAssets returns all assets found in world state
|
||||||
|
func (s *SmartContract) GetAllAssets(ctx contractapi.TransactionContextInterface) ([]*Data, error) {
|
||||||
|
// range query with empty string for startKey and endKey does an
|
||||||
|
// open-ended query of all assets in the chaincode namespace.
|
||||||
|
resultsIterator, err := ctx.GetStub().GetStateByRange("", "")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer resultsIterator.Close()
|
||||||
|
|
||||||
|
var dataSamples []*Data
|
||||||
|
for resultsIterator.HasNext() {
|
||||||
|
queryResponse, err := resultsIterator.Next()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var data Data
|
||||||
|
err = json.Unmarshal(queryResponse.Value, &data)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
dataSamples = append(dataSamples, &data)
|
||||||
|
}
|
||||||
|
|
||||||
|
return dataSamples, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// AssetExists returns true when asset with given ID exists in world state
|
||||||
|
func (s *SmartContract) AssetExists(ctx contractapi.TransactionContextInterface, Id string) (bool, error) {
|
||||||
|
assetJSON, err := ctx.GetStub().GetState(Id)
|
||||||
|
if err != nil {
|
||||||
|
return false, fmt.Errorf("failed to read from world state: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return assetJSON != nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// JSON Validation
|
||||||
|
|
||||||
|
func (s *SmartContract) ValidJson(ctx contractapi.TransactionContextInterface, AbsolutePathToJsonFile string) (bool, error) {
|
||||||
|
|
||||||
|
//schemaLoader := gojsonschema.NewReferenceLoader("file:///Users/fernando/Projects/OSC-IS/fabric-samples/test-network/JsonSchemaValidationTests/Schema.json")
|
||||||
|
//documentLoader := gojsonschema.NewReferenceLoader("file:////Users/fernando/Projects/OSC-IS/fabric-samples/test-network/JsonSchemaValidationTests/testFile.json")
|
||||||
|
|
||||||
|
//schemaLoader := gojsonschema.NewReferenceLoader("file:///home/chaincode/Schema.json")
|
||||||
|
//documentLoader := gojsonschema.NewReferenceLoader("file:////home/chaincode/testFile.json")
|
||||||
|
|
||||||
|
// PATH Needs to be absolute path (From root '/'). Add something that takes care of that.
|
||||||
|
|
||||||
|
m := schemas[len(schemas) - 1].JsonFileContent
|
||||||
|
schemaLoader := gojsonschema.NewGoLoader(m)
|
||||||
|
documentLoader := gojsonschema.NewReferenceLoader("file://" + AbsolutePathToJsonFile)
|
||||||
|
|
||||||
|
result, err := gojsonschema.Validate(schemaLoader, documentLoader)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
panic(err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
if result.Valid() {
|
||||||
|
fmt.Printf("The document is valid\n")
|
||||||
|
} else {
|
||||||
|
fmt.Printf("The document is not valid. see errors :\n")
|
||||||
|
for _, desc := range result.Errors() {
|
||||||
|
fmt.Printf("- %s\n", desc)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result.Valid(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateDataSample issues a new Data Sample to the world state with given details.
|
||||||
|
func (s *SmartContract) CreateDataSample(ctx contractapi.TransactionContextInterface,
|
||||||
|
Contributor string, ContributorId string, ContentHash string, Id string, AbsolutePathToJsonFile string) error {
|
||||||
|
|
||||||
|
exists, err := s.AssetExists(ctx, Id)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if exists {
|
||||||
|
return fmt.Errorf("the asset %s already exists", Id)
|
||||||
|
}
|
||||||
|
|
||||||
|
valid, err := s.ValidJson(ctx, AbsolutePathToJsonFile)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if !valid {
|
||||||
|
return fmt.Errorf("the json file provided is not valid")
|
||||||
|
} else {
|
||||||
|
jsonFileContent, err := s.JsonReader(ctx, AbsolutePathToJsonFile)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
} else {
|
||||||
|
data := Data{
|
||||||
|
Contributor: Contributor,
|
||||||
|
ContributorId: ContributorId,
|
||||||
|
ContentHash: ContentHash,
|
||||||
|
Id: Id,
|
||||||
|
Owners: []string{"DOE", "DOS", "DOJ"},
|
||||||
|
JsonFileContent: jsonFileContent,
|
||||||
|
}
|
||||||
|
|
||||||
|
assetJSON, err := json.Marshal(data)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
|
||||||
|
}
|
||||||
|
return ctx.GetStub().PutState(Id, assetJSON)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
// UpdateAsset updates an existing asset in the world state with provided parameters.
|
||||||
|
func (s *SmartContract) UpdateAsset(ctx contractapi.TransactionContextInterface,
|
||||||
|
Contributor string, ContributorId string, ContentHash string, Id string) error {
|
||||||
|
exists, err := s.AssetExists(ctx, Id)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if !exists {
|
||||||
|
return fmt.Errorf("the asset %s does not exist", Id)
|
||||||
|
}
|
||||||
|
|
||||||
|
// overwriting original asset with new asset
|
||||||
|
|
||||||
|
data := Data{
|
||||||
|
Contributor: Contributor,
|
||||||
|
ContributorId: ContributorId,
|
||||||
|
ContentHash: ContentHash,
|
||||||
|
Id: Id,
|
||||||
|
Owners: []string{"DOE", "DOS", "DOJ"},
|
||||||
|
}
|
||||||
|
|
||||||
|
assetJSON, err := json.Marshal(data)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return ctx.GetStub().PutState(Id, assetJSON)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *SmartContract) DeleteAsset(ctx contractapi.TransactionContextInterface, Id string) error {
|
||||||
|
exists, err := s.AssetExists(ctx, Id)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if !exists {
|
||||||
|
return fmt.Errorf("the asset %s does not exist", Id)
|
||||||
|
}
|
||||||
|
|
||||||
|
return ctx.GetStub().DelState(Id)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *SmartContract) ReadAsset(ctx contractapi.TransactionContextInterface, Id string) (*Data, error) {
|
||||||
|
assetJSON, err := ctx.GetStub().GetState(Id)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to read from world state: %v", err)
|
||||||
|
}
|
||||||
|
if assetJSON == nil {
|
||||||
|
return nil, fmt.Errorf("the asset %s does not exist", Id)
|
||||||
|
}
|
||||||
|
|
||||||
|
var data Data
|
||||||
|
err = json.Unmarshal(assetJSON, &data)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &data, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// TransferAsset updates the owner field of asset with given id in world state, and returns the old owner.
|
||||||
|
func (s *SmartContract) TransferAsset(ctx contractapi.TransactionContextInterface, Id string, newOwners []string) ([]string, error) {
|
||||||
|
data, err := s.ReadAsset(ctx, Id)
|
||||||
|
if err != nil {
|
||||||
|
return []string{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
data.Owners = newOwners
|
||||||
|
|
||||||
|
assetJSON, err := json.Marshal(data)
|
||||||
|
if err != nil {
|
||||||
|
return []string{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = ctx.GetStub().PutState(Id, assetJSON)
|
||||||
|
if err != nil {
|
||||||
|
return []string{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return data.Owners, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
|
|
||||||
|
// ReadAsset returns the asset stored in the world state with given id.
|
||||||
|
func (s *SmartContract) ReadAsset(ctx contractapi.TransactionContextInterface, id string) (*Asset, error) {
|
||||||
|
assetJSON, err := ctx.GetStub().GetState(id)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to read from world state: %v", err)
|
||||||
|
}
|
||||||
|
if assetJSON == nil {
|
||||||
|
return nil, fmt.Errorf("the asset %s does not exist", id)
|
||||||
|
}
|
||||||
|
|
||||||
|
var asset Asset
|
||||||
|
err = json.Unmarshal(assetJSON, &asset)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &asset, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// TransferAsset updates the owner field of asset with given id in world state, and returns the old owner.
|
||||||
|
func (s *SmartContract) TransferAsset(ctx contractapi.TransactionContextInterface, id string, newOwner string) (string, error) {
|
||||||
|
asset, err := s.ReadAsset(ctx, id)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
oldOwner := asset.Owner
|
||||||
|
asset.Owner = newOwner
|
||||||
|
|
||||||
|
assetJSON, err := json.Marshal(asset)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = ctx.GetStub().PutState(id, assetJSON)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
return oldOwner, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetAllAssets returns all assets found in world state
|
||||||
|
func (s *SmartContract) GetAllAssets(ctx contractapi.TransactionContextInterface) ([]*Asset, error) {
|
||||||
|
// range query with empty string for startKey and endKey does an
|
||||||
|
// open-ended query of all assets in the chaincode namespace.
|
||||||
|
resultsIterator, err := ctx.GetStub().GetStateByRange("", "")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer resultsIterator.Close()
|
||||||
|
|
||||||
|
var assets []*Asset
|
||||||
|
for resultsIterator.HasNext() {
|
||||||
|
queryResponse, err := resultsIterator.Next()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var asset Asset
|
||||||
|
err = json.Unmarshal(queryResponse.Value, &asset)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
assets = append(assets, &asset)
|
||||||
|
}
|
||||||
|
|
||||||
|
return assets, nil
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
@ -65,20 +65,17 @@ type Schema struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// InitLedger adds a base set of Data entries to the ledger
|
// InitLedger adds a base set of Data entries to the ledger
|
||||||
func (s *SmartContract) InitLedger(ctx contractapi.TransactionContextInterface, pathSchema string, pathFirstJsonFile string) ([]Data, []Schema) {
|
func (s *SmartContract) InitLedger(ctx contractapi.TransactionContextInterface, pathSchema string, pathFirstJsonFile 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 into
|
// 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 into
|
||||||
// the docker container of the commited chaincode (For now)
|
// the docker container of the commited chaincode (For now)
|
||||||
schemaJsonFileContent, error_schema := s.JsonReader(ctx, pathSchema)
|
schemaJsonFileContent, error_schema := s.JsonReader(ctx, pathSchema)
|
||||||
firstJsonFileContent, error_file := s.JsonReader(ctx, pathFirstJsonFile)
|
firstJsonFileContent, error_file := s.JsonReader(ctx, pathFirstJsonFile)
|
||||||
|
|
||||||
dataEntries := []Data
|
|
||||||
initSchema := []Schema
|
|
||||||
|
|
||||||
if error_schema != nil {
|
if error_schema != nil {
|
||||||
return dataEntries, initSchema //, fmt.Errorf("failed to read shcema.json file or NewJsonFile: %v", error_schema)
|
return fmt.Errorf("failed to read shcema.json file: %v", error_schema)
|
||||||
} else if error_file != nil {
|
} else if error_file != nil {
|
||||||
return dataEntries, initSchema//, fmt.Errorf("failed to read shcema.json file or NewJsonFile: %v", error_file)
|
return fmt.Errorf("failed to read 1st json files: %v", error_file)
|
||||||
} else {
|
} else {
|
||||||
fmt.Printf("Schema Content: %v", schemaJsonFileContent)
|
fmt.Printf("Schema Content: %v", schemaJsonFileContent)
|
||||||
fmt.Printf("Test Json file Content: %v", firstJsonFileContent)
|
fmt.Printf("Test Json file Content: %v", firstJsonFileContent)
|
||||||
|
|
@ -90,8 +87,33 @@ func (s *SmartContract) InitLedger(ctx contractapi.TransactionContextInterface,
|
||||||
//This is the definition of the Schema that we should use for validate all the JSON files from now on.
|
//This is the definition of the Schema that we should use for validate all the JSON files from now on.
|
||||||
initSchema := Schema{Contributor: "pepitoperes@email.com", ContributorId: "ABC123", ContentHash: "ZXCVBNM", Id: "00000", Owners: []string{"CIA", "DEA", "FBI"}, JsonFileContent: schemaJsonFileContent}
|
initSchema := Schema{Contributor: "pepitoperes@email.com", ContributorId: "ABC123", ContentHash: "ZXCVBNM", Id: "00000", Owners: []string{"CIA", "DEA", "FBI"}, JsonFileContent: schemaJsonFileContent}
|
||||||
schemas = append(schemas, initSchema)
|
schemas = append(schemas, initSchema)
|
||||||
return dataEntries, schemas //, nil
|
|
||||||
|
for _, data := range schemas {
|
||||||
|
assetJSON, err := json.Marshal(data)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = ctx.GetStub().PutState(data.Id, assetJSON)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to put to world state. %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, data := range dataEntries {
|
||||||
|
assetJSON, err := json.Marshal(data)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = ctx.GetStub().PutState(data.Id, assetJSON)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to put to world state. %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SmartContract) JsonReader(ctx contractapi.TransactionContextInterface, path string) (map[string]interface{}, error) {
|
func (s *SmartContract) JsonReader(ctx contractapi.TransactionContextInterface, path string) (map[string]interface{}, error) {
|
||||||
|
|
@ -138,6 +160,67 @@ func (s *SmartContract) GetAllAssets(ctx contractapi.TransactionContextInterface
|
||||||
return dataSamples, nil
|
return dataSamples, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *SmartContract) CreateNewSchema(ctx contractapi.TransactionContextInterface,
|
||||||
|
Contributor string, ContributorId string, ContentHash string, Id string, AbsolutePathToJsonFile string) error {
|
||||||
|
|
||||||
|
// We assume this new schema is different from what existed previously
|
||||||
|
//exists, err := s.AssetExists(ctx, Id)
|
||||||
|
//if err != nil {
|
||||||
|
// return err
|
||||||
|
//}
|
||||||
|
//if exists {
|
||||||
|
// return fmt.Errorf("the asset %s already exists", Id)
|
||||||
|
//}
|
||||||
|
|
||||||
|
jsonFileContent, err := s.JsonReader(ctx, AbsolutePathToJsonFile)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
} else {
|
||||||
|
newSchema := Schema{
|
||||||
|
Contributor: Contributor,
|
||||||
|
ContributorId: ContributorId,
|
||||||
|
ContentHash: ContentHash,
|
||||||
|
Id: Id,
|
||||||
|
Owners: []string{"DOE", "DOS", "DOJ"},
|
||||||
|
JsonFileContent: jsonFileContent,
|
||||||
|
}
|
||||||
|
|
||||||
|
schemas = append(schemas, newSchema)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// GetAllSchemas returns all schemas found in world state
|
||||||
|
|
||||||
|
func (s *SmartContract) GetAllSchemas(ctx contractapi.TransactionContextInterface) ([]Schema, error) {
|
||||||
|
// range query with empty string for startKey and endKey does an
|
||||||
|
// open-ended query of all schemas in the chaincode namespace.
|
||||||
|
//resultsIterator, err := ctx.GetStub().GetStateByRange("", "")
|
||||||
|
//if err != nil {
|
||||||
|
// return nil, err
|
||||||
|
//}
|
||||||
|
//defer resultsIterator.Close()
|
||||||
|
|
||||||
|
//var schemaSamples []*Schema
|
||||||
|
//for resultsIterator.HasNext() {
|
||||||
|
// queryResponse, err := resultsIterator.Next()
|
||||||
|
// if err != nil {
|
||||||
|
// return nil, err
|
||||||
|
// }
|
||||||
|
|
||||||
|
// var schm Schema
|
||||||
|
// err = json.Unmarshal(queryResponse.Value, &schm)
|
||||||
|
// if err != nil {
|
||||||
|
// return nil, err
|
||||||
|
// }
|
||||||
|
// schemaSamples = append(schemaSamples, &schm)
|
||||||
|
//}
|
||||||
|
|
||||||
|
return schemas, nil
|
||||||
|
}
|
||||||
|
|
||||||
// AssetExists returns true when asset with given ID exists in world state
|
// AssetExists returns true when asset with given ID exists in world state
|
||||||
func (s *SmartContract) AssetExists(ctx contractapi.TransactionContextInterface, Id string) (bool, error) {
|
func (s *SmartContract) AssetExists(ctx contractapi.TransactionContextInterface, Id string) (bool, error) {
|
||||||
assetJSON, err := ctx.GetStub().GetState(Id)
|
assetJSON, err := ctx.GetStub().GetState(Id)
|
||||||
|
|
|
||||||
10
test-network/JsonSchemaValidationTests/NewSchema.json
Executable file
10
test-network/JsonSchemaValidationTests/NewSchema.json
Executable file
|
|
@ -0,0 +1,10 @@
|
||||||
|
{
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"number": { "type": "number" },
|
||||||
|
"street_name": { "type": "string" },
|
||||||
|
"street_type": { "enum": ["Street", "Avenue", "Boulevard"] }
|
||||||
|
},
|
||||||
|
"additionalProperties": false,
|
||||||
|
"required": [ "number", "street_name", "street_type"]
|
||||||
|
}
|
||||||
|
|
@ -50,9 +50,6 @@ func (s *SmartContract) InitLedger(ctx contractapi.TransactionContextInterface)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
//data := Data{
|
|
||||||
//"DocType": "TestType", "Id": "00000", "Title": "TestSample", "Description": "description", "Type": "TestType", "DOI": "https://doi.org/10.57873/T34W2R", "Url": "sdsc.edu", "Manifest": "TestManifest", "Footprint": "", "Keywords": '["SmartContrac", "ChainCode", "Peer"]'ß, "OtherDataIdName": "None", "OtherDataIdValue": "None", "FundingAgencies": ["DOS", "NASA"], "Acknowledgment": "SDSC", "NoteForChange": "NONE", "Contributor": "AveryhardworkingUser@email.com", "Contributor_id": "ABC123"
|
|
||||||
//}
|
|
||||||
|
|
||||||
schemaLoader := gojsonschema.NewReferenceLoader("file:///home/ofgarzon2662/OSC-IS/fabric-samples/test-network/JsonSchemaValidationTests/Schema.json")
|
schemaLoader := gojsonschema.NewReferenceLoader("file:///home/ofgarzon2662/OSC-IS/fabric-samples/test-network/JsonSchemaValidationTests/Schema.json")
|
||||||
documentLoader := gojsonschema.NewReferenceLoader("file:////home/ofgarzon2662/OSC-IS/fabric-samples/test-network/JsonSchemaValidationTests/testFile.json")
|
documentLoader := gojsonschema.NewReferenceLoader("file:////home/ofgarzon2662/OSC-IS/fabric-samples/test-network/JsonSchemaValidationTests/testFile.json")
|
||||||
|
|
|
||||||
1
test-network/JsonSchemaValidationTests/testFileNewSample.json
Executable file
1
test-network/JsonSchemaValidationTests/testFileNewSample.json
Executable file
|
|
@ -0,0 +1 @@
|
||||||
|
{ "number": 11357, "street_name": "Markab Dr", "street_type": "Avenue" }
|
||||||
|
|
@ -81,6 +81,10 @@ echo "========= CC Query: Get all DataSamples ==========="
|
||||||
|
|
||||||
peer chaincode query -C mychannel -n basic -c '{"Args":["GetAllAssets"]}'
|
peer chaincode query -C mychannel -n basic -c '{"Args":["GetAllAssets"]}'
|
||||||
|
|
||||||
|
echo "========= CC Query: Get all Schemas ==========="
|
||||||
|
|
||||||
|
peer chaincode query -C mychannel -n basic -c '{"Args":["GetAllSchemas"]}'
|
||||||
|
|
||||||
echo "========= Check if sample with id = '00000' Exists ==========="
|
echo "========= Check if sample with id = '00000' Exists ==========="
|
||||||
|
|
||||||
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 basic --peerAddresses localhost:7051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt" --peerAddresses localhost:9051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt" -c '{"function":"AssetExists","Args":["00000"]}'
|
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 basic --peerAddresses localhost:7051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt" --peerAddresses localhost:9051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt" -c '{"function":"AssetExists","Args":["00000"]}'
|
||||||
|
|
@ -90,9 +94,21 @@ echo "========= Check creation of a new sample wit id = '00001' ==========="
|
||||||
# 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 basic --peerAddresses localhost:7051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt" --peerAddresses localhost:9051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt" -c '{"function":"CreateDataSample","Args":["docTypeTest2","00001","TestTitle2","Desc2","Neuroscience","TestDOI","www.ncis.edu","TestManifest","TestFootPrint", "Neuroscience, brain", "None", "None", "None","None","None","None","None", "None"]}'
|
# 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 basic --peerAddresses localhost:7051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt" --peerAddresses localhost:9051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt" -c '{"function":"CreateDataSample","Args":["docTypeTest2","00001","TestTitle2","Desc2","Neuroscience","TestDOI","www.ncis.edu","TestManifest","TestFootPrint", "Neuroscience, brain", "None", "None", "None","None","None","None","None", "None"]}'
|
||||||
|
|
||||||
#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 basic --peerAddresses localhost:7051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt" --peerAddresses localhost:9051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt" -c '{"function":"CreateDataSample","Args":["UCSD-SDSC", "Contributor123", "PhonyHash!@#$$%#@", "00001", ["DOE FER"]]}'
|
#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 basic --peerAddresses localhost:7051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt" --peerAddresses localhost:9051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt" -c '{"function":"CreateDataSample","Args":["UCSD-SDSC", "Contributor123", "PhonyHash!@#$$%#@", "00001", ["DOE FER"]]}'
|
||||||
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 basic --peerAddresses localhost:7051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt" --peerAddresses localhost:9051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt" -c '{"function":"CreateDataSample","Args":["UCSD-SDSC", "Contributor123", "PhonyHash!@#$$%#@", "00001"]}'
|
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 basic --peerAddresses localhost:7051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt" --peerAddresses localhost:9051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt" -c '{"function":"CreateDataSample","Args":["UCSD-SDSC", "Contributor123", "PhonyHash!@#$$%#@", "00001", "/home/chaincode/testFileNewSample.json"]}'
|
||||||
|
|
||||||
#peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile \
|
#peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile \
|
||||||
|
|
||||||
|
echo "========= Check creation of a new Schema ==========="
|
||||||
|
|
||||||
|
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 basic --peerAddresses localhost:7051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt" --peerAddresses localhost:9051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt" -c '{"function":"CreateNewSchema","Args":["UCSD-SDSC", "Contributor123", "PhonyHash!@#$$%#@", "00001", "/home/chaincode/NewSchema.json"]}'
|
||||||
|
|
||||||
|
echo "========= Check creation of a new sample wit id = '00002' based on New Schema ==========="
|
||||||
|
|
||||||
|
# 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 basic --peerAddresses localhost:7051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt" --peerAddresses localhost:9051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt" -c '{"function":"CreateDataSample","Args":["docTypeTest2","00001","TestTitle2","Desc2","Neuroscience","TestDOI","www.ncis.edu","TestManifest","TestFootPrint", "Neuroscience, brain", "None", "None", "None","None","None","None","None", "None"]}'
|
||||||
|
|
||||||
|
#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 basic --peerAddresses localhost:7051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt" --peerAddresses localhost:9051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt" -c '{"function":"CreateDataSample","Args":["UCSD-SDSC", "Contributor123", "PhonyHash!@#$$%#@", "00001", ["DOE FER"]]}'
|
||||||
|
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 basic --peerAddresses localhost:7051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt" --peerAddresses localhost:9051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt" -c '{"function":"CreateDataSample","Args":["UCSD-SDSC", "Contributor123", "PhonyHash!@#$$%#@", "00002", "/home/chaincode/testFileNewSample.json"]}'
|
||||||
|
|
||||||
#"${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem" \
|
#"${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem" \
|
||||||
# -C mychannel -n basic --peerAddresses localhost:7051 --tlsRootCertFiles \
|
# -C mychannel -n basic --peerAddresses localhost:7051 --tlsRootCertFiles \
|
||||||
# "${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt" --peerAddresses localhost:9051 \
|
# "${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt" --peerAddresses localhost:9051 \
|
||||||
|
|
@ -155,3 +171,10 @@ peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.exa
|
||||||
echo "========= Transfer Sample 00000 to new contributor ==========="
|
echo "========= Transfer Sample 00000 to new contributor ==========="
|
||||||
|
|
||||||
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 basic --peerAddresses localhost:7051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt" --peerAddresses localhost:9051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt" -c '{"function":"TransferAsset","Args":["00000", "NSF", "NSF_ID"]}'
|
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 basic --peerAddresses localhost:7051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt" --peerAddresses localhost:9051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt" -c '{"function":"TransferAsset","Args":["00000", "NSF", "NSF_ID"]}'
|
||||||
|
|
||||||
|
echo "========= Creation of json files inside installed chaincode in peers ==========="
|
||||||
|
|
||||||
|
vi /home/chaincode/schema.json
|
||||||
|
vi /home/chaincode/NewSchema.json
|
||||||
|
vi /home/chaincode/firstJson.json
|
||||||
|
vi /home/chaincode/testFileNewSample.json
|
||||||
Loading…
Reference in a new issue