あなたは承認ピアを支持全体で一貫していることを確認する必要があるので、それは例えば、より良いそのクライアントのアプリケーションは学生IDを生成し、それをチェーンコードに渡します。今
package main
import (
"encoding/json"
"fmt"
"github.com/hyperledger/fabric/core/chaincode/shim"
"github.com/hyperledger/fabric/protos/peer"
)
// Student
type Person struct {
ID string `json:"id"`
Name string `json:"name"`
Faculty string `json:"faculty"`
Address string `json:"address"`
}
// StudentAction
type StudentAction func(params []string, stub shim.ChaincodeStubInterface) peer.Response
// studentManagement the chaincode interface implementation to manage
// the ledger of person records
type studentManagement struct {
actions map[string]StudentAction
}
// Init initialize chaincode with mapping between actions and real methods
func (pm *studentManagement) Init(stub shim.ChaincodeStubInterface) peer.Response {
pm.actions = map[string]StudentAction{
"addStudent": pm.AddStudent,
}
fmt.Println("Chaincode has been initialized")
fmt.Println("Following actions are available")
for action := range pm.actions {
fmt.Printf("\t\t%s\n", action)
}
return shim.Success(nil)
}
// Invoke handles chaincode invocation logic, executes actual code
// for given action name
func (pm *studentManagement) Invoke(stub shim.ChaincodeStubInterface) peer.Response {
actionName, params := stub.GetFunctionAndParameters()
if action, ok := pm.actions[actionName]; ok {
return action(params, stub)
}
return shim.Error(fmt.Sprintf("No <%s> action defined", actionName))
}
// AddStudent inserts new person into ledger
func (pm *personManagement) AddStudent(params []string, stub shim.ChaincodeStubInterface) peer.Response {
jsonObj := params[0]
var student Student
// Read person info into struct
json.Unmarshal([]byte(jsonObj), &student)
// Make uniqueness check
val, err := stub.GetState(student.ID)
if err != nil {
fmt.Printf("[ERROR] cannot get state, because of %s\n", err)
return shim.Error(fmt.Sprintf("%s", err))
}
if val != nil {
errMsg := fmt.Sprintf("[ERROR] student already exists, cannot create two accounts with same ID <%d>", student.ID)
fmt.Println(errMsg)
return shim.Error(errMsg)
}
fmt.Println("Adding new student", person)
if err = stub.PutState(student.ID, []byte(jsonObj)); err != nil {
errMsg := fmt.Sprintf("[ERROR] cannot store student record with id <%d>, due to %s", student.ID, err)
fmt.Println(errMsg)
return shim.Error(errMsg)
}
return shim.Success(nil)
}
とchaincodeがインストールされたら、あなたは道を以下にピアのCLIコマンドを使用して、新しい学生を追加してみてくださいすることができます。これらの線に沿って何か行くには、おそらく良い方法
peer chaincode invoke -o localhost:7050 \
-n students1 -v 1.0 -C exampleChannel \
-c '{"Args": ["addStudent”, "{ \"id\": \”12345678\”, \"Name\": \”John Doe\”, \”Faculty\”: \”Mathematics\”, \"address\": \”MIT\”}”]}’
もちろん、それははるかに便利でありますSDK。
ありがとうございました。 しかし、この学生の詳細を編集する必要がある場合は、それを取得して更新するためにid = "12345678"が必要です。これを行うには私は 'id'が必要です どこでもこのIDを保存する必要がありますか?または、あなたが提案したい他のアプローチですか? –
元帳から生徒記録を取得したい場合は、その生徒に一意の識別子を提供する必要があります。別のアプローチは、学生の名前と別の情報を送信し、すべての生徒の記録を繰り返すことです。例として、範囲照会を使用することができます。 –
だから私はこのレコードを "John Doe"という名前で取り出すことができます。 John Doeという名前の学生が複数いる可能性があります。 IDなしで特定の生徒を特定するにはどうすればよいですか? –