-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontract.go
129 lines (94 loc) · 3.15 KB
/
contract.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
import (
"encoding/json"
"fmt"
"github.com/hyperledger/fabric-contract-api-go/contractapi"
)
type SmartContract struct {
contractapi.Contract
}
// I think this is "Create or Update" function
func (s *SmartContract) CreateData(ctx contractapi.TransactionContextInterface, id string, data string, metadataJson string) error {
var metadata Metadata
if err := json.Unmarshal([]byte(metadataJson), &metadata); err != nil {
return fmt.Errorf("invalid metadata format: %s", err.Error())
}
trainingData := TrainingData{
ID: id,
Data: data,
Metadata: metadata,
}
if err := validateTrainingData(trainingData); err != nil {
return err
}
trainingDataAsBytes, _ := json.Marshal(trainingData)
return ctx.GetStub().PutState(id, trainingDataAsBytes)
}
func (s *SmartContract) QueryData(ctx contractapi.TransactionContextInterface, id string) (*TrainingData, error) {
dataAsBytes, err := ctx.GetStub().GetState(id)
if err != nil {
return nil, fmt.Errorf("failed to read from world state. %s", err.Error())
}
if dataAsBytes == nil {
return nil, fmt.Errorf("%s does not exist", id)
}
trainingData := new(TrainingData)
_ = json.Unmarshal(dataAsBytes, trainingData)
return trainingData, nil
}
func (s *SmartContract) QueryMetadata(ctx contractapi.TransactionContextInterface, id string) (*Metadata, error) {
dataAsBytes, err := ctx.GetStub().GetState(id)
if err != nil {
return nil, fmt.Errorf("failed to read from world state. %s", err.Error())
}
if dataAsBytes == nil {
return nil, fmt.Errorf("%s does not exist", id)
}
trainingData := new(TrainingData)
_ = json.Unmarshal(dataAsBytes, trainingData)
return &trainingData.Metadata, nil
}
func (s *SmartContract) QueryAllMetadatas(ctx contractapi.TransactionContextInterface) ([]QueryResult, error) {
resultsIterator, err := ctx.GetStub().GetStateByRange("", "")
if err != nil {
return nil, err
}
defer resultsIterator.Close()
results := []QueryResult{}
for resultsIterator.HasNext() {
queryResponse, err := resultsIterator.Next()
if err != nil {
return nil, err
}
trainingData := new(TrainingData)
_ = json.Unmarshal(queryResponse.Value, trainingData)
queryResult := QueryResult{ID: trainingData.ID, Metadata: &trainingData.Metadata}
results = append(results, queryResult)
}
return results, nil
}
func (s *SmartContract) ChangeMetadata(ctx contractapi.TransactionContextInterface, id string, metadataJson string) error {
trainingData, err := s.QueryData(ctx, id)
if err != nil {
return err
}
var metadata Metadata
if err := json.Unmarshal([]byte(metadataJson), &metadata); err != nil {
return fmt.Errorf("invalid metadata format: %s", err.Error())
}
if err := validateMetadata(metadata); err != nil {
return err
}
trainingData.Metadata = metadata
dataAsBytes, _ := json.Marshal(trainingData)
return ctx.GetStub().PutState(id, dataAsBytes)
}
func (s *SmartContract) ChangeData(ctx contractapi.TransactionContextInterface, id string, data string) error {
trainingData, err := s.QueryData(ctx, id)
if err != nil {
return err
}
trainingData.Data = data
dataAsBytes, _ := json.Marshal(trainingData)
return ctx.GetStub().PutState(id, dataAsBytes)
}