Solidity
這是我的範常式式碼,setName 函式中的合約呼叫是同步的還是非同步的?
pragma solidity ^0.4.22; contract Testing { function setName(bytes32 firstName , bytes32 lastName) public returns(bool) { Sample _test = new Sample(firstName); _test.addLastName(firstName, lastName); return true; } } contract Sample { address a; bytes32 firstName; bytes32 lastName; constructor(bytes32 _firstName) public { firstName = _firstName; } modifier onlyBy(bytes32 _firstName) { require( firstName == _firstName, "Error" ); _; } function addLastName(bytes32 firstName, bytes32 _name) public onlyBy(firstName) returns(bool) { lastName = _name; return true; } } I am getting this error (node:12667) UnhandledPromiseRejectionWarning: Error: Transaction has been reverted by the EVM: { "blockHash":"0xe709d446057c70ba4928424a71e072aa0b144461ceea0fc535849cd05c7b1cda", "blockNumber":4301, "contractAddress":null, "cumulativeGasUsed":62775, "from":"0x112fda795ce61992653b8775597a9152ee776e4c", "gasUsed":62775, "logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "status":false, "to":"0x41e864e68ecebe2fd69cea1eed49b5fc516e9af9", "transactionHash":"0x33b3921491631d4e2d35a3887858f21a3c02b2dd552b624bb243e2c33f6c4e86", "transactionIndex":0, "events":{ } }
前提
如前所述,EVM 完全確定性地按順序執行。
回答
您從 javascript 中得到異常,因為執行耗盡了氣體。你可以猜到,因為使用的氣體是相同數量的“gasLimit”。這意味著要麼你分配的 gas 太低,要麼你使用了太多的 gas 來執行。我贊成後者,因為每次呼叫 test 時,您都在網路中部署了一個新合約
Sample _test = new Sample(firstName);
.當您
new
在後台呼叫時,它會使用CREATE
指令。它的成本為 32000 天然氣。此外,您還必須為儲存在區塊鏈上的每個字節(即合約的持久程式碼)支付 200 gas。此外,您還需要為執行的指令和執行期間使用的記憶體付費。此外,交易的內在成本是 21000 單位的 gas。您還為每個輸入字節為 0 支付 4 個氣體單位,為每個不等於 0 的輸入字節支付 68 個氣體單位。所以讓我們總結一下使用的氣體量:
21000 + |input_price| + 32000 + |o|*200 + |amount of gas for the rest of execution|
在哪裡 |o| 是將保留在區塊鏈上的程式碼的大小。您可以很容易地看到 62775 太低而無法授予整個執行權限。