简体   繁体   English

松露测试因“无效的操作码”而失败

[英]Truffle test failed with "invalid opcode"

I am writing a unit test for my smart contract using truffle and when I run the test using "truffle test" the test failed with this error "Error: Returned error: VM Exception while processing transaction: invalid opcode".我正在使用 truffle 为我的智能合约编写单元测试,当我使用“truffle test”运行测试时,测试失败并出现此错误“错误:返回错误:处理事务时 VM 异常:操作码无效”。

smart contract code:智能合约代码:

pragma solidity ^0.4.3;

contract owned {
    address public owner;

    /* Initialise contract creator as owner */
    function owned() public {
        owner = msg.sender;
    }

    /* Function to dictate that only the designated owner can call a function */
      modifier onlyOwner {
        require(owner == msg.sender);
        _;
    }

    /* Transfer ownership of this contract to someone else */
    function transferOwnership(address newOwner) public onlyOwner() {
        owner = newOwner;
    }
}

/*
 * @title AsnScRegistry
 *  Open Vote Network
 *  A self-talling protocol that supports voter privacy.
 *
 *  Author: Shahrul Sharudin
 */
contract AsnScRegistry is owned{
  struct IPAddress {
    uint128 ip;
    uint8 mask;
    }

  struct MemberAddresses {
    address contractAddress;
    address walletAddress;
    uint index;
  }

  mapping (uint => IPAddress[]) managedIps;
  mapping (uint => MemberAddresses) ethAddresses;

  uint[] private registeredAsn;

  function getManagedIpByAsn(uint _asn) view public returns (uint128[] _ip, uint8[] _mask){
    IPAddress[] memory addresses = managedIps[_asn];
    uint128[] memory ips = new uint128[](addresses.length);
    uint8[] memory mask = new uint8[](addresses.length);

    for (uint i = 0; i < addresses.length; i++) {
            ips[i] = addresses[i].ip;
            mask[i] = addresses[i].mask;
        }
    return (ips, mask);
  }

  function getMemberAddressesByAsn(uint _asn) view public returns (address _contractAddress, address _walletAddress){
    MemberAddresses memory memberAddresses = ethAddresses[_asn];
    return (memberAddresses.contractAddress, memberAddresses.walletAddress);
  }

  function addMember(uint _asn, uint128[] _ip, uint8[] _mask, address _contractAddress, address _walletAddress) public {
    MemberAddresses memory member = ethAddresses[_asn];
    //throw error if member already exist
    assert(member.contractAddress != 0);
    for(uint i=0; i<_ip.length; i++){
      managedIps[_asn].push(IPAddress({ip:_ip[i], mask:_mask[i]}));
    }
    uint idx = registeredAsn.push(_asn)-1;
    ethAddresses[_asn] = MemberAddresses({contractAddress:_contractAddress, walletAddress:_walletAddress, index:idx});
  }

  function removeMember(uint _asn) public returns (uint[] _registeredAsn){
    //uint memory index = ethAddresses[_asn].index;

    if (ethAddresses[_asn].index >= registeredAsn.length) return;

    for (uint i = ethAddresses[_asn].index; i<registeredAsn.length - 1; i++){
      registeredAsn[i] = registeredAsn[i+1];
    }
        registeredAsn.length--;
        delete managedIps[_asn];
        delete ethAddresses[_asn];

        return registeredAsn;
  }

  function getTotalMembers() view public returns (uint _totalMembers) {
    return (registeredAsn.length);
  }

  function getRegisteredAsn() view public returns (uint[] _asn){
    return (registeredAsn);
  }

}

javascript unit test: javascript 单元测试:

const AsnScRegistry = artifacts.require('./AsnScRegistry.sol')
const assert = require('assert')

const ip = [3524503734,3232235776];
const mask = [255,255];
const contractAddress = '0x1234567890123456789012345678901234567891';
const walletAddress = '0x1234567890123456789012345678901234567891';
const asn = 123;

let registryInstance;

contract("AsnScRegistry", accounts => {
  it("Should add a new member", () =>
    AsnScRegistry.deployed()
    .then(instance => {
      registryInstance = instance;
      return instance.addMember(asn, ip, mask, contractAddress, walletAddress);
    })
    .then(() => registryInstance.getTotalMembers())
    .then(total => assert.equal(total.toNumber(), 1, "xxxxx"))
  );
});

the full source is available here: https://github.com/shaza4061/electioncommissioner完整的源代码在这里: https://github.com/shaza4061/electioncommissioner

I expect the addMember() function to store the information in the smart contract and getTotalMembers() to return the number of record in registeredAsn[] array and the assertion in the unit test to pass.我希望 addMember() function 将信息存储在智能合约中, getTotalMembers() 返回registeredAsn []数组中的记录数和单元测试中的断言通过。

the following error message "Error: Returned error: VM Exception while processing transaction: invalid opcode" indicates a failed assert statement in solidity.以下错误消息“错误:返回的错误:处理事务时的 VM 异常:无效的操作码”表明断言语句失败。

In your code, you have an assert which causes this error.在您的代码中,您有一个导致此错误的断言

 //throw error if member already exist
    assert(member.contractAddress != 0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 松露JS测试不起作用 - Truffle JS test not working 使用简单的Solidity合同和脚本生成无效的操作码错误 - invalid opcode error with a simple Solidity contract and script 如何创建/添加帐户到松露测试环境 - How to create/add an account to truffle test environment 松露 - 有没有办法自动生成测试用例? - Truffle - Is There Any Way to Auto Generate Test Cases? truffle &amp; solidity 在测试中无法访问 function - truffle & solidity cant access to a function in test 尝试访问 Hardhat 中的 Solidity 结构数组时出现无效操作码错误 - Error invalid opcode trying to access Solidity array of structs in Hardhat 如何为整个测试套件松露创建合同的新实例 - How to create new instance of contract for whole test suite truffle 如何为javascript / truffle中的每个测试创建新的以太坊/实体合约 - how to create new ethereum/solidity contract for each test in javascript/truffle 如何使用松露测试代码牢固地读取公共变量? - How to read public variable in solidity with truffle test codes? Solidity &amp; Truffle - 在 Solidity 和 JavaScript 中自动生成测试用例,最好的选择是什么? 利弊? - Solidity & Truffle - Automatic test case generate in Solidity and JavaScript, What is the best option? Props and Cons?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM