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(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 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): 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)
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]))
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()
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]))
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)
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()
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)
def execute(cls, stack: MichelsonStack, stdout: List[str], context: AbstractContext): pair = cast(PairType, stack.pop1()) pair.assert_type_in(PairType) index = cls.args[0].get_int() res = pair.access_comb(index) stack.push(res) stdout.append(format_stdout(cls.prim, [pair], [res], index)) return cls()
def execute(cls, stack: MichelsonStack, stdout: List[str], context: AbstractContext): contract = cast(ContractType, stack.pop1()) contract.assert_type_in(ContractType) res = AddressType.from_value(contract.get_address()) stack.push(res) stdout.append(format_stdout(cls.prim, [contract], [res])) return cls()
def execute(cls, stack: MichelsonStack, stdout: List[str], context: AbstractContext): a = cast(Union[NatType, BLS12_381_FrType], stack.pop1()) a.assert_type_in(NatType, BLS12_381_FrType) res = IntType.from_value(int(a)) stack.push(res) stdout.append(f'{cls.prim} / {repr(a)} => {repr(res)}') return cls(stack_items_added=1)
def execute(cls, stack: MichelsonStack, stdout: List[str], context: AbstractContext): a = cast(IntType, stack.pop1()) a.assert_type_equal(IntType) res = NatType.from_value(abs(int(a))) stack.push(res) stdout.append(format_stdout(cls.prim, [a], [res])) # type: ignore return cls(stack_items_added=1)
def execute(cls, stack: MichelsonStack, stdout: List[str], context: AbstractContext): pair = cast(PairType, stack.pop1()) pair.assert_type_in(PairType) left, right = tuple(iter(pair)) stack.push(right) stack.push(left) stdout.append(format_stdout(cls.prim, [pair], [left, right])) return cls()
def execute(cls, stack: MichelsonStack, stdout: List[str], context: AbstractContext): depth = cls.args[0].get_int() # type: ignore res = stack.pop1() stack.protect(count=depth) stack.push(res) stack.restore(count=depth) stdout.append(format_stdout(cls.prim, [res, *Wildcard.n(depth)], [*Wildcard.n(depth), res], depth)) # type: ignore return cls(stack_items_added=1)
def execute(cls, stack: MichelsonStack, stdout: List[str], context: AbstractContext): res = stack.pop1() # TODO: will become obsolete in the next protocol? (because annots are no longer part of the type) # cast_type = cast(Type[MichelsonType], cls.args[0]) # res = cast_type.from_micheline_value(top.to_micheline_value()) stack.push(res) stdout.append(format_stdout(cls.prim, [res], [res])) # type: ignore return cls(stack_items_added=1)
def execute(cls, stack: MichelsonStack, stdout: List[str], context: AbstractContext): top = stack.pop1() cast_type = cast(Type[MichelsonType], cls.args[0]) res = cast_type.from_micheline_value(top.to_micheline_value()) stack.push(res) stdout.append(format_stdout(cls.prim, [top], [res])) return cls()
def execute(cls, stack: MichelsonStack, stdout: List[str], context: AbstractContext): ticket = cast(TicketType, stack.pop1()) ticket.assert_type_in(TicketType) res = ticket.to_comb() stack.push(ticket) stack.push(res) stdout.append(format_stdout(cls.prim, [ticket], [res, ticket])) return cls()
def execute(cls, stack: MichelsonStack, stdout: List[str], context: AbstractContext): or_ = cast(OrType, stack.pop1()) or_.assert_type_in(OrType) branch = cls.args[0] if or_.is_left() else cls.args[1] res = or_.resolve() stack.push(res) stdout.append(format_stdout(cls.prim, [or_], [res])) # type: ignore item = branch.execute(stack, stdout, context=context) return cls(item)
def execute(cls, stack: MichelsonStack, stdout: List[str], context: AbstractContext): a = cast(KeyType, stack.pop1()) a.assert_type_equal(KeyType) key = Key.from_encoded_key(str(a)) res = KeyHashType.from_value(key.public_key_hash()) stack.push(res) stdout.append(format_stdout(cls.prim, [a], [res])) # type: ignore return cls(stack_items_added=1)
def execute(cls, stack: MichelsonStack, stdout: List[str], context: AbstractContext): key_hash = cast(KeyHashType, stack.pop1()) key_hash.assert_type_equal(KeyHashType) res = ContractType.create_type(args=[UnitType]).from_value( str(key_hash)) stack.push(res) stdout.append(format_stdout(cls.prim, [key_hash], [res])) return cls()
def execute(cls, stack: MichelsonStack, stdout: List[str], context: AbstractContext): src = cast(Union[StringType, BytesType, ListType, SetType, MapType], stack.pop1()) src.assert_type_in(StringType, BytesType, ListType, SetType, MapType) res = NatType.from_value(len(src)) stack.push(res) stdout.append(format_stdout(cls.prim, [src], [res])) return cls()
def execute(cls, stack: MichelsonStack, stdout: List[str], context: AbstractContext): count = cls.args[0].get_int() assert count >= 2, f'invalid argument, must be >= 2' pair = cast(PairType, stack.pop1()) pair.assert_type_in(PairType) leaves = list(pair.iter_comb()) assert len(leaves) == count, f'expected {count} leaves, got {len(leaves)}' for leaf in reversed(leaves): stack.push(leaf) stdout.append(format_stdout(cls.prim, [pair], leaves, count)) return cls()
def execute(cls, stack: MichelsonStack, stdout: List[str], context: AbstractContext): a = cast(IntType, stack.pop1()) a.assert_type_equal(IntType) if int(a) >= 0: res = OptionType.from_some(NatType.from_value(int(a))) else: res = OptionType.none(NatType) stack.push(res) stdout.append(format_stdout(cls.prim, [a], [res])) return cls()
def execute(cls, stack: MichelsonStack, stdout: List[str], context: AbstractContext): delegate = cast(OptionType, stack.pop1()) delegate.assert_type_equal(OptionType.create_type(args=[KeyHashType])) delegation = OperationType.delegation( source=context.get_self_address(), delegate=None if delegate.is_none() else str(delegate.get_some())) stack.push(delegation) stdout.append(format_stdout(cls.prim, [delegate], [delegation])) return cls()
def execute(cls, stack: MichelsonStack, stdout: List[str], context: AbstractContext): a = cast(Union[IntType, NatType, BoolType], stack.pop1()) res_type, convert = dispatch_types(type(a), mapping={ (NatType,): (IntType, lambda x: ~int(x)), (IntType,): (IntType, lambda x: ~int(x)), (BoolType,): (BoolType, lambda x: not bool(x)) }) res = res_type.from_value(convert(a)) stack.push(res) stdout.append(format_stdout(cls.prim, [a], [res])) # type: ignore return cls(stack_items_added=1)
def execute(cls, stack: MichelsonStack, stdout: List[str], context: AbstractContext): items = [] while True: cond = cast(BoolType, stack.pop1()) cond.assert_type_equal(BoolType) stdout.append(format_stdout(cls.prim, [cond], [])) # type: ignore if bool(cond): item = cls.args[0].execute(stack, stdout, context=context) items.append(item) else: break return cls(items)
def end(self, stack: MichelsonStack, stdout: List[str], output_mode='readable') \ -> Tuple[List[dict], Any, List[dict], PairType]: res = cast(PairType, stack.pop1()) assert len(stack) == 0, f'stack is not empty: {repr(stack)}' res.assert_type_equal(PairType.create_type(args=[ ListType.create_type(args=[OperationType]), self.storage.args[0] ]), message='list of operations + resulting storage') operations = [op.content for op in res.items[0]] lazy_diff = [] storage = res.items[1].aggregate_lazy_diff(lazy_diff).to_micheline_value(mode=output_mode) stdout.append(format_stdout(f'END %{self.entrypoint}', [res], [])) return operations, storage, lazy_diff, res
def execute(cls, stack: MichelsonStack, stdout: List[str], context: AbstractContext): a = cast(BytesType, stack.pop1()) a.assert_type_equal(BytesType) try: some = cls.args[0].unpack(bytes(a)) res = OptionType.from_some(some) except Exception as e: stdout.append(f'{cls.prim}: {e}') res = OptionType.none(cls.args[0]) stack.push(res) stdout.append(format_stdout(cls.prim, [a], [res])) return cls()