def test_tx_version(self): from hathor.transaction.base_transaction import TxVersion # test the 1st byte of version field is ignored version = TxVersion(0xFF00) self.assertEqual(version.get_cls(), Block) version = TxVersion(0xFF01) self.assertEqual(version.get_cls(), Transaction) # test serialization doesn't mess up with version block = Block(version=0xFF00, nonce=100, weight=1) block2 = block.clone() self.assertEqual(block.version, block2.version)
def setUp(self, tx_storage, reactor=None): if not reactor: self.reactor = Clock() else: self.reactor = reactor self.reactor.advance(time.time()) self.tx_storage = tx_storage assert tx_storage.first_timestamp > 0 tx_storage._manually_initialize() self.genesis = self.tx_storage.get_all_genesis() self.genesis_blocks = [tx for tx in self.genesis if tx.is_block] self.genesis_txs = [tx for tx in self.genesis if not tx.is_block] from hathor.manager import HathorManager self.tmpdir = tempfile.mkdtemp() wallet = Wallet(directory=self.tmpdir) wallet.unlock(b'teste') self.manager = HathorManager(self.reactor, tx_storage=self.tx_storage, wallet=wallet) self.tx_storage.wallet_index = WalletIndex(self.manager.pubsub) self.tx_storage.tokens_index = TokensIndex() block_parents = [tx.hash for tx in chain(self.genesis_blocks, self.genesis_txs)] output = TxOutput(200, bytes.fromhex('1e393a5ce2ff1c98d4ff6892f2175100f2dad049')) self.block = Block(timestamp=MIN_TIMESTAMP, weight=12, outputs=[output], parents=block_parents, nonce=100781, storage=tx_storage) self.block.resolve() self.block.verify() tx_parents = [tx.hash for tx in self.genesis_txs] tx_input = TxInput( tx_id=self.genesis_blocks[0].hash, index=0, data=bytes.fromhex('46304402203470cb9818c9eb842b0c433b7e2b8aded0a51f5903e971649e870763d0266a' 'd2022049b48e09e718c4b66a0f3178ef92e4d60ee333d2d0e25af8868acf5acbb35aaa583' '056301006072a8648ce3d020106052b8104000a034200042ce7b94cba00b654d4308f8840' '7345cacb1f1032fb5ac80407b74d56ed82fb36467cb7048f79b90b1cf721de57e942c5748' '620e78362cf2d908e9057ac235a63')) self.tx = Transaction( timestamp=MIN_TIMESTAMP + 2, weight=10, nonce=932049, inputs=[tx_input], outputs=[output], tokens=[bytes.fromhex('0023be91834c973d6a6ddd1a0ae411807b7c8ef2a015afb5177ee64b666ce602')], parents=tx_parents, storage=tx_storage) self.tx.resolve() # Disable weakref to test the internal methods. Otherwise, most methods return objects from weakref. self.tx_storage._disable_weakref() self.tx_storage.enable_lock()
def test_block_number_parents(self): address = get_address_from_public_key(self.genesis_public_key) output_script = P2PKH.create_output_script(address) tx_outputs = [TxOutput(100, output_script)] parents = [tx.hash for tx in self.genesis_txs] block = Block( nonce=100, outputs=tx_outputs, parents=parents, weight=1, # low weight so we don't waste time with PoW storage=self.tx_storage) block.resolve() with self.assertRaises(IncorrectParents): block.verify()
def test_block_with_htr_authority(self): parents = [tx.hash for tx in self.genesis] output_script = P2PKH.create_output_script(self.address) output = TxOutput(0b11, output_script, 0b10000000) self.assertTrue(output.is_token_authority()) block = Block( nonce=100, outputs=[output], parents=parents, weight=1, # low weight so we don't waste time with PoW storage=self.manager.tx_storage) block.resolve() with self.assertRaises(InvalidToken): block.verify()
def test_tokens_in_block(self): # a block with token index > 1 should be invalid parents = [tx.hash for tx in self.genesis] output_script = P2PKH.create_output_script(self.address) tx_outputs = [TxOutput(100, output_script, 1)] block = Block( nonce=100, outputs=tx_outputs, parents=parents, weight=1, # low weight so we don't waste time with PoW storage=self.manager.tx_storage) block.resolve() with self.assertRaises(BlockWithTokensError): block.verify()
def test_block_unknown_parent(self): address = get_address_from_public_key(self.genesis_public_key) output_script = P2PKH.create_output_script(address) tx_outputs = [TxOutput(100, output_script)] # Random unknown parent parents = [hashlib.sha256().digest()] block = Block( nonce=100, outputs=tx_outputs, parents=parents, weight=1, # low weight so we don't waste time with PoW storage=self.tx_storage) block.resolve() with self.assertRaises(ParentDoesNotExist): block.verify()
def test_block_outputs(self): from hathor.transaction import MAX_NUM_OUTPUTS from hathor.transaction.exceptions import TooManyOutputs # a block should have no more than MAX_NUM_OUTPUTS outputs parents = [tx.hash for tx in self.genesis] address = get_address_from_public_key(self.genesis_public_key) output_script = P2PKH.create_output_script(address) tx_outputs = [TxOutput(100, output_script)] * (MAX_NUM_OUTPUTS + 1) block = Block( nonce=100, outputs=tx_outputs, parents=parents, weight=1, # low weight so we don't waste time with PoW storage=self.tx_storage) with self.assertRaises(TooManyOutputs): block.verify_outputs()
def test_block_inputs(self): # a block with inputs should be invalid parents = [tx.hash for tx in self.genesis] genesis_block = self.genesis_blocks[0] tx_inputs = [TxInput(genesis_block.hash, 0, b'')] address = get_address_from_public_key(self.genesis_public_key) output_script = P2PKH.create_output_script(address) tx_outputs = [TxOutput(100, output_script)] block = Block( nonce=100, outputs=tx_outputs, parents=parents, weight=1, # low weight so we don't waste time with PoW storage=self.tx_storage) block.inputs = tx_inputs block.resolve() with self.assertRaises(BlockWithInputs): block.verify()
limitations under the License. """ from typing import List, Optional from hathor.conf import HathorSettings from hathor.transaction import BaseTransaction, Block, Transaction, TxOutput from hathor.transaction.storage import TransactionStorage settings = HathorSettings() BLOCK_GENESIS = Block( hash=settings.GENESIS_BLOCK_HASH, nonce=settings.GENESIS_BLOCK_NONCE, timestamp=settings.GENESIS_TIMESTAMP, weight=settings.MIN_BLOCK_WEIGHT, outputs=[ TxOutput(settings.GENESIS_TOKENS, settings.GENESIS_OUTPUT_SCRIPT), ], ) TX_GENESIS1 = Transaction( hash=settings.GENESIS_TX1_HASH, nonce=settings.GENESIS_TX1_NONCE, timestamp=settings.GENESIS_TIMESTAMP + 1, weight=settings.MIN_TX_WEIGHT, ) TX_GENESIS2 = Transaction( hash=settings.GENESIS_TX2_HASH, nonce=settings.GENESIS_TX2_NONCE,
def _test_deferred_methods(self): # Testing without cloning self.cache_storage._clone_if_needed = False block_parents = [tx.hash for tx in self.genesis] output = TxOutput( 200, bytes.fromhex('1e393a5ce2ff1c98d4ff6892f2175100f2dad049')) obj = Block(timestamp=MIN_TIMESTAMP, weight=12, outputs=[output], parents=block_parents, nonce=100781, storage=self.cache_storage) obj.resolve() self.cache_storage.save_transaction_deferred(obj) loaded_obj1 = yield self.cache_storage.get_transaction_deferred( obj.hash) metadata_obj1_def = yield self.cache_storage.get_metadata_deferred( obj.hash) metadata_obj1 = obj.get_metadata() self.assertEqual(metadata_obj1_def, metadata_obj1) metadata_error = yield self.cache_storage.get_metadata_deferred( bytes.fromhex( '0001569c85fffa5782c3979e7d68dce1d8d84772505a53ddd76d636585f3977e' )) self.assertIsNone(metadata_error) self.cache_storage._flush_to_storage( self.cache_storage.dirty_txs.copy()) self.cache_storage.cache = collections.OrderedDict() loaded_obj2 = yield self.cache_storage.get_transaction_deferred( obj.hash) self.assertEqual(loaded_obj1, loaded_obj2) self.assertTrue( (yield self.cache_storage.transaction_exists_deferred(obj.hash))) self.assertFalse((yield self.cache_storage.transaction_exists_deferred( '0001569c85fffa5782c3979e7d68dce1d8d84772505a53ddd76d636585f3977e') )) self.assertFalse( self.cache_storage.transaction_exists( '0001569c85fffa5782c3979e7d68dce1d8d84772505a53ddd76d636585f3977e' )) self.assertEqual(obj, loaded_obj1) self.assertEqual(obj.is_block, loaded_obj1.is_block) count = yield self.cache_storage.get_count_tx_blocks_deferred() self.assertEqual(count, 4) all_transactions = yield self.cache_storage.get_all_transactions_deferred( ) total = 0 for tx in all_transactions: total += 1 self.assertEqual(total, 4)