Example #1
0
    def test_get_transaction_from_block_uint256(self):
        call_flags = Integer(CallFlags.ALL).to_byte_array(signed=True, min_length=1)
        method = String('getTransactionFromBlock').to_bytes()

        expected_output = (
            Opcode.INITSLOT
            + b'\x00\x02'
            + Opcode.LDARG1
            + Opcode.LDARG0
            + Opcode.PUSH2
            + Opcode.PACK
            + Opcode.PUSHDATA1
            + Integer(len(call_flags)).to_byte_array()
            + call_flags
            + Opcode.PUSHDATA1
            + Integer(len(method)).to_byte_array()
            + method
            + Opcode.PUSHDATA1
            + Integer(len(constants.LEDGER_SCRIPT)).to_byte_array()
            + constants.LEDGER_SCRIPT
            + Opcode.SYSCALL
            + Interop.CallContract.interop_method_hash
            + Opcode.RET
        )
        path = self.get_contract_path('GetTransactionFromBlockUInt256.py')
        output = Boa3.compile(path)
        self.assertEqual(expected_output, output)

        path_burn_gas = self.get_contract_path('../runtime', 'BurnGas.py')
        engine = TestEngine()

        engine.increase_block(10)
        sender = bytes(range(20))
        self.run_smart_contract(engine, path_burn_gas, 'main', 100, signer_accounts=[sender])

        block_10 = engine.current_block
        block_hash = block_10.hash
        self.assertIsNotNone(block_hash)
        txs = block_10.get_transactions()
        tx_hash = txs[0].hash
        tx_script = txs[0]._script

        engine.increase_block()

        result = self.run_smart_contract(engine, path, 'main', block_hash, 0)
        self.assertEqual(8, len(result))
        if isinstance(result[0], str):
            result[0] = String(result[0]).to_bytes()
        self.assertEqual(UInt256(tx_hash), UInt256(result[0]))  # hash
        self.assertIsInstance(result[1], int)  # version
        self.assertIsInstance(result[2], int)  # nonce
        if isinstance(result[3], str):
            result[3] = String(result[3]).to_bytes()
        self.assertEqual(UInt160(sender), UInt160(result[3]))  # sender
        self.assertIsInstance(result[4], int)  # system_fee
        self.assertIsInstance(result[5], int)  # network_fee
        self.assertIsInstance(result[6], int)  # valid_until_block
        if isinstance(result[7], str):
            result[7] = String(result[7]).to_bytes()
        self.assertEqual(tx_script, result[7])  # script
Example #2
0
    def from_json(cls, json: Dict[str, Any]) -> Transaction:
        script = base64.b64decode(json['script'])
        tx = cls(script)
        if 'signers' in json:
            signers_json = json['signers']
            if not isinstance(signers_json, list):
                signers_json = [signers_json]
            tx._signers = [Signer.from_json(js) for js in signers_json]

        if 'witnesses' in json:
            witnesses_json = json['witnesses']
            if not isinstance(witnesses_json, list):
                witnesses_json = [witnesses_json]
            tx._witnesses = [Witness.from_json(js) for js in witnesses_json]

        if 'attributes' in json:
            attributes_json = json['attributes']
            if not isinstance(attributes_json, list):
                attributes_json = [attributes_json]
            tx._attributes = [
                tx_attribute.TransactionAttribute.from_json(js)
                for js in attributes_json
            ]

        if 'hash' in json and isinstance(json['hash'], str):
            tx._hash = UInt256(from_hex_str(json['hash']))
        return tx
Example #3
0
    def evaluate_literal(self, *args: Any) -> Any:
        from boa3.neo3.core.types import UInt256

        if len(args) == 0:
            return UInt256.zero().to_array()

        if len(args) == 1:
            arg = args[0]
            if isinstance(arg, int):
                from boa3.neo.vm.type.Integer import Integer
                arg = Integer(arg).to_byte_array(min_length=UInt256._BYTE_LEN)
            if isinstance(arg, bytes):
                value = UInt256(arg).to_array()
                return value

        return super().evaluate_literal(*args)
Example #4
0
    def _internal_transaction_get(self, hash: types.UInt256):
        tx_bytes = self._real_db.get(DBPrefixes.TRANSACTIONS + hash.to_array())
        if tx_bytes is None:
            # this is a must if not found!
            raise KeyError

        with serialization.BinaryReader(tx_bytes) as br:
            tx = payloads.Transaction()
            tx.deserialize_special(br)
            return tx
Example #5
0
    def test_transaction_init(self):
        path = self.get_contract_path('Transaction.py')
        engine = TestEngine()

        result = self.run_smart_contract(engine, path, 'main')
        self.assertEqual(8, len(result))
        if isinstance(result[0], str):
            result[0] = String(result[0]).to_bytes()
        self.assertEqual(UInt256(), UInt256(result[0]))  # hash
        self.assertEqual(0, result[1])  # version
        self.assertEqual(0, result[2])  # nonce
        if isinstance(result[3], str):
            result[3] = String(result[3]).to_bytes()
        self.assertEqual(UInt160(), UInt160(result[3]))  # sender
        self.assertEqual(0, result[4])  # system_fee
        self.assertEqual(0, result[5])  # network_fee
        self.assertEqual(0, result[6])  # valid_until_block
        if isinstance(result[7], str):
            result[7] = String(result[7]).to_bytes()
        self.assertEqual(b'', result[7])  # script
Example #6
0
    def from_json(cls, json: Dict[str, Any]) -> Block:
        # 'index' and 'timestamp' fields are required
        block = cls(int(json['index']))
        block._timestamp = int(json['timestamp'])

        if 'transactions' in json:
            tx_json = json['transactions']
            if not isinstance(tx_json, list):
                tx_json = [tx_json]
            block._transactions = [Transaction.from_json(js) for js in tx_json]

        if 'hash' in json and isinstance(json['hash'], str):
            block._hash = UInt256(from_hex_str(json['hash']))

        return block
Example #7
0
    def _internal_block_delete(self, hash: types.UInt256, batch=None):
        if batch:
            db = batch
        else:
            db = self._real_db

        block_hash_bytes = hash.to_array()
        block_bytes = self._real_db.get(DBPrefixes.BLOCKS + block_hash_bytes)
        if block_bytes is not None:
            # Instead of full block deserialization, which includes merkletree calculation and such, we manually extract
            # the 4 bytes block.index from the stream.
            start_idx = 4 + 32 + 32 + 8
            block_height_bytes = block_bytes[start_idx:start_idx + 4]
            db.delete(DBPrefixes.BLOCKS + block_hash_bytes)
            db.delete(DBPrefixes.BLOCKS_HEIGHT_MAP + block_height_bytes)
Example #8
0
    def test_block_constructor(self):
        path = self.get_contract_path('Block.py')
        engine = TestEngine()

        result = self.run_smart_contract(engine, path, 'main')
        self.assertIsInstance(result, list)
        self.assertEqual(10, len(result))
        for k in range(len(result)):
            if isinstance(result[k], str):
                result[k] = String(result[k]).to_bytes()
        self.assertEqual(UInt256(), UInt256(result[0]))  # hash
        self.assertEqual(0, result[1])  # version
        self.assertEqual(UInt256(), UInt256(result[2]))  # previous_hash
        self.assertEqual(UInt256(), UInt256(result[3]))  # merkle_root
        self.assertEqual(0, result[4])  # timestamp
        self.assertEqual(0, result[5])  # nonce
        self.assertEqual(0, result[6])  # index
        self.assertEqual(0, result[7])  # primary_index
        self.assertEqual(UInt160(), UInt160(result[8]))  # next_consensus
        self.assertEqual(0, result[9])  # transaction_count
Example #9
0
    def _internal_block_get(self, hash: types.UInt256):
        block_bytes = self._real_db.get(DBPrefixes.BLOCKS + hash.to_array())
        if block_bytes is None:
            raise KeyError

        return payloads.Block.deserialize_from_bytes(block_bytes)
Example #10
0
 def _internal_transaction_delete(self, hash: types.UInt256, batch=None):
     if batch:
         db = batch
     else:
         db = self._real_db
     db.delete(DBPrefixes.TRANSACTIONS + hash.to_array())