I am trying to run my smart contract in remix ide, below is my smart contract code:
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;
}
}
I am running private block wherein my miners are mining the blocks. I had my miners running all the time but they were adding empty blocks so I used --preload mineWhenNeeded.js flag and limit the miners to mine until 12 blocks using following code in my mineWhenNeeded.js
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();
Here is my geth command
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
But now after some time I am getting error Returned error: gas required exceeds allowance (8000000) or always failing transaction
After few experiments I realize that if I let my miners run for a while and check the limit of my latest block using following code eth.getBlock("pending").gasLimit in geth console, the limit is decreasing gradually. It seems like if I let my miners run all the time then I will not get that gas required exceeds allowance issue anymore. Now I am wondering, should I remove the restriction to mine only 12 blocks but it gonna start adding empty blocks again. What should I do?