when I develop my smart contract with solidity, I encounter a problem. And Below is my solidity code.
pragma solidity ^0.4.0;
contract RegisterContract{
event setNewUser(bytes32 name,address etherAddr, address contractAddr,uint now);
address owner;
struct User{
bytes32 name;
address etherAddr;
address contractAddr;
}
User[] private users;
constructor() public{
owner = msg.sender;
}
modifier checkOwner(){
require(msg.sender == owner);
_;
}
function getOwner() public view returns (address){
return owner;
}
// for a new user create a contract
function registerUser(bytes32 name,address etherAddr, address contractAddr) public checkOwner{
User memory newUser;
newUser.name = name;
newUser.etherAddr = etherAddr;
newUser.contractAddr = contractAddr;
users.push(newUser);
emit setNewUser(name,etherAddr,contractAddr,now);
}
//
function setAddress(bytes32 name,address etherAddr, address contractAddr) public checkOwner{
for(uint8 i=0;i<users.length;i++){
if(users[i].name==name){
users[i].etherAddr=etherAddr;
users[i].contractAddr=contractAddr;
}
}
}
// when systems assess all user
function getUsers() public checkOwner view returns (bytes32[],address[],address[]) {
bytes32[] memory names= new bytes32[](users.length);
address[] memory etherAddr = new address[](users.length);
address[] memory contractAddr = new address[](users.length);
for(uint8 i=0;i<users.length;i++){
names[i]= users[i].name;
etherAddr[i] = users[i].etherAddr;
contractAddr[i] = users[i].contractAddr;
}
return (names,etherAddr,contractAddr);
}
//for a user who import contract
function getContractAddress(address etherAddr) public checkOwner view returns (bytes32,address) {
for(uint8 i=0;i<users.length;i++){
if(users[i].etherAddr==etherAddr){
return (users[i].name,users[i].contractAddr);
}
}
}
}
The question is when I want to call registerUser and when I using web3.js calling registerContract.methods.registerUser(name,etheraddress,contractaddress) It throws an error on geth console. The error is
Error: Transaction has been reverted by the EVM: { "blockHash": "0x45fe755c8c1f600108b55a12fa3bdf59dac0fe76d39883f23d15b2f9603d868d", "blockNumber": 22339, "contractAddress": null, "cumulativeGasUsed": 90000, "from": "0x5869c2317ce2df31cb1269d8028e9062ff470749", "gasUsed": 90000, "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "status": false, "to": "0xc249fa432a1c659e7aa4ad57e24e405215461afa", "transactionHash": "0xb52e0fabe160070597bd40192658b6f84779d52d4b295b39295d381eb0856f2d", "transactionIndex": 0, "events": {} }
Is it anything wrong when I addressed struct User and array push? Or there is any problems which I was unaware of at the time I developed?
for
loops which is a well-known anti-pattern.