Chaincode slowly evolving

This commit is contained in:
Fernando Garzon 2022-10-24 15:30:53 -07:00
parent 9acb406996
commit ee6954ddac

View file

@ -63,46 +63,53 @@ 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) (error) { 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 := jsonReader(ctx, pathSchema) schemaJsonFileContent, error_schema := s.JsonReader(ctx, pathSchema)
firstJsonFileContent := jsonReader(ctx, pathFirstJsonFile) firstJsonFileContent, error_file := s.JsonReader(ctx, pathFirstJsonFile)
dataEntries := []Data{ if error_schema != nil {
{Contributor: "pepitoperes@email.com", ContributorId: "ABC123", ContentHash: "ZXCVBNM", Id: "00000", Owners: []string{"CIA", "DEA", "FBI"}, firstJsonFileContent}, return fmt.Errorf("failed to read shcema.json file or NewJsonFile: %v", error_schema)
} } else if error_file != nil {
return fmt.Errorf("failed to read shcema.json file or NewJsonFile: %v", error_file)
schema := Schema{ } else {
{Contributor: "pepitoperes@email.com", ContributorId: "ABC123", ContentHash: "ZXCVBNM", Id: "00000", Owners: []string{"CIA", "DEA", "FBI"}, schemaJsonFileContent}, dataEntries := []Data{
} {Contributor: "pepitoperes@email.com", ContributorId: "ABC123", ContentHash: "ZXCVBNM", Id: "00000", Owners: []string{"CIA", "DEA", "FBI"}, JsonFileContent: firstJsonFileContent},
for _, data := range schema {
assetJSON, err := json.Marshal(data)
if err != nil {
return err
} }
err = ctx.GetStub().PutState(data.Id, assetJSON) //This is the definition of the Schema that we should use for validate all the JSON files from now on.
if err != nil { schema := []Schema{
return fmt.Errorf("failed to put to world state. %v", err) {Contributor: "pepitoperes@email.com", ContributorId: "ABC123", ContentHash: "ZXCVBNM", Id: "00000", Owners: []string{"CIA", "DEA", "FBI"}, JsonFileContent: schemaJsonFileContent},
} }
for _, data := range schema {
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
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 schema, 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) {
@ -118,7 +125,7 @@ func (s *SmartContract) JsonReader(ctx contractapi.TransactionContextInterface,
if err != nil { if err != nil {
log.Fatal("Error during Unmarshal(): ", err) log.Fatal("Error during Unmarshal(): ", err)
} }
return payload != nil, nil return payload, nil
} }
// GetAllAssets returns all assets found in world state // GetAllAssets returns all assets found in world state
@ -169,9 +176,8 @@ func (s *SmartContract) ValidJson(ctx contractapi.TransactionContextInterface, A
//schemaLoader := gojsonschema.NewReferenceLoader("file:///home/chaincode/Schema.json") //schemaLoader := gojsonschema.NewReferenceLoader("file:///home/chaincode/Schema.json")
//documentLoader := gojsonschema.NewReferenceLoader("file:////home/chaincode/testFile.json") //documentLoader := gojsonschema.NewReferenceLoader("file:////home/chaincode/testFile.json")
``` // PATH Needs to be absolute path (From root '/'). Add something that takes care of that.
PATH Needs to be absolute path (From root '/'). Add something that takes care of that.
```
m := schema.JsonFileContent.JsonFileContent m := schema.JsonFileContent.JsonFileContent
schemaLoader := gojsonschema.NewGoLoader(m) schemaLoader := gojsonschema.NewGoLoader(m)
documentLoader := gojsonschema.NewReferenceLoader("file://" + AbsolutePathToJsonFile) documentLoader := gojsonschema.NewReferenceLoader("file://" + AbsolutePathToJsonFile)
@ -212,13 +218,15 @@ func (s *SmartContract) CreateDataSample(ctx contractapi.TransactionContextInter
if !valid { if !valid {
return fmt.Errorf("the json file provided is not valid") return fmt.Errorf("the json file provided is not valid")
} else { } else {
jsonFileContent := s.JsonReader(ctx, AbsolutePathToJsonFile)
data := Data{ data := Data{
Contributor: Contributor, Contributor: Contributor,
ContributorId: ContributorId, ContributorId: ContributorId,
ContentHash: ContentHash, ContentHash: ContentHash,
Id: Id, Id: Id,
Owners: []string{"DOE", "DOS", "DOJ"}}, Owners: []string{"DOE", "DOS", "DOJ"},
JsonFileContent: JsonFileContent: jsonFileContent,
}
assetJSON, err := json.Marshal(data) assetJSON, err := json.Marshal(data)
if err != nil { if err != nil {