Solidity

礦工開採區塊的限制應該是多少

  • March 23, 2020

我正在嘗試在 remix ide 中執行我的智能合約,下面是我的智能合約程式碼:

pragma solidity >=0.4.17 <0.7.0;
pragma experimental ABIEncoderV2;
contract Department{

   struct Employee{
       uint empId;
       string empName;
       uint256 empSalary;
   }

   uint departmentId;
   string departmentName;
   mapping (uint => Employee) public employees;
   uint public employeeCount;

   constructor(uint _departmentId, string memory _departmentName) public {
       employeeCount = 0;
       departmentId = _departmentId;
       departmentName = _departmentName;
   }

   function addEmployee(uint _empId, string memory _empName, uint _empSalary ) public {
       employees[employeeCount] = Employee(_empId, _empName, _empSalary);
       employeeCount++;
   }

   //return Single structure
   function get(uint _index) public view returns(Employee memory) {
       return employees[_index];
   }

   //return Array of structure Value
   function getEmployee() public view returns (uint[] memory, string[] memory, uint[] memory) {
       uint[]    memory id = new uint[](employeeCount);
       string[]  memory name = new string[](employeeCount);
       uint[]    memory salary = new uint[](employeeCount);
       for (uint i = 0; i < employeeCount; i++) {
         Employee storage employee = employees[i];
         id[i] = employee.empId;
         name[i] = employee.empName;
         salary[i] = employee.empSalary;
       }
       return (id, name, salary);
   }

   //return Array of structure
   function getEmployees() public view returns (Employee[] memory) {
       Employee[]  memory id = new Employee[](employeeCount);
       for (uint i = 0; i < employeeCount; i++) {
         Employee storage employee = employees[i];
         id[i] = employee;
       }
       return id;
   }

}

我正在執行私人區塊,我的礦工正在開採這些區塊。我的礦工一直在執行,但他們正在添加空塊,所以我使用了 –preload mineWhenNeeded.js 標誌,並使用 mineWhenNeeded.js 中的以下程式碼將礦工限制為 12 個塊

var minimum_confirmations = 12;
var mining_threads = 1
var txBlock = 0
function checkWork() {
   if (eth.getBlock("pending").transactions.length > 0) {
       txBlock = eth.getBlock("pending").number
       if (eth.mining) return;
       console.log("  Transactions pending. Mining...");
       miner.start(mining_threads)
       interval = setInterval(function () {
           if (eth.getBlock("latest").number < txBlock + minimum_confirmations) {
               if (eth.getBlock("pending").transactions.length > 0) txBlock = eth.getBlock("pending").number;
           } else {
               console.log(minimum_confirmations + " confirmations achieved; mining stopped.");
               miner.stop()
               clearInterval(interval);
           }
       }, 600)
   }
}

eth.filter("latest", function (err, block) { checkWork(); });
eth.filter("pending", function (err, block) { checkWork(); });

checkWork();

這是我的 geth 命令

geth --datadir $ethereum_home/chaindata --preload "scripts/mineWhenNeeded.js" --networkid 45638 --verbosity 6 --rpc --port 30304 --rpcport 8545 --rpcaddr "0.0.0.0" --rpcapi "web3,eth,personal,net" --rpccorsdomain remix.ethereum.org --nodiscover --gcmode archive --allow-insecure-unlock console 2

但是現在經過一段時間後我收到錯誤返回錯誤:所需的氣體超過限額(8000000)或總是失敗的交易

經過幾次實驗後,我意識到如果我讓我的礦工執行一段時間並在 geth 控制台中使用以下程式碼**eth.getBlock(“pending”).gasLimit檢查我的最新塊的限制,限制會逐漸減少。**似乎如果我讓我的礦工一直執行,那麼我將不會再遇到所需的 gas 超過限額的問題。現在我想知道,我是否應該取消僅開採 12 個區塊的限制,但它會再次開始添加空區塊。我該怎麼辦?

區塊氣體限制可以通過兩個geth 命令行選項來控制

  • --miner.gastarget value 開採區塊的目標氣底
  • --miner.gaslimit value 開採區塊的目標氣體上限

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