Ganache

部署 ganache-local w/brownie:處理事務時出現 VM 異常:操作碼無效

  • March 7, 2022

我正在學習本課程的 brownie_fund_me 部分:

https://github.com/PatrickAlphaC/brownie_fund_me

初始部署,.py 工作正常,但我不斷收到一些無效操作碼的變化,當我嘗試部署到 ganache-local 時,事務不斷恢復。我能夠在這個項目中執行測試到 development/ganache-cli,但由於 ganache-local 和 Rinkeby 而失敗。

預期行為

合約應使用 Ganache UI 正確部署和提取資金。

目前行為

執行`brownie run deploy.py –network ganache-local。我在執行或故障排除時遇到這些錯誤。

  • ValueError:氣體估計失敗:‘執行恢復:處理事務時 VM 異常:操作碼無效’。該交易可能會恢復。如果你想廣播,你必須手動設置氣體限制。
  • ValueError:呼叫期間執行恢復:‘執行恢復:處理事務時VM異常:操作碼無效’。該交易可能會恢復。如果您希望廣播,請將其allow_revert:True作為交易參數包含在內。
  • VirtualMachineError:無效的操作碼:無效的操作碼

重現和故障排除的步驟

  1. git複製https://github.com/PatrickAlphaC/brownie_fund_me
  2. 布朗尼編譯-a。編譯成功。
 Solc version: 0.6.12
 Optimizer: Enabled  Runs: 200
 EVM Version: Istanbul
Generating build data...
- smartcontractkit/chainlink-brownie-contracts@1.1.1/AggregatorInterface
- smartcontractkit/chainlink-brownie-contracts@1.1.1/AggregatorV2V3Interface
- smartcontractkit/chainlink-brownie-contracts@1.1.1/AggregatorV3Interface
- smartcontractkit/chainlink-brownie-contracts@1.1.1/SafeMathChainlink
- FundMe
- MockV3Aggregator
Project has been compiled. Build artifacts saved at /Users/davidandrews/Documents/Projects/Web3/bss/build/contracts
  1. 使用預設氣體限制和值部署 ganache ui。
  2. 執行brownie run scripts/deploy.py --network ganache-local
~/Documents/Projects/Web3/brownie_fund_me (main●●)$ brownie run scripts/deploy.py --network ganache-local 
Brownie v1.17.1 - Python development framework for Ethereum

brownie_fund_me is the active project.

Running 'scripts/deploy.py::main'...
The active network is ganache-local
Deploying Mocks...
Transaction sent: 0xe7b501480f70836256dbdd37a159100198d1422897117777e33588d1b2e1a566
 Gas price: 20.0 gwei   Gas limit: 540226   Nonce: 0
 MockV3Aggregator.constructor confirmed   Block: 1   Gas used: 491115 (90.91%)
 MockV3Aggregator deployed at: 0xF5F2B13A72901f1621d2CD9b382c1c513ac0724D

Mocks Deployed!
Transaction sent: 0xc5a26f42997e9c648ee4d12df1805746cd3ca6508c315747f9b836ce5fb39516
 Gas price: 20.0 gwei   Gas limit: 528696   Nonce: 1
 FundMe.constructor confirmed   Block: 2   Gas used: 480633 (90.91%)
 FundMe deployed at: 0xEDa314a434Ef759d85F60a54804FdfdbEB26F179

Contract deployed to 0xEDa314a434Ef759d85F60a54804FdfdbEB26F179
  1. 執行: brownie run scripts/fund_and_withdraw.py --network ganache-local

BssProject is the active project.

Running 'scripts/fund_and_withdraw.py::main'...
25000000000000000
The current entry fee is 25000000000000000
Funding
Transaction sent: 0xffd686e74b439f0afff97ba5ab77ce31a48a66a6f2710d123b3cf37bc8b8bade
 Gas price: 20.0 gwei   Gas limit: 94207   Nonce: 2
 FundMe.fund confirmed   Block: 3   Gas used: 85643 (90.91%)

 File "brownie/_cli/run.py", line 50, in main
   return_value, frame = run(
 File "brownie/project/scripts.py", line 103, in run
   return_value = f_locals[method_name](*args, **kwargs)
 File "./scripts/fund_and_withdraw.py", line 23, in main
   withdraw()
 File "./scripts/fund_and_withdraw.py", line 18, in withdraw
   fund_me.withdraw({"from": account})
 File "brownie/network/contract.py", line 1625, in __call__
   return self.transact(*args)
 File "brownie/network/contract.py", line 1498, in transact
   return tx["from"].transfer(
 File "brownie/network/account.py", line 644, in transfer
   receipt, exc = self._make_transaction(
 File "brownie/network/account.py", line 727, in _make_transaction
   raise VirtualMachineError(e) from None
 File "brownie/exceptions.py", line 121, in __init__
   raise ValueError(str(exc)) from None
ValueError: Gas estimation failed: 'execution reverted: VM Exception while processing transaction: invalid opcode'. This transaction will likely revert. If you wish to broadcast, you must set the gas limit manually.
  1. 將 gas_limit 參數更改為預設 Ganache UI gas_limit: fund_me.withdraw({"from": account})->fund_me.withdraw({"from": account, 'gas_limit': 6721975})並執行fund_and_withdraw.py.
Brownie v1.17.1 - Python development framework for Ethereum

BssProject is the active project.

Running 'scripts/fund_and_withdraw.py::main'...
25000000000000000
The current entry fee is 25000000000000000
Funding
Transaction sent: 0x996f97219b9c8cf1dd3f88753adf4c94335d27d1784972b976c550cea5f13c6d
 Gas price: 20.0 gwei   Gas limit: 61207   Nonce: 3
 FundMe.fund confirmed   Block: 4   Gas used: 55643 (90.91%)

 File "brownie/_cli/run.py", line 50, in main
   return_value, frame = run(
 File "brownie/project/scripts.py", line 103, in run
   return_value = f_locals[method_name](*args, **kwargs)
 File "./scripts/fund_and_withdraw.py", line 24, in main
   withdraw()
 File "./scripts/fund_and_withdraw.py", line 19, in withdraw
   fund_me.withdraw({"from": account, 'gas_limit': 6721975})
 File "brownie/network/contract.py", line 1625, in __call__
   return self.transact(*args)
 File "brownie/network/contract.py", line 1498, in transact
   return tx["from"].transfer(
 File "brownie/network/account.py", line 644, in transfer
   receipt, exc = self._make_transaction(
 File "brownie/network/account.py", line 752, in _make_transaction
   exc = VirtualMachineError(e)
 File "brownie/exceptions.py", line 121, in __init__
   raise ValueError(str(exc)) from None
ValueError: Execution reverted during call: 'execution reverted: VM Exception while processing transaction: invalid opcode'. This transaction will likely revert. If you wish to broadcast, include `allow_revert:True` as a transaction parameter.
  1. 添加fund_me.withdraw({"from": account, 'gas_limit': 6721975, 'allow_revert': True})
Brownie v1.17.1 - Python development framework for Ethereum

BrownieFundMeProject is the active project.

Running 'scripts/fund_and_withdraw.py::main'...
25000000000000000
The current entry fee is 25000000000000000
Funding
Transaction sent: 0x1709a9bb9cf6213526c0307eb01c91c00a644a227c8a0efffa71310f3b94a106
 Gas price: 20.0 gwei   Gas limit: 61207   Nonce: 4
 FundMe.fund confirmed   Block: 5   Gas used: 55643 (90.91%)

 FundMe.fund confirmed   Block: 5   Gas used: 55643 (90.91%)

Transaction sent: 0x5e4947dc2d642dbb337eefdb6711c33b39eca70e2294aed10c05e3d15861b3a2
 Gas price: 20.0 gwei   Gas limit: 6721975   Nonce: 5
 FundMe.withdraw confirmed (invalid opcode)   Block: 6   Gas used: 6721975 (100.00%)

 File "brownie/_cli/run.py", line 50, in main
   return_value, frame = run(
 File "brownie/project/scripts.py", line 103, in run
   return_value = f_locals[method_name](*args, **kwargs)
 File "./scripts/fund_and_withdraw.py", line 28, in main
   withdraw()
 File "./scripts/fund_and_withdraw.py", line 21, in withdraw
   tx = fund_me.withdraw({"from": account, 'gas_limit': 6721975, 'allow_revert': True})
 File "brownie/network/contract.py", line 1625, in __call__
   return self.transact(*args)
 File "brownie/network/contract.py", line 1498, in transact
   return tx["from"].transfer(
 File "brownie/network/account.py", line 682, in transfer
   receipt._raise_if_reverted(exc)
 File "brownie/network/transaction.py", line 446, in _raise_if_reverted
   raise exc._with_attr(
VirtualMachineError: invalid opcode: invalid opcode
  1. 我還在執行之間刪除建構工件。
  2. 我試過這個:
  • 從您的巧克力蛋糕網路列表中刪除 ganache-local
  • 刪除 build/deployments/1337(或任何你的chainID)上的相應文件夾以及map.json文件的相應chainid行
  • 在您的 Ganache 實例上,而不是點擊快速啟動,點擊創建工作區
  • 將gas limit設置為比目前更高的數字(我選擇了8000000),其餘的可以保持原樣
  • 在您的 VSC 終端或您用來輸入程式碼的任何程序上,重新引入網路 ganache-local(布朗尼網路添加 Ethereum ganache-local host=http://YOURHOSTNUMBER chainid=YOURCHAINID

我嘗試將 ganache ui 中的 gas_limit 設置為 999999999999999 並將 gas 價格設置為 gwei。

可能的解決方案

這似乎是 Ganache UI 的 gas 估算和 gas 價格問題。

語境

我正在努力完成這門課程:https://github.com/smartcontractkit/full-blockchain-solidity-course-py#lesson-6-brownie-fund-me

你的環境

  • 蟒蛇 3.9.8
  • Ganache 版本:Ganache CLI v6.12.2 (ganache-core: 2.13.2)RPC 伺服器:HTTP://127.0.0.1:7545 chainid=1377
  • 甘納許使用者界面版本:Version 2.5.4 (2.5.4.1367)
  • 節點版本:v16.8.0
  • 作業系統和版本:macOS Monterey 12.1 Beta
  • 松露版:Truffle v5.4.21
  • 連結到您的項目:https://github.com/PatrickAlphaC/brownie_fund_me. 我已經複製了這個 repo 以確保我自己的程式碼中沒有錯誤。

getPrice() 沒有從模擬中返回你想要的數字,在 2B 附近的某個地方。認為這是 Ganache 實現的一個錯誤——在 getEntranceFee() 中執行計算(最低美元 * 精度)/價格會給你一個小於 1 的數字——並且,由於 Solidity 無法處理浮點數,Solidity 只是將其視為 0,整個事情都出錯了。

問題似乎與 Ganache 的版本有關:如果您使用 ganache GUI,您會得到關於主菜費用和氣體估計錯誤的錯誤計算。如果您在本地啟動 ganache-cli,在另一個視窗中,它可以工作,計算和呼叫withdraw():

ganache-cli –accounts 10 –hardfork istanbul –gasPrice 2000000000 –gasLimit 6721975 –mnemonic brownie –port 8545

我正在使用從 npm -g install ganache 安裝的 ganache@7.0.3

如果您在另一個視窗中啟動安全帽節點,它也可以工作,在 hardhat-c​​onfig.js 中具有以下定義:

網路:{ hardhat:{ initialBaseFeePerGas:0,accounts:{助記詞:“test test test test test test test test test test junk”,路徑:“m/44’/60’/0’/0”,計數:10 , 賬戶餘額: “100000000000000000000”, }, }, }

BrownieFundMeProject 是活躍的項目。

執行’腳本/ fund_and_withdraw.py ::主“…… 25000000000000001目前的報名費是25000000000000001資金交易發送:> 0xcd8f0db14647958c4902dcce05b2baf41f1a6604999867f4bd726df02273dc68氣價:2.0 GWEI氣上限:99258隨機數:5 FundMe.fund證實塊:6所用氣體:90235 (90.91%)

發送的交易:>0xab399fb117adac26e2a381e6fab907880a1bc796d64a26f6f7915925fd6b90d5 氣體價格:2.0 gwei 氣體限制:76963 隨機數:6 FundMe.withdraw 確認區塊:7 使用氣體:24967 (32.44%)

所以它看起來像 Ganache GUI 中的一個錯誤。

引用自:https://ethereum.stackexchange.com/questions/114889