Exemple #1
0
 def execute(cls, stack: MichelsonStack, stdout: List[str],
             context: AbstractContext):
     a = cast(Union[StringType, BytesType, ListType], stack.pop1())
     a.assert_type_in(StringType, BytesType, ListType)
     if isinstance(a, ListType):
         a.assert_type_in(ListType)
         res_type, convert, delim = dispatch_types(
             a.args[0],
             mapping={
                 (StringType, ): (StringType, str, ''),
                 (BytesType, ): (BytesType, bytes, b'')
             })
         res = res_type.from_value(delim.join(map(convert, a)))
         stdout.append(format_stdout(cls.prim, [a], [res]))
     else:
         b = cast(Union[StringType, BytesType], stack.pop1())
         res_type, convert = dispatch_types(type(a),
                                            type(b),
                                            mapping={
                                                (StringType, StringType):
                                                (StringType, str),
                                                (BytesType, BytesType):
                                                (BytesType, bytes)
                                            })
         res = res_type.from_value(convert(a) + convert(b))
         stdout.append(format_stdout(cls.prim, [a, b], [res]))
     stack.push(res)
     return cls()
    def execute(cls, stack: MichelsonStack, stdout: List[str], context: AbstractContext):
        stack_items_added = 0
        src = cast(Union[ListType, MapType], stack.pop1())
        executions = []
        items = []
        popped = [src]
        for elt in src:
            if isinstance(src, MapType):
                elt = PairType.from_comb(list(elt))  # type: ignore
            stack.push(elt)  # type: ignore
            stack_items_added += 1
            stdout.append(format_stdout(cls.prim, popped, [elt]))  # type: ignore
            execution = cls.args[0].execute(stack, stdout, context=context)
            executions.append(execution)
            new_elt = stack.pop1()
            if isinstance(src, MapType):
                items.append((elt[0], new_elt))
            else:
                items.append(new_elt)  # type: ignore
            popped = [new_elt]  # type: ignore

        if items:
            res = type(src).from_items(items)  # type: ignore
        else:
            res = src  # TODO: need to deduce argument types
        stack.push(res)
        stack_items_added += 1
        stdout.append(format_stdout(cls.prim, popped, [res]))  # type: ignore
        return cls(stack_items_added, executions)
def execute_dip(prim: str, stack: MichelsonStack, stdout: List[str],
                count: int, body: Type[MichelsonInstruction], context: AbstractContext) -> MichelsonInstruction:
    stdout.append(format_stdout(prim, [*Wildcard.n(count)], []))
    stack.protect(count=count)
    item = body.execute(stack, stdout, context=context)
    stack.restore(count=count)
    stdout.append(format_stdout(prim, [], [*Wildcard.n(count)], count))
    return item
Exemple #4
0
 def execute(cls, stack: MichelsonStack, stdout: List[str],
             context: AbstractContext):
     opt = cast(OptionType, stack.pop1())
     opt.assert_type_in(OptionType)
     if opt.is_none():
         branch = cls.args[0]
         stdout.append(format_stdout(cls.prim, [opt], []))
     else:
         some = opt.get_some()
         stack.push(some)
         stdout.append(format_stdout(cls.prim, [opt], [some]))
         branch = cls.args[1]
     item = branch.execute(stack, stdout, context=context)
     return cls(item)
Exemple #5
0
 def execute(cls, stack: MichelsonStack, stdout: List[str],
             context: AbstractContext):
     lst = cast(ListType, stack.pop1())
     lst.assert_type_in(ListType)
     if len(lst) > 0:
         head, tail = lst.split_head()
         stack.push(tail)
         stack.push(head)
         stdout.append(format_stdout(cls.prim, [lst], [head, tail]))
         branch = cls.args[0]
     else:
         stdout.append(format_stdout(cls.prim, [lst], []))
         branch = cls.args[1]
     item = branch.execute(stack, stdout, context=context)
     return cls(item)
Exemple #6
0
    def execute(cls, stack: MichelsonStack, stdout: List[str],
                context: AbstractContext):
        sequence = cast(MichelineSequence, cls.args[0])
        assert len(sequence.args
                   ) == 3, f'expected 3 sections, got {len(sequence.args)}'
        assert {arg.prim
                for arg in sequence.args} == {'parameter', 'storage',
                                              'code'}, f'unexpected sections'
        storage_type = cast(
            Type[MichelsonType],
            next(arg.args[0] for arg in sequence.args
                 if arg.prim == 'storage'))

        delegate, amount, initial_storage = cast(
            Tuple[OptionType, MutezType, MichelsonType], stack.pop3())
        delegate.assert_type_equal(OptionType.create_type(args=[KeyHashType]))
        amount.assert_type_equal(MutezType)
        initial_storage.assert_type_equal(storage_type)

        originated_address = AddressType.from_value(
            context.get_originated_address())
        context.spend_balance(int(amount))
        origination = OperationType.origination(
            source=context.get_self_address(),
            script=cls.args[0],  # type: ignore
            storage=initial_storage,
            balance=int(amount),
            delegate=None if delegate.is_none() else str(delegate.get_some()))

        stack.push(originated_address)
        stack.push(origination)
        stdout.append(
            format_stdout(cls.prim, [delegate, amount, initial_storage],
                          [origination, originated_address]))  # type: ignore
        return cls(stack_items_added=2)
Exemple #7
0
    def execute(cls, stack: MichelsonStack, stdout: List[str],
                context: AbstractContext):
        parameter, amount, destination = cast(
            Tuple[MichelsonType, MutezType, ContractType], stack.pop3())
        amount.assert_type_equal(MutezType)
        assert isinstance(
            destination,
            ContractType), f'expected contract, got {destination.prim}'
        parameter.assert_type_equal(destination.args[0])

        ep_type = get_entrypoint_type(context,
                                      destination.get_entrypoint(),
                                      address=destination.get_address())
        if ep_type:
            parameter.assert_type_equal(
                ep_type, message='destination contract parameter')

        transaction = OperationType.transaction(
            source=context.get_self_address(),
            destination=destination.get_address(),
            amount=int(amount),
            entrypoint=destination.get_entrypoint(),
            parameter=parameter)
        stack.push(transaction)
        stdout.append(
            format_stdout(cls.prim, [parameter, amount, destination],
                          [transaction]))  # type: ignore
        return cls(stack_items_added=1)
Exemple #8
0
 def execute(cls, stack: MichelsonStack, stdout: List[str],
             context: AbstractContext):
     now = context.get_now()
     res = TimestampType.from_value(now)
     stack.push(res)
     stdout.append(format_stdout(cls.prim, [], [res]))  # type: ignore
     return cls(stack_items_added=1)
Exemple #9
0
 def execute(cls, stack: MichelsonStack, stdout: List[str],
             context: AbstractContext):
     amount = context.get_amount()
     res = MutezType.from_value(amount)
     stack.push(res)
     stdout.append(format_stdout(cls.prim, [], [res]))  # type: ignore
     return cls(stack_items_added=1)
Exemple #10
0
 def execute(cls, stack: MichelsonStack, stdout: List[str],
             context: AbstractContext):
     left = stack.pop1()
     res = OrType.from_left(left, cls.args[0])  # type: ignore
     stack.push(res)
     stdout.append(format_stdout(cls.prim, [left], [res]))  # type: ignore
     return cls()
Exemple #11
0
 def execute(cls, stack: MichelsonStack, stdout: List[str], context: AbstractContext):
     elt, lst = cast(Tuple[MichelsonType, ListType], stack.pop2())
     lst.assert_type_in(ListType)
     res = lst.prepend(elt)
     stack.push(res)
     stdout.append(format_stdout(cls.prim, [elt, lst], [res]))
     return cls()
Exemple #12
0
    def execute(cls, stack: MichelsonStack, stdout: List[str],
                context: AbstractContext):
        # FIXME: MichelsonProgram copypaste
        parameter_literal, storage_literal = cls.args  # type: ignore

        parameter_type_expr = context.get_parameter_expr()
        storage_type_expr = context.get_storage_expr()
        if parameter_type_expr is None:
            raise Exception('parameter type is not initialized')
        if storage_type_expr is None:
            raise Exception('storage type is not initialized')

        parameter_type = ParameterSection.match(parameter_type_expr)
        storage_type = StorageSection.match(storage_type_expr)
        parameter = parameter_type.from_micheline_value(
            parameter_literal.as_micheline_expr())
        storage = storage_type.from_micheline_value(
            storage_literal.as_micheline_expr())

        parameter.attach_context(context)
        storage.attach_context(context)
        res = PairType.from_comb([parameter.item, storage.item])
        stack.items = []
        stack.push(res)
        stdout.append(format_stdout(f'BEGIN %default', [], [res]))
        return cls(stack_items_added=1)
Exemple #13
0
 def execute(cls, stack: MichelsonStack, stdout: List[str],
             context: AbstractContext):
     a, b = stack.pop2()
     stack.push(a)
     stack.push(b)
     stdout.append(format_stdout(cls.prim, [a, b], [b, a]))
     return cls()
Exemple #14
0
 def execute(cls, stack: MichelsonStack, stdout: List[str],
             context: AbstractContext):
     res = MapType.empty(key_type=cls.args[0],
                         val_type=cls.args[1])  # type: ignore
     stack.push(res)
     stdout.append(format_stdout(cls.prim, [], [res]))  # type: ignore
     return cls(stack_items_added=1)
Exemple #15
0
 def execute(cls, stack: MichelsonStack, stdout: List[str],
             context: AbstractContext):
     chain_id = context.get_chain_id()
     res = ChainIdType.from_value(chain_id)
     stack.push(res)
     stdout.append(format_stdout(cls.prim, [], [res]))  # type: ignore
     return cls(stack_items_added=1)
Exemple #16
0
 def execute(cls, stack: MichelsonStack, stdout: List[str],
             context: AbstractContext):
     some = stack.pop1()
     res = OptionType.from_some(some)
     stack.push(res)
     stdout.append(format_stdout(cls.prim, [some], [res]))  # type: ignore
     return cls(stack_items_added=1)
Exemple #17
0
def execute_zero_compare(prim: str, stack: MichelsonStack, stdout: List[str],
                         compare: Callable[[int], bool]):
    a = cast(IntType, stack.pop1())
    a.assert_type_equal(IntType)
    res = BoolType(compare(int(a)))
    stack.push(res)
    stdout.append(format_stdout(prim, [a], [res]))
Exemple #18
0
 def execute(cls, stack: MichelsonStack, stdout: List[str],
             context: AbstractContext):
     sender = context.get_sender()
     res = AddressType.from_value(sender)
     stack.push(res)
     stdout.append(format_stdout(cls.prim, [], [res]))
     return cls()
Exemple #19
0
 def execute(cls, stack: MichelsonStack, stdout: List[str],
             context: AbstractContext):
     balance = context.get_balance()
     res = MutezType.from_value(balance)
     stack.push(res)
     stdout.append(format_stdout(cls.prim, [], [res]))
     return cls()
Exemple #20
0
 def execute(cls, stack: MichelsonStack, stdout: List[str],
             context: AbstractContext):
     right = stack.pop1()
     res = OrType.from_right(right, cls.args[0])  # type: ignore
     stack.push(res)
     stdout.append(format_stdout(cls.prim, [right], [res]))  # type: ignore
     return cls(stack_items_added=1)
Exemple #21
0
 def execute(cls, stack: MichelsonStack, stdout: List[str],
             context: AbstractContext):
     source = context.get_source()
     res = AddressType.from_value(source)
     stack.push(res)
     stdout.append(format_stdout(cls.prim, [], [res]))  # type: ignore
     return cls(stack_items_added=1)
Exemple #22
0
def execute_hash(prim: str, stack: MichelsonStack, stdout: List[str],
                 hash_digest: Callable[[bytes], bytes]):
    a = cast(BytesType, stack.pop1())
    a.assert_type_equal(BytesType)
    res = BytesType.from_value(hash_digest(bytes(a)))
    stack.push(res)
    stdout.append(format_stdout(prim, [a], [res]))
Exemple #23
0
    def pull(cls, stack: MichelsonStack, stdout: List[str],
             context: AbstractContext):
        res_type: MichelsonType
        literal: Type[MichelineLiteral]
        res_type, literal = cls.args  # type: ignore
        res = res_type.from_literal(literal)
        if res_type.is_pushable():
            expected_res = stack.pop1()
        elif res_type.prim == 'big_map':
            if issubclass(literal, MichelineSequence):
                expected_res = stack.pop1()
            else:
                expected_res = stack.pop1()
                # NOTE: We care about validity of the pointer, not it's contents
                res = expected_res
        else:
            raise Exception(
                f'`{res_type.prim}` is neither pushable nor big_map')

        if res != expected_res:
            logger.debug('expected: %s(%s)', expected_res.__class__.__name__,
                         expected_res.__dict__)
            logger.debug('actual: %s(%s)', res.__class__.__name__,
                         res.__dict__)
            raise Exception('Stack content is not equal to expected')

        stdout.append(format_stdout(cls.prim, [], [res]))  # type: ignore
 def execute(cls, stack: MichelsonStack, stdout: List[str],
             context: AbstractContext):
     a, b = cast(
         Tuple[Union[IntType, NatType, MutezType, TimestampType,
                     BLS12_381_G1Type, BLS12_381_G2Type, BLS12_381_FrType],
               ...], stack.pop2())
     res_type, = dispatch_types(
         type(a),
         type(b),
         mapping={
             (NatType, NatType): (NatType, ),
             (NatType, IntType): (IntType, ),
             (IntType, NatType): (IntType, ),
             (IntType, IntType): (IntType, ),
             (TimestampType, IntType): (TimestampType, ),
             (IntType, TimestampType): (TimestampType, ),
             (MutezType, MutezType): (MutezType, ),
             (BLS12_381_FrType, BLS12_381_FrType): (BLS12_381_FrType, ),
             (BLS12_381_G1Type, BLS12_381_G1Type): (BLS12_381_G1Type, ),
             (BLS12_381_G2Type, BLS12_381_G2Type): (BLS12_381_G2Type, )
         })
     res_type = cast(
         Union[Type[IntType], Type[NatType], Type[TimestampType],
               Type[MutezType], Type[BLS12_381_G1Type],
               Type[BLS12_381_G2Type], Type[BLS12_381_FrType]], res_type)
     if issubclass(res_type, IntType):
         res = res_type.from_value(int(a) + int(b))  # type: ignore
     else:
         res = res_type.from_point(bls12_381.add(
             a.to_point(), b.to_point()))  # type: ignore
     stack.push(res)
     stdout.append(format_stdout(cls.prim, [a, b], [res]))  # type: ignore
     return cls(stack_items_added=1)
Exemple #25
0
 def execute(cls, stack: MichelsonStack, stdout: List[str], context: AbstractContext):
     res_type, literal = cls.args  # type: Type[MichelsonType], Type[Micheline]  # type: ignore
     assert res_type.is_pushable(), f'{res_type.prim} contains non-pushable arguments'
     res = res_type.from_literal(literal)
     stack.push(res)
     stdout.append(format_stdout(cls.prim, [], [res]))  # type: ignore
     return cls(stack_items_added=1)
 def execute(cls, stack: MichelsonStack, stdout: List[str],
             context: AbstractContext):
     a, b = cast(
         Tuple[Union[IntType, NatType, MutezType, TimestampType], ...],
         stack.pop2())
     q_type, r_type = dispatch_types(
         type(a),
         type(b),
         mapping={  # type: ignore
             (NatType, NatType): (NatType, NatType),
             (NatType, IntType): (IntType, NatType),
             (IntType, NatType): (IntType, NatType),
             (IntType, IntType): (IntType, NatType),
             (MutezType, NatType): (MutezType, MutezType),
             (MutezType, MutezType): (NatType, MutezType)
         }
     )  # type: Union[Type[IntType], Type[NatType], Type[TimestampType], Type[MutezType]]
     if int(b) == 0:
         res = OptionType.none(PairType.create_type(args=[q_type, r_type]))
     else:
         q, r = divmod(int(a), int(b))
         if r < 0:
             r += abs(int(b))
             q += 1
         items = [q_type.from_value(q), r_type.from_value(r)]
         res = OptionType.from_some(PairType.from_comb(items))
     stack.push(res)
     stdout.append(format_stdout(cls.prim, [a, b], [res]))  # type: ignore
     return cls(stack_items_added=1)
Exemple #27
0
 def execute(cls, stack: MichelsonStack, stdout: List[str],
             context: AbstractContext):
     a = stack.pop1()
     res = BytesType.from_value(a.pack())
     stack.push(res)
     stdout.append(format_stdout(cls.prim, [a], [res]))
     return cls()
Exemple #28
0
 def execute(cls, stack: MichelsonStack, stdout: List[str], context: AbstractContext):
     cond = cast(BoolType, stack.pop1())
     cond.assert_type_equal(BoolType)
     stdout.append(format_stdout(cls.prim, [cond], []))  # type: ignore
     branch = cls.args[0] if bool(cond) else cls.args[1]
     item = branch.execute(stack, stdout, context=context)
     return cls(item)
Exemple #29
0
 def execute(cls, stack: MichelsonStack, stdout: List[str], context: AbstractContext):
     key, src = cast(Tuple[MichelsonType, Union[SetType, MapType, BigMapType]], stack.pop2())
     src.assert_type_in(MapType, BigMapType, SetType)
     res = BoolType.from_value(src.contains(key))
     stack.push(res)
     stdout.append(format_stdout(cls.prim, [key, src], [res]))
     return cls()
Exemple #30
0
 def execute(cls, stack: MichelsonStack, stdout: List[str],
             context: AbstractContext):
     memo_size = cls.args[0].get_int()
     res = SaplingStateType.empty(memo_size)
     res.attach_context(context)
     stack.push(res)
     stdout.append(format_stdout(cls.prim, [], [res], memo_size))
     return cls()