Hyperledger Fabric:有没有办法阻止组织成员访问通道上链码内的特定智能合约?

Hyperledger Fabric: is there a way to prevent a member of an organization to access a specific smart contract within a chaincode on a channel?

假设我在同一个频道上有两个组织,A 和 B,以及一个包含这些方法的链代码:

如何限制对单个方法的访问,例如,A 的成员只能访问 create、update 和 queryA; B 的成员只能访问 submitNewData 和 queryB。因此,A 的成员可以创建资产并修改字段子集(使用“update”),B 的成员只能修改另一个字段子集(根据“submitNewData”)而不能创建资产。

如果 B 的节点执行“节点链代码调用”来创建或查询 A,则访问被拒绝。

我应该使用 ACL 吗?但是如何在链码中引用具体的智能合约呢?

你开始谈论“成员”,但后来你谈论“同行”。

您不能限制每个点的操作。安装了链码并加入通道的每个节点都必须以相同的方式进行,以便链码以确定的方式工作。

但是,您当然可以通过评估其用户 ID、MSP ID、证书属性或您想要的任何内容来限制请求者的操作。例如,在 Go 链代码中,它通常在 BeforeTransaction 函数中求值:


type SmartContract struct {
    contractapi.Contract
}

func checkACL(ctx contractapi.TransactionContextInterface) error {
    // Read incoming data from stub
    stub := ctx.GetStub()

    // Extract operation name
    operation, parameters := stub.GetFunctionAndParameters()
    operationSplitted := strings.Split(operation, ":")
    operationName := operationSplitted[len(operationSplitted)-1]

    // Get requestor info from stub
    mspID, err := cid.GetMSPID(stub)
    userID, err := cid.GetID(stub)
    value, found, err := cid.GetAttributeValue(stub, "role")

    // Evaluate your ACLs by contrasting the operation and the requestor your own way
    // ...

    // Return error when disallowed

    // Operation allowed
    return nil
}

func BeforeTransaction(ctx contractapi.TransactionContextInterface) error {
    return checkACL(ctx)
}

func NewSmartContract() *SmartContract {
    sc := new(SmartContract)
    sc.BeforeTransaction = BeforeTransaction
    // ...
    return sc
}