4

I've been reading this tutorial on contracts and implementing them in Serpent and on the bottom of page 13:

Any time we reference a function within the contract, we must call it from self (a reference to the current contract). Note that any function can be called directly by a user. For example, lets say we have a function A and a function B. If B has the logic that sends ether and A just checks if the ether should be sent, and A calls B to send the ether, an adversary could simply call function B and get the ether without ever going through the check. We can fix this by not putting that type of logic in separate functions.

It's not clear to me whether functions A and B are part of the same contract. Is it possible for a contract to call a function from another on the same node? If yes, how does it or how can it know its instance (the self)?

  • 1
    I would recommend looking at Solidity instead of Serpent, it deals with these issues pretty well. – Tjaden Hess Jun 3 '16 at 16:27
2

A and B are functions in the same contract.

The issue described is that a naive implementation of B can be invoked by anyone. Serpent currently doesn't have a "private" modifier which could be attached to specific functions, so that invoking it from outside the contract causes an exception. The way to do that for now is to check that the caller (msg.sender) is self:

def B():
    if msg.sender != self:
        ~invalid()
    # send the ether

An alternative is to make B a macro. B will not be a function and will inline B's code into A, effectively implementing what the tutorial suggests: "We can fix this by not putting that type of logic in separate functions."


It is possible for a contract to call another contract in Serpent, and its syntax is similar to other languages: "instance.foo" where inside foo, self will be the instance.

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.