1

https://gist.github.com/ChrisCalderon/2818e6ba0214c25962b16c02e761dc26

Whenever I try to run it, I get "Transaction Failed". What it's supposed to do is call it's own functions double and half using delegatecall from function foo and then return the result.

Here's the code from the link at the time the question was asked:

import serpent
from ethereum import tester as t

code = '''\
def foo(x, flag):
    with prefix = 0:
        if flag == "double":
            prefix = 0x6ffa1caa*2**248
        else:
            prefix = 0x70cd69ae*2**248
        delegate_calldata = alloc(36)
        out = alloc(32)
        mstore(ref(delegate_calldata), prefix / 2**248)
        prefix *= 2**8
        mstore8(ref(delegate_calldata) + 1, prefix / 2**248)
        prefix *= 2**8
        mstore8(ref(delegate_calldata) + 2, prefix / 2**248)
        prefix *= 2**8
        mstore8(ref(delegate_calldata) + 3, prefix / 2**248)
        mstore(ref(delegate_calldata) + 4, x)
        delegatecall(msg.gas, self, 128, 36, 128 + 36, 32)
        return(out[0])
def double(x):
    return(2*x)
def half(x):
    return(x/2)'''

print serpent.compile_to_lll(code)
s = t.state()
c = s.abi_contract(code)
double = int('double'.encode('hex').ljust(64, '0'), 16)
print c.foo(1, double)

UPDATE: This is the correct way to do it, with the new delegatecall support:

import serpent
from ethereum import tester as t

code = '''\
def foo(x, flag):
    with prefix = 0:
        if flag == "double":
            return(self.double(x, call=delegatecall))
        else:
            return(self.half(x, call=delegatecall))
def double(x):
    return(2*x)
def half(x):
    return(x/2)'''

print serpent.compile_to_lll(code)
s = t.state()
c = s.abi_contract(code)
double = int('double'.encode('hex').ljust(64, '0'), 16)
print c.foo(1, double)
2

Vitalik just added support for delegatecall to Serpent, which answers my question by making my method obsolete. You use it by adding call=delegatecall to your functions calls.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.