Solidity

如何診斷“錯誤:交易已被 EVM 還原”?

  • January 24, 2020

這是從我的 javascript 中截獲的錯誤,使用 web3.js

Error: Transaction has been reverted by the EVM:
   {
   "blockHash": "0xba0e277e390d8a455cf3634f6fe38fd1a0fb0c4dc673fdf2e383a12beb8237db",
   "blockNumber": 5838119,
   "contractAddress": null,
   "cumulativeGasUsed": 79499,
   "from": "0x8d8d315a5b1bb25e8c0b1566726454fcc11b3647",
   "gasUsed": 35463,
   "logs": [],
   "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
   "status": false,
   "to": "0x6474d5b92127ef04f30dac3f48626334d36ab489",
   "transactionHash": "0x5b36d00330a2603450fc076ed74ff96c28a5003f14641589bc8cf77f2dde6455",
   "transactionIndex": 2
}
at Object.TransactionError (C:\progetti\nterilizer\node_modules\web3-core-helpers\src\errors.js:63:21)
at Object.TransactionRevertedWithoutReasonError (C:\progetti\nterilizer\node_modules\web3-core-helpers\src\errors.js:75:21)
at C:\progetti\nterilizer\node_modules\web3-core-method\src\index.js:448:48
at process._tickCallback (internal/process/next_tick.js:68:7)

receipt:
{ 
   blockHash:
   '0xba0e277e390d8a455cf3634f6fe38fd1a0fb0c4dc673fdf2e383a12beb8237db',
   blockNumber: 5838119,
   contractAddress: null,
   cumulativeGasUsed: 79499,
   from: '0x8d8d315a5b1bb25e8c0b1566726454fcc11b3647',
   gasUsed: 35463,
   logs: [],
   logsBloom:
   '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
   status: false,
   to: '0x6474d5b92127ef04f30dac3f48626334d36ab489',
   transactionHash:
   '0x5b36d00330a2603450fc076ed74ff96c28a5003f14641589bc8cf77f2dde6455',
   transactionIndex: 2 
}

這是我要執行的測試契約

pragma solidity >=0.4.0 <0.6.0;

contract test013 {

   // These will be assigned at the construction
   // phase, where `msg.sender` is the account
   // creating this contract.
   address public owner = msg.sender;

   // record id => json
   mapping (uint256 => string ) private storedData;

   modifier isOwner() {    
       require(owner == msg.sender);
       _;
   }

   modifier dataNotStored(uint256 _index) {
       require( keccak256(abi.encodePacked(storedData[_index])) == keccak256(abi.encodePacked('')) );
       _;
   }

   function set(uint _index, string calldata _data_to_store) external dataNotStored(_index) {
       storedData[_index] = _data_to_store;
   }

   function forceSet(uint _index, string calldata _data_to_store) isOwner external {
       storedData[_index] = _data_to_store;
   }

   function get(uint _index) public view returns (string memory) {
       return storedData[_index];
   }
}

失敗的函式呼叫是set.

我將它與我發送的數據一起使用,我沒有遇到已經使用過的映射 ID。

我必須不覆蓋條目以確保數據可寫一次。

我有一個疑問,我一次不能發送超過一筆交易……可能是這樣嗎?

基於

失敗的函式呼叫是集合

兩名嫌疑人:

嫌疑人一

require( keccak256(abi.encodePacked(storedData[_index])) == keccak256(abi.encodePacked('')) );

如果我沒看錯,要求是storedData == '',所以可能不是這樣。

嫌疑人二

require(owner == msg.sender);

在呼叫函式時檢查owner()並確保這是你的。{from: <address>}``0x8d8d315a5b1bb25e8c0b1566726454fcc11b3647

推薦

這是非常模棱兩可的:

pragma solidity >=0.4.0 <0.6.0;

使用最近的編譯器並將其設置為您實際使用的編譯器。較新的編譯器讓您插入有助於調試的還原原因。

例如

require(false, "This message will output on the console if your client supports it.);"

希望能幫助到你。

在解構合約和 js 程式碼之後,我發現我使用 web3.js 發送了一個簽名交易,使用十進製而不是十六進制的“gasLimit”。

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