Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Returning arbitrary length bytes instead of bytes32 #9

Open
snario opened this issue May 11, 2018 · 1 comment
Open

Returning arbitrary length bytes instead of bytes32 #9

snario opened this issue May 11, 2018 · 1 comment

Comments

@snario
Copy link

snario commented May 11, 2018

As a means of generalizing the conditional execution object to something nicely usable by other projects in the community such as state channels or Plasma contracts, or perhaps within future extensions of this project, it would be nice if callWithData returned bytes using returndatasize.

@kosecki123
Copy link
Contributor

Agree, that would be more general approach to conditionals. The reason for returning bytes32 is we want to get true/false value from the condition.

In order to allow reading bytes from call take a look at this quick'n'dirty test code:

contract Test1 {
    function add(int a, int b) returns(bytes){  //Simply add the two arguments and return
        return abi.encode(a+b);
    }
}

contract Test2 {
    Test1 test1;

    function Test2(){
        test1 = new Test1(); 
    }
    
    function testAdd() constant returns(bytes) {
        //encoded add(int256,int256) call add(2,2) expected return 4
        bytes memory a = hex"a5f3c23b00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002";
        return callWithData(address(test1), a);
    }
  
    function callWithData(address dest, bytes data) private returns (bytes c) {
        assembly {
            let freemem := mload(0x40)
            
            pop(
            call(      
                5000,
                dest,
                0,
                add(data, 0x20),
                mload(data),
                0,
                0
            )
            )
            
            let size := returndatasize
            returndatacopy(freemem, 0, size)
            
            return (freemem, size)
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants