Solidity
Solc 錯誤操作碼無效,但適用於松露
我正在嘗試在 solc(nodejs) 中編譯 Solidity 程式碼,但到目前為止沒有成功。我為展示創建了一個範例應用程序。這是範例的目錄結構(node.js 和 truffle 組合):
│ 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
以下是源連結: https ://drive.google.com/file/d/1jeBRM_F0PfAqBZy0JWK3gKv53M9mxdcC/view?usp=drivesdk
npm 包:
npm i express path fs solc ethereumjs-tx web3-utils web3@0.20.7 morgan
它適用於松露 getTotalCount() 和 add() 即,
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)>
但是當在郵遞員中輸入 localhost:34000/d 時,我得到:
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)
應用程序版本:
> truffle version Truffle v5.0.2 (core: 5.0.2) Solidity - 0.5.2 (solc-js) Node v10.14.2
solc npm 包版本:
"_id": "solc@0.5.7",
甘納許:v2.0.0-beta.2
我試圖解決這個問題已經有好幾天了,但不能。當通過 solc 編譯時,我的契約中的所有方法都給出了無效的操作碼錯誤,但在松露中它似乎工作正常,我無法確定問題的根源。
君士坦丁堡/聖路易斯引入了新的操作碼。聖彼得堡硬分叉。從0.4.24 開始,Solidity編譯器就能夠生成這樣的操作碼,但這個功能在0.5.5中預設啟用。Ganache Core 從2.5.1版本開始支持此操作碼,但 Ganache 2.0.0-beta.2基於 Ganache Core 2.3.3。這就是為什麼你的契約沒有工作。
將 Ganache 更新到最新版本(v2.0.1)後,它工作了。