2017-08-08 18 views
2

鏈碼基本上將數據存儲在鍵值對(STATE)中。正確的方法來維護鑰匙以存儲鏈碼中的值(STATE)。

如果我必須存儲學生數據,我必須傳遞鍵值,例如1001- {學生信息}。只需我可以創建任意數量的學生。

但問題是如果學生想動態註冊。要管理它,學生必須通過獨特的studentId或創建動態密鑰。

這是實施此的正確方法。

任何人都可以幫助我理解這個基本流程。

感謝

回答

3

,因爲你需要確保代言橫跨贊同同行一致的,它是更好的客戶端,例如該應用程序將生成學生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。

+0

謝謝阿爾喬姆尋求幫助。 但是,如果我必須編輯此學生的詳細信息,我需要id =「12345678」來檢索並更新它。要做到這一點我需要'身份證' 所以我應該在任何地方存儲這個ID。或者你想建議的其他方法? –

+0

如果您想從分類帳中檢索學生記錄,那麼您必須爲該學生提供唯一標識符。另一種方法是發送學生姓名和其他信息,並重復所有學生的記錄。您可以使用範圍查詢作爲示例。 –

+0

,所以我可以通過名字「John Doe」來檢索這個記錄。但可能有多名學生有姓名John Doe。 如何識別沒有ID的特定學生? –

相關問題