我打算将以下合同用作其他合同中的外部合同:
pragma solidity ^0.4.24;
import "./Ownable.sol";
import "./oraclizeAPI.sol";
interface EtherHiLoRandomNumberRequester {
function incomingRandomNumber(address player, uint8 randomNumber) external;
function incomingRandomNumberError(address player) external;
}
interface EtherHiLoRandomNumberGenerator {
function generateRandomNumber(address player, uint8 max) external returns (bool);
}
/// @title EtherHiLoRandom
/// @dev the contract than handles the EtherHiLo random numbers
contract EtherHiLoRandom is usingOraclize, Ownable, EtherHiLoRandomNumberGenerator {
uint8 constant FAILED_ROLE = 69;
uint public rngCallbackGas;
mapping(bytes32 => uint) private failedRolls;
mapping(bytes32 => address) private rollIdToPlayer;
mapping(bytes32 => uint8) private rollIdToMax;
mapping(bytes32 => address) private rollIdToCaller;
constructor() public {
oraclize_setProof(proofType_Ledger);
setRNGCallbackGasConfig(1500000, 10000000000);
}
function generateRandomNumber(address player, uint8 max) external returns (bool) {
bytes32 rollId = oraclize_newRandomDSQuery(0, 7, rngCallbackGas);
if (failedRolls[rollId] == FAILED_ROLE) {
delete failedRolls[rollId];
delete rollIdToPlayer[rollId];
delete rollIdToMax[rollId];
delete rollIdToCaller[rollId];
return false;
}
rollIdToPlayer[rollId] = player;
rollIdToMax[rollId] = max;
rollIdToCaller[rollId] = msg.sender;
return true;
}
function __callback(bytes32 rollId, string _result, bytes _proof) public {
require(msg.sender == oraclize_cbAddress());
address player = rollIdToPlayer[rollId];
address caller = rollIdToCaller[rollId];
uint8 max = rollIdToMax[rollId];
// avoid reorgs
if (player == address(0)) {
failedRolls[rollId] = FAILED_ROLE;
return;
}
delete failedRolls[rollId];
delete rollIdToPlayer[rollId];
delete rollIdToMax[rollId];
delete rollIdToCaller[rollId];
EtherHiLoRandomNumberRequester requester = EtherHiLoRandomNumberRequester(caller);
if (oraclize_randomDS_proofVerify__returnCode(rollId, _result, _proof) != 0) {
requester.incomingRandomNumberError(player);
} else {
uint8 randomNumber = uint8(uint(keccak256(_result)) % max);
requester.incomingRandomNumber(player, randomNumber);
}
}
/// OWNER / MANAGEMENT RELATED FUNCTIONS
function transferBalance(address to, uint amount) public onlyOwner {
to.transfer(amount);
}
function setRNGCallbackGasConfig(uint gas, uint price) public onlyOwner {
rngCallbackGas = gas;
oraclize_setCustomGasPrice(price);
}
function destroyAndSend(address _recipient) public onlyOwner {
selfdestruct(_recipient);
}
}
我有以下测试:
pragma solidity ^0.4.24;
import "truffle/Assert.sol";
import "truffle/DeployedAddresses.sol";
import "../contracts/EtherHiLoRandom.sol";
contract TestEtherHiLoRandom is EtherHiLoRandomNumberRequester {
function incomingRandomNumber(address player, uint8 randomNumber) {
//require(false, "incomingRandomNumber");
}
function incomingRandomNumberError(address player) {
//require(false, "incomingRandomNumberError");
}
function testInitialBalanceUsingDeployedContract() {
EtherHiLoRandom subject = EtherHiLoRandom(DeployedAddresses.EtherHiLoRandom());
bool result = subject.generateRandomNumber(msg.sender, 5);
Assert.isTrue(result, "generateRandomNumber failed");
Assert.isTrue(true, "itchy balls");
}
}
我的测试在调用TestEtherHiLoRandom
时在oraclize_newRandomDSQuery
中失败。我得到的错误是:
TestEtherHiLoRandom
1) testInitialBalanceUsingDeployedContract
> No events were emitted
0 passing (3s) 1 failing
1) TestEtherHiLoRandom
testInitialBalanceUsingDeployedContract:
Error: VM Exception while processing transaction: revert
at Object.InvalidResponse (node_modules/truffle/build/webpack:/~/web3/lib/web3/errors.js:38:1)
at node_modules/truffle/build/webpack:/~/web3/lib/web3/requestmanager.js:86:1
at node_modules/truffle/build/webpack:/packages/truffle-provider/wrapper.js:134:1
at XMLHttpRequest.request.onreadystatechange (node_modules/truffle/build/webpack:/~/web3/lib/web3/httpprovider.js:128:1)
at XMLHttpRequestEventTarget.dispatchEvent (node_modules/truffle/build/webpack:/~/xhr2/lib/xhr2.js:64:1)
at XMLHttpRequest._setReadyState (node_modules/truffle/build/webpack:/~/xhr2/lib/xhr2.js:354:1)
at XMLHttpRequest._onHttpResponseEnd (node_modules/truffle/build/webpack:/~/xhr2/lib/xhr2.js:509:1)
at IncomingMessage.<anonymous> (node_modules/truffle/build/webpack:/~/xhr2/lib/xhr2.js:469:1)
at endReadableNT (_stream_readable.js:1056:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
知道我在做什么错吗?我尚未在任何测试网络上运行它,仅在本地testrpc上运行过(我正在使用ethereum-bridge
,并已验证Oraclize合同已正确部署在testrpc上。)