Invalid opcode on deploying

Hi everyone:
hope you can help me.
i’m now developing smart contracts using truffle (5.0.7) and i just join to a project and i have an assignment to upgrade truffle and ganache-cli.
i just check all the requirements that solidity demands but right now i have a strange error:
“BitsUtilities” hit an invalid opcode while deploying. Try:

  • Verifying that your constructor params satisfy all assert conditions.
  • Verifying your constructor code doesn’t access an array out of bounds.
  • Adding reason strings to your assert statements.

    at /usr/lib/node_modules/truffle/build/webpack:/packages/truffle-deployer/src/deployment.js:364:1
    at
    at process._tickCallback (internal/process/next_tick.js:189:7)
    Truffle v5.0.7 (core: 5.0.7)
    Node v8.15.1

Any idea of the reason? it is not saying any reason. i was expecting some overflow error but i already test the library without anything and still have this error.

Library code:

pragma solidity >=0.4.22 <0.6.0;

library BitsUtilities {
function and(byte a, byte b) public pure returns (byte) {
return a & b;
}

function or(bytes1 a, bytes1 b) public pure returns (byte) {
return a | b;
}

function xor(bytes1 a, bytes1 b) public pure returns (byte) {
return a ^ b;
}

function negate(byte a) public pure returns (byte) {
return (~a);
}

function shiftLeft(byte a, uint8 n) public pure returns (byte) {
uint8 a1 = uint8(a);
uint8 shifted = a1 << n;
return toByte (shifted);
}

function shiftRight(byte a, uint8 n) public pure returns (byte) {
uint8 shifted = uint8(a) >> n;
return byte(shifted);
}

function getFirstN(byte a, uint8 n) public pure returns (byte) {
byte nOnes = byte(power(2, n) - 1);
byte mask = shiftLeft(nOnes, 8 - n); // Total 8 bits
return a & mask;
}

function getLastN(byte a, uint8 n) public pure returns (byte) {
uint8 lastN = uint8(a) % power(2,n);
return byte(lastN);
}

// Get bit value at position
function getBit(byte a, uint8 n) public pure returns (bool) {
return a & shiftLeft(0x01, n) != 0;
}

// Set bit value at position
function setBit(byte a, uint8 n) public pure returns (byte) {
return a | shiftLeft(0x01, n);
}

// Set the bit into state “false”
function clearBit(byte a, uint8 n) public pure returns (byte) {
bytes1 mask = negate(shiftLeft(0x01, n));
return a & mask;
}

function power(uint8 base, uint8 exponent) public pure returns (uint8) {
return base ** exponent;
}

function toByte (uint8 decimalValue) public pure returns (byte b) {
require(decimalValue <= 255);
return byte (decimalValue);
}

}

i’m thinking that it could be around migraton file, i just saw that this error was related to a migration file related to that library, this is the related migration file:

var Licens3dFiles = artifacts.require("Licens3dFiles");

var Licens3dPermissions = artifacts.require(“Licens3dPermissions”);
var Licens3dReader = artifacts.require(“Licens3dReader”);
var Licens3dStorage = artifacts.require(“Licens3dStorage”);
var BitsUtilities = artifacts.require("./BitsUtilities.sol");

var config = require(’…/…/Web/config/config’);
var Web3 = require(‘web3’);
var fs = require(‘fs’);
var web3 = new Web3(new Web3.providers.HttpProvider(config.ethereum.host));

var writeToFile = function (filesContract = null, permissionsContract = null, readerContract = null){
var contractsContent = {};

if (filesContract !== null) {
    contractsContent.files = {address: filesContract.address, abi: filesContract.abi};
}

if (permissionsContract !== null) {
    contractsContent.permissions = {address: permissionsContract.address, abi: permissionsContract.abi};
}

if (readerContract !== null) {
    contractsContent.reader = {address: readerContract.address, abi: readerContract.abi};
}

fs.writeFile("../contracts.json", JSON.stringify(contractsContent), function(err) {
    if(err) return console.log(err);
});

};

var logGasUsage = async function(tx) {
if (tx !== undefined) {
let receipt = await web3.eth.getTransactionReceipt(tx.transactionHash);
if (receipt) {
console.log(" Gas Usage: " + receipt.gasUsed);
}
}
};

module.exports = async function(deployer) {
writeToFile("","","");

deployer.deploy(BitsUtilities);
deployer.link(BitsUtilities, Licens3dFiles);
deployer.link(BitsUtilities, Licens3dPermissions);
deployer.link(BitsUtilities, Licens3dReader);

var StorageInstance;

return deployer.deploy(Licens3dStorage).then(async function(tx){
    await logGasUsage(tx);
    return deployer.deploy(Licens3dFiles, Licens3dStorage.address).then(async function(tx){
        await logGasUsage(tx);
        return deployer.deploy(Licens3dPermissions, Licens3dStorage.address).then(async function(tx){
            await logGasUsage(tx);
            return deployer.deploy(Licens3dReader, Licens3dStorage.address).then(async function(tx){
                await logGasUsage(tx);
                return  Licens3dStorage.deployed().then(function(instance) {
                    StorageInstance = instance;
                    return StorageInstance.addWriter(Licens3dFiles.address).then(function(value) {
                        return StorageInstance.addWriter(Licens3dPermissions.address).then(function(value) {
                            writeToFile(Licens3dFiles, Licens3dPermissions, Licens3dReader);
                        });
                    });
                });
            });
        });
    });
});

};

Hello! Just to be clear on whats going on: What is your goal? What are you trying to do? When you mention “assignment to upgrade truffle and ganache-cli” what do you mean exactly? Are you trying to create upgradable smart contracts using ZeppelinOS or are you trying to actually upgrade from an earlier version of truffle (4.0 for example) to 5.0 and you need to refactor your code? Are you using ZeppelinOS?

FYI: We have a company retreat the rest of this week, so we might be slow in responding the next couple days. :slight_smile:

2 Likes

Hi @panicape, I’m moving this to #support since #support:guides-and-tutorials is meant for actual tutorials. We might have to reorganize a bit our categories to make it even more clear :slight_smile:

1 Like

Hello @panicape, welcome to the forum!

The code of your library seems fine at a first glance, and shoulnd’t be giving you any issues. Your deployment script, on the other hand, is rather complex and unwieldy: have you tried removing unrelated pieces of it to only deploy the contract that is throwing that error, to check if you still run into the same issue?

Well, in fact they were using truffle 4.1.18 and the idea is to change to last version and the thing is that i have got this error and i’m so lost with this because it doesn’t mean anything to me because it doesn’t say anything related.
Anfter this error i only left the library empty to see what it the problem and still have the same issue.
What can it cause this error?? i don’t know what to check…

So you had an empty migrations script and still got this error? That doesn’t sound right - the error must come from one of your deployments. Are you sure no other contract is being deployed anywhere else?

@panicape were you able to overcome this?