![](/img/trans.png)
[英]Error: Returned error: VM Exception while processing transaction: revert only owner can call this function
[英]Returned error: VM Exception while processing transaction: revert only owner can call this function
我正在嘗試使用“松露測試”測試此合同,但它顯示以下錯誤:錯誤:返回錯誤:處理事務時的 VM 異常:還原只有所有者可以調用此 function - - 給出的原因:只有所有者可以調用此 function。
問題 打開 test 文件夾中的 TestGaming.sol 合約。 為 function 提取資金編寫一個測試。 您的測試應該檢查調用withdrawFunds function 的所有者的余額。 您的測試還應該驗證所有者的余額是否增加了 10 以太幣。
游戲.sol
pragma solidity ^0.5.0;
contract Gaming {
/* Our Online gaming contract */
address public owner;
bool public online;
struct Player {
uint wins;
uint losses;
}
mapping (address => Player) public players;
constructor() public payable {
owner = msg.sender;
online = true;
}
modifier isOwner() {
require(msg.sender == owner, "Only owner can call this function");
_;
}
event GameFunded(address funder, uint amount);
event PlayerWon(address player, uint amount, uint mysteryNumber, uint displayedNumber);
event PlayerLost(address player, uint amount, uint mysteryNumber, uint displayedNumber);
function mysteryNumber() internal view returns (uint) {
uint randomNumber = uint(blockhash(block.number-1))%10 + 1;
return randomNumber;
}
function determineWinner(uint number, uint display, bool guess) public pure returns (bool) {
if (guess == true && number > display) {
return true;
} else if (guess == true && number < display) {
return false;
} else if (guess == false && number > display) {
return false;
} else if (guess == false && number < display) {
return true;
}
}
function winOrLose(uint display, bool guess) external payable returns (bool, uint) {
/* Use true for a higher guess, false for a lower guess */
require(online == true, "The game is online");
require(msg.sender.balance > msg.value, "Insufficient funds");
uint mysteryNumber_ = mysteryNumber();
bool isWinner = determineWinner(mysteryNumber_, display, guess);
Player storage player = players[msg.sender];
/* Player Won */
if (isWinner == true) {
player.wins += 1;
msg.sender.transfer(msg.value * 2);
emit PlayerWon(msg.sender, msg.value, mysteryNumber_, display);
return (true, mysteryNumber_);
/* Player Lost */
} else if (isWinner == false) {
player.losses += 1;
emit PlayerLost(msg.sender, msg.value, mysteryNumber_, display);
return (false, mysteryNumber_);
}
}
}
測試游戲.sol
pragma solidity ^0.5.0;
import "truffle/Assert.sol";
import "truffle/DeployedAddresses.sol";
import "../contracts/Gaming.sol";
contract TestGaming {
uint public initialBalance = 10 ether;
Gaming gaming;
address owner;
function beforeAll() public {
gaming = Gaming(DeployedAddresses.Gaming());
owner = gaming.owner();
}
function testWithdrawFunds() public {
uint ownerBalanceBefore = owner.balance;
gaming.withdrawFunds();
uint ownerBalanceAfter = owner.balance;
Assert.equal (initialBalance, ownerBalanceAfter - ownerBalanceBefore, "The owner's balance should have increased by 10 ether");
}
function testPlayerWonGuessHigher() public {
bool expected = true;
bool result = gaming.determineWinner(5, 4, true);
Assert.equal(expected, result, "The player should have won by guessing the mystery number was higher than their number");
}
function testPlayerWonGuessLower() public {
bool expected = true;
bool result = gaming.determineWinner(5, 6, false);
Assert.equal(expected, result, "The player should have won by guessing the mystery number was lower than their number");
}
function testPlayerLostGuessLower() public {
bool expected = false;
bool result = gaming.determineWinner(5, 4, false);
Assert.equal(expected, result, "The player should have lost by guessing the mystery number was lower than their number");
}
function testPlayerLostGuessHigher() public {
bool expected = false;
bool result = gaming.determineWinner(5, 6, true);
Assert.equal(expected, result, "The player should have lost by guessing the mystery number was higher than their number");
}
}
錯誤
問題是您正在通過另一個合同測試您的合同。 由於 function withdraw
要求調用者是合約的owner
,即合約的部署者,因此由於TestGaming.sol
正在調用 function,因此檢查失敗; TestGaming.sol
不是Gaming
合約的部署者。
您必須從TestGaming
合約內部實例化Gaming
合約。
pragma solidity ^0.5.0;
import "truffle/Assert.sol";
import "truffle/DeployedAddresses.sol";
import "../contracts/Gaming.sol";
contract TestGaming {
uint public initialBalance = 10 ether;
Gaming gaming;
address owner;
function beforeAll() public {
//---Instead of grabbing Gaming address, deploy it fresh for the test
gaming = new Gaming();
owner = gaming.owner();
}
function testWithdrawFunds() public {
uint ownerBalanceBefore = owner.balance;
gaming.withdrawFunds();
uint ownerBalanceAfter = owner.balance;
Assert.equal (initialBalance, ownerBalanceAfter - ownerBalanceBefore, "The owner's balance should have increased by 10 ether");
}
.
.
.
}
這樣, TestGaming
將成為Gaming
的部署者和owner
,測試將通過。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.