1

I am new to Michelson and Smartpy so forgive if I have any mistakes, I will try to correct them.

I have not been able to find an answer to the question if a function like the below, prevents the caller from altering the contract storage, or if that would be possible.

Smartpy:

@sp.entry_point
def addops(self, ops):
    sp.set_type(ops, sp.TLambda(sp.TUnit, sp.TList(sp.TOperation)))
    sp.add_operations(ops(sp.unit))

The Michelson output for a super-simple contract with the above entry-point, a %default entry-point and a nat (x) in storage is then as below.

Michelson output:

{
    storage nat;
    parameter
        (or
            (lambda %addops
                unit
                (list operation))
            (unit %default));
    code
        {
            UNPAIR;
            IF_LEFT
                {
                    NIL operation;
                    SWAP;
                    UNIT;
                    EXEC;
                    ITER
                        {
                            CONS
                        }
                }
                {
                    DROP;
                    NIL operation
                };
            NIL operation;
            SWAP;
            ITER
                {
                    CONS
                };
            PAIR
        }
}

So, by calling %addops here it would [as far as I understand] be possible through the lambda to call an arbitrary entry-point in another contract with arbitrary parameters, ... but I have also been told that 'Michelson [lambdas] are purely functional with no side effect (besides possibly failing with an exception)'.

Is this true? Is the lambda an environemnt it is not possible to break out of, so storage can't be modified when exeuting a lambda?

In the sources I have been able to find, I couldn't really find a reference that explains these limitations on lambda. If anyone knows a good one, it would be much appreciated.

On the other hand, if it is not true. Then how might one go about modifying x - the nat in storage in this contract?

1 Answer 1

2

This code and what you have been told are compatible.

As you can see, the operations are added outside of the lambda (after it is evaluated). The lambda creates some value which happens to be operations and the contract decides to consider them as their own.

The lambda is evaluated with a stack that starts with a single element (its parameters). It finishes by leaving a stack with also a single element: its result.

https://tezos.gitlab.io/michelson-reference/#instr-EXEC

Such a function can absolutely return a value that replaces the storage but it must be explicit either by doing (in SmartPy)

self.data … = …

Or by using the with_storage mechanism in SmartPy.

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.