安装链码后尝试查询状态数据库时出现Hyperledger错误 [英] Hyperledger error when trying to query the state database after installing the chaincode

查看:62
本文介绍了安装链码后尝试查询状态数据库时出现Hyperledger错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这只是我的chaincod包的一部分,但是我使用功能 ListDoctorPermissions 来查询资产后的状态数据库,在我的情况下是具有以下结构的权限:

This is just an extract of my chaincod package but I use the function ListDoctorPermissions to query the state database after an asset, which in my case is a permission with the struct of:

type Permission struct {
    PermissionId    string `json:"permissionId"`
    DataCategory    string `json:"dataCategory"`
    PatientId       string `json:"patientId"`
    DoctorId        string `json:"doctorId"`
    Right           string `json:"right"`
    From            string `json:"from"`
    To              string `json:"to"`
}

在部署链码的过程中,我在最后运行查询以测试链码,但出现错误:

In the process of deploying the chaincode I run the query at the end to test the chaincode but I get an error:

Error: endorsement failure during query. response: status:500 message:"Error handling success response. Value did not match schema:\n1. return: Invalid type. Expected: array, given: string" 

这是我调用链码的方式:

This is how I invoke the chaincode:

peer chaincode query -C mychannel -n permissions -c '{"Args":["ListDoctorPermissions", "doctor1"]}'

在运行查询之前,我在状态数据库中创建一个带有DoctorId为"doctor1"的权限.因此在数据库(couchDB)中找到CompositeKey和条目应该没有问题.

Before running the query I create a permission in the state database with the doctorId of "doctor1" so there shouldn't be a problem finding the compositeKey and entry in the database(couchDB).

// List all Permissions given to a doctor
func (s *SmartContract) ListDoctorPermissions(ctx contractapi.TransactionContextInterface, doctorId string) ([]byte, error) {
    doctorIterator, err := ctx.GetStub().GetStateByPartialCompositeKey("permissionId", []string{doctorId})
    if err != nil {
        return nil, err
    }
    fmt.Printf("the doctor Iterator is: %s", doctorIterator)

    defer doctorIterator.Close()

    var dataCategory string
    var patientId string
    var permissionId string

    var permissions []byte
    bArrayPermissionAlreadyWritten := false

    for doctorIterator.HasNext() {
        responseRange, err := doctorIterator.Next()
        if err != nil {
            return nil, err
        }

        objectType, compositeKeyParts, err := ctx.GetStub().SplitCompositeKey(responseRange.Key)
        if err != nil {
            return nil, err
        }
        fmt.Printf("the objectType is: %s", objectType)

        dataCategory = compositeKeyParts[1]
        patientId = compositeKeyParts[2]
        permissionId = doctorId + dataCategory + patientId
        fmt.Printf("the compositeKeyParts are: %s", compositeKeyParts[0], compositeKeyParts[1], compositeKeyParts[2])

        permissionAsBytes, err := ctx.GetStub().GetState(permissionId)
        if err != nil {
            return nil, err
        }

        if bArrayPermissionAlreadyWritten == true {
            newBytes := append([]byte(","), permissionAsBytes...)
            permissions = append(permissions, newBytes...)
        } else {
            permissions = append(permissions, permissionAsBytes...)
            fmt.Print(permissions)
        }

        fmt.Printf("Found a asset for index : %s asset id : ", objectType, compositeKeyParts[0], compositeKeyParts[1], compositeKeyParts[2])
        bArrayPermissionAlreadyWritten = true

    }

    permissions = append(permissions, []byte("]")...)
    fmt.Print(permissions)
    return permissions, nil
}

推荐答案

我已经运行了上面提供的代码,您的评论正确的是,该问题似乎源于使用 [] byte作为您的返回类型.我不完全确定为什么会发生这种情况,因为那应该是有效的返回类型.我可能会发现它是一个错误.

I have had a run of the code supplied above and you are correct in your comment that the issue appears to stem from the use of []byte as your return type. I'm not entirely sure why this happened as that should be a valid return type. I will look further into this as it may be a bug.

通过将返回类型转换为 string 并返回 string(permissions)

I was able to get the data returned correctly by converting the return type to string and returning string(permissions)

在执行此操作时,我注意到您的代码正在基于getState字节构建字节数组.因此,另一个选择是将get状态调用中​​的字节 json.Unmarshal 放入 Permission 结构中,并将其附加到数组中.然后,您可以将返回类型设为 [] Permission .

I noticed whilst doing that, that your code is building a byte array based on the getState bytes. Therefore another option is to json.Unmarshal the bytes from the get state call into a Permission struct and append this to an array. You can then have the return type as []Permission.

我现在建议您采用字符串或Permission数组方法,因为我将不得不研究使用 [] byte 作为返回类型的情况.

I would recomend for now you take either the string or Permission array approach as I will have to look into what is happening with using []byte as a return type.

// List all Permissions given to a doctor
func (s *SmartContract) ListDoctorPermissions(ctx contractapi.TransactionContextInterface, doctorId string) (string, error) {
    doctorIterator, err := ctx.GetStub().GetStateByPartialCompositeKey("permissionId", []string{doctorId})
    if err != nil {
        return "", err
    }
    fmt.Printf("the doctor Iterator is: %s", doctorIterator)

    defer doctorIterator.Close()

    var dataCategory string
    var patientId string
    var permissionId string

    var permissions []byte
    bArrayPermissionAlreadyWritten := false

    for doctorIterator.HasNext() {
        responseRange, err := doctorIterator.Next()
        if err != nil {
            return "", err
        }

        objectType, compositeKeyParts, err := ctx.GetStub().SplitCompositeKey(responseRange.Key)
        if err != nil {
            return "", err
        }
        fmt.Printf("the objectType is: %s", objectType)

        dataCategory = compositeKeyParts[1]
        patientId = compositeKeyParts[2]
        permissionId = doctorId + dataCategory + patientId
        fmt.Printf("the compositeKeyParts are: %s", compositeKeyParts[0], compositeKeyParts[1], compositeKeyParts[2])

        permissionAsBytes, err := ctx.GetStub().GetState(permissionId)
        if err != nil {
            return "", err
        }

        if bArrayPermissionAlreadyWritten == true {
            newBytes := append([]byte(","), permissionAsBytes...)
            permissions = append(permissions, newBytes...)
        } else {
            permissions = append(permissions, permissionAsBytes...)
            fmt.Print(permissions)
        }

        fmt.Printf("Found a asset for index : %s asset id : ", objectType, compositeKeyParts[0], compositeKeyParts[1], compositeKeyParts[2])
        bArrayPermissionAlreadyWritten = true

    }

    permissions = append(permissions, []byte("]")...)
    fmt.Print(permissions)
    return string(permissions), nil
}

[]权限:

// List all Permissions given to a doctor
func (s *SmartContract) ListDoctorPermissions(ctx contractapi.TransactionContextInterface, doctorId string) ([]Permission, error) {
    doctorIterator, err := ctx.GetStub().GetStateByPartialCompositeKey("permissionId", []string{doctorId})
    if err != nil {
        return nil, err
    }
    fmt.Printf("the doctor Iterator is: %s", doctorIterator)

    defer doctorIterator.Close()

    var dataCategory string
    var patientId string
    var permissionId string

    var permissions []Permission

    for doctorIterator.HasNext() {
        responseRange, err := doctorIterator.Next()
        if err != nil {
            return nil, err
        }

        objectType, compositeKeyParts, err := ctx.GetStub().SplitCompositeKey(responseRange.Key)
        if err != nil {
            return nil, err
        }
        fmt.Printf("the objectType is: %s", objectType)

        dataCategory = compositeKeyParts[1]
        patientId = compositeKeyParts[2]
        permissionId = doctorId + dataCategory + patientId
        fmt.Printf("the compositeKeyParts are: %s", compositeKeyParts[0], compositeKeyParts[1], compositeKeyParts[2])

        permissionAsBytes, err := ctx.GetStub().GetState(permissionId)
        if err != nil {
            return nil, err
        }

        foundPermission := new(Permission)

        err = json.Unmarshal(permissionAsBytes, foundPermission)

        if err != nil {
            return nil, err
        }

        permissions = append(permissions, *foundPermission)
    }

    fmt.Printf("Permissions %v", permissions)

    return permissions, nil
}

这篇关于安装链码后尝试查询状态数据库时出现Hyperledger错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆