def test_small_int(self): items = [ (LInstructions([types.One(), types.Two(), types.IfDup()]), 1), (LInstructions([types.One(), types.Zero(), types.IfDup()]), 0), ] for script, expected_delta in items: self._do_context(script) ifdup = script[2] self.assertIsInstance(ifdup, types.IfDup) self.assertEqual(expected_delta, ifdup.delta)
def visit_Pick(self, op): """Attempt to determine opcode argument.""" arg = LInstructions.instruction_to_int(self.instructions[op.idx - 1]) if arg is None: return op.args = [1, arg + 2]
def visit_IfDup(self, op): """Attempt to determine opcode's delta.""" arg = LInstructions.instruction_to_int(self.instructions[op.idx - 1]) if arg is None: return op.delta = 1 if arg else 0
def test_invalid_hash256(self): for script in [ [types.Five(), types.Hash256(), types.Push(b'\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01'), types.EqualVerify()], [types.Five(), types.Sha256(), types.Push(b'\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01'), types.EqualVerify()], ]: script = LInstructions(script) self.assertRaises(IRError, self._do_context, script)
def test_valid_hash256(self): for script in [ [types.Five(), types.Hash256(), types.Push(b'\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01'), types.EqualVerify()], [types.Five(), types.Sha256(), types.Push(b'\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01'), types.EqualVerify()], ]: script = LInstructions(script) self._do_context(script)
def transform(self, node, symbol_table=None): # Whether we've finished visiting a conditional that results in a different # number of stack items depending on whether or not it is true. self.after_uneven_conditional = False self.symbol_table = symbol_table self.script = node self.instructions = LInstructions(self.visit(node.script)) return self.instructions
def test_small_int(self): script = LInstructions([types.Five(), types.Six(), types.Seven(), types.Two(), types.Pick()]) self._do_context(script) pick = script[4] self.assertIsInstance(pick, types.Pick) self.assertEqual([1, 4], pick.args) node = script[pick.idx - pick.args[1]] self.assertEqual(5, node.value)
def visit_CheckMultiSig(self, op): """Attempt to determine opcode arguments.""" i = 1 num_pubkeys = LInstructions.instruction_to_int(self.instructions[op.idx - i]) if num_pubkeys is None: return i += 1 i += num_pubkeys num_sigs = LInstructions.instruction_to_int(self.instructions[op.idx - i]) if num_sigs is None: return i += 1 i += num_sigs op.num_pubkeys = num_pubkeys op.num_sigs = num_sigs op.args = range(i)
def visit_CheckMultiSig(self, op): """Attempt to determine opcode arguments.""" i = 1 num_pubkeys = LInstructions.instruction_to_int( self.instructions[op.idx - i]) if num_pubkeys is None: return i += 1 i += num_pubkeys num_sigs = LInstructions.instruction_to_int(self.instructions[op.idx - i]) if num_sigs is None: return i += 1 i += num_sigs op.num_pubkeys = num_pubkeys op.num_sigs = num_sigs op.args = range(i)
def test_multisig(self): sigs = [types.Push(formats.int_to_bytearray(i)) for i in [100]] pubs = [types.Push(formats.int_to_bytearray(i)) for i in [300, 400]] script = [types.Zero()] # Dummy value. script.extend(sigs + [types.One()]) # 1 signature. script.extend(pubs + [types.Two()]) # 2 public keys. script.append(types.CheckMultiSig()) script = LInstructions(script) self._do_context(script) checkmultisig = script[6] self.assertIsInstance(checkmultisig, types.CheckMultiSig) self.assertEqual(2, checkmultisig.num_pubkeys) self.assertEqual(1, checkmultisig.num_sigs)
def test_push(self): script = LInstructions([types.One(), types.Push(formats.int_to_bytearray(20)), types.IfDup()]) self._do_context(script) ifdup = script[2] self.assertIsInstance(ifdup, types.IfDup) self.assertEqual(1, ifdup.delta)
def _do_test(self, expected, ops_list): script = LInstructions(ops_list) original = str(script) self.optimizer(self.symbol_table).optimize(script) expected = str(expected.split(' ')) self.assertEqual(expected, str(script), '%s != %s (original: %s)' % (expected, str(script), original))