我有這些代碼行
// SPDX-License-Identifier: MIT
pragma solidity >=0.7.0 <0.9.0;
contract wasteManagement2 {
struct Route{
string date; //struct date{ uint day; uint month; uint year;}
// eg. monday
string vehicle;
string driver; //struct driver { string name, string lname, uint id}
string location;
string routebinId;
}
mapping (uint => Route) public routes;
uint public routeCount;
constructor() {
routeCount = 0;
}
function setRoute(string memory _date,string memory _vehicle, string memory _driver, string memory _location, string memory _routebinId) public{
routes[routeCount] = Route (_date,_vehicle, _driver, _location, _routebinId);
routeCount ;
}
function getRoute(uint _routeCount) public view returns(Route memory){
return routes[_routeCount];
}
}
如果出現 6000 多個不同的注冊管理機構,我想測驗合同的運作方式,需要多少費用。提前致謝。
這是現在的測驗檔案:
const Routes = artifacts.require("Routes");
contract ("Routes", (accounts) => {
before(async () => {
instance = await Routes.deployed()
})
it ('ensures that the array is empty', async () => {
let count = await instance.setRoute()
assert.equal(count, 0, 'The array should be empty')
})
})
uj5u.com熱心網友回復:
我將展示如何計算呼叫函式的成本,然后您必須為 6000 個注冊表執行 for 回圈。假設您正確初始化合同:
const result = await instance.setRoute()
如果console.log(result你得到這個物件
result {
tx: '0x1550f6f4f3e7abe0e2d39a43127714e4422e548e6a45d54a3fe12c2ed8b1c180',
receipt: {
transactionHash: '0x1550f6f4f3e7abe0e2d39a43127714e4422e548e6a45d54a3fe12c2ed8b1c180',
transactionIndex: 0,
blockHash: '0x6d13903f40a7b3c989b79accf70d5bb1f7ef673ee59a0eb534b09d375db1bd7e',
blockNumber: 1249,
from: '0xd76536f6b5722f78d444ba0c3b8aae84b7a226ba',
to: '0xde7b6dd9d647e4249f85ac15e5f7c88e7e424fa',
gasUsed: 31167,
cumulativeGasUsed: 31167,
contractAddress: null,
logs: [],
status: true,
logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
rawLogs: []
},
logs: []
}
要獲取 gas 成本,請撰寫一個函式:
const getGas = async (result) => {
const tx = await web3.eth.getTransaction(result.tx);
const gasUsed = toBN(result.receipt.gasUsed);
const gasPrice = toBN(tx.gasPrice);
const gas = gasUsed.mul(gasPrice);
return gas;
};
toBN是一個輔助函式:
const toBN = (value) => web3.utils.toBN(value);
最后得到gas成本:
const result = await instance.setRoute()
const gas = await getGas(result);
uj5u.com熱心網友回復:
在Stack Exchange有手動計算 gas 成本的解釋:
我使用黃皮書,附錄 G,第 25 頁作為參考。
部署合約的 gas 成本可以這樣計算:
21000 因為所有交易都支付這個 (Gtransaction) 32000 因為是合約創建 (Gcreate) 你的交易將有輸入資料,這將花費你的 gas:
4 對于輸入資料中值為 0 的每個位元組 (Gtxdatazero) 對于輸入資料中的每個非零位元組 68 (Gtxdatanonzero) 初始化變數并運行建構式會花費您:
當存盤值設定為非零(Gsset)時,每個 SSTORE 20000 當存盤值設定為零時,每個 SSTORE 5000(Gsreset) 建構式正在執行的每個 OPCODE 的額外氣體(請參閱參考資料) 最后,您必須支付存盤代碼的費用,這將花費您:
存盤在狀態中的每個代碼位元組為 200。當你編譯你的代碼時,你會得到你的位元組碼,這就是 > 你可以找到你的智能合約執行的所有 OPCODES 的地方。
您還將獲得正在運行(或已部署)的位元組碼,這是將存盤在狀態中的 > 代碼。它等于位元組碼減去初始化和建構式代碼(未存盤在狀態中)。
如何使用 truffle javascript 測驗檔案知道代碼大小您可以在 test 檔案夾內的 js 檔案中使用以下代碼:
var YourContract = artifacts.require("YourContract"); contract('YourContract', function(accounts) { it("get the size of the contract", function() { return YourContract.deployed().then(function(instance) { var bytecode = instance.constructor._json.bytecode; var deployed = instance.constructor._json.deployedBytecode; var sizeOfB = bytecode.length / 2; var sizeOfD = deployed.length / 2; console.log("size of bytecode in bytes = ", sizeOfB); console.log("size of deployed in bytes = ", sizeOfD); console.log("initialisation and constructor code in bytes = ", sizeOfB - sizeOfD); }); }); });之后,運行松露測驗。
如果您想自動化該程序,這篇關于 Medium 的文章也可能會有所幫助:
Measuring Gas Cost In order to determine how much gas (many cycles) of the EVM (Ethereum virtual machine) each option takes, we need to measure them. There are many useful blockchain features such as a system function called gasleft() that reports how much gas is left for the running contract, and it is also possible to pass functions to other functions. We can use these features to provide a function that will measure the gas cost of a given function, fun:
function GasCost(string memory name, function () internal returns (string memory) fun) internal returns (string memory) { uint u0 = gasleft(); string memory sm = fun(); uint u1 = gasleft(); uint diff = u0 - u1; return concat(name, " GasCost: ", stringOfUint(diff), " returns(", sm, ")"); }
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/453595.html
