链码基本上将数据存储在键值对(STATE)中。
如果我必须存储学生数据,我必须传递键值,例如 1001-{学生信息}。简单地说,我可以创建任意数量的学生。
但问题是如果学生想动态注册。要管理它,学生必须通过唯一的 studentId 或必须创建动态密钥。?
哪个是实现这个的正确方法。?
谁能帮我理解这个基本流程。?
谢谢
链码基本上将数据存储在键值对(STATE)中。
如果我必须存储学生数据,我必须传递键值,例如 1001-{学生信息}。简单地说,我可以创建任意数量的学生。
但问题是如果学生想动态注册。要管理它,学生必须通过唯一的 studentId 或必须创建动态密钥。?
哪个是实现这个的正确方法。?
谁能帮我理解这个基本流程。?
谢谢
由于您需要确保背书在背书节点之间保持一致,因此最好让客户端(例如应用程序)生成学生 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)
}
现在,一旦安装了链码,您就可以尝试使用 peer 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 更方便。