gpt4 book ai didi

hyperledger - 在链代码中维护键以存储值(状态)的正确方法。?

转载 作者:行者123 更新时间:2023-12-04 03:04:01 25 4
gpt4 key购买 nike

链码基本上将数据存储在键值对(STATE)中。

如果我必须存储学生数据,我必须传递键值,例如1001-{学生信息}。简单地说,我可以创建任意数量的学生。

但问题是如果学生想动态注册。要管理它,学生必须传递唯一的 studentId 或必须创建动态 key 。?

哪个是实现它的正确方法?

谁能帮我理解这个基本流程。?

谢谢

最佳答案

由于您需要确保背书在背书同行之间是一致的,因此最好是客户,例如该应用程序将生成学生 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就方便多了。

关于hyperledger - 在链代码中维护键以存储值(状态)的正确方法。?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45562918/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com