本文介绍了Hyperledger Fabric Chaincode抛出MVCC_READ_CONFLICT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

调用链码函数时出现错误.我为该函数创建了两种改编.一个使用常规密钥,另一个使用组合密钥.我以为使用复合键可以解决任何MVCC_READ_CONFLICT,因为我不再更新相同的键.

I'm getting an error when I invoke a chaincode function. I've created two adaptations for the function. One uses a regular key, the other a composite key. I thought that using a composite key would solve any MVCC_READ_CONFLICT's since I'm no longer updating the same key.

但是我在两个函数上都收到错误消息.请注意,两个函数都包含在相同的链码中.我不知道这是否会引起冲突.

However I get the error on both functions. Note that both function are contained in the same chaincode. I don't know if that can cause conflicts.

这是带有常规键的功能:

Here's the function with a regular key:

    func (*AddTokenCallFunction) Start(stub shim.ChaincodeStubInterface, args []string) pb.Response {
        if len(args) != 2 {
            s := fmt.Sprintf(ERROR_INCORRECT_AMOUNT_OF_ARGUMENTS, "add-tokens", 2, len(args))
            return shim.Error(s)
        }

        account := args[0]
        tokens := args[1]

        currentTokensBytes, err := stub.GetState(account)
        if err != nil {
            s := fmt.Sprintf(ERROR_SYSTEM, err.Error())
            return shim.Error(s)
        }
        currentAmountOfTokens := binary.LittleEndian.Uint64(currentTokensBytes)
        tokensToAdd, err := strconv.ParseUint(tokens, 10, 64)
        if err != nil {
            s := fmt.Sprintf(ERROR_SYSTEM, err.Error())
            return shim.Error(s)
        }
        currentAmountOfTokens += tokensToAdd
        tokenBytes, err := UintToBytes(currentAmountOfTokens)
        if err != nil {
            s := fmt.Sprintf(ERROR_SYSTEM, err.Error())
            return shim.Error(s)
        }
        err = stub.PutState(account, tokenBytes)
        if err != nil {
            s := fmt.Sprintf(ERROR_SYSTEM, err.Error())
            return shim.Error(s)
        }

        return shim.Success(nil)
    }

具有相同的功能,但带有复合键:

Here's the same function but with a composite-key:

func (*AddTokenCompositeCallFunction) Start(stub shim.ChaincodeStubInterface, args []string) pb.Response {
    if len(args) != 2 {
        s := fmt.Sprintf(ERROR_INCORRECT_AMOUNT_OF_ARGUMENTS, "add-composite-tokens", 2, len(args))
        return shim.Error(s)
    }

    account := args[0]
    tokens := args[1]

    // Retrieve info needed for the update procedure
    txid := stub.GetTxID()
    compositeIndexaccount := "account~tokens~txID"

    // Create the composite key that will allow us to query for all deltas on a particular variable
    compositeKey, compositeErr := stub.CreateCompositeKey(compositeIndexaccount, []string{account, tokens, txid})
    if compositeErr != nil {
        return shim.Error(fmt.Sprintf("Could not create a composite key for %s: %s", account, compositeErr.Error()))
    }

    // Save the composite key index
    compositePutErr := stub.PutState(compositeKey, []byte{0x00})
    if compositePutErr != nil {
        return shim.Error(fmt.Sprintf("Could not put operation for %s in the ledger: %s", account, compositePutErr.Error()))
    }

    return shim.Success([]byte(fmt.Sprintf("Successfully added %s to %s", tokens, account)))
}

有人可以解释为什么我在以后的实现中仍然得到MVCC_READ_CONFLICT吗?我究竟做错了什么?我正在进行基准测试并多次发送相同的accountID.尽管我的印象是,使用复合键并没有关系.

Could someone explain why I'm still getting a MVCC_READ_CONFLICT on the later implementation? What am I doing wrong? I'm benchmarking and sending the same accountID several times. Though I was under the impression that this would not matter when using a composite-key.

谢谢.

推荐答案

我解决了此问题,方法是删除自己的实现,并用高吞吐量示例[ https://github.com/hyperledger/fabric-samples/blob/release/high -throughput/chaincode/high-throughput.go ].

I resolved this issue by removing my own implementation and replacing it with the one from the high-throughput sample [ https://github.com/hyperledger/fabric-samples/blob/release/high-throughput/chaincode/high-throughput.go ].

我的猜测是我在实现中做了Golang不认同的事情.由于实现方式没有什么不同.

My guess is that I'm doing something in my implementation that Golang does not agree with. Since the implementations are not that different.

这篇关于Hyperledger Fabric Chaincode抛出MVCC_READ_CONFLICT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 20:54