Javascript
簡單範常式式碼中的錯誤?
我正在玩松露,我在這裡有一個簡單的程序,我似乎無法開始工作……
contract MyContract { struct Test { bool testBool; } mapping (address => Test[]) public tests; function issueTest(address _owner) public { Test memory test; test.testBool = false; tests[_owner].push(test); } function setTestBool(address _owner, uint _index) public returns (bool sucess) { tests[_owner][_index].testBool = true; return tests[_owner][_index].testBool; } function getTestBool(address _owner, uint _index) public returns (bool testBool) { return tests[_owner][_index].testBool; } }
在我的測試文件中,我有:
let myContract; var reciever = web3.eth.accounts[1]; before(async() => { myContract = await MyContract.deployed(); }); it("Test Bool", async() => { await myContract.issueTest.call(reciever); var sucess = await myContract.setTestBool.call(reciever, 0); assert.equal(sucess, true, "Test Bool was set."); var result = await myContract.getTestBool.call(reciever, 0); assert.equal(result, true, "Bool is true."); });
當我做松露測試時,我得到這個錯誤:
Error: Error: VM Exception while executing eth_call: invalid opcode
我認為問題出在 .call() 上。我不明白什麼電話做得很好。
任何幫助都會很棒謝謝!
如果您收到無效的操作碼錯誤,您很可能嘗試通過或處於不一致的狀態訪問 EVM。例如
getTestBool
,使用尚未設置的地址呼叫issueTest
.至於測試,對於松露,我認為使用 Promises 鏈會更乾淨。
var MyContract = artifacts.require("./MyContract.sol"); contract('MyContract', function(accounts) { var mainAccount = accounts[0]; var nobody = accounts[1]; var contract; it("Test Bool", function() { // Build the contract ... return MyContract.deployed().then(function(instance) { contract = instance; // ... then issue the test ... return contract.issueTest(mainAccount, {from: mainAccount}); }).then(function() { // ... then set the bool ... return contract.setTestBool.call(mainAccount, 0, {from: mainAccount}); }).then(function(result) { // ... then assert it was set. assert.equal(true, result, "Did not set testBool to true"); return contract.setTestBool.call(mainAccount, 0, {from: mainAccount}); // no need for from because call does not change EVM state return contract.getTestBool.call(mainAccount, 0); }).then(function(result) { assert.equal(true, result, "testBool failed"); }); }); });