mirror of
https://github.com/hyperledger/fabric-samples.git
synced 2026-06-24 18:45:09 +00:00
Keep debugging
This commit is contained in:
parent
f6d9a32dfe
commit
ed74286f4d
4 changed files with 89 additions and 69 deletions
|
|
@ -10,6 +10,7 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
|
"reflect"
|
||||||
)
|
)
|
||||||
|
|
||||||
// SmartContract provides functions for managing an Asset
|
// SmartContract provides functions for managing an Asset
|
||||||
|
|
@ -76,97 +77,87 @@ func (s *SmartContract) InitLedger(ctx contractapi.TransactionContextInterface,
|
||||||
return fmt.Errorf("failed to read 1st json files: %v", error_file)
|
return fmt.Errorf("failed to read 1st json files: %v", error_file)
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
firstJsonFileHash, _ := s.Hash(InitData)
|
firstJsonFileHash, initDataHashError := s.Hash(ctx, InitData)
|
||||||
schemaJsonFileHash, _ := s.Hash(InitSchema)
|
schemaJsonFileHash, schemaHashError := s.Hash(ctx, InitSchema)
|
||||||
lastSchemaHash = schemaJsonFileHash
|
lastSchemaHash = schemaJsonFileHash
|
||||||
|
if initDataHashError != nil {
|
||||||
data := Data{
|
return fmt.Errorf("failed to calculate 1st json file hash: %v", initDataHashError)
|
||||||
|
} else if schemaHashError != nil {
|
||||||
|
return fmt.Errorf("failed to calculate schema hash: %v", schemaHashError)
|
||||||
|
} else{
|
||||||
|
data := Data{
|
||||||
Contributor: "pepitoperes@email.com",
|
Contributor: "pepitoperes@email.com",
|
||||||
ContributorId: "ABC123",
|
ContributorId: "ABC123",
|
||||||
ContentHash: firstJsonFileHash,
|
ContentHash: firstJsonFileHash,
|
||||||
Id: "00000",
|
Id: "00000",
|
||||||
Owners: []string{"CIA", "DEA", "FBI"},
|
Owners: []string{"CIA", "DEA", "FBI"},
|
||||||
JsonFileContent: firstJsonFileContent,
|
JsonFileContent: firstJsonFileContent,
|
||||||
|
}
|
||||||
|
|
||||||
|
assetJSON, err := json.Marshal(data)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = ctx.GetStub().PutState(data.ContentHash, assetJSON)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to put to world state. %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
//This is the definition of the Schema that we should use for validate all the JSON files from now on.
|
||||||
|
|
||||||
|
initSchema := Schema{
|
||||||
|
Version: 1,
|
||||||
|
Hash: schemaJsonFileHash,
|
||||||
|
JsonSchemaContent: schemaJsonFileContent,
|
||||||
|
}
|
||||||
|
|
||||||
|
assetJSON, err = json.Marshal(initSchema)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = ctx.GetStub().PutState(initSchema.Hash, assetJSON)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to put to world state. %v", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
assetJSON, err := json.Marshal(data)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = ctx.GetStub().PutState(data.ContentHash, assetJSON)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to put to world state. %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
//This is the definition of the Schema that we should use for validate all the JSON files from now on.
|
|
||||||
|
|
||||||
initSchema := Schema{
|
|
||||||
Version: 1,
|
|
||||||
Hash: schemaJsonFileHash,
|
|
||||||
JsonSchemaContent: schemaJsonFileContent,
|
|
||||||
}
|
|
||||||
|
|
||||||
assetJSON, err = json.Marshal(initSchema)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = ctx.GetStub().PutState(initSchema.Hash, assetJSON)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to put to world state. %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SmartContract) LastSchemaHash() (string) {
|
func (s *SmartContract) LastSchemaHash(ctx contractapi.TransactionContextInterface) (string) {
|
||||||
return lastSchemaHash
|
return lastSchemaHash
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SmartContract) Hash(doc string) (string, error) {
|
func (s *SmartContract) Hash(ctx contractapi.TransactionContextInterface, doc string) (string, error) {
|
||||||
result := s.isJSON(doc)
|
|
||||||
if !result{
|
var v interface{}
|
||||||
return "HASH CRASH", fmt.Errorf("String passed as parameter is not in JSON format")
|
err := json.Unmarshal([]byte(doc), &v)
|
||||||
} else{
|
if err != nil{
|
||||||
var v interface{}
|
return "HASH CRASH", fmt.Errorf("Unable to unmarshal Json String passed as parameter. No hash calculation can be completed: %v", err)
|
||||||
err := json.Unmarshal([]byte(doc), &v)
|
} else {
|
||||||
|
cdoc, err := json.Marshal(v)
|
||||||
if err != nil{
|
if err != nil{
|
||||||
return "HASH CRASH", fmt.Errorf("Unable to unmarshal Json String passed as parameter. No hash calculation can be completed: %v", err)
|
return "HASH CRASH", fmt.Errorf("Unable to re-marshal interface into json format. No hash calculation can be completed: %v", err)
|
||||||
} else {
|
} else{
|
||||||
cdoc, err := json.Marshal(v)
|
sum := sha256.Sum256(cdoc)
|
||||||
if err != nil{
|
return hex.EncodeToString(sum[0:]), nil
|
||||||
return "HASH CRASH", fmt.Errorf("Unable to re-marshal interface into json format. No hash calculation can be completed: %v", err)
|
|
||||||
} else{
|
|
||||||
sum := sha256.Sum256(cdoc)
|
|
||||||
return hex.EncodeToString(sum[0:]), nil
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (smct *SmartContract) isJSON(s string) bool {
|
|
||||||
var js interface{}
|
|
||||||
return json.Unmarshal([]byte(s), &js) == nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *SmartContract) JsonReader(ctx contractapi.TransactionContextInterface, content string) (map[string]interface{}, error) {
|
func (s *SmartContract) JsonReader(ctx contractapi.TransactionContextInterface, content string) (map[string]interface{}, error) {
|
||||||
|
|
||||||
var payload map[string]interface{}
|
var payload map[string]interface{}
|
||||||
IsJson := s.isJSON(content)
|
// Now let's unmarshall the data into `payload`
|
||||||
|
err := json.Unmarshal([]byte(content), &payload)
|
||||||
if !IsJson {
|
if err != nil {
|
||||||
return payload, log.Fatal("The String passed is not in JSON format. Check String", interface{})
|
log.Fatal("Error during Unmarshal() of string into type Interface: ", err)
|
||||||
} else {
|
|
||||||
// Now let's unmarshall the data into `payload`
|
|
||||||
err := json.Unmarshal([]byte(content), &payload)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal("Error during Unmarshal() of string into type Interface: ", err)
|
|
||||||
}
|
|
||||||
return payload, nil
|
|
||||||
}
|
}
|
||||||
|
return payload, nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetAllAssets returns all assets found in world state
|
// GetAllAssets returns all assets found in world state
|
||||||
|
|
@ -187,11 +178,21 @@ func (s *SmartContract) GetAllAssets(ctx contractapi.TransactionContextInterface
|
||||||
}
|
}
|
||||||
|
|
||||||
var data Data
|
var data Data
|
||||||
err = json.Unmarshal(queryResponse.Value, &data)
|
func PrintRandomDiv() {
|
||||||
|
defer func() {
|
||||||
|
if panicInfo := recover(); panicInfo != nil {
|
||||||
|
fmt.Printf("%v, %s", panicInfo, string(debug.Stack()))
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
err = json.Unmarshal(queryResponse.Value, &data)
|
||||||
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Fatal("Error during Unmarshal() of string into type Data: ", err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
dataSamples = append(dataSamples, &data)
|
dataSamples = append(dataSamples, &data)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return dataSamples, nil
|
return dataSamples, nil
|
||||||
|
|
@ -223,7 +224,7 @@ func (s *SmartContract) CreateNewSchema(ctx contractapi.TransactionContextInterf
|
||||||
return err
|
return err
|
||||||
} else {
|
} else {
|
||||||
// Verify that an schema with exact same structure doesn't exist yet.
|
// Verify that an schema with exact same structure doesn't exist yet.
|
||||||
hashContent, _ := s.Hash(newSchemaContent)
|
hashContent, _ := s.Hash(ctx, newSchemaContent)
|
||||||
exists, err := s.SchemaExists(ctx, hashContent)
|
exists, err := s.SchemaExists(ctx, hashContent)
|
||||||
if exists{
|
if exists{
|
||||||
return fmt.Errorf("Schema already exists: %v", err)
|
return fmt.Errorf("Schema already exists: %v", err)
|
||||||
|
|
@ -304,7 +305,7 @@ func (s *SmartContract) ValidJson(ctx contractapi.TransactionContextInterface, J
|
||||||
|
|
||||||
//m := schemas[len(schemas) - 1].JsonFileContent
|
//m := schemas[len(schemas) - 1].JsonFileContent
|
||||||
|
|
||||||
CurrentSchemaHash := s.LastSchemaHash()
|
CurrentSchemaHash := s.LastSchemaHash(ctx)
|
||||||
schema, _ := s.ReadSchema(ctx, CurrentSchemaHash)
|
schema, _ := s.ReadSchema(ctx, CurrentSchemaHash)
|
||||||
|
|
||||||
schemaLoader := gojsonschema.NewGoLoader(schema.JsonSchemaContent)
|
schemaLoader := gojsonschema.NewGoLoader(schema.JsonSchemaContent)
|
||||||
|
|
@ -331,7 +332,7 @@ func (s *SmartContract) ValidJson(ctx contractapi.TransactionContextInterface, J
|
||||||
func (s *SmartContract) CreateDataSample(ctx contractapi.TransactionContextInterface,
|
func (s *SmartContract) CreateDataSample(ctx contractapi.TransactionContextInterface,
|
||||||
Contributor string, ContributorId string, Id string, JsonFileContent string) error {
|
Contributor string, ContributorId string, Id string, JsonFileContent string) error {
|
||||||
|
|
||||||
ContentHash, err := s.Hash(JsonFileContent)
|
ContentHash, err := s.Hash(ctx, JsonFileContent)
|
||||||
exists, err := s.AssetExists(ctx, ContentHash)
|
exists, err := s.AssetExists(ctx, ContentHash)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
||||||
|
|
@ -8,3 +8,8 @@
|
||||||
"additionalProperties": false,
|
"additionalProperties": false,
|
||||||
"required": [ "number", "street_name"]
|
"required": [ "number", "street_name"]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
"{ \"bar\": { \"abc\": 12, \"baz\": true }, \"foo\": 12.3 "
|
||||||
|
|
||||||
|
|
||||||
|
"{\"type\": object, \"properties\": { \"number\": { \"type\": number }, \"street_name\": { \"type\": string }, \"street_type\": { \"enum\": [Street, Avenue,Boulevard] }}, \"additionalProperties\": false, \"required\": [ number, street_name]}"
|
||||||
|
|
@ -1 +1,3 @@
|
||||||
{ "number": 1600, "street_name": "Pennsylvania", "street_type": "Avenue" }
|
{ "number": 1600, "street_name": "Pennsylvania", "street_type": "Avenue" }
|
||||||
|
|
||||||
|
"{ \"number\": 1600, \"street_name\": Pennsylvania, \"street_type\": Avenue }"
|
||||||
|
|
@ -69,13 +69,25 @@ peer lifecycle chaincode querycommitted --channelID mychannel --name basic --caf
|
||||||
|
|
||||||
echo "========= CC Test of jsonRead Function ==========="
|
echo "========= CC Test of jsonRead Function ==========="
|
||||||
|
|
||||||
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":"JsonReader","Args":["/home/chaincode/schema.json"]}'
|
#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":"JsonReader","Args":["/home/chaincode/schema.json"]}'
|
||||||
|
|
||||||
|
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":"JsonReader","Args":["{ \"number\": 1600, \"street_name\": \"Pennsylvania\", \"street_type\": \"Avenue\" }"]}'
|
||||||
|
|
||||||
|
|
||||||
echo "========= CC Invoke: Inizialitation ==========="
|
echo "========= CC Invoke: Inizialitation ==========="
|
||||||
|
|
||||||
#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":"InitLedger","Args":[]}'
|
#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":"InitLedger","Args":[]}'
|
||||||
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":"InitLedger","Args":["/home/chaincode/schema.json", "/home/chaincode/firstJson.json"]}'
|
#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":"InitLedger","Args":["/home/chaincode/schema.json", "/home/chaincode/firstJson.json"]}'
|
||||||
|
|
||||||
|
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":"InitLedger","Args":[" {\"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\" }"]}'
|
||||||
|
|
||||||
|
echo "========= CC Invoke: Query Last Schema ==========="
|
||||||
|
|
||||||
|
peer chaincode query -C mychannel -n basic -c '{"Args":["LastSchemaHash"]}'
|
||||||
|
|
||||||
|
echo "========= CC Invoke: Query Hash Calculation ==========="
|
||||||
|
|
||||||
|
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":"Hash","Args":["{ \"number\": 1600, \"street_name\": Pennsylvania,\"street_type\": Avenue}"]}'
|
||||||
|
|
||||||
echo "========= CC Query: Get all DataSamples ==========="
|
echo "========= CC Query: Get all DataSamples ==========="
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue