微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

处理事务时获取 VM 异常:还原错误

如何解决处理事务时获取 VM 异常:还原错误

我是区块链的新手。我想调用一个函数来从合同中读取一个变量。 我的合同

bool public isVoting = false;
function getIsVoting() public returns (bool) {
        return isVoting;
    }

在客户端,我这样称呼

const isVoting = async () => {
        const _isVoting = await ElectionInstance.methods
            .getIsVoting()
            .call()
            .then(console.log);
    };

然后我收到错误但不知道为什么:

{
  "message": "VM Exception while processing transaction: revert","code": -32000,"data": {
    "0xdbe5e039374fdc83fe873f5e55d91f05ec5d19e2e3c88351130c3f3672644e08": {
      "error": "revert","program_counter": 130,"return": "0x"
    },"stack": "RuntimeError: VM Exception while processing transaction: revert\n    at Function.RuntimeError.fromresults (/tmp/.mount_ganachnMw5dG/resources/static/node/node_modules/ganache-core/lib/utils/runtimeerror.js:94:13)\n    at /tmp/.mount_ganachnMw5dG/resources/static/node/node_modules/ganache-core/lib/blockchain_double.js:568:26","name": "RuntimeError"
  }
}

你能帮帮我吗?我是新来的。

解决方法

web3 不同,Truffle 不使用 sorted_test_dict = dict(sorted(test_dict.items(),key=lambda kv: kv[1]['fruit'])) 关键字。调用失败,因为它试图访问合同的(不存在的)methods 属性。

如果您想使用链接的 methods,您应该使用方法名称作为属性(不带括号)。

您还结合使用了 call() 和回调函数 await,这可能会导致一些意外结果(但不会恢复事务)。

有关详细信息,请参阅 docs

或者,您可以将 Solidity 函数标记为 then,因为它不会将任何数据写入存储。这也将允许您将其用作 view 函数,而无需在 Truffle 中使用 ElectionInstance.getIsVoting()

.call()
function getIsVoting() public view returns (bool) { // added `view`
    return isVoting;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。