0

I am trying to compile solidity code in solc(nodejs) but unsuccessful so far. I have created a sample app for demo. Here is sample's directory structure (node.js and truffle combined):

│   app.js
│   compile.js
│   deploy.js
│   routes.js
│   truffle-config.js
│
├───build
│   └───contracts
│           A.json
│           Migrations.json
│           U.json
│
├───contracts
│       A.sol
│       Migrations.sol
│       U.sol
│
├───migrations
│       1_initial_migration.js
│       2_deploy.js
│
└───test

Here is the link to source: https://drive.google.com/file/d/1jeBRM_F0PfAqBZy0JWK3gKv53M9mxdcC/view?usp=drivesdk

npm packages:

npm i express path fs solc ethereumjs-tx web3-utils web3@0.20.7 morgan

It works in truffle both getTotalCount() and add() i.e.,

truffle(development)> let a = await A.deployed()
undefined
truffle(development)> a.getTotalCount()
<BN: 0>
truffle(development)> a.add(12)
{ tx:
   '0xfa53d1aac439dbdfea4b004410ebca35275a5f4377b54f64f9ab52388a08e0bf',
  receipt:
   { transactionHash:
      '0xfa53d1aac439dbdfea4b004410ebca35275a5f4377b54f64f9ab52388a08e0bf',
     transactionIndex: 0,
     blockHash:
      '0xe743d66e3cb5bf15ac741e9a1efd869249c2eed52ab63f8efc6233b35661fc1b',
     blockNumber: 164,
     from: '0x07df0e5ab4793197007a867d97a3aeeea5347456',
     to: '0x8b421f5c74b48e0f6ae763702754ce172deeb293',
     gasUsed: 47006,
     cumulativeGasUsed: 47006,
     contractAddress: null,
     logs: [],
     status: true,
     logsBloom:
      '0x00...//intentional',
     v: '0x1c',
     r:
      '0xd2ac433f565e69e3587d07ebd3fe00a14204add708a47cfbc6406d2c092061f2',
     s:
      '0x7f7f6a80f6d67199b88cc2cde924a0c2c39421aadb29201661891bf03b346219',
     rawLogs: [] },
  logs: [] }
truffle(development)> a.getTotalCount()
<BN: 1>
truffle(development)>

but when type localhost:34000/d in postman i get:

GET /d 200 4051.218 ms - 12
Error: VM Exception while processing transaction: invalid opcode
    at Object.InvalidResponse (D:\assignments\blockchain\assign\assignments\blockchain\src\anarik-4\anarik-v5.0\src_3\anarik-v5.0.2\test\smartContract\solc\node_modules\web3\lib\web3\errors.js:38:16)
    at D:\assignments\blockchain\assign\assignments\blockchain\src\anarik-4\anarik-v5.0\src_3\anarik-v5.0.2\test\smartContract\solc\node_modules\web3\lib\web3\requestmanager.js:86:36
    at XMLHttpRequest.request.onreadystatechange (D:\assignments\blockchain\assign\assignments\blockchain\src\anarik-4\anarik-v5.0\src_3\anarik-v5.0.2\test\smartContract\solc\node_modules\web3\lib\web3\httpprovider.js:129:7)
    at XMLHttpRequestEventTarget.dispatchEvent (D:\assignments\blockchain\assign\assignments\blockchain\src\anarik-4\anarik-v5.0\src_3\anarik-v5.0.2\test\smartContract\solc\node_modules\xhr2-cookies\dist\xml-http-request-event-target.js:34:22)
    at XMLHttpRequest._setReadyState (D:\assignments\blockchain\assign\assignments\blockchain\src\anarik-4\anarik-v5.0\src_3\anarik-v5.0.2\test\smartContract\solc\node_modules\xhr2-cookies\dist\xml-http-request.js:208:14)
    at XMLHttpRequest._onHttpResponseEnd (D:\assignments\blockchain\assign\assignments\blockchain\src\anarik-4\anarik-v5.0\src_3\anarik-v5.0.2\test\smartContract\solc\node_modules\xhr2-cookies\dist\xml-http-request.js:318:14)
    at IncomingMessage.<anonymous> (D:\assignments\blockchain\assign\assignments\blockchain\src\anarik-4\anarik-v5.0\src_3\anarik-v5.0.2\test\smartContract\solc\node_modules\xhr2-cookies\dist\xml-http-request.js:289:61)
    at IncomingMessage.emit (events.js:187:15)
    at endReadableNT (_stream_readable.js:1094:12)
    at process._tickCallback (internal/process/next_tick.js:63:19) null
_http_outgoing.js:470
    throw new ERR_HTTP_HEADERS_SENT('set');
    ^

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at ServerResponse.setHeader (_http_outgoing.js:470:11)

Apps' versions:

> truffle version
Truffle v5.0.2 (core: 5.0.2)
Solidity - 0.5.2 (solc-js)
Node v10.14.2

solc npm package version:

  "_id": "solc@0.5.7",

Ganache: v2.0.0-beta.2

Its been days that I am trying to solve this problem but can't. All methods in my contract give invalid opcode error when compiled via solc, but in truffle it seems to work fine, and I can't identify source of the problem.

  • I'd suggest first try updating ganache. What do calls do you make when /d invoked? What do you do in your contract functions look like? It is better if you reduce your example to the minimal code that causes the error. – Ismael Apr 20 at 6:53
  • I am suspicious of the solc version difference but more suspicious of the postman testing methodology. We can see that it works just fine when the request is well-formed with truffle. While I don't dive into RPC every day it just doesn't seem like a complete message with a sender and a function signature - localhost:34000/d. What about the rest of it? Have a look at this for possible hints: ethereum.stackexchange.com/questions/3514/… – Rob Hitchens Apr 20 at 22:04
1

New opcodes were introduced in Constantinople/St. Petersburg hard fork. Solidity compiler was able to produce such opcodes since 0.4.24, but this feature became enabled by default only in 0.5.5. Ganache Core supports this opcodes since version 2.5.1, but Ganache 2.0.0-beta.2 is based on Ganache Core 2.3.3. That's why you contract failed to work.

0

After updating Ganache to latest version(v2.0.1) it worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.