2017-08-08 13 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'が必要です どこでもこのIDを保存する必要がありますか?または、あなたが提案したい他のアプローチですか? –

+0

元帳から生徒記録を取得したい場合は、その生徒に一意の識別子を提供する必要があります。別のアプローチは、学生の名前と別の情報を送信し、すべての生徒の記録を繰り返すことです。例として、範囲照会を使用することができます。 –

+0

だから私はこのレコードを "John Doe"という名前で取り出すことができます。 John Doeという名前の学生が複数いる可能性があります。 IDなしで特定の生徒を特定するにはどうすればよいですか? –

関連する問題