mirror of
https://github.com/hyperledger/fabric-samples.git
synced 2026-06-23 01:55:10 +00:00
Advance Nov 11/2022
This commit is contained in:
parent
5e3e50fd37
commit
8f946586bd
2 changed files with 52 additions and 38 deletions
|
|
@ -65,8 +65,7 @@ type Schema struct {
|
|||
// InitLedger adds a base set of Data entries to the ledger
|
||||
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 into
|
||||
// the docker container of the commited chaincode (For now)
|
||||
// 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)
|
||||
firstJsonFileContent, error_file := s.JsonReader(ctx, InitData)
|
||||
|
||||
|
|
@ -163,7 +162,8 @@ func (s *SmartContract) JsonReader(ctx contractapi.TransactionContextInterface,
|
|||
}
|
||||
|
||||
// GetAllAssets returns all assets found in world state
|
||||
/*func (s *SmartContract) GetAllAssets(ctx contractapi.TransactionContextInterface) ([]*Data, error) {
|
||||
|
||||
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("", "")
|
||||
|
|
@ -179,27 +179,23 @@ func (s *SmartContract) JsonReader(ctx contractapi.TransactionContextInterface,
|
|||
return nil, err
|
||||
}
|
||||
|
||||
var data Data
|
||||
func PrintRandomDiv() {
|
||||
defer func() {
|
||||
if panicInfo := recover(); panicInfo != nil {
|
||||
fmt.Printf("%v, %s", panicInfo, string(debug.Stack()))
|
||||
}
|
||||
}()
|
||||
err = json.Unmarshal(queryResponse.Value, &data)
|
||||
}
|
||||
|
||||
var data map[string]interface{}
|
||||
err = json.Unmarshal(queryResponse.Value, &data)
|
||||
if err != nil {
|
||||
log.Fatal("Error during Unmarshal() of string into type Data: ", err)
|
||||
return nil, err
|
||||
} else if _, ok := data["Id"]; ok {
|
||||
var dataSruct Data
|
||||
err = json.Unmarshal(queryResponse.Value, &dataSruct)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
dataSamples = append(dataSamples, &dataSruct)
|
||||
}
|
||||
}
|
||||
dataSamples = append(dataSamples, &data)
|
||||
|
||||
}
|
||||
|
||||
return dataSamples, nil
|
||||
}
|
||||
*/
|
||||
|
||||
func (s *SmartContract) SchemaExists(ctx contractapi.TransactionContextInterface, Hash string) (bool, error) {
|
||||
assetJSON, err := ctx.GetStub().GetState(Hash)
|
||||
|
|
@ -284,13 +280,21 @@ func (s *SmartContract) CreateNewSchema(ctx contractapi.TransactionContextInterf
|
|||
//}
|
||||
|
||||
// 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)
|
||||
func (s *SmartContract) AssetExists(ctx contractapi.TransactionContextInterface, Hash string) (bool, error) {
|
||||
assetJSON, err := ctx.GetStub().GetState(Hash)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("failed to read from world state: %v", err)
|
||||
return false, fmt.Errorf("Failed to read from world state. Asset dosen't exist: %v", err)
|
||||
} else {
|
||||
var data map[string]interface{}
|
||||
err2 := json.Unmarshal(assetJSON, &data)
|
||||
if err2 != nil {
|
||||
return false, fmt.Errorf("failed to read from world state: %v", err2)
|
||||
} else if _, ok := data["Id"]; ok {
|
||||
return assetJSON != nil, nil
|
||||
} else {
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
|
||||
return assetJSON != nil, nil
|
||||
}
|
||||
|
||||
// JSON Validation
|
||||
|
|
@ -334,7 +338,7 @@ func (s *SmartContract) ValidJson(ctx contractapi.TransactionContextInterface, J
|
|||
func (s *SmartContract) CreateDataSample(ctx contractapi.TransactionContextInterface,
|
||||
Contributor string, ContributorId string, Id string, Owner string, JsonFileContent string) error {
|
||||
|
||||
ContentHash, err := s.Hash(ctx, JsonFileContent)
|
||||
ContentHash, _ := s.Hash(ctx, JsonFileContent)
|
||||
exists, err := s.AssetExists(ctx, ContentHash)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
@ -375,8 +379,8 @@ func (s *SmartContract) CreateDataSample(ctx contractapi.TransactionContextInter
|
|||
}
|
||||
|
||||
// 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 {
|
||||
/*func (s *SmartContract) UpdateAsset(ctx contractapi.TransactionContextInterface,
|
||||
Contributor string, ContributorId string, Id string, Owner string) error {
|
||||
exists, err := s.AssetExists(ctx, Id)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
@ -392,7 +396,7 @@ func (s *SmartContract) UpdateAsset(ctx contractapi.TransactionContextInterface,
|
|||
ContributorId: ContributorId,
|
||||
ContentHash: ContentHash,
|
||||
Id: Id,
|
||||
Owners: []string{"DOE", "DOS", "DOJ"},
|
||||
Owner: Owner,
|
||||
}
|
||||
|
||||
assetJSON, err := json.Marshal(data)
|
||||
|
|
@ -402,6 +406,7 @@ func (s *SmartContract) UpdateAsset(ctx contractapi.TransactionContextInterface,
|
|||
|
||||
return ctx.GetStub().PutState(Id, assetJSON)
|
||||
}
|
||||
*/
|
||||
|
||||
func (s *SmartContract) DeleteAsset(ctx contractapi.TransactionContextInterface, Id string) error {
|
||||
exists, err := s.AssetExists(ctx, Id)
|
||||
|
|
@ -452,25 +457,25 @@ func (s *SmartContract) ReadSchema(ctx contractapi.TransactionContextInterface,
|
|||
}
|
||||
|
||||
// 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) {
|
||||
func (s *SmartContract) TransferAsset(ctx contractapi.TransactionContextInterface, Id string, newOwner string) (string, error) {
|
||||
data, err := s.ReadAsset(ctx, Id)
|
||||
if err != nil {
|
||||
return []string{}, err
|
||||
return "Read Asset function failed excecution", err
|
||||
}
|
||||
|
||||
data.Owners = newOwners
|
||||
data.Owner = newOwner
|
||||
|
||||
assetJSON, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return []string{}, err
|
||||
return "Marshal of Data not one", err
|
||||
}
|
||||
|
||||
err = ctx.GetStub().PutState(Id, assetJSON)
|
||||
if err != nil {
|
||||
return []string{}, err
|
||||
return "Unable to update asset", err
|
||||
}
|
||||
|
||||
return data.Owners, nil
|
||||
return data.Owner, nil
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
# This script is used to quckly set up a TestNetwork and test ChainCode in ../../asset-transfer-basic/chaincode-go/chaincode/smartcontract.go
|
||||
|
||||
echo "========= Killing Previous network and Setting up New One ==========="
|
||||
docker rm -f $(docker ps -q)
|
||||
#docker rm -f $(docker ps -q)
|
||||
./network.sh down
|
||||
./network.sh up createChannel
|
||||
echo "========= Channel created and successfully joined ==========="
|
||||
|
|
@ -69,13 +69,20 @@ peer lifecycle chaincode querycommitted --channelID mychannel --name basic --caf
|
|||
|
||||
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":["{ \"number\": 1600, \"street_name\": \"Pennsylvania\", \"street_type\": \"Avenue\" }"]}'
|
||||
|
||||
|
||||
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":["/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 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\"}"]}'
|
||||
|
||||
|
||||
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":["{\"type\": \"object\", \"properties\": { \"number\": { \"type\": \"number\" }, \"street_name\": { \"type\": \"string\" }, \"street_type\": { \"enum\": [\"Street\", \"Avenue\",\"Boulevard\"] }}, \"required\": [ \"number\", \"street_name\"], \"additionalProperties\": false}"]}'
|
||||
|
||||
|
||||
echo "========= CC Query: Get all DataSamples ==========="
|
||||
|
||||
|
|
@ -100,14 +107,16 @@ peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.exa
|
|||
|
||||
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"]}'
|
||||
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":["juanitoAlimania@blades.com", "Contributor123", "00001", "SDSC", "/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"]}'
|
||||
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":["juanitoAlimania@blades.com", "Contributor123", "00002", "SDSC", "{ \"number\": 1603, \"street_name\": \"Pennsylvania\", \"street_type\": \"Avenue\"}"]}'
|
||||
|
||||
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":["juanitoAlimania@blades.com", "Contributor123", "00002", "SDSC", "{ \"number\": 1600, \"street_name\": \"Pennsylvania\", \"street_type\": \"Avenue\" }"]}'
|
||||
|
||||
echo "========= The code below will upgrade an existing ChainCode ==========="
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue