1

我有以下合同,我计划在另一份合同中用作外部合同:

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 上)。

4

0 回答 0