有些交易有效,有些则无效。处理事务时出现 UnhandledPromiseRejectionWarning 和 VM 异常

Posted

技术标签:

【中文标题】有些交易有效,有些则无效。处理事务时出现 UnhandledPromiseRejectionWarning 和 VM 异常【英文标题】:Some transactions work and some don't. UnhandledPromiseRejectionWarning and VM Exception while processing transaction 【发布时间】:2019-10-11 16:26:52 【问题描述】:

我想通过node.js和web3.js调用某个Solidity的合约方法,但是不行,我想知道为什么。

我正在关注这两个教程:https://cryptozombies.io/en/lesson/6/chapter/9 和 http://demystifyblockchain.com/2018/06/29/getting-started-with-ethereum-blockchain-development/

我在我的项目中使用 truffle、ganache-cli、node.js、web3.js 和solidity。

该方法工作了一段时间,但由于某种未知原因不再有效。

我使用 node 来调用这个 javascript 方法:

   async function main() 
    let mainAccount = await getMainAccount();
    const counter = new web3.eth.Contract(abi, contractAddress);
    counter.methods.setCooldownTime(mainAccount).send(from: mainAccount)
    .on('receipt', function (receipt) 
        if (receipt.status) 
            let events = Object.keys(receipt.events);
            if (events.length > 0) 
            let event = receipt.events[events[0]];
            console.log("event Name  ", event.event);
            let returnValues = event.returnValues;
            let returnedCount = returnValues.count;
            console.log("count ", returnedCount);
        
        
    )
    .on('error', console.error);

它应该在 Solidity 中触发这段代码:

uint cooldownTime = 36 hours;
function setCooldownTime() public
    cooldownTime = 1 hours;

这给了我一个我不知道如何修复的错误:

Error: Node error: "message":"VM Exception while processing transaction: revert","code":-32000,"data":"0xbf9f7df4bb490662840d3f5f3294cf96a00ba22ec3271c0eb1145989589f70c0":"error":"revert","program_counter":278,"return":"0x","stack":"o: VM Exception while processing transaction: revert\n    at Function.o.fromResults (/home/user/.nvm/versions/node/v12.3.0/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:10:81931)\n    at /home/user/.nvm/versions/node/v12.3.0/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:47:121973\n    at /home/user/.nvm/versions/node/v12.3.0/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:47:102384\n    at p (/home/user/.nvm/versions/node/v12.3.0/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:47:102041)\n    at /home/user/.nvm/versions/node/v12.3.0/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:47:102094\n    at t.default (/home/user/.nvm/versions/node/v12.3.0/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:52:497368)\n    at /home/user/.nvm/versions/node/v12.3.0/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:47:103008\n    at w.n.emit (/home/user/.nvm/versions/node/v12.3.0/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:61:1211583)\n    at /home/user/.nvm/versions/node/v12.3.0/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:61:1850382\n    at /home/user/.nvm/versions/node/v12.3.0/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:61:1850405\n    at /home/user/.nvm/versions/node/v12.3.0/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:61:500011\n    at /home/user/.nvm/versions/node/v12.3.0/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:2:26124\n    at i (/home/user/.nvm/versions/node/v12.3.0/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:2:41179)\n    at /home/user/.nvm/versions/node/v12.3.0/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:2:21647\n    at processTicksAndRejections (internal/process/task_queues.js:82:9)","name":"o"
    at Function.validate (/mnt/c/Users/Sloth/Solidity/firstBlockchainApp/node_modules/web3-providers/dist/web3-providers.cjs.js:114:18)
    at HttpProvider._callee$ (/mnt/c/Users/Sloth/Solidity/firstBlockchainApp/node_modules/web3-providers/dist/web3-providers.cjs.js:710:61)
    at tryCatch (/mnt/c/Users/Sloth/Solidity/firstBlockchainApp/node_modules/regenerator-runtime/runtime.js:45:40)
    at Generator.invoke [as _invoke] (/mnt/c/Users/Sloth/Solidity/firstBlockchainApp/node_modules/regenerator-runtime/runtime.js:271:22)
    at Generator.prototype.<computed> [as next] (/mnt/c/Users/Sloth/Solidity/firstBlockchainApp/node_modules/regenerator-runtime/runtime.js:97:21)
    at asyncGeneratorStep (/mnt/c/Users/Sloth/Solidity/firstBlockchainApp/node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:24)
    at _next (/mnt/c/Users/Sloth/Solidity/firstBlockchainApp/node_modules/@babel/runtime/helpers/asyncToGenerator.js:25:9)
    at processTicksAndRejections (internal/process/task_queues.js:89:5) false 0

当我像前面提到的教程那样写作时:

counter.methods.setCooldownTime()... 

代替:

counter.methods.setCooldownTime(mainAccount)...

它也会给我一个错误:

(node:5006) UnhandledPromiseRejectionWarning: Error: types/values length mismatch (count="types":1,"values":0, value="types":["name":"newOwner","type":"address"],"values":[], version=4.0.28)
    at Object.throwError (/mnt/c/Users/Sloth/Solidity/firstBlockchainApp/node_modules/ethers/errors.js:76:17)
    at AbiCoder.encode (/mnt/c/Users/Sloth/Solidity/firstBlockchainApp/node_modules/ethers/utils/abi-coder.js:922:20)
    at AbiCoder.encodeParameters (/mnt/c/Users/Sloth/Solidity/firstBlockchainApp/node_modules/web3-eth-abi/dist/web3-eth-abi.cjs.js:45:34)
    at MethodEncoder.encode (/mnt/c/Users/Sloth/Solidity/firstBlockchainApp/node_modules/web3-eth-contract/dist/web3-eth-contract.cjs.js:143:45)
    at MethodsProxy.createMethod (/mnt/c/Users/Sloth/Solidity/firstBlockchainApp/node_modules/web3-eth-contract/dist/web3-eth-contract.cjs.js:556:57)
    at MethodsProxy.executeMethod (/mnt/c/Users/Sloth/Solidity/firstBlockchainApp/node_modules/web3-eth-contract/dist/web3-eth-contract.cjs.js:534:23)
    at Function.ContractMethod.send (/mnt/c/Users/Sloth/Solidity/firstBlockchainApp/node_modules/web3-eth-contract/dist/web3-eth-contract.cjs.js:507:27)
    at main (/mnt/c/Users/Sloth/Solidity/firstBlockchainApp/interaction/interaction.js:20:39)
    at processTicksAndRejections (internal/process/task_queues.js:89:5)

【问题讨论】:

【参考方案1】:

问题出在我的 JavaScript 代码的前 4 行。由于未知原因,abi 中的数据被弄乱了。该问题可以通过手动写入 abi 数据来解决。

const Web3 = require('web3');
const web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
const metadata = require('../tokens/slavetoken.sol');
const abi = metadata.abi;

【讨论】:

以上是关于有些交易有效,有些则无效。处理事务时出现 UnhandledPromiseRejectionWarning 和 VM 异常的主要内容,如果未能解决你的问题,请参考以下文章

C++ 将向量传递给重载类 - 有些调用语句有效,有些则无效。

插入文件时出现 Azure Data Lake 错误:访问控制列表值无效

mysql事务处理失效原因

Xaml 产生路径错误,但有些仍然有效

解决win8.1安装qq时出现安装路径无效的有效方法

处理交易时出现虚拟机异常:气体不足