def test_dump_value(self): code = """ storage unit ; parameter unit ; code { PUSH int 0 ; PUSH int 1 ; DUMP 1; } """ self._execute_code(code) self.assertEqual( [ IntType(1), IntType(0), ], self.stack.items, ) self.assertEqual( [ 'PUSH / _ => 0', 'PUSH / _ => 1', 'DUMP => [1]', ], self.stdout, )
def execute(cls, stack: MichelsonStack, stdout: List[str], context: AbstractContext): a, b = stack.pop2() a.assert_type_equal(type(b)) res = IntType.from_value(compare(a, b)) stack.push(res) stdout.append(format_stdout(cls.prim, [a, b], [res])) # type: ignore return cls(stack_items_added=1)
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 test_execute(self) -> None: # Arrange interpreter = Interpreter() code = "PUSH int 1; PUSH int 2; PAIR" # Act result = interpreter.execute(code) # Assert self.assertEqual(None, result.error) self.assertEqual( [ "PUSH / _ => 1", "PUSH / _ => 2", "PAIR / 2 : 1 => (2 * 1)", ], result.stdout, ) self.assertEqual([PairType((IntType(2), IntType(1)))], interpreter.stack.items)
def test_execute_rollback(self) -> None: # Arrange interpreter = Interpreter() code = "PUSH int 1; PUSH int 2; PAIR" bad_code = "PUSH int 1; PAIR; PAIR;" # Act interpreter.execute(code) result = interpreter.execute(bad_code) # Assert self.assertIsInstance(result.error, MichelsonRuntimeError) self.assertEqual( [ "PUSH / _ => 1", "PAIR / 1 : (2 * 1) => (1 * (2 * 1))", "PAIR: got 1 items on the stack, want to pop 2", ], result.stdout, ) self.assertEqual([PairType((IntType(2), IntType(1)))], interpreter.stack.items)
def execute(cls, stack: MichelsonStack, stdout: List[str], context: AbstractContext): a = cast( Union[IntType, NatType, BLS12_381_FrType, BLS12_381_G1Type, BLS12_381_G2Type], stack.pop1()) res_type, = dispatch_types(type(a), mapping={ (IntType, ): (IntType, ), (NatType, ): (IntType, ), (BLS12_381_FrType, ): (BLS12_381_FrType, ), (BLS12_381_G1Type, ): (BLS12_381_G1Type, ), (BLS12_381_G2Type, ): (BLS12_381_G2Type, ) }) if issubclass(res_type, IntType): res = IntType.from_value(-int(a)) else: res = res_type.from_point(bls12_381.neg(a.to_point())) stack.push(res) stdout.append(format_stdout(cls.prim, [a], [res])) return cls()