【问题标题】:Invalid asm.js: Invalid member of stdlib无效的 asm.js:stdlib 的无效成员
【发布时间】:2019-05-17 16:11:54
【问题描述】:

(node:7894) V8: /var/www/html/testeth/node_modules/solc/soljson.js:3 无效的 asm.js: 无效的 stdlib 成员

我正在 ganache-cli 简单合同上进行测试部署,但它显示了该警告。请帮助解决这个问题。

下面是“index.sol”的代码

pragma solidity ^0.4.17;

contract testalk{

string public message;

function testalk(string initialMsg) public {
    message = initialMsg;
}

function setMessage(string nwMsg) public {
    message = nwMsg;
}

}

我正在使用“mocha”和 ganache-cli 提供程序对其进行测试,如下代码:-

const assert = require('assert');
const ganache = require('ganache-cli');
const Web3 = require('web3');

const web3 = new Web3(ganache.provider());

const { interface, bytecode} = require('../compile');

require('events').EventEmitter.defaultMaxListeners = 15;

let accounts;
let testeth;

beforeEach( async ()=>{

accounts = await web3.eth.getAccounts();

testeth = await new web3.eth.Contract(JSON.parse(interface))
                .deploy({data: bytecode, arguments: ['Hi Alok!']})
                .send({gas: '1000000',from: accounts['0']});

});

describe("testalk",() => {
it('deploy a contract', () =>{
    assert.ok(testeth.options.address);
});

it('get the message', async () => {
    const message = await testeth.methods.message().call();
    assert.equal('Hi Alok!', message);
    //console.log(message);
})

it('get the message', async () => {
    await testeth.methods.setMessage("Bye Alok!").send({from: accounts[0], gas: '1000000'});

    const message = await testeth.methods.message().call();
    console.log(message);

});

});

我正在使用 Ubuntu 和 nodejs。

【问题讨论】:

标签: solidity


【解决方案1】:

我建议您选择较新版本的 solc 编译器(例如,检查 Remix 以查看哪个版本适用于您的代码)。检查您的 Solidity 代码的pragma 语句中的版本是否与您使用 node 安装的 solc 版本相同。检查 solc 版本中的示例用法并复制 JS 代码。我用的是 0.7.4 https://libraries.io/npm/solc/0.7.4

之后,您需要调整编译脚本以将 ABI 和字节码返回到您的测试中。名称必须完全匹配。在这里,我以 JSON 格式返回值,因此我不必在测试文件中使用 JSON.parse(interface)。请注意,字节码只是 HEX 值,因此我返回 contract.evm.bytecode.object。通过您的合同名称更改 Lottery(如果您有多个合同,您想查看文档或尝试 for 循环)。

const path = require('path');
const fs = require('fs');
const solc = require('solc');

const lotteryPath = path.resolve(__dirname, 'contracts', 'Lottery.sol');
const source = fs.readFileSync(lotteryPath, 'utf8')

var input = {
    language: 'Solidity',
    sources: {
      'test.sol': {
        content: source
      }
    },
    settings: {
      outputSelection: {
        '*': {
          '*': ['*']
        }
      }
    }
  };
  
var output = JSON.parse(solc.compile(JSON.stringify(input)));
var contract = output.contracts['test.sol'].Lottery;
var bytecode = contract.evm.bytecode.object;
var interface = contract.abi;
module.exports = {interface, bytecode};

我的测试文件没有太大变化,但它是:

const assert = require('assert');
const ganache = require('ganache-cli');
const Web3 = require('web3');

const web3 = new Web3(ganache.provider());
const { interface, bytecode } = require('../compile');

let lottery;
let accounts;

beforeEach(async () => {
    accounts = await web3.eth.getAccounts();
    lottery = await new web3.eth.Contract(interface)
        .deploy({ data: bytecode })
        .send({ from: accounts[0], gas: '6000000' });
});

describe('Lottery Contract', () => {
    it('deploys a contract', () => {
        assert.ok(lottery.options.address);
    });
});

【讨论】:

    【解决方案2】:

    我通过删除当前目录中的“node-modules”文件夹,然后从终端重新安装模块,解决了 stdlib 的无效成员和“部署合同”问题的“before each”钩子:

    npm install --save ganache-cli mocha web3@1.0.0-beta.37 solc@0.4.17

    如果您的计算机上安装了 anaconda,您应该在安装这些模块之前使用 conda deactivate 命令。我在互联网上找不到解决方案,希望这也能帮助您解决问题...

    【讨论】:

      【解决方案3】:

      发生这种情况的原因有很多。如果您使用的是 remix ide,那么默认情况下 asm 在 chrome 中被禁用,这可能是一个问题。您可能会以某种不兼容的方式使用旧的 Solidity 编译器。您的代码本身可能存在问题。除非您提供代码的所有细节,您使用的环境(如 IDE 和操作系统等),否则任何人都只能猜测最大值。

      【讨论】:

        猜你喜欢
        • 2022-01-18
        • 2019-10-07
        • 2014-03-24
        • 2021-07-18
        • 2021-05-06
        • 2021-04-28
        • 2021-08-29
        • 2020-09-12
        • 2020-07-28
        相关资源
        最近更新 更多