Ethereum Smart contract calling a function in another smart contract that has a address payable return value give error

by Deepal   Last Updated September 11, 2019 02:28 AM

I am new to solidity. Anyway, I have written the following two smart contracts:

pragma solidity >=0.4.22 <0.7.0;

contract Validator {

  struct User {
        address payable validator; // The address of the validator that added the Merkel-hash
        bytes32 hash; //the Merkel-hash of the user
   } 

   User public user;

   constructor() public {

    user.validator = msg.sender; //assign the address of the creator of the smart contract/the validator
    user.hash = 0x33c897fb3febb48f0a8a9ff79518057a9943ed879d644bba81a8a1050ccfaa4f; //the Merkel-hash that the validator calculated
   }


  function verifyHash(bytes32 _Mhash) public view returns(address payable) { 

      /*the SP smart contract calls this function and parses the user entered Merkel-hash
       if this is equivalent to the stored Merkel-hash in block,
       return the address of the validator that added it
      */

   if(keccak256(abi.encode(user.hash)) == keccak256(abi.encode(_Mhash))){
       return user.validator;
   }

}
}

pragma solidity >=0.4.22 <0.7.0;

contract SP {

    address BlockAddress;
    bytes32 _Mhash;
    //address Validator_address;
    //address payable public User_address;
    //uint RepTokens;
    //uint AccessCode;

    struct Values {
        address payable User_address;
        uint RepTokens;
        uint AccessCode;
    }

    Values public values;

    constructor() public {
        values.User_address = msg.sender;
        values.RepTokens = 50;
        values.AccessCode = 20;
    }

    /*the user calls this function by parsing the address of the smart contract that has the user Merkel-hash 
    (sent via text message by the validator)
    */
    function readBlockAddress(address _t) public { 

        BlockAddress = _t; 
    }
    /*the user calls this function and parses the Merkel-hash that it received from the validator's text message*/
    function readMerkelHash(bytes32 hash) public { 
        _Mhash = hash;
        //verifyHash_ASM(_Mhash);
    }

    /*The SP calls this function on the user's behalf. 
    SP smart contract retrieves validator address of the validator that added a smart contract with the user Merkel-hash*/
    function verifyHash_ASM(bytes32 _MHash) public returns (address payable Validator_address) {

        bytes4 sig = bytes4(keccak256("verifyHash(string)"));
        assembly {
            // move pointer to free memory spot
            let ptr := mload(0x40)
            // put function sig at memory spot
            mstore(ptr,sig)
            // append argument after function sig
            mstore(add(ptr,0x04), _MHash)

            let result := call(
              15000, // gas limit
              sload(BlockAddress_slot),  // to addr. append var to _slot to access storage variable
              0, // not transfer any ether
              ptr, // Inputs are stored at location ptr
              0x20, // Inputs are 32 bytes long
              ptr,  //Store output over input
              0x20) //Outputs are 32 bytes long

            if eq(result, 0) {
                revert(0, 0)
            }

            Validator_address := mload(ptr) // Assign output to Validator_address var
            mstore(0x40,add(ptr,0x24)) // Set storage pointer to new space

        }
        //PayTokens(Validator_address, User_address, RepTokens);
        //return Validator_address;
    }

    /*pay reputation tokens to the validator and send access code to the user 
    (we assume the value of ether transferred is the access code value which the user will use to access services of SP)
    */
    function PayTokens(address payable Validator_Address, uint Reps) private {
           Validator_Address.transfer(Reps);
           (values.User_address).transfer(values.AccessCode);
}
}

I deployed both contracts in Remix without error. However, when I call the verifyHash_ASM function and parse the byte32 value 0x33c897fb3febb48f0a8a9ff79518057a9943ed879d644bba81a8a1050ccfaa4f which is equivalent to the one assigned in the Validator smart contract, I get the following error:

"transact to SP.verifyHash_ASM errored: VM error: revert. revert The transaction has been reverted to the initial state. Note: The called function should be payable if you send value and the value you send should be less than your current balance. Debug the transaction to get more information."

The status is: "0x0 Transaction mined but execution failed"

Please note that I executed the function without values in Remix. I have also tried executing this by adding gas values and changing the function to "payable",but I still got this error.

The compiler I am using is 0.5.11 and the execution environment is JavaScript VM in Remix.

Your help and support is much appreciated

Thank you



Related Questions


Updated September 05, 2019 16:28 PM

Updated January 10, 2018 22:28 PM

Updated February 21, 2018 19:28 PM

Updated November 29, 2017 10:28 AM

Updated January 06, 2018 08:28 AM