doushang4293 2017-03-08 08:38
浏览 61
已采纳

通过价值获取世界状态

I'm new to blockchain and working with hyperledger fabric (v:0.6 for now) to create an application for learning perspective.

I'm keeping a ledger of financial transactions on blockchain, soon as a transaction takes place (A web based component notifies on transaction occurrence and calls the chaincode).

The structure of transactions looks something like this:

type Transactions struct { 
    ReferenceNumber string `json:"ReferenceNumber"`
    BillNumber string `json:"BillNumber"`
    BillingCompany string `json:"BillingCompany"`
    Amount string `json:"Amount"`
    Status string `json:"Status"`
}

I json marshal this and save it to state with ReferenceNumber as the key.

Now I can get the transaction from state on the basis of ReferenceNumber. But what if I want to get the transaction from state on the basis of let's say 'Status' like how many transactions on the ledger have status as 'reconciled'.

Is there any way to query state not on the basis of key but value?

  • 写回答

2条回答 默认 最新

  • doujia4759 2017-03-08 12:26
    关注

    Worldstate level storage works at the {key,value} level. And as obvious its only intended for a single value lookup for a specified key. I think what you are looking for calls for a next level higher level of Abstraction of WorldState - called Table constructs. fabric/examples/chaincode/go/asset_management_interactive/asset_management.go has an example on how to create a table with the columns you want. While defining the primary keys of your data structure to hold the transaction, you include Status as one of the keys and you would be able to retrieve data on the basis of Status as well.

    Some sample code to create the table is as below

    func createTableTwo(stub shim.ChaincodeStubInterface) error {
        var columnDefsTableTwo []*shim.ColumnDefinition
        columnOneTableTwoDef := shim.ColumnDefinition{Name: "colOneTableTwo",
            Type: shim.ColumnDefinition_STRING, Key: true}
        columnTwoTableTwoDef := shim.ColumnDefinition{Name: "colTwoTableTwo",
            Type: shim.ColumnDefinition_INT32, Key: false}
        columnThreeTableTwoDef := shim.ColumnDefinition{Name: "colThreeTableThree",
            Type: shim.ColumnDefinition_INT32, Key: true}
        columnFourTableTwoDef := shim.ColumnDefinition{Name: "colFourTableFour",
            Type: shim.ColumnDefinition_STRING, Key: true}
        columnDefsTableTwo = append(columnDefsTableTwo, &columnOneTableTwoDef)
        columnDefsTableTwo = append(columnDefsTableTwo, &columnTwoTableTwoDef)
        columnDefsTableTwo = append(columnDefsTableTwo, &columnThreeTableTwoDef)
        columnDefsTableTwo = append(columnDefsTableTwo, &columnFourTableTwoDef)
        return stub.CreateTable("tableTwo", columnDefsTableTwo)
    }
    

    Now to insert data into this table, as shown

    if len(args) < 4 {
                return nil, errors.New("insertRowTableTwo failed. Must include 4 column values")
            }
    
            col1Val := args[0]
            col2Int, err := strconv.ParseInt(args[1], 10, 32)
            if err != nil {
                return nil, errors.New("insertRowTableTwo failed. arg[1] must be convertable to int32")
            }
            col2Val := int32(col2Int)
            col3Int, err := strconv.ParseInt(args[2], 10, 32)
            if err != nil {
                return nil, errors.New("insertRowTableTwo failed. arg[2] must be convertable to int32")
            }
            col3Val := int32(col3Int)
            col4Val := args[3]
    
            var columns []*shim.Column
            col1 := shim.Column{Value: &shim.Column_String_{String_: col1Val}}
            col2 := shim.Column{Value: &shim.Column_Int32{Int32: col2Val}}
            col3 := shim.Column{Value: &shim.Column_Int32{Int32: col3Val}}
            col4 := shim.Column{Value: &shim.Column_String_{String_: col4Val}}
            columns = append(columns, &col1)
            columns = append(columns, &col2)
            columns = append(columns, &col3)
            columns = append(columns, &col4)
    
            row := shim.Row{Columns: columns}
            ok, err := stub.InsertRow("tableTwo", row)
            if err != nil {
                return nil, fmt.Errorf("insertRowTableTwo operation failed. %s", err)
            }
            if !ok {
                return nil, errors.New("insertRowTableTwo operation failed. Row with given key already exists")
            }
    

    Now to query this data by not specifying all the keys, do as below

    if len(args) < 1 {
                return nil, errors.New("getRowsTableTwo failed. Must include at least key values")
            }
    
            var columns []shim.Column
    
            col1Val := args[0]
            col1 := shim.Column{Value: &shim.Column_String_{String_: col1Val}}
            columns = append(columns, col1)
    
            if len(args) > 1 {
                col2Int, err := strconv.ParseInt(args[1], 10, 32)
                if err != nil {
                    return nil, errors.New("getRowsTableTwo failed. arg[1] must be convertable to int32")
                }
                col2Val := int32(col2Int)
                col2 := shim.Column{Value: &shim.Column_Int32{Int32: col2Val}}
                columns = append(columns, col2)
            }
    
            rowChannel, err := stub.GetRows("tableTwo", columns)
            if err != nil {
                return nil, fmt.Errorf("getRowsTableTwo operation failed. %s", err)
            }
    
            var rows []shim.Row
            for {
                select {
                case row, ok := <-rowChannel:
                    if !ok {
                        rowChannel = nil
                    } else {
                        rows = append(rows, row)
                    }
                }
                if rowChannel == nil {
                    break
                }
            }
    
            jsonRows, err := json.Marshal(rows)
            if err != nil {
                return nil, fmt.Errorf("getRowsTableTwo operation failed. Error marshaling JSON: %s", err)
            }
    
            return jsonRows, nil
    

    Once you have inserted the data, the API stub.GetRows("tableTwo", columns) lets you retrieve it without specifying all the key columns.

    Above code is quoted from a file which was present in the Fabric github repo earlier in the following path gerrit/src/github.com/hyperledger/fabric/bddtests/chaincode/go/table/table.go

    Hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案